github.com/go-maxhub/gremlins@v1.0.1-0.20231227222204-b03a6a1e3e09/core/coverage/profile_test.go (about) 1 /* 2 * Copyright 2022 The Gremlins 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 coverage_test 18 19 import ( 20 "go/token" 21 "testing" 22 23 "github.com/go-maxhub/gremlins/core/coverage" 24 ) 25 26 func TestIsCovered(t *testing.T) { 27 testCases := []struct { 28 name string 29 proFilename string 30 posFilename string 31 proStartL int 32 proEndL int 33 proStartC int 34 proEndC int 35 posL int 36 posC int 37 expected bool 38 }{ 39 // Single line coverage 40 { 41 name: "true when single line, matches first column", 42 proFilename: "test", 43 proStartL: 10, 44 proEndL: 10, 45 proStartC: 10, 46 proEndC: 10, 47 posFilename: "test", 48 posL: 10, 49 posC: 10, 50 expected: true, 51 }, 52 { 53 name: "true when single line, matches last column", 54 proFilename: "test", 55 proStartL: 10, 56 proEndL: 10, 57 proStartC: 10, 58 proEndC: 11, 59 posFilename: "test", 60 posL: 10, 61 posC: 11, 62 expected: true, 63 }, 64 { 65 name: "true when single line, matches column lies in between", 66 proFilename: "test", 67 proStartL: 10, 68 proEndL: 10, 69 proStartC: 10, 70 proEndC: 12, 71 posFilename: "test", 72 posL: 10, 73 posC: 11, 74 expected: true, 75 }, 76 { 77 name: "false when single line and column lies before", 78 proFilename: "test", 79 proStartL: 10, 80 proEndL: 10, 81 proStartC: 10, 82 proEndC: 12, 83 posFilename: "test", 84 posL: 10, 85 posC: 9, 86 expected: false, 87 }, 88 { 89 name: "false when single line and column lies after", 90 proFilename: "test", 91 proStartL: 10, 92 proEndL: 10, 93 proStartC: 10, 94 proEndC: 12, 95 posFilename: "test", 96 posL: 10, 97 posC: 13, 98 expected: false, 99 }, 100 // Multi line - in between 101 { 102 name: "true when multi line and line lies in between (all cols covered)", 103 proFilename: "test", 104 proStartL: 10, 105 proEndL: 12, 106 proStartC: 10, 107 proEndC: 10, 108 posFilename: "test", 109 posL: 11, 110 posC: 1, 111 expected: true, 112 }, 113 // Multi line - first line 114 { 115 name: "true when covered on first line and col matches first col", 116 proFilename: "test", 117 proStartL: 10, 118 proEndL: 11, 119 proStartC: 10, 120 proEndC: 11, 121 posFilename: "test", 122 posL: 10, 123 posC: 10, 124 expected: true, 125 }, 126 { 127 name: "true when covered on first line and col is covered to end of line", 128 proFilename: "test", 129 proStartL: 10, 130 proEndL: 11, 131 proStartC: 10, 132 proEndC: 10, 133 posFilename: "test", 134 posL: 10, 135 posC: 200, 136 expected: true, 137 }, 138 { 139 name: "false when covered on first line and col lies before col", 140 proFilename: "test", 141 proStartL: 10, 142 proEndL: 11, 143 proStartC: 10, 144 proEndC: 10, 145 posFilename: "test", 146 posL: 10, 147 posC: 9, 148 expected: false, 149 }, 150 // Multi line - last line 151 { 152 name: "true when covered on last line and col matches last col", 153 proFilename: "test", 154 proStartL: 10, 155 proEndL: 11, 156 proStartC: 10, 157 proEndC: 11, 158 posFilename: "test", 159 posL: 11, 160 posC: 11, 161 expected: true, 162 }, 163 { 164 name: "true when covered on last line and col lies before last col (ignores first col)", 165 proFilename: "test", 166 proStartL: 10, 167 proEndL: 11, 168 proStartC: 10, 169 proEndC: 11, 170 posFilename: "test", 171 posL: 11, 172 posC: 9, 173 expected: true, 174 }, 175 { 176 name: "false when covered on last line and col lies after last col", 177 proFilename: "test", 178 proStartL: 10, 179 proEndL: 11, 180 proStartC: 10, 181 proEndC: 11, 182 posFilename: "test", 183 posL: 11, 184 posC: 12, 185 expected: false, 186 }, 187 } 188 189 for _, tc := range testCases { 190 tCase := tc 191 t.Run(tCase.name, func(t *testing.T) { 192 profile := coverage.Profile{ 193 tCase.proFilename: { 194 { 195 StartLine: tCase.proStartL, 196 StartCol: tCase.proStartC, 197 EndLine: tCase.proEndL, 198 EndCol: tCase.proEndC, 199 }, 200 }, 201 } 202 203 position := token.Position{ 204 Filename: tCase.posFilename, 205 Offset: 100, 206 Line: tCase.posL, 207 Column: tCase.posC, 208 } 209 210 got := profile.IsCovered(position) 211 212 if got != tCase.expected { 213 t.Errorf("expected coverage to be %v, got %v", tCase.expected, got) 214 } 215 }) 216 } 217 }