github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/testhelpers/comparehelpers/deep_compare_test.go (about) 1 package comparehelpers_test 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/buildpacks/pack/testhelpers" 8 "github.com/buildpacks/pack/testhelpers/comparehelpers" 9 10 "github.com/heroku/color" 11 "github.com/sclevine/spec" 12 "github.com/sclevine/spec/report" 13 ) 14 15 func TestDeepContains(t *testing.T) { 16 color.Disable(true) 17 defer color.Disable(false) 18 spec.Run(t, "Builder Writer", testDeepContains, spec.Parallel(), spec.Report(report.Terminal{})) 19 } 20 21 func testDeepContains(t *testing.T, when spec.G, it spec.S) { 22 var ( 23 assert = testhelpers.NewAssertionManager(t) 24 ) 25 when("DeepContains", func() { 26 var ( 27 containerJSON string 28 container interface{} 29 ) 30 when("Searching for array containment", func() { 31 it.Before(func() { 32 containerJSON = `[ 33 { 34 "Name": "Platypus", 35 "Order": "Monotremata", 36 "Info": [ 37 { 38 "Population": 5000, 39 "Habitat": ["splish-spash", "waters"] 40 }, 41 { 42 "Geography" : "Moon" 43 }, 44 { 45 "Discography": "My records are all platynum" 46 } 47 ] 48 }, 49 { 50 "Name": "Quoll", 51 "Order": "Dasyuromorphia", 52 "Info": [] 53 } 54 ]` 55 56 assert.Succeeds(json.Unmarshal([]byte(containerJSON), &container)) 57 }) 58 when("subarray is contained", func() { 59 it("return true", func() { 60 containedJSON := `[{ "Geography":"Moon" }, {"Discography": "My records are all platynum"}]` 61 62 var contained interface{} 63 assert.Succeeds(json.Unmarshal([]byte(containedJSON), &contained)) 64 65 out := comparehelpers.DeepContains(container, contained) 66 assert.Equal(out, true) 67 }) 68 }) 69 when("subarray is not contained", func() { 70 it("returns false", func() { 71 containedJSON := `[{ "Geography":"Moon" }, {"Discography": "Splish-splash Cash III"}]` 72 73 var contained interface{} 74 assert.Succeeds(json.Unmarshal([]byte(containedJSON), &contained)) 75 76 out := comparehelpers.DeepContains(container, contained) 77 assert.Equal(out, false) 78 }) 79 }) 80 }) 81 when("Searching for map containment", func() { 82 var ( 83 containerJSON string 84 container interface{} 85 ) 86 it.Before(func() { 87 containerJSON = `[ 88 { 89 "Name": "Platypus", 90 "Order": "Monotremata", 91 "Info": [ 92 { 93 "Population": 5000, 94 "Size": "smol", 95 "Habitat": ["shallow", "waters"] 96 }, 97 { 98 "Geography" : "Moon" 99 }, 100 { 101 "Discography": "My records are all platynum" 102 } 103 ] 104 }, 105 { 106 "Name": "Quoll", 107 "Order": "Dasyuromorphia", 108 "Info": [] 109 } 110 ]` 111 assert.Succeeds(json.Unmarshal([]byte(containerJSON), &container)) 112 }) 113 when("map is contained", func() { 114 it("returns true", func() { 115 containedJSON := `{"Population": 5000, "Size": "smol"}` 116 var contained interface{} 117 assert.Succeeds(json.Unmarshal([]byte(containedJSON), &contained)) 118 119 out := comparehelpers.DeepContains(container, contained) 120 assert.Equal(out, true) 121 }) 122 }) 123 when("map is not contained", func() { 124 it("returns false", func() { 125 containedJSON := `{"Order": "Nemotode"}` 126 var contained interface{} 127 assert.Succeeds(json.Unmarshal([]byte(containedJSON), &contained)) 128 129 out := comparehelpers.DeepContains(container, contained) 130 assert.Equal(out, false) 131 }) 132 }) 133 }) 134 }) 135 when("json is not contained", func() { 136 it("return false", func() { 137 containerJSON := `[ 138 {"Name": "Platypus", "Order": "Monotremata"}, 139 {"Name": "Quoll", "Order": "Dasyuromorphia"} 140 ]` 141 var container interface{} 142 assert.Succeeds(json.Unmarshal([]byte(containerJSON), &container)) 143 144 containedJSON := `[{"Name": "Notapus", "Order": "Monotremata"}]` 145 146 var contained interface{} 147 assert.Succeeds(json.Unmarshal([]byte(containedJSON), &contained)) 148 149 out := comparehelpers.DeepContains(container, contained) 150 assert.Equal(out, false) 151 }) 152 }) 153 }