Skip to content

Commit

Permalink
style: update comments and table driven tests variable name
Browse files Browse the repository at this point in the history
  • Loading branch information
wI2L committed Nov 12, 2023
1 parent b9ab68b commit 9f65129
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 59 deletions.
10 changes: 5 additions & 5 deletions differ.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (d *Differ) Reset() {
}

// WithOpts applies the given options to the Differ
// and returns it to allow chained calls.
// instance and returns it to allow chained calls.
func (d *Differ) WithOpts(opts ...Option) *Differ {
for _, o := range opts {
o(d)
Expand All @@ -64,14 +64,14 @@ func (d *Differ) WithOpts(opts ...Option) *Differ {
}

// Patch returns the list of JSON patch operations
// generated by the Differ. The patch is valid for usage
// until the next comparison or reset.
// generated by the Differ instance. The patch is
// valid for usage until the next comparison or reset.
func (d *Differ) Patch() Patch {
return d.patch
}

// Compare computes the differences between src and tgt as
// a series of JSON Patch operations.
// Compare computes the differences between src and tgt
// as a series of JSON Patch operations.
func (d *Differ) Compare(src, tgt interface{}) {
if d.opts.factorize {
d.prepare(d.ptr, src, tgt)
Expand Down
16 changes: 8 additions & 8 deletions differ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestDiffer_Reset(t *testing.T) {
func TestOptions(t *testing.T) {
makeopts := func(opts ...Option) []Option { return opts }

for _, tt := range []struct {
for _, tc := range []struct {
testfile string
options []Option
}{
Expand All @@ -73,12 +73,12 @@ func TestOptions(t *testing.T) {
{"testdata/tests/options/all.json", makeopts(Factorize(), Rationalize(), Invertible(), Equivalent())},
} {
var (
ext = filepath.Ext(tt.testfile)
base = filepath.Base(tt.testfile)
ext = filepath.Ext(tc.testfile)
base = filepath.Base(tc.testfile)
name = strings.TrimSuffix(base, ext)
)
t.Run(name, func(t *testing.T) {
runCasesFromFile(t, tt.testfile, tt.options...)
runCasesFromFile(t, tc.testfile, tc.options...)
})
}
}
Expand Down Expand Up @@ -191,7 +191,7 @@ func runTestCase(t *testing.T, tc testcase, pc patchGetter, opts ...Option) {
}

func TestDiffer_unorderedDeepEqualSlice(t *testing.T) {
for _, tt := range []struct {
for _, tc := range []struct {
src, tgt []interface{}
equal bool
}{
Expand Down Expand Up @@ -224,9 +224,9 @@ func TestDiffer_unorderedDeepEqualSlice(t *testing.T) {
},
} {
d := Differ{}
eq := d.unorderedDeepEqualSlice(tt.src, tt.tgt)
if eq != tt.equal {
t.Errorf("equality mismatch, got %t, want %t", eq, tt.equal)
eq := d.unorderedDeepEqualSlice(tc.src, tc.tgt)
if eq != tc.equal {
t.Errorf("equality mismatch, got %t, want %t", eq, tc.equal)
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions equal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func BenchmarkGetType(b *testing.B) {
}

func Test_jsonTypeSwitch(t *testing.T) {
for _, tt := range []struct {
for _, tc := range []struct {
val any
valid bool
kind jsonValueType
Expand Down Expand Up @@ -85,15 +85,15 @@ func Test_jsonTypeSwitch(t *testing.T) {
jsonObject,
},
} {
k := jsonTypeSwitch(tt.val)
if k != tt.kind {
t.Errorf("got %s, want %s", k, tt.kind)
k := jsonTypeSwitch(tc.val)
if k != tc.kind {
t.Errorf("got %s, want %s", k, tc.kind)
}
}
}

func Test_deepEqualValue(t *testing.T) {
for _, tt := range []struct {
for _, tc := range []struct {
src, tgt interface{}
equal bool
}{
Expand Down Expand Up @@ -123,9 +123,9 @@ func Test_deepEqualValue(t *testing.T) {
false,
},
} {
ok := deepEqualValue(tt.src, tt.tgt)
if ok != tt.equal {
t.Errorf("got %t, want %t", ok, tt.equal)
ok := deepEqualValue(tc.src, tc.tgt)
if ok != tc.equal {
t.Errorf("got %t, want %t", ok, tc.equal)
}
}
}
Expand Down
36 changes: 18 additions & 18 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func Test_findKey(t *testing.T) {
for _, tt := range []struct {
for _, tc := range []struct {
json string
key string
want string
Expand All @@ -34,21 +34,21 @@ func Test_findKey(t *testing.T) {
},
} {
// Valid JSON input and result.
if len(tt.json) != 0 && !json.Valid([]byte(tt.json)) {
t.Errorf("invalid JSON input: %q", tt.json)
if len(tc.json) != 0 && !json.Valid([]byte(tc.json)) {
t.Errorf("invalid JSON input: %q", tc.json)
}
if len(tt.want) != 0 && !json.Valid([]byte(tt.want)) {
t.Errorf("invalid JSON result: %q", tt.want)
if len(tc.want) != 0 && !json.Valid([]byte(tc.want)) {
t.Errorf("invalid JSON result: %q", tc.want)
}
s := findKey(tt.json, tt.key)
if s != tt.want {
t.Errorf("got %q, want %q", s, tt.want)
s := findKey(tc.json, tc.key)
if s != tc.want {
t.Errorf("got %q, want %q", s, tc.want)
}
}
}

func Test_findIndex(t *testing.T) {
for _, tt := range []struct {
for _, tc := range []struct {
json string
index int
want string
Expand Down Expand Up @@ -130,25 +130,25 @@ func Test_findIndex(t *testing.T) {
},
} {
// Valid JSON input and result.
if len(tt.json) != 0 && !json.Valid([]byte(tt.json)) {
t.Errorf("invalid JSON input: %q", tt.json)
if len(tc.json) != 0 && !json.Valid([]byte(tc.json)) {
t.Errorf("invalid JSON input: %q", tc.json)
}
if len(tt.want) != 0 && !json.Valid([]byte(tt.want)) {
t.Errorf("invalid JSON result: %q", tt.want)
if len(tc.want) != 0 && !json.Valid([]byte(tc.want)) {
t.Errorf("invalid JSON result: %q", tc.want)
}
s := findIndex(tt.json, tt.index)
if s != tt.want {
t.Errorf("got %q, want %q", s, tt.want)
s := findIndex(tc.json, tc.index)
if s != tc.want {
t.Errorf("got %q, want %q", s, tc.want)
}
}
}

func Test__compact(t *testing.T) {
func Test_compactInPlace(t *testing.T) {
small, err := os.ReadFile("testdata/benchs/small/source.json")
if err != nil {
t.Fatal(err)
}
b := _compact(small, small)
b := compactInPlace(small)

const want = `{"pine":true,"silence":{"feathers":"could","lion":false,"provide":["lake",1886677335,"research",false],"ate":"nearest"},"already":true,"it":false}`

Expand Down
10 changes: 5 additions & 5 deletions lcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func Test_lcs(t *testing.T) {
for _, tt := range []struct {
for _, tc := range []struct {
name string
src []interface{}
tgt []interface{}
Expand Down Expand Up @@ -75,10 +75,10 @@ func Test_lcs(t *testing.T) {
pairs: [][2]int{},
},
} {
t.Run(tt.name, func(t *testing.T) {
pairs := lcs(tt.src, tt.tgt)
if !reflect.DeepEqual(pairs, tt.pairs) {
t.Errorf("got %v, want %v", pairs, tt.pairs)
t.Run(tc.name, func(t *testing.T) {
pairs := lcs(tc.src, tc.tgt)
if !reflect.DeepEqual(pairs, tc.pairs) {
t.Errorf("got %v, want %v", pairs, tc.pairs)
}
})
}
Expand Down
30 changes: 15 additions & 15 deletions pointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func Test_parsePointer(t *testing.T) {
for _, tt := range []struct {
for _, tc := range []struct {
ptr string
valid bool
err error
Expand Down Expand Up @@ -147,27 +147,27 @@ func Test_parsePointer(t *testing.T) {
nil,
},
} {
tokens, err := parsePointer(tt.ptr)
if tt.valid && err != nil {
tokens, err := parsePointer(tc.ptr)
if tc.valid && err != nil {
t.Errorf("expected valid pointer, got error: %q", err)
}
if !tt.valid {
if !tc.valid {
if err == nil {
t.Errorf("expected error, got none")
} else if !errors.Is(err, tt.err) {
t.Errorf("error mismtahc, got %q, want %q", err, tt.err)
} else if !errors.Is(err, tc.err) {
t.Errorf("error mismtahc, got %q, want %q", err, tc.err)
}
}
if l := len(tokens); l != tt.count {
t.Errorf("got %d tokens, want %d: %q", l, tt.count, tt.ptr)
} else if !reflect.DeepEqual(tokens, tt.tokens) {
t.Errorf("tokens mismatch, got %v, want %v", tokens, tt.tokens)
if l := len(tokens); l != tc.count {
t.Errorf("got %d tokens, want %d: %q", l, tc.count, tc.ptr)
} else if !reflect.DeepEqual(tokens, tc.tokens) {
t.Errorf("tokens mismatch, got %v, want %v", tokens, tc.tokens)
}
}
}

func TestPointer_escapeKey(t *testing.T) {
for _, tt := range []struct {
for _, tc := range []struct {
key string
esc string
}{
Expand All @@ -181,11 +181,11 @@ func TestPointer_escapeKey(t *testing.T) {
},
} {
p := pointer{
buf: make([]byte, 0, len(tt.key)*2),
buf: make([]byte, 0, len(tc.key)*2),
}
p.appendEscapeKey(tt.key)
if s := p.copy(); s != tt.esc {
t.Errorf("got %q, want %q", s, tt.esc)
p.appendEscapeKey(tc.key)
if s := p.copy(); s != tc.esc {
t.Errorf("got %q, want %q", s, tc.esc)
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions testdata/tests/options/ignore.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,8 @@
"ignores": [],
"patch": [
{ "op": "replace", "path": "/2", "value": "d" }
],
"partial_patch": [
{ "op": "replace", "path": "/2", "value": "d" }
]
}]

0 comments on commit 9f65129

Please sign in to comment.