github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/gopherage/pkg/cov/equality_test.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package cov 18 19 import ( 20 "testing" 21 22 "golang.org/x/tools/cover" 23 ) 24 25 func TestBlocksEqualValid(t *testing.T) { 26 a := cover.ProfileBlock{ 27 StartLine: 1, 28 StartCol: 2, 29 EndLine: 3, 30 EndCol: 4, 31 NumStmt: 5, 32 Count: 6, 33 } 34 b := cover.ProfileBlock{ 35 StartLine: 1, 36 StartCol: 2, 37 EndLine: 3, 38 EndCol: 4, 39 NumStmt: 5, 40 Count: 7, 41 } 42 if !blocksEqual(a, b) { 43 t.Error("equivalent blocks treated as mismatching") 44 } 45 } 46 47 func TestBlocksEqualBadStartLine(t *testing.T) { 48 a := cover.ProfileBlock{ 49 StartLine: 1, 50 StartCol: 2, 51 EndLine: 3, 52 EndCol: 4, 53 NumStmt: 5, 54 Count: 6, 55 } 56 b := cover.ProfileBlock{ 57 StartLine: 8, 58 StartCol: 2, 59 EndLine: 3, 60 EndCol: 4, 61 NumStmt: 5, 62 Count: 7, 63 } 64 if blocksEqual(a, b) { 65 t.Error("mismatching StartLine considered equivalent") 66 } 67 } 68 69 func TestBlocksEqualBadStartCol(t *testing.T) { 70 a := cover.ProfileBlock{ 71 StartLine: 1, 72 StartCol: 2, 73 EndLine: 3, 74 EndCol: 4, 75 NumStmt: 5, 76 Count: 6, 77 } 78 b := cover.ProfileBlock{ 79 StartLine: 1, 80 StartCol: 8, 81 EndLine: 3, 82 EndCol: 4, 83 NumStmt: 5, 84 Count: 7, 85 } 86 if blocksEqual(a, b) { 87 t.Error("mismatching StartCol considered equivalent") 88 } 89 } 90 91 func TestBlocksEqualBadEndLine(t *testing.T) { 92 a := cover.ProfileBlock{ 93 StartLine: 1, 94 StartCol: 2, 95 EndLine: 3, 96 EndCol: 4, 97 NumStmt: 5, 98 Count: 6, 99 } 100 b := cover.ProfileBlock{ 101 StartLine: 1, 102 StartCol: 2, 103 EndLine: 8, 104 EndCol: 4, 105 NumStmt: 5, 106 Count: 7, 107 } 108 if blocksEqual(a, b) { 109 t.Error("mismatching EndLine considered equivalent") 110 } 111 } 112 113 func TestBlocksEqualBadEndCol(t *testing.T) { 114 a := cover.ProfileBlock{ 115 StartLine: 1, 116 StartCol: 2, 117 EndLine: 3, 118 EndCol: 4, 119 NumStmt: 5, 120 Count: 6, 121 } 122 b := cover.ProfileBlock{ 123 StartLine: 1, 124 StartCol: 2, 125 EndLine: 3, 126 EndCol: 8, 127 NumStmt: 5, 128 Count: 7, 129 } 130 if blocksEqual(a, b) { 131 t.Error("mismatching EndCol considered equivalent") 132 } 133 } 134 135 func TestBlocksEqualBadNumStmt(t *testing.T) { 136 a := cover.ProfileBlock{ 137 StartLine: 1, 138 StartCol: 2, 139 EndLine: 3, 140 EndCol: 4, 141 NumStmt: 5, 142 Count: 6, 143 } 144 b := cover.ProfileBlock{ 145 StartLine: 1, 146 StartCol: 2, 147 EndLine: 3, 148 EndCol: 4, 149 NumStmt: 8, 150 Count: 7, 151 } 152 if blocksEqual(a, b) { 153 t.Error("mismatching NumStmt considered equivalent") 154 } 155 } 156 157 func TestEnsureProfilesMatch(t *testing.T) { 158 a := &cover.Profile{ 159 FileName: "a.go", 160 Mode: "count", 161 Blocks: []cover.ProfileBlock{ 162 {StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3}, 163 {StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2}, 164 }, 165 } 166 b := &cover.Profile{ 167 FileName: "a.go", 168 Mode: "count", 169 Blocks: []cover.ProfileBlock{ 170 {StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 7}, 171 {StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2}, 172 }, 173 } 174 175 if err := ensureProfilesMatch(a, b); err != nil { 176 t.Errorf("unexpected error comparing matching profiles: %v", err) 177 } 178 } 179 180 func TestEnsureProfilesMatchBadName(t *testing.T) { 181 a := &cover.Profile{ 182 FileName: "a.go", 183 Mode: "count", 184 Blocks: []cover.ProfileBlock{ 185 {StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3}, 186 {StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2}, 187 }, 188 } 189 b := &cover.Profile{ 190 FileName: "b.go", 191 Mode: "count", 192 Blocks: []cover.ProfileBlock{ 193 {StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 7}, 194 {StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2}, 195 }, 196 } 197 198 if ensureProfilesMatch(a, b) == nil { 199 t.Errorf("expected profiles with mismatching FileName to not match") 200 } 201 } 202 203 func TestEnsureProfilesMatchBadMode(t *testing.T) { 204 a := &cover.Profile{ 205 FileName: "a.go", 206 Mode: "count", 207 Blocks: []cover.ProfileBlock{ 208 {StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3}, 209 {StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2}, 210 }, 211 } 212 b := &cover.Profile{ 213 FileName: "a.go", 214 Mode: "set", 215 Blocks: []cover.ProfileBlock{ 216 {StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 7}, 217 {StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2}, 218 }, 219 } 220 221 if ensureProfilesMatch(a, b) == nil { 222 t.Errorf("expected profiles with mismatching Mode to not match") 223 } 224 } 225 226 func TestEnsureProfilesMatchBadBlockCount(t *testing.T) { 227 a := &cover.Profile{ 228 FileName: "a.go", 229 Mode: "count", 230 Blocks: []cover.ProfileBlock{ 231 {StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3}, 232 {StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2}, 233 }, 234 } 235 b := &cover.Profile{ 236 FileName: "b.go", 237 Mode: "count", 238 Blocks: []cover.ProfileBlock{ 239 {StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 7}, 240 }, 241 } 242 243 if ensureProfilesMatch(a, b) == nil { 244 t.Errorf("expected profiles with mismatching block count to not match") 245 } 246 } 247 248 func TestEnsureProfilesMatchBadBlock(t *testing.T) { 249 a := &cover.Profile{ 250 FileName: "a.go", 251 Mode: "count", 252 Blocks: []cover.ProfileBlock{ 253 {StartLine: 1, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 3}, 254 {StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2}, 255 }, 256 } 257 b := &cover.Profile{ 258 FileName: "b.go", 259 Mode: "count", 260 Blocks: []cover.ProfileBlock{ 261 {StartLine: 2, StartCol: 14, EndLine: 5, EndCol: 13, NumStmt: 4, Count: 7}, 262 {StartLine: 7, StartCol: 4, EndLine: 12, EndCol: 4, NumStmt: 3, Count: 2}, 263 }, 264 } 265 266 if ensureProfilesMatch(a, b) == nil { 267 t.Errorf("expected profiles with mismatching block content to not match") 268 } 269 }