...

Source file src/github.com/cli/go-gh/v2/pkg/jsonpretty/format_test.go

Documentation: github.com/cli/go-gh/v2/pkg/jsonpretty

     1  package jsonpretty
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"testing"
     7  )
     8  
     9  func TestWrite(t *testing.T) {
    10  	type args struct {
    11  		r        io.Reader
    12  		indent   string
    13  		colorize bool
    14  	}
    15  	tests := []struct {
    16  		name    string
    17  		args    args
    18  		wantW   string
    19  		wantErr bool
    20  	}{
    21  		{
    22  			name: "blank",
    23  			args: args{
    24  				r:        bytes.NewBufferString(``),
    25  				indent:   "",
    26  				colorize: true,
    27  			},
    28  			wantW:   "",
    29  			wantErr: false,
    30  		},
    31  		{
    32  			name: "empty object",
    33  			args: args{
    34  				r:        bytes.NewBufferString(`{}`),
    35  				indent:   "",
    36  				colorize: true,
    37  			},
    38  			wantW:   "\x1b[1;38m{\x1b[m\x1b[1;38m}\x1b[m\n",
    39  			wantErr: false,
    40  		},
    41  		{
    42  			name: "nested object",
    43  			args: args{
    44  				r:        bytes.NewBufferString(`{"hash":{"a":1,"b":2},"array":[3,4]}`),
    45  				indent:   "\t",
    46  				colorize: true,
    47  			},
    48  			wantW: "\x1b[1;38m{\x1b[m\n\t\x1b[1;34m\"hash\"\x1b[m\x1b[1;38m:\x1b[m " +
    49  				"\x1b[1;38m{\x1b[m\n\t\t\x1b[1;34m\"a\"\x1b[m\x1b[1;38m:\x1b[m 1\x1b[1;38m,\x1b[m\n\t\t\x1b[1;34m\"b\"\x1b[m\x1b[1;38m:\x1b[m 2\n\t\x1b[1;38m}\x1b[m\x1b[1;38m,\x1b[m" +
    50  				"\n\t\x1b[1;34m\"array\"\x1b[m\x1b[1;38m:\x1b[m \x1b[1;38m[\x1b[m\n\t\t3\x1b[1;38m,\x1b[m\n\t\t4\n\t\x1b[1;38m]\x1b[m\n\x1b[1;38m}\x1b[m\n",
    51  			wantErr: false,
    52  		},
    53  		{
    54  			name: "no color",
    55  			args: args{
    56  				r:        bytes.NewBufferString(`{"hash":{"a":1,"b":2},"array":[3,4]}`),
    57  				indent:   "\t",
    58  				colorize: false,
    59  			},
    60  			wantW:   "{\n\t\"hash\": {\n\t\t\"a\": 1,\n\t\t\"b\": 2\n\t},\n\t\"array\": [\n\t\t3,\n\t\t4\n\t]\n}\n",
    61  			wantErr: false,
    62  		},
    63  		{
    64  			name: "string",
    65  			args: args{
    66  				r:        bytes.NewBufferString(`"foo"`),
    67  				indent:   "",
    68  				colorize: true,
    69  			},
    70  			wantW:   "\x1b[32m\"foo\"\x1b[m\n",
    71  			wantErr: false,
    72  		},
    73  		{
    74  			name: "error",
    75  			args: args{
    76  				r:        bytes.NewBufferString(`{{`),
    77  				indent:   "",
    78  				colorize: true,
    79  			},
    80  			wantW:   "\x1b[1;38m{\x1b[m\n",
    81  			wantErr: true,
    82  		},
    83  	}
    84  	for _, tt := range tests {
    85  		t.Run(tt.name, func(t *testing.T) {
    86  			w := &bytes.Buffer{}
    87  			if err := Format(w, tt.args.r, tt.args.indent, tt.args.colorize); (err != nil) != tt.wantErr {
    88  				t.Errorf("Write() error = %v, wantErr %v", err, tt.wantErr)
    89  				return
    90  			}
    91  			if w.String() != tt.wantW {
    92  				t.Errorf("got: %q, want: %q", w.String(), tt.wantW)
    93  			}
    94  		})
    95  	}
    96  }
    97  

View as plain text