File: format_test.go

package info (click to toggle)
golang-github-cli-go-gh 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 444 kB
  • sloc: makefile: 2
file content (96 lines) | stat: -rw-r--r-- 2,255 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package jsonpretty

import (
	"bytes"
	"io"
	"testing"
)

func TestWrite(t *testing.T) {
	type args struct {
		r        io.Reader
		indent   string
		colorize bool
	}
	tests := []struct {
		name    string
		args    args
		wantW   string
		wantErr bool
	}{
		{
			name: "blank",
			args: args{
				r:        bytes.NewBufferString(``),
				indent:   "",
				colorize: true,
			},
			wantW:   "",
			wantErr: false,
		},
		{
			name: "empty object",
			args: args{
				r:        bytes.NewBufferString(`{}`),
				indent:   "",
				colorize: true,
			},
			wantW:   "\x1b[1;38m{\x1b[m\x1b[1;38m}\x1b[m\n",
			wantErr: false,
		},
		{
			name: "nested object",
			args: args{
				r:        bytes.NewBufferString(`{"hash":{"a":1,"b":2},"array":[3,4]}`),
				indent:   "\t",
				colorize: true,
			},
			wantW: "\x1b[1;38m{\x1b[m\n\t\x1b[1;34m\"hash\"\x1b[m\x1b[1;38m:\x1b[m " +
				"\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" +
				"\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",
			wantErr: false,
		},
		{
			name: "no color",
			args: args{
				r:        bytes.NewBufferString(`{"hash":{"a":1,"b":2},"array":[3,4]}`),
				indent:   "\t",
				colorize: false,
			},
			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",
			wantErr: false,
		},
		{
			name: "string",
			args: args{
				r:        bytes.NewBufferString(`"foo"`),
				indent:   "",
				colorize: true,
			},
			wantW:   "\x1b[32m\"foo\"\x1b[m\n",
			wantErr: false,
		},
		{
			name: "error",
			args: args{
				r:        bytes.NewBufferString(`{{`),
				indent:   "",
				colorize: true,
			},
			wantW:   "\x1b[1;38m{\x1b[m\n",
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			w := &bytes.Buffer{}
			if err := Format(w, tt.args.r, tt.args.indent, tt.args.colorize); (err != nil) != tt.wantErr {
				t.Errorf("Write() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if w.String() != tt.wantW {
				t.Errorf("got: %q, want: %q", w.String(), tt.wantW)
			}
		})
	}
}