github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/cmd/swagger/commands/diff/spec_difference_test.go (about) 1 package diff_test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 "github.com/thetreep/go-swagger/cmd/swagger/commands/diff" 8 ) 9 10 func TestMatches(t *testing.T) { 11 urlOnly := diff.SpecDifference{DifferenceLocation: diff.DifferenceLocation{URL: "bob"}} 12 urlOnlyDiff := diff.SpecDifference{DifferenceLocation: diff.DifferenceLocation{URL: "notbob"}} 13 urlOnlySame := diff.SpecDifference{DifferenceLocation: diff.DifferenceLocation{URL: "bob"}} 14 15 require.True(t, urlOnly.Matches(urlOnlySame)) 16 require.False(t, urlOnly.Matches(urlOnlyDiff)) 17 18 withMethod := urlOnly 19 withMethod.DifferenceLocation.Method = "PUT" 20 withMethodSame := withMethod 21 withMethodDiff := withMethod 22 withMethodDiff.DifferenceLocation.Method = "GET" 23 24 require.True(t, withMethod.Matches(withMethodSame)) 25 require.False(t, withMethod.Matches(withMethodDiff)) 26 27 withResponse := urlOnly 28 withResponse.DifferenceLocation.Response = 0 29 withResponseSame := withResponse 30 withResponseDiff := withResponse 31 withResponseDiff.DifferenceLocation.Response = 2 32 33 require.True(t, withResponse.Matches(withResponseSame)) 34 require.False(t, withResponse.Matches(withResponseDiff)) 35 36 withNode := urlOnly 37 withNode.DifferenceLocation.Node = &diff.Node{Field: "FieldA", TypeName: "TypeA"} 38 withNodeSame := withNode 39 withNodeSame.DifferenceLocation.Node = &diff.Node{Field: "FieldA", TypeName: "TypeA"} 40 41 withNodeDiff := withNode 42 withNodeDiff.DifferenceLocation.Node = &diff.Node{Field: "FieldA", TypeName: "TypeB"} 43 44 require.True(t, withNode.Matches(withNodeSame)) 45 require.False(t, withNode.Matches(withNodeDiff)) 46 47 withNodeDiff.DifferenceLocation.Node = &diff.Node{Field: "FieldB", TypeName: "TypeA"} 48 49 require.True(t, withNode.Matches(withNodeSame)) 50 require.False(t, withNode.Matches(withNodeDiff)) 51 52 withNestedNode := withNode 53 withNestedNode.DifferenceLocation = withNestedNode.DifferenceLocation.AddNode(&diff.Node{Field: "ChildA", TypeName: "ChildA"}) 54 withNestedNodeSame := withNode 55 withNestedNodeSame.DifferenceLocation = withNestedNodeSame.DifferenceLocation.AddNode(&diff.Node{Field: "ChildA", TypeName: "ChildA"}) 56 withNestedNodeDiff := withNode 57 withNestedNodeDiff.DifferenceLocation = withNestedNodeDiff.DifferenceLocation.AddNode(&diff.Node{Field: "ChildB", TypeName: "ChildA"}) 58 59 require.True(t, withNestedNode.Matches(withNestedNodeSame)) 60 require.False(t, withNestedNode.Matches(withNestedNodeDiff)) 61 }