1package jd 2 3import ( 4 "testing" 5) 6 7func checkJson(ctx *testContext, a, b string) { 8 nodeA, err := ReadJsonString(a) 9 if err != nil { 10 ctx.t.Errorf(err.Error()) 11 } 12 nodeAJson := nodeA.Json(ctx.metadata...) 13 if nodeAJson != b { 14 ctx.t.Errorf("%v.Json() = %v. Want %v.", nodeA, nodeAJson, b) 15 } 16} 17 18func checkEqual(ctx *testContext, a, b string) { 19 nodeA, err := unmarshal([]byte(a)) 20 if err != nil { 21 ctx.t.Errorf(err.Error()) 22 } 23 nodeB, err := unmarshal([]byte(b)) 24 if err != nil { 25 ctx.t.Errorf(err.Error()) 26 } 27 if !nodeA.Equals(nodeB, ctx.metadata...) { 28 ctx.t.Errorf("%v.Equals(%v) == false. Want true.", nodeA, nodeB) 29 } 30 if !nodeB.Equals(nodeA, ctx.metadata...) { 31 ctx.t.Errorf("%v.Equals(%v) == false. Want true.", nodeA, nodeB) 32 } 33 if !nodeA.Equals(nodeA, ctx.metadata...) { 34 ctx.t.Errorf("%v.Equals(%v) == false. Want true.", nodeA, nodeB) 35 } 36 if !nodeB.Equals(nodeB, ctx.metadata...) { 37 ctx.t.Errorf("%v.Equals(%v) == false. Want true.", nodeA, nodeB) 38 } 39} 40 41func checkNotEqual(ctx *testContext, a, b string, metadata ...Metadata) { 42 nodeA, err := unmarshal([]byte(a)) 43 if err != nil { 44 ctx.t.Errorf(err.Error()) 45 } 46 nodeB, err := unmarshal([]byte(b)) 47 if err != nil { 48 ctx.t.Errorf(err.Error()) 49 } 50 if nodeA.Equals(nodeB, ctx.metadata...) { 51 ctx.t.Errorf("nodeA.Equals(nodeB) == true. Want false.") 52 } 53 if nodeB.Equals(nodeA, ctx.metadata...) { 54 ctx.t.Errorf("nodeB.Equals(nodeA) == true. Want false.") 55 } 56} 57 58func checkHash(ctx *testContext, a, b string, wantSame bool) { 59 nodeA, err := unmarshal([]byte(a)) 60 if err != nil { 61 ctx.t.Fatalf(err.Error()) 62 } 63 nodeB, err := unmarshal([]byte(b)) 64 if err != nil { 65 ctx.t.Fatalf(err.Error()) 66 } 67 hashA := nodeA.hashCode(ctx.metadata) 68 hashB := nodeB.hashCode(ctx.metadata) 69 if wantSame && hashA != hashB { 70 ctx.t.Errorf("%v.hashCode = %v. %v.hashCode = %v. Want the same.", 71 a, hashA, b, hashB) 72 } 73 if !wantSame && hashA == hashB { 74 ctx.t.Errorf("%v.hashCode = %v. %v.hashCode = %v. Want the different.", 75 a, hashA, b, hashB) 76 } 77} 78 79func checkDiff(ctx *testContext, a, b string, diffLines ...string) { 80 nodeA, err := ReadJsonString(a) 81 if err != nil { 82 ctx.t.Fatalf(err.Error()) 83 } 84 nodeB, err := ReadJsonString(b) 85 if err != nil { 86 ctx.t.Fatalf(err.Error()) 87 } 88 diff := "" 89 for _, dl := range diffLines { 90 diff += dl + "\n" 91 } 92 d := nodeA.Diff(nodeB, ctx.metadata...) 93 expectedDiff, err := ReadDiffString(diff) 94 if err != nil { 95 ctx.t.Fatalf(err.Error()) 96 } 97 want := expectedDiff.Render() 98 got := d.Render() 99 if got != want { 100 ctx.t.Errorf("%v.Diff(%v) = \n%v. Want %v.", nodeA, nodeB, got, want) 101 } 102} 103 104func checkPatch(ctx *testContext, a, e string, diffLines ...string) { 105 diffString := "" 106 for _, dl := range diffLines { 107 diffString += dl + "\n" 108 } 109 initial, err := ReadJsonString(a) 110 if err != nil { 111 ctx.t.Errorf(err.Error()) 112 } 113 diff, err := ReadDiffString(diffString) 114 if err != nil { 115 ctx.t.Errorf(err.Error()) 116 } 117 expect, err := ReadJsonString(e) 118 if err != nil { 119 ctx.t.Errorf(err.Error()) 120 } 121 b, err := initial.Patch(diff) 122 if err != nil { 123 ctx.t.Errorf(err.Error()) 124 } 125 if !expect.Equals(b, ctx.metadata...) { 126 ctx.t.Errorf("%v.Patch(%v) = %v. Want %v.", 127 a, diffLines, renderJson(b), e) 128 } 129} 130 131func checkPatchError(ctx *testContext, a string, diffLines ...string) { 132 diffString := "" 133 for _, dl := range diffLines { 134 diffString += dl + "\n" 135 } 136 initial, err := ReadJsonString(a) 137 if err != nil { 138 ctx.t.Errorf(err.Error()) 139 } 140 diff, err := ReadDiffString(diffString) 141 if err != nil { 142 ctx.t.Errorf(err.Error()) 143 } 144 b, err := initial.Patch(diff) 145 if b != nil { 146 ctx.t.Errorf("%v.Patch(%v) = %v. Want nil.", initial, diff, b) 147 } 148 if err == nil { 149 ctx.t.Errorf("Expected error. Got nil.") 150 } 151} 152 153func s(s ...string) []string { 154 return s 155} 156 157func m(m ...Metadata) []Metadata { 158 return m 159} 160 161type testContext struct { 162 t *testing.T 163 metadata []Metadata 164} 165 166func newTestContext(t *testing.T) *testContext { 167 return &testContext{ 168 t: t, 169 metadata: make([]Metadata, 0), 170 } 171} 172 173func (tc *testContext) withMetadata(metadata ...Metadata) *testContext { 174 tc.metadata = append(tc.metadata, metadata...) 175 return tc 176} 177