New upstream snapshot.
Debian Janitor
1 year, 4 months ago
0 | 0 | language: go |
1 | 1 | |
2 | 2 | go: |
3 | - 1.7 | |
4 | - 1.8 | |
5 | - tip | |
3 | - "1.14.x" | |
4 | - "1.15.x" | |
5 | ||
6 | arch: | |
7 | - amd64 | |
8 | - arm64 | |
9 | - ppc64le |
0 | golang-github-kylelemons-godebug (1.1.0+git20201106.1.e693023-1) UNRELEASED; urgency=low | |
1 | ||
2 | * New upstream snapshot. | |
3 | ||
4 | -- Debian Janitor <janitor@jelmer.uk> Sat, 10 Dec 2022 18:13:48 -0000 | |
5 | ||
0 | 6 | golang-github-kylelemons-godebug (1.1.0-2) unstable; urgency=medium |
1 | 7 | |
2 | 8 | [ Debian Janitor ] |
15 | 15 | package diff |
16 | 16 | |
17 | 17 | import ( |
18 | "bytes" | |
19 | 18 | "fmt" |
20 | 19 | "strings" |
21 | 20 | ) |
40 | 39 | func Diff(A, B string) string { |
41 | 40 | aLines := strings.Split(A, "\n") |
42 | 41 | bLines := strings.Split(B, "\n") |
42 | return Render(DiffChunks(aLines, bLines)) | |
43 | } | |
43 | 44 | |
44 | chunks := DiffChunks(aLines, bLines) | |
45 | ||
46 | buf := new(bytes.Buffer) | |
45 | // Render renders the slice of chunks into a representation that prefixes | |
46 | // the lines with '+', '-', or ' ' depending on whether the line was added, | |
47 | // removed, or equal (respectively). | |
48 | func Render(chunks []Chunk) string { | |
49 | buf := new(strings.Builder) | |
47 | 50 | for _, c := range chunks { |
48 | 51 | for _, line := range c.Added { |
49 | 52 | fmt.Fprintf(buf, "+%s\n", line) |
134 | 134 | } |
135 | 135 | } |
136 | 136 | |
137 | func TestRender(t *testing.T) { | |
138 | tests := []struct { | |
139 | desc string | |
140 | chunks []Chunk | |
141 | out string | |
142 | }{ | |
143 | { | |
144 | desc: "ordering", | |
145 | chunks: []Chunk{ | |
146 | { | |
147 | Added: []string{"1"}, | |
148 | Deleted: []string{"2"}, | |
149 | Equal: []string{"3"}, | |
150 | }, | |
151 | { | |
152 | Added: []string{"4"}, | |
153 | Deleted: []string{"5"}, | |
154 | }, | |
155 | }, | |
156 | out: strings.TrimSpace(` | |
157 | +1 | |
158 | -2 | |
159 | 3 | |
160 | +4 | |
161 | -5 | |
162 | `), | |
163 | }, | |
164 | { | |
165 | desc: "only_added", | |
166 | chunks: []Chunk{ | |
167 | { | |
168 | Added: []string{"1"}, | |
169 | }, | |
170 | }, | |
171 | out: strings.TrimSpace(` | |
172 | +1 | |
173 | `), | |
174 | }, | |
175 | { | |
176 | desc: "only_deleted", | |
177 | chunks: []Chunk{ | |
178 | { | |
179 | Deleted: []string{"1"}, | |
180 | }, | |
181 | }, | |
182 | out: strings.TrimSpace(` | |
183 | -1 | |
184 | `), | |
185 | }, | |
186 | } | |
187 | ||
188 | for _, test := range tests { | |
189 | t.Run(test.desc, func(t *testing.T) { | |
190 | if got, want := Render(test.chunks), test.out; got != want { | |
191 | t.Errorf("Render(%q):", test.chunks) | |
192 | t.Errorf("GOT\n%s", got) | |
193 | t.Errorf("WANT\n%s", want) | |
194 | } | |
195 | }) | |
196 | } | |
197 | } | |
198 | ||
137 | 199 | func ExampleDiff() { |
138 | 200 | constitution := strings.TrimSpace(` |
139 | 201 | We the People of the United States, in Order to form a more perfect Union, |
19 | 19 | // |
20 | 20 | // See the Reflect and Print examples for what the output looks like. |
21 | 21 | package pretty |
22 | ||
23 | // TODO: | |
24 | // - Catch cycles |
15 | 15 | |
16 | 16 | import ( |
17 | 17 | "testing" |
18 | "time" | |
18 | 19 | ) |
19 | 20 | |
20 | 21 | func TestDiff(t *testing.T) { |
125 | 126 | } |
126 | 127 | } |
127 | 128 | } |
129 | ||
130 | func TestRegressions(t *testing.T) { | |
131 | tests := []struct { | |
132 | issue string | |
133 | config *Config | |
134 | value interface{} | |
135 | want string | |
136 | }{ | |
137 | { | |
138 | issue: "kylelemons/godebug#13", | |
139 | config: &Config{ | |
140 | PrintStringers: true, | |
141 | }, | |
142 | value: struct{ Day *time.Weekday }{}, | |
143 | want: "{Day: nil}", | |
144 | }, | |
145 | } | |
146 | ||
147 | for _, test := range tests { | |
148 | t.Run(test.issue, func(t *testing.T) { | |
149 | if got, want := test.config.Sprint(test.value), test.want; got != want { | |
150 | t.Errorf("%#v.Sprint(%#v) = %q, want %q", test.config, test.value, got, want) | |
151 | } | |
152 | }) | |
153 | } | |
154 | } |
114 | 114 | return n |
115 | 115 | } |
116 | 116 | |
117 | func (r *reflector) val2node(val reflect.Value) node { | |
117 | func (r *reflector) val2node(val reflect.Value) (ret node) { | |
118 | 118 | if !val.IsValid() { |
119 | 119 | return rawVal("nil") |
120 | 120 | } |
121 | 121 | |
122 | 122 | if val.CanInterface() { |
123 | // Detect panics in calling functions on nil pointers. | |
124 | // | |
125 | // We still want to call them, as it's possible that a nil value is | |
126 | // valid for the particular type. | |
127 | // | |
128 | // If we detect a panic, just return raw nil. | |
129 | if val.Kind() == reflect.Ptr && val.IsNil() { | |
130 | defer func() { | |
131 | if r := recover(); r != nil { | |
132 | ret = rawVal("nil") | |
133 | } | |
134 | }() | |
135 | } | |
136 | ||
123 | 137 | v := val.Interface() |
124 | 138 | if formatter, ok := r.Formatter[val.Type()]; ok { |
125 | 139 | if formatter != nil { |
146 | 146 | }, |
147 | 147 | }, |
148 | 148 | { |
149 | desc: "struct w/ IncludeUnexported", | |
149 | desc: "struct with IncludeUnexported", | |
150 | 150 | raw: struct{ Zaphod, Ford, foo string }{"beeblebrox", "prefect", "GOOD"}, |
151 | 151 | cfg: &Config{ |
152 | 152 | IncludeUnexported: true, |
166 | 166 | }, |
167 | 167 | }, |
168 | 168 | { |
169 | desc: "time w/ nil Formatter", | |
169 | desc: "time with nil Formatter", | |
170 | 170 | raw: struct{ Date time.Time }{time.Unix(1234567890, 0).UTC()}, |
171 | 171 | cfg: &Config{ |
172 | 172 | PrintStringers: true, |
179 | 179 | }, |
180 | 180 | }, |
181 | 181 | { |
182 | desc: "time w/ PrintTextMarshalers", | |
182 | desc: "time with PrintTextMarshalers", | |
183 | 183 | raw: struct{ Date time.Time }{time.Unix(1234567890, 0).UTC()}, |
184 | 184 | cfg: &Config{ |
185 | 185 | PrintTextMarshalers: true, |
189 | 189 | }, |
190 | 190 | }, |
191 | 191 | { |
192 | desc: "time w/ PrintStringers", | |
192 | desc: "time with PrintStringers", | |
193 | 193 | raw: struct{ Date time.Time }{time.Unix(1234567890, 0).UTC()}, |
194 | 194 | cfg: &Config{ |
195 | 195 | PrintStringers: true, |
196 | 196 | }, |
197 | 197 | want: keyvals{ |
198 | 198 | {"Date", stringVal("2009-02-13 23:31:30 +0000 UTC")}, |
199 | }, | |
200 | }, | |
201 | { | |
202 | desc: "nil with PrintStringers", | |
203 | raw: struct{ Date *time.Time }{}, | |
204 | cfg: &Config{ | |
205 | PrintStringers: true, | |
206 | }, | |
207 | want: keyvals{ | |
208 | {"Date", rawVal("nil")}, | |
209 | }, | |
210 | }, | |
211 | { | |
212 | desc: "nilIsFine with PrintStringers", | |
213 | raw: struct{ V *nilIsFine }{}, | |
214 | cfg: &Config{ | |
215 | PrintStringers: true, | |
216 | }, | |
217 | want: keyvals{ | |
218 | {"V", stringVal("<nil is fine>")}, | |
219 | }, | |
220 | }, | |
221 | { | |
222 | desc: "nilIsFine non-nil with PrintStringers", | |
223 | raw: struct{ V *nilIsFine }{V: new(nilIsFine)}, | |
224 | cfg: &Config{ | |
225 | PrintStringers: true, | |
226 | }, | |
227 | want: keyvals{ | |
228 | {"V", stringVal("<not nil is fine>")}, | |
199 | 229 | }, |
200 | 230 | }, |
201 | 231 | { |
265 | 295 | } |
266 | 296 | |
267 | 297 | for _, test := range tests { |
268 | ref := &reflector{ | |
269 | Config: test.cfg, | |
270 | } | |
271 | if test.cfg.TrackCycles { | |
272 | ref.pointerTracker = new(pointerTracker) | |
273 | } | |
274 | if got, want := ref.val2node(reflect.ValueOf(test.raw)), test.want; !reflect.DeepEqual(got, want) { | |
275 | t.Run(test.desc, func(t *testing.T) { | |
298 | t.Run(test.desc, func(t *testing.T) { | |
299 | ref := &reflector{ | |
300 | Config: test.cfg, | |
301 | } | |
302 | if test.cfg.TrackCycles { | |
303 | ref.pointerTracker = new(pointerTracker) | |
304 | } | |
305 | if got, want := ref.val2node(reflect.ValueOf(test.raw)), test.want; !reflect.DeepEqual(got, want) { | |
276 | 306 | t.Errorf(" got %#v", got) |
277 | 307 | t.Errorf("want %#v", want) |
278 | 308 | t.Errorf("Diff: (-got +want)\n%s", Compare(got, want)) |
279 | }) | |
280 | } | |
309 | } | |
310 | }) | |
281 | 311 | } |
282 | 312 | } |
283 | 313 | |
414 | 444 | }) |
415 | 445 | } |
416 | 446 | } |
447 | ||
448 | type nilIsFine struct{} | |
449 | ||
450 | func (n *nilIsFine) String() string { | |
451 | if n == nil { | |
452 | return "<nil is fine>" | |
453 | } | |
454 | return "<not nil is fine>" | |
455 | } |