this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

tools/trim: convert inline tests to txtar

txtar is the preferred mechanism for tests that drive cue.
Convert all existing trim tests to txtar.
The txtar tests run as part of the existing TestTrimFiles target
(renamed from TestData to be a little more descriptive).
Remove TestX because the TxTarTest code supports DebugArchive which
allows easy overriding for the purposes of experimentation.

Signed-off-by: Matthew Sackman <matthew@cue.works>
Change-Id: I985f2e27b32fe01fb6b36b5e124cbb5c986c180e
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1197348
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+195 -296
+8
tools/trim/testdata/defaults_can_remove_non-defaults.txtar
··· 1 + # TODO: make this optional 2 + -- in.cue -- 3 + foo: [string]: a: *1 | int 4 + foo: b: a: 1 5 + -- out/trim -- 6 + == in.cue 7 + foo: [string]: a: *1 | int 8 + foo: b: {}
+27
tools/trim/testdata/do_not_overmark_comprehension.txtar
··· 1 + -- in.cue -- 2 + foo: multipath: { 3 + t: [string]: { x: 5 } 4 + 5 + // Don't remove u! 6 + t: u: { x: 5 } 7 + } 8 + 9 + group: { 10 + for k, v in foo { 11 + comp: "\(k)": v 12 + } 13 + } 14 + -- out/trim -- 15 + == in.cue 16 + foo: multipath: { 17 + t: [string]: {x: 5} 18 + 19 + // Don't remove u! 20 + t: u: {} 21 + } 22 + 23 + group: { 24 + for k, v in foo { 25 + comp: "\(k)": v 26 + } 27 + }
+7
tools/trim/testdata/do_not_remove_field.txtar
··· 1 + -- in.cue -- 2 + {[_]: x: "hello"} 3 + a: x: "hello" 4 + -- out/trim -- 5 + == in.cue 6 + {[_]: x: "hello"} 7 + a: {}
+9
tools/trim/testdata/issue303.txtar
··· 1 + -- in.cue -- 2 + foo: c: true 3 + foo: #M 4 + #M: c?: bool 5 + -- out/trim -- 6 + == in.cue 7 + foo: c: true 8 + foo: #M 9 + #M: c?: bool
+15
tools/trim/testdata/list_removal_1.txtar
··· 1 + -- in.cue -- 2 + service: [string]: { 3 + ports: [{a: 1}, {a: 1}, ...{ extra: 3 }] 4 + } 5 + service: a: { 6 + ports: [{a: 1}, {a: 1, extra: 3}, {}, { extra: 3 }] 7 + } 8 + -- out/trim -- 9 + == in.cue 10 + service: [string]: { 11 + ports: [{a: 1}, {a: 1}, ...{extra: 3}] 12 + } 13 + service: a: { 14 + ports: [{}, {extra: 3}, {}, {}] 15 + }
+13
tools/trim/testdata/list_removal_2.txtar
··· 1 + -- in.cue -- 2 + service: [string]: { 3 + ports: [{a: 1}, {a: 1}, ...{ extra: 3 }] 4 + } 5 + service: a: { 6 + ports: [{a: 1}, {a: 1,}] 7 + } 8 + -- out/trim -- 9 + == in.cue 10 + service: [string]: { 11 + ports: [{a: 1}, {a: 1}, ...{extra: 3}] 12 + } 13 + service: a: {}
+23
tools/trim/testdata/optional_does_not_remove_required.txtar
··· 1 + -- in.cue -- 2 + a: ["aFoo"]: 3 3 + a: aFoo: _ 4 + 5 + a: { 6 + {["aFoo"]: 3} 7 + aFoo: _ 8 + } 9 + 10 + ["aFoo"]: 3 11 + aFoo: _ 12 + -- out/trim -- 13 + == in.cue 14 + a: ["aFoo"]: 3 15 + a: aFoo: _ 16 + 17 + a: { 18 + {["aFoo"]: 3} 19 + aFoo: _ 20 + } 21 + 22 + ["aFoo"]: 3 23 + aFoo: _
+45
tools/trim/testdata/remove_due_to_simplification.txtar
··· 1 + -- in.cue -- 2 + foo: [string]: { 3 + t: [string]: { 4 + x: >=0 & <=5 5 + } 6 + } 7 + 8 + foo: multipath: { 9 + t: [string]: { 10 + // Combined with the other constraints, we know the value must be 5 and 11 + // thus the entry below can be eliminated. 12 + x: >=5 & <=8 & int 13 + } 14 + 15 + t: u: { x: 5 } 16 + } 17 + 18 + group: { 19 + for k, v in foo { 20 + comp: "\(k)": v 21 + } 22 + } 23 + -- out/trim -- 24 + == in.cue 25 + foo: [string]: { 26 + t: [string]: { 27 + x: >=0 & <=5 28 + } 29 + } 30 + 31 + foo: multipath: { 32 + t: [string]: { 33 + // Combined with the other constraints, we know the value must be 5 and 34 + // thus the entry below can be eliminated. 35 + x: >=5 & <=8 & int 36 + } 37 + 38 + t: u: {} 39 + } 40 + 41 + group: { 42 + for k, v in foo { 43 + comp: "\(k)": v 44 + } 45 + }
+18
tools/trim/testdata/remove_implied_interpolations.txtar
··· 1 + -- in.cue -- 2 + foo: [string]: { 3 + a: string 4 + b: "--\(a)--" 5 + } 6 + foo: entry: { 7 + a: "insert" 8 + b: "--insert--" 9 + } 10 + -- out/trim -- 11 + == in.cue 12 + foo: [string]: { 13 + a: string 14 + b: "--\(a)--" 15 + } 16 + foo: entry: { 17 + a: "insert" 18 + }
+29
tools/trim/testdata/remove_top-level_struct.txtar
··· 1 + -- in.cue -- 2 + a: b: 3 3 + for k, v in a { 4 + c: "\(k)": v 5 + } 6 + c: b: 3 7 + 8 + z: { 9 + 10 + a: b: 3 11 + for k, v in a { 12 + c: "\(k)": v 13 + } 14 + c: b: 3 15 + } 16 + -- out/trim -- 17 + == in.cue 18 + a: b: 3 19 + for k, v in a { 20 + c: "\(k)": v 21 + } 22 + 23 + z: { 24 + 25 + a: b: 3 26 + for k, v in a { 27 + c: "\(k)": v 28 + } 29 + }
+1 -296
tools/trim/trim_test.go
··· 17 17 import ( 18 18 "testing" 19 19 20 - "golang.org/x/tools/txtar" 21 - 22 - "cuelang.org/go/cue" 23 - "cuelang.org/go/cue/ast" 24 20 "cuelang.org/go/cue/cuecontext" 25 21 "cuelang.org/go/cue/errors" 26 - "cuelang.org/go/cue/format" 27 - "cuelang.org/go/cue/parser" 28 22 "cuelang.org/go/internal/core/runtime" 29 23 "cuelang.org/go/internal/cuetdtest" 30 24 "cuelang.org/go/internal/cuetxtar" ··· 37 31 matrix = cuetdtest.DefaultOnlyMatrix 38 32 ) 39 33 40 - func TestFiles(t *testing.T) { 41 - testCases := []struct { 42 - name string 43 - in string 44 - out string 45 - }{{ 46 - name: "optional does not remove required", 47 - in: ` 48 - a: ["aFoo"]: 3 49 - a: aFoo: _ 50 - 51 - a: { 52 - {["aFoo"]: 3} 53 - aFoo: _ 54 - } 55 - 56 - ["aFoo"]: 3 57 - aFoo: _ 58 - `, 59 - out: `a: ["aFoo"]: 3 60 - a: aFoo: _ 61 - 62 - a: { 63 - {["aFoo"]: 3} 64 - aFoo: _ 65 - } 66 - 67 - ["aFoo"]: 3 68 - aFoo: _ 69 - `, 70 - }, { 71 - // TODO: make this optional 72 - name: "defaults can remove non-defaults", 73 - in: ` 74 - foo: [string]: a: *1 | int 75 - foo: b: a: 1 76 - `, 77 - out: `foo: [string]: a: *1 | int 78 - foo: b: {} 79 - `, 80 - }, { 81 - name: "remove top-level struct", 82 - in: ` 83 - a: b: 3 84 - for k, v in a { 85 - c: "\(k)": v 86 - } 87 - c: b: 3 88 - 89 - z: { 90 - 91 - a: b: 3 92 - for k, v in a { 93 - c: "\(k)": v 94 - } 95 - c: b: 3 96 - } 97 - `, 98 - out: `a: b: 3 99 - for k, v in a { 100 - c: "\(k)": v 101 - } 102 - 103 - z: { 104 - 105 - a: b: 3 106 - for k, v in a { 107 - c: "\(k)": v 108 - } 109 - } 110 - `, 111 - }, { 112 - name: "do not remove field", 113 - in: ` 114 - {[_]: x: "hello"} 115 - a: x: "hello" 116 - `, 117 - out: ` 118 - {[_]: x: "hello"} 119 - a: {} 120 - `, 121 - }, { 122 - name: "issue303", 123 - in: ` 124 - foo: c: true 125 - foo: #M 126 - #M: c?: bool 127 - `, 128 - out: `foo: c: true 129 - foo: #M 130 - #M: c?: bool 131 - `, 132 - }, { 133 - name: "remove due to simplification", 134 - in: ` 135 - foo: [string]: { 136 - t: [string]: { 137 - x: >=0 & <=5 138 - } 139 - } 140 - 141 - foo: multipath: { 142 - t: [string]: { 143 - // Combined with the other constraints, we know the value must be 5 and 144 - // thus the entry below can be eliminated. 145 - x: >=5 & <=8 & int 146 - } 147 - 148 - t: u: { x: 5 } 149 - } 150 - 151 - group: { 152 - for k, v in foo { 153 - comp: "\(k)": v 154 - } 155 - } 156 - 157 - `, 158 - out: `foo: [string]: { 159 - t: [string]: { 160 - x: >=0 & <=5 161 - } 162 - } 163 - 164 - foo: multipath: { 165 - t: [string]: { 166 - 167 - x: >=5 & <=8 & int 168 - } 169 - 170 - t: u: {} 171 - } 172 - 173 - group: { 174 - for k, v in foo { 175 - comp: "\(k)": v 176 - } 177 - } 178 - `, 179 - }, { 180 - name: "list removal", 181 - in: ` 182 - service: [string]: { 183 - ports: [{a: 1}, {a: 1}, ...{ extra: 3 }] 184 - } 185 - service: a: { 186 - ports: [{a: 1}, {a: 1, extra: 3}, {}, { extra: 3 }] 187 - } 188 - `, 189 - out: `service: [string]: { 190 - ports: [{a: 1}, {a: 1}, ...{extra: 3}] 191 - } 192 - service: a: { 193 - ports: [{}, {extra: 3}, {}, {}] 194 - } 195 - `, 196 - }, { 197 - name: "list removal", 198 - in: ` 199 - service: [string]: { 200 - ports: [{a: 1}, {a: 1}, ...{ extra: 3 }] 201 - } 202 - service: a: { 203 - ports: [{a: 1}, {a: 1,}] 204 - } 205 - `, 206 - out: `service: [string]: { 207 - ports: [{a: 1}, {a: 1}, ...{extra: 3}] 208 - } 209 - service: a: {} 210 - `, 211 - }, { 212 - name: "do not overmark comprehension", 213 - in: ` 214 - foo: multipath: { 215 - t: [string]: { x: 5 } 216 - 217 - // Don't remove u! 218 - t: u: { x: 5 } 219 - } 220 - 221 - group: { 222 - for k, v in foo { 223 - comp: "\(k)": v 224 - } 225 - } 226 - 227 - `, 228 - out: `foo: multipath: { 229 - t: [string]: {x: 5} 230 - 231 - t: u: {} 232 - } 233 - 234 - group: { 235 - for k, v in foo { 236 - comp: "\(k)": v 237 - } 238 - } 239 - `, 240 - }, { 241 - name: "remove implied interpolations", 242 - in: ` 243 - foo: [string]: { 244 - a: string 245 - b: "--\(a)--" 246 - } 247 - foo: entry: { 248 - a: "insert" 249 - b: "--insert--" 250 - } 251 - `, 252 - out: `foo: [string]: { 253 - a: string 254 - b: "--\(a)--" 255 - } 256 - foo: entry: { 257 - a: "insert" 258 - } 259 - `, 260 - }} 261 - for _, tc := range testCases { 262 - matrix.Run(t, tc.name, func(t *cuetdtest.M) { 263 - f, err := parser.ParseFile("test", tc.in) 264 - if err != nil { 265 - t.Fatal(err) 266 - } 267 - r := cuecontext.New() 268 - t.UpdateRuntime((*runtime.Runtime)(r)) 269 - v := r.BuildFile(f) 270 - if err := v.Err(); err != nil { 271 - t.Fatal(err) 272 - } 273 - err = Files([]*ast.File{f}, v, &Config{Trace: false}) 274 - if err != nil { 275 - t.Fatal(err) 276 - } 277 - 278 - out := formatNode(t.T, f) 279 - if got := string(out); got != tc.out { 280 - t.Errorf("\ngot:\n%s\nwant:\n%s", got, tc.out) 281 - } 282 - }) 283 - } 284 - } 285 - 286 34 const trace = false 287 35 288 - func TestData(t *testing.T) { 36 + func TestTrimFiles(t *testing.T) { 289 37 test := cuetxtar.TxTarTest{ 290 38 Root: "./testdata", 291 39 Name: "trim", ··· 313 61 } 314 62 }) 315 63 } 316 - 317 - func formatNode(t *testing.T, n ast.Node) []byte { 318 - t.Helper() 319 - 320 - b, err := format.Node(n) 321 - if err != nil { 322 - t.Fatal(err) 323 - } 324 - return b 325 - } 326 - 327 - // For debugging, do not remove. 328 - func TestX(t *testing.T) { 329 - in := ` 330 - -- in.cue -- 331 - ` 332 - 333 - t.Skip() 334 - 335 - a := txtar.Parse([]byte(in)) 336 - instances := cuetxtar.Load(a, t.TempDir()) 337 - 338 - inst := cue.Build(instances)[0] 339 - if inst.Err != nil { 340 - t.Fatal(inst.Err) 341 - } 342 - 343 - files := instances[0].Files 344 - 345 - Debug = true 346 - 347 - err := Files(files, inst, &Config{ 348 - Trace: true, 349 - }) 350 - if err != nil { 351 - t.Fatal(err) 352 - } 353 - 354 - for _, f := range files { 355 - b := formatNode(t, f) 356 - t.Error(string(b)) 357 - } 358 - }