Something went wrong. Try again.
forked from tailscale/tailscale
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181// Copyright (c) Tailscale Inc & contributors// SPDX-License-Identifier: BSD-3-Clause
package safediff
import ( "strings" "testing"
"github.com/google/go-cmp/cmp")
func init() { diffTest = true }
func TestLines(t *testing.T) { // The diffs shown below technically depend on the stability of cmp, // but that should be fine for sufficiently simple diffs like these. // If the output does change, that would suggest a significant regression // in the optimality of cmp's diffing algorithm.
x := `{ "firstName": "John", "lastName": "Smith", "isAlive": true, "age": 27, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "phoneNumbers": [{ "type": "home", "number": "212 555-1234" }, { "type": "office", "number": "646 555-4567" }], "children": [ "Catherine", "Thomas", "Trevor" ], "spouse": null}` y := x y = strings.ReplaceAll(y, `"New York"`, `"Los Angeles"`) y = strings.ReplaceAll(y, `"NY"`, `"CA"`) y = strings.ReplaceAll(y, `"646 555-4567"`, `"315 252-8888"`)
wantDiff := `… 5 identical lines "address": { "streetAddress": "21 2nd Street",- "city": "New York",- "state": "NY",+ "city": "Los Angeles",+ "state": "CA", "postalCode": "10021-3100" },… 3 identical lines }, { "type": "office",- "number": "646 555-4567"+ "number": "315 252-8888" }],… 7 identical lines`[1:] gotDiff, gotTrunc := Lines(x, y, -1) if d := cmp.Diff(gotDiff, wantDiff); d != "" { t.Errorf("Lines mismatch (-got +want):\n%s\ngot:\n%s\nwant:\n%s", d, gotDiff, wantDiff) } else if gotTrunc == true { t.Errorf("Lines: output unexpectedly truncated") }
wantDiff = `… 5 identical lines "address": { "streetAddress": "21 2nd Street",- "city": "New York",- "state": "NY",+ "city": "Los Angeles",… 15 identical, 1 removed, and 2 inserted lines`[1:] gotDiff, gotTrunc = Lines(x, y, 200) if d := cmp.Diff(gotDiff, wantDiff); d != "" { t.Errorf("Lines mismatch (-got +want):\n%s\ngot:\n%s\nwant:\n%s", d, gotDiff, wantDiff) } else if gotTrunc == false { t.Errorf("Lines: output unexpectedly not truncated") }
wantDiff = "… 17 identical, 3 removed, and 3 inserted lines\n" gotDiff, gotTrunc = Lines(x, y, 0) if d := cmp.Diff(gotDiff, wantDiff); d != "" { t.Errorf("Lines mismatch (-got +want):\n%s\ngot:\n%s\nwant:\n%s", d, gotDiff, wantDiff) } else if gotTrunc == false { t.Errorf("Lines: output unexpectedly not truncated") }
x = `{ "unrelated": [ "unrelated", ], "related": { "unrelated": [ "unrelated", ], "related": { "unrelated": [ "unrelated", ], "related": { "related": "changed", }, "unrelated": [ "unrelated", ], }, "unrelated": [ "unrelated", ], }, "unrelated": [ "unrelated", ],}` y = strings.ReplaceAll(x, "changed", "CHANGED")
wantDiff = `… 4 identical lines "related": {… 3 identical lines "related": {… 3 identical lines "related": {- "related": "changed",+ "related": "CHANGED", },… 3 identical lines },… 3 identical lines },… 4 identical lines`[1:] gotDiff, gotTrunc = Lines(x, y, -1) if d := cmp.Diff(gotDiff, wantDiff); d != "" { t.Errorf("Lines mismatch (-got +want):\n%s\ngot:\n%s\nwant:\n%s", d, gotDiff, wantDiff) } else if gotTrunc == true { t.Errorf("Lines: output unexpectedly truncated") }
x = `{ "ACLs": [ { "Action": "accept", "Users": ["group:all"], "Ports": ["tag:tmemes:80"], }, ],}` y = strings.ReplaceAll(x, "tag:tmemes:80", "tag:tmemes:80,8383") wantDiff = ` { "ACLs": [ { "Action": "accept", "Users": ["group:all"],- "Ports": ["tag:tmemes:80"],+ "Ports": ["tag:tmemes:80,8383"], }, ], }`[1:] gotDiff, gotTrunc = Lines(x, y, -1) if d := cmp.Diff(gotDiff, wantDiff); d != "" { t.Errorf("Lines mismatch (-got +want):\n%s\ngot:\n%s\nwant:\n%s", d, gotDiff, wantDiff) } else if gotTrunc == true { t.Errorf("Lines: output unexpectedly truncated") }}