From 476dbc713f716102257c3541edc356506081350a Mon Sep 17 00:00:00 2001 From: Guillaume de Sagazan Date: Thu, 27 Apr 2017 14:27:53 +0200 Subject: [PATCH] Adding tests for IsMissing and IsExcess --- diff/walk_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/diff/walk_test.go b/diff/walk_test.go index 3e4df0a..dd52e7a 100644 --- a/diff/walk_test.go +++ b/diff/walk_test.go @@ -79,3 +79,69 @@ func TestWalkError(t *testing.T) { } } } + +func TestIsExcess(t *testing.T) { + for _, test := range []struct { + LHS interface{} + RHS interface{} + }{ + {[]int{1, 2}, []int{1, 2, 3}}, + {map[int]int{1: 2, 3: 4}, map[int]int{1: 2, 3: 4, 5: 6}}, + } { + d, err := Diff(test.LHS, test.RHS) + if err != nil { + t.Errorf("Diff(%+v, %+v): unexpected error: %s", test.LHS, test.RHS, err) + continue + } + if d.Diff() != ContentDiffer { + t.Errorf("Diff(%+v, %+v).Diff() = %s, expected %s", test.LHS, test.RHS, d.Diff(), ContentDiffer) + } + + d, err = Walk(d, func(_, diff Differ, _ string) (Differ, error) { + if IsExcess(diff) { + return Ignore() + } + return nil, nil + }) + if err != nil { + t.Errorf("Walk(...): unexpected error: %s", err) + continue + } + if d.Diff() != Identical { + t.Errorf("Walk(...)).Diff() = %s, expected %s", d.Diff(), Identical) + } + } +} + +func TestIsMissing(t *testing.T) { + for _, test := range []struct { + LHS interface{} + RHS interface{} + }{ + {[]int{1, 2, 3}, []int{1, 2}}, + {map[int]int{1: 2, 3: 4, 5: 6}, map[int]int{1: 2, 3: 4}}, + } { + d, err := Diff(test.LHS, test.RHS) + if err != nil { + t.Errorf("Diff(%+v, %+v): unexpected error: %s", test.LHS, test.RHS, err) + continue + } + if d.Diff() != ContentDiffer { + t.Errorf("Diff(%+v, %+v).Diff() = %s, expected %s", test.LHS, test.RHS, d.Diff(), ContentDiffer) + } + + d, err = Walk(d, func(_, diff Differ, _ string) (Differ, error) { + if IsMissing(diff) { + return Ignore() + } + return nil, nil + }) + if err != nil { + t.Errorf("Walk(...): unexpected error: %s", err) + continue + } + if d.Diff() != Identical { + t.Errorf("Walk(...)).Diff() = %s, expected %s", d.Diff(), Identical) + } + } +}