wal-g

Форк
0
/
entities_test.go 
209 строк · 4.8 Кб
1
package printlist
2

3
import (
4
	"bytes"
5
	"fmt"
6
	"strings"
7
	"testing"
8

9
	"github.com/stretchr/testify/assert"
10
)
11

12
func TestList(t *testing.T) {
13
	entities := []Entity{
14
		shortEntity,
15
		longEntity,
16
		emptyEntity,
17
	}
18

19
	tests := []struct {
20
		name         string
21
		entities     []Entity
22
		pretty, json bool
23
		wantOutput   string
24
		wantErr      assert.ErrorAssertionFunc
25
	}{
26
		{
27
			name:       "print plain json",
28
			entities:   entities,
29
			pretty:     false,
30
			json:       true,
31
			wantOutput: fmt.Sprintf("[%s,%s,%s]\n", shortEntityPlainJSON, longEntityPlainJSON, emptyEntityPlainJSON),
32
			wantErr:    assert.NoError,
33
		},
34
		{
35
			name:       "print indented json",
36
			entities:   entities,
37
			pretty:     true,
38
			json:       true,
39
			wantOutput: fmt.Sprintf("[\n%s,\n%s,\n%s\n]\n", shortEntityIndentedJSON, longEntityIndentedJSON, emptyEntityIndentedJSON),
40
			wantErr:    assert.NoError,
41
		},
42
		{
43
			name:       "print empty json",
44
			entities:   []Entity{},
45
			pretty:     true,
46
			json:       true,
47
			wantOutput: "[]\n",
48
			wantErr:    assert.NoError,
49
		},
50
		{
51
			name:     "print ascii table",
52
			entities: entities,
53
			pretty:   true,
54
			json:     false,
55
			wantOutput: fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n",
56
				testEntityASCIIHeader,
57
				shortEntityASCIIRow,
58
				longEntityASCIIRow,
59
				emptyEntityASCIIRow,
60
				testEntityASCIISeparator,
61
			),
62
			wantErr: assert.NoError,
63
		},
64
		{
65
			name:       "print empty ascii table",
66
			entities:   []Entity{},
67
			pretty:     true,
68
			json:       false,
69
			wantOutput: "",
70
			wantErr:    assert.NoError,
71
		},
72
		{
73
			name:     "print tabbed table",
74
			entities: entities,
75
			pretty:   false,
76
			json:     false,
77
			wantOutput: fmt.Sprintf("%s\n%s\n%s\n%s\n",
78
				testEntityTabbedHeader,
79
				shortEntityTabbedRow,
80
				longEntityTabbedRow,
81
				emptyEntityTabbedRow,
82
			),
83
			wantErr: assert.NoError,
84
		},
85
		{
86
			name:       "print empty tabbed table",
87
			entities:   []Entity{},
88
			pretty:     false,
89
			json:       false,
90
			wantOutput: "",
91
			wantErr:    assert.NoError,
92
		},
93
	}
94
	for _, tt := range tests {
95
		t.Run(tt.name, func(t *testing.T) {
96
			output := new(bytes.Buffer)
97
			err := List(tt.entities, output, tt.pretty, tt.json)
98
			t.Logf("actual output:\n%s<end>", output)
99
			if !tt.wantErr(t, err) {
100
				return
101
			}
102
			assert.Equal(t, tt.wantOutput, output.String())
103
		})
104
	}
105
}
106

107
type testEntity struct {
108
	A         string `json:"a"`
109
	APretty   string `json:"-"`
110
	B         string `json:"b"`
111
	CJSONOnly string `json:"c,omitempty"`
112
}
113

114
func (e testEntity) PrintableFields() []TableField {
115
	return []TableField{
116
		{
117
			Name:        "a",
118
			PrettyName:  "A (pretty)",
119
			Value:       e.A,
120
			PrettyValue: &e.APretty,
121
		},
122
		{
123
			Name:        "b",
124
			PrettyName:  "B (pretty)",
125
			Value:       e.B,
126
			PrettyValue: nil,
127
		},
128
	}
129
}
130

131
var (
132
	testEntityASCIISeparator = "+---+----------------------------------------------------------+------------+"
133

134
	testEntityASCIIHeader = strings.Join(
135
		[]string{
136
			testEntityASCIISeparator,
137
			"| # | A (PRETTY)                                               | B (PRETTY) |",
138
			testEntityASCIISeparator,
139
		},
140
		"\n",
141
	)
142

143
	testEntityTabbedHeader = "a                                                      b"
144
)
145

146
var (
147
	shortEntity = testEntity{
148
		A:         "1692753495",
149
		APretty:   "Thu, 23 Aug 2023 09:26:25",
150
		B:         "123",
151
		CJSONOnly: "kek",
152
	}
153

154
	shortEntityPlainJSON = `{"a":"1692753495","b":"123","c":"kek"}`
155

156
	shortEntityIndentedJSON = `    {
157
        "a": "1692753495",
158
        "b": "123",
159
        "c": "kek"
160
    }`
161

162
	shortEntityASCIIRow = `| 0 | Thu, 23 Aug 2023 09:26:25                                | 123        |`
163

164
	shortEntityTabbedRow = `1692753495                                             123`
165
)
166

167
var (
168
	longValue       = "lorem ipsum dolor sit amet consectetur adipiscing elit"
169
	prettyLongValue = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
170

171
	longEntity = testEntity{
172
		A:         longValue,
173
		APretty:   prettyLongValue,
174
		B:         "some value",
175
		CJSONOnly: "details",
176
	}
177

178
	longEntityPlainJSON = `{"a":"lorem ipsum dolor sit amet consectetur adipiscing elit","b":"some value","c":"details"}`
179

180
	longEntityIndentedJSON = `    {
181
        "a": "lorem ipsum dolor sit amet consectetur adipiscing elit",
182
        "b": "some value",
183
        "c": "details"
184
    }`
185

186
	longEntityASCIIRow = `| 1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. | some value |`
187

188
	longEntityTabbedRow = `lorem ipsum dolor sit amet consectetur adipiscing elit some value`
189
)
190

191
var (
192
	emptyEntity = testEntity{
193
		A:         "",
194
		APretty:   "",
195
		B:         "",
196
		CJSONOnly: "",
197
	}
198

199
	emptyEntityPlainJSON = `{"a":"","b":""}`
200

201
	emptyEntityIndentedJSON = `    {
202
        "a": "",
203
        "b": ""
204
    }`
205

206
	emptyEntityASCIIRow = `| 2 |                                                          |            |`
207

208
	emptyEntityTabbedRow = `                                                       `
209
)
210

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.