1// Copyright 2021 The Tint Authors. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package substr 16 17import ( 18 "strings" 19 "testing" 20) 21 22func TestFixSubstr(t *testing.T) { 23 type test struct { 24 body string 25 substr string 26 expect string 27 } 28 29 for _, test := range []test{ 30 { 31 body: "abc_def_ghi_jkl_mno", 32 substr: "def_XXX_jkl", 33 expect: "def_ghi_jkl", 34 }, 35 { 36 body: "abc\ndef\nghi\njkl\nmno", 37 substr: "def\nXXX\njkl", 38 expect: "def\nghi\njkl", 39 }, 40 { 41 body: "aaaaa12345ccccc", 42 substr: "1x345", 43 expect: "12345", 44 }, 45 { 46 body: "aaaaa12345ccccc", 47 substr: "12x45", 48 expect: "12345", 49 }, 50 { 51 body: "aaaaa12345ccccc", 52 substr: "123x5", 53 expect: "12345", 54 }, 55 { 56 body: "aaaaaaaaaaaaa", 57 substr: "bbbbbbbbbbbbb", 58 expect: "", // cannot produce a sensible diff 59 }, { /////////////////////////////////////////////////////////////////// 60 body: `Return{ 61 { 62 ScalarConstructor[not set]{42u} 63 } 64} 65`, 66 substr: `Return{ 67 { 68 ScalarConstructor[not set]{42} 69 } 70}`, 71 expect: `Return{ 72 { 73 ScalarConstructor[not set]{42u} 74 } 75}`, 76 }, { /////////////////////////////////////////////////////////////////// 77 body: `VariableDeclStatement{ 78 Variable{ 79 x_1 80 function 81 __u32 82 } 83} 84Assignment{ 85 Identifier[not set]{x_1} 86 ScalarConstructor[not set]{42u} 87} 88Assignment{ 89 Identifier[not set]{x_1} 90 ScalarConstructor[not set]{0u} 91} 92Return{} 93`, 94 substr: `Assignment{ 95 Identifier[not set]{x_1} 96 ScalarConstructor[not set]{42} 97} 98Assignment{ 99 Identifier[not set]{x_1} 100 ScalarConstructor[not set]{0} 101}`, 102 expect: `Assignment{ 103 Identifier[not set]{x_1} 104 ScalarConstructor[not set]{42u} 105} 106Assignment{ 107 Identifier[not set]{x_1} 108 ScalarConstructor[not set]{0u} 109}`, 110 }, { /////////////////////////////////////////////////////////////////// 111 body: `VariableDeclStatement{ 112 Variable{ 113 a 114 function 115 __bool 116 { 117 ScalarConstructor[not set]{true} 118 } 119 } 120} 121VariableDeclStatement{ 122 Variable{ 123 b 124 function 125 __bool 126 { 127 ScalarConstructor[not set]{false} 128 } 129 } 130} 131VariableDeclStatement{ 132 Variable{ 133 c 134 function 135 __i32 136 { 137 ScalarConstructor[not set]{-1} 138 } 139 } 140} 141VariableDeclStatement{ 142 Variable{ 143 d 144 function 145 __u32 146 { 147 ScalarConstructor[not set]{1u} 148 } 149 } 150} 151VariableDeclStatement{ 152 Variable{ 153 e 154 function 155 __f32 156 { 157 ScalarConstructor[not set]{1.500000} 158 } 159 } 160} 161`, 162 substr: `VariableDeclStatement{ 163 Variable{ 164 a 165 function 166 __bool 167 { 168 ScalarConstructor[not set]{true} 169 } 170 } 171} 172VariableDeclStatement{ 173 Variable{ 174 b 175 function 176 __bool 177 { 178 ScalarConstructor[not set]{false} 179 } 180 } 181} 182VariableDeclStatement{ 183 Variable{ 184 c 185 function 186 __i32 187 { 188 ScalarConstructor[not set]{-1} 189 } 190 } 191} 192VariableDeclStatement{ 193 Variable{ 194 d 195 function 196 __u32 197 { 198 ScalarConstructor[not set]{1} 199 } 200 } 201} 202VariableDeclStatement{ 203 Variable{ 204 e 205 function 206 __f32 207 { 208 ScalarConstructor[not set]{1.500000} 209 } 210 } 211} 212`, 213 expect: `VariableDeclStatement{ 214 Variable{ 215 a 216 function 217 __bool 218 { 219 ScalarConstructor[not set]{true} 220 } 221 } 222} 223VariableDeclStatement{ 224 Variable{ 225 b 226 function 227 __bool 228 { 229 ScalarConstructor[not set]{false} 230 } 231 } 232} 233VariableDeclStatement{ 234 Variable{ 235 c 236 function 237 __i32 238 { 239 ScalarConstructor[not set]{-1} 240 } 241 } 242} 243VariableDeclStatement{ 244 Variable{ 245 d 246 function 247 __u32 248 { 249 ScalarConstructor[not set]{1u} 250 } 251 } 252} 253VariableDeclStatement{ 254 Variable{ 255 e 256 function 257 __f32 258 { 259 ScalarConstructor[not set]{1.500000} 260 } 261 } 262} 263`, 264 }, 265 } { 266 body := strings.ReplaceAll(test.body, "\n", "") 267 substr := strings.ReplaceAll(test.substr, "\n", "") 268 expect := strings.ReplaceAll(test.expect, "\n", ``) 269 got := strings.ReplaceAll(Fix(test.body, test.substr), "\n", "") 270 if got != expect { 271 t.Errorf("Test failure:\nbody: '%v'\nsubstr: '%v'\nexpect: '%v'\ngot: '%v'\n\n", body, substr, expect, got) 272 } 273 274 } 275} 276