|
| 1 | +package buildattr |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "cuelang.org/go/cue/errors" |
| 7 | + "cuelang.org/go/cue/format" |
| 8 | + "cuelang.org/go/cue/parser" |
| 9 | + |
| 10 | + "github.com/go-quicktest/qt" |
| 11 | +) |
| 12 | + |
| 13 | +var shouldBuildFileTests = []struct { |
| 14 | + testName string |
| 15 | + syntax string |
| 16 | + tags map[string]bool |
| 17 | + wantOK bool |
| 18 | + wantAttr string |
| 19 | + wantError string |
| 20 | + wantTagCalls map[string]bool |
| 21 | +}{{ |
| 22 | + testName: "EmptyFile", |
| 23 | + syntax: "", |
| 24 | + wantOK: true, |
| 25 | +}, { |
| 26 | + testName: "PackageWithIf", |
| 27 | + syntax: ` |
| 28 | +@if(foo) |
| 29 | +
|
| 30 | +package something |
| 31 | +`, |
| 32 | + wantOK: false, |
| 33 | + wantTagCalls: map[string]bool{"foo": true}, |
| 34 | + wantAttr: "@if(foo)", |
| 35 | +}, { |
| 36 | + testName: "PackageWithIfSuccess", |
| 37 | + syntax: ` |
| 38 | +@if(foo) |
| 39 | +
|
| 40 | +package something |
| 41 | +`, |
| 42 | + tags: map[string]bool{"foo": true}, |
| 43 | + wantOK: true, |
| 44 | + wantTagCalls: map[string]bool{"foo": true}, |
| 45 | + wantAttr: "@if(foo)", |
| 46 | +}, { |
| 47 | + testName: "PackageWithIfAfterPackageClause", |
| 48 | + syntax: ` |
| 49 | +package something |
| 50 | +
|
| 51 | +@if(foo) |
| 52 | +`, |
| 53 | + wantOK: false, |
| 54 | + wantTagCalls: map[string]bool{"foo": true}, |
| 55 | + wantAttr: "@if(foo)", |
| 56 | +}, { |
| 57 | + testName: "InvalidExpr", |
| 58 | + syntax: ` |
| 59 | +
|
| 60 | +@if(foo + bar) |
| 61 | +
|
| 62 | +package something |
| 63 | +`, |
| 64 | + wantOK: false, |
| 65 | + wantAttr: "@if(foo + bar)", |
| 66 | + wantError: `invalid operator \+ |
| 67 | +`, |
| 68 | +}, { |
| 69 | + testName: "MultipleIfAttributes", |
| 70 | + syntax: ` |
| 71 | +
|
| 72 | +@if(foo) |
| 73 | +@if(bar) |
| 74 | +
|
| 75 | +package something |
| 76 | +`, |
| 77 | + wantOK: false, |
| 78 | + wantError: `previous declaration here: |
| 79 | + testfile.cue:3:1 |
| 80 | +multiple @if attributes: |
| 81 | + testfile.cue:4:1 |
| 82 | +`, |
| 83 | +}, { |
| 84 | + testName: "MultipleIfAttributesWithOneAfterPackage", |
| 85 | + syntax: ` |
| 86 | +
|
| 87 | +@if(foo) |
| 88 | +
|
| 89 | +package something |
| 90 | +
|
| 91 | +@if(bar) |
| 92 | +`, |
| 93 | + wantOK: false, |
| 94 | + wantTagCalls: map[string]bool{"foo": true}, |
| 95 | + wantError: `previous declaration here: |
| 96 | + testfile.cue:3:1 |
| 97 | +multiple @if attributes: |
| 98 | + testfile.cue:7:1 |
| 99 | +`, |
| 100 | +}, { |
| 101 | + testName: "And#0", |
| 102 | + syntax: ` |
| 103 | +@if(foo && bar) |
| 104 | +
|
| 105 | +package something |
| 106 | +`, |
| 107 | + wantOK: false, |
| 108 | + wantAttr: "@if(foo && bar)", |
| 109 | + wantTagCalls: map[string]bool{ |
| 110 | + "foo": true, |
| 111 | + "bar": true, |
| 112 | + }, |
| 113 | +}, { |
| 114 | + testName: "And#1", |
| 115 | + syntax: ` |
| 116 | +@if(foo && bar) |
| 117 | +
|
| 118 | +package something |
| 119 | +`, |
| 120 | + tags: map[string]bool{"foo": true}, |
| 121 | + wantOK: false, |
| 122 | + wantAttr: "@if(foo && bar)", |
| 123 | + wantTagCalls: map[string]bool{ |
| 124 | + "foo": true, |
| 125 | + "bar": true, |
| 126 | + }, |
| 127 | +}, { |
| 128 | + testName: "And#2", |
| 129 | + syntax: ` |
| 130 | +@if(foo && bar) |
| 131 | +
|
| 132 | +package something |
| 133 | +`, |
| 134 | + tags: map[string]bool{"bar": true}, |
| 135 | + wantOK: false, |
| 136 | + wantAttr: "@if(foo && bar)", |
| 137 | + wantTagCalls: map[string]bool{ |
| 138 | + "foo": true, |
| 139 | + "bar": true, |
| 140 | + }, |
| 141 | +}, { |
| 142 | + testName: "And#3", |
| 143 | + syntax: ` |
| 144 | +@if(foo && bar) |
| 145 | +
|
| 146 | +package something |
| 147 | +`, |
| 148 | + tags: map[string]bool{"foo": true, "bar": true}, |
| 149 | + wantOK: true, |
| 150 | + wantAttr: "@if(foo && bar)", |
| 151 | + wantTagCalls: map[string]bool{ |
| 152 | + "foo": true, |
| 153 | + "bar": true, |
| 154 | + }, |
| 155 | +}, { |
| 156 | + testName: "Or#0", |
| 157 | + syntax: ` |
| 158 | +@if(foo || bar) |
| 159 | +
|
| 160 | +package something |
| 161 | +`, |
| 162 | + wantOK: false, |
| 163 | + wantAttr: "@if(foo || bar)", |
| 164 | + wantTagCalls: map[string]bool{ |
| 165 | + "foo": true, |
| 166 | + "bar": true, |
| 167 | + }, |
| 168 | +}, { |
| 169 | + testName: "Or#1", |
| 170 | + syntax: ` |
| 171 | +@if(foo || bar) |
| 172 | +
|
| 173 | +package something |
| 174 | +`, |
| 175 | + tags: map[string]bool{"foo": true}, |
| 176 | + wantOK: true, |
| 177 | + wantAttr: "@if(foo || bar)", |
| 178 | + wantTagCalls: map[string]bool{ |
| 179 | + "foo": true, |
| 180 | + "bar": true, |
| 181 | + }, |
| 182 | +}, { |
| 183 | + testName: "Or#2", |
| 184 | + syntax: ` |
| 185 | +@if(foo || bar) |
| 186 | +
|
| 187 | +package something |
| 188 | +`, |
| 189 | + tags: map[string]bool{"bar": true}, |
| 190 | + wantOK: true, |
| 191 | + wantAttr: "@if(foo || bar)", |
| 192 | + wantTagCalls: map[string]bool{ |
| 193 | + "foo": true, |
| 194 | + "bar": true, |
| 195 | + }, |
| 196 | +}, { |
| 197 | + testName: "Or#3", |
| 198 | + syntax: ` |
| 199 | +@if(foo || bar) |
| 200 | +
|
| 201 | +package something |
| 202 | +`, |
| 203 | + tags: map[string]bool{"foo": true, "bar": true}, |
| 204 | + wantOK: true, |
| 205 | + wantAttr: "@if(foo || bar)", |
| 206 | + wantTagCalls: map[string]bool{ |
| 207 | + "foo": true, |
| 208 | + "bar": true, |
| 209 | + }, |
| 210 | +}, { |
| 211 | + testName: "Not#0", |
| 212 | + syntax: ` |
| 213 | +@if(!foo) |
| 214 | +
|
| 215 | +package something |
| 216 | +`, |
| 217 | + wantOK: true, |
| 218 | + wantAttr: "@if(!foo)", |
| 219 | + wantTagCalls: map[string]bool{ |
| 220 | + "foo": true, |
| 221 | + }, |
| 222 | +}, { |
| 223 | + testName: "Not#1", |
| 224 | + syntax: ` |
| 225 | +@if(!foo) |
| 226 | +
|
| 227 | +package something |
| 228 | +`, |
| 229 | + tags: map[string]bool{"foo": true}, |
| 230 | + wantOK: false, |
| 231 | + wantAttr: "@if(!foo)", |
| 232 | + wantTagCalls: map[string]bool{ |
| 233 | + "foo": true, |
| 234 | + }, |
| 235 | +}, { |
| 236 | + testName: "ComplexExpr", |
| 237 | + syntax: ` |
| 238 | +@if(foo || (!bar && baz)) |
| 239 | +
|
| 240 | +package something |
| 241 | +`, |
| 242 | + tags: map[string]bool{ |
| 243 | + "baz": true, |
| 244 | + }, |
| 245 | + wantOK: false, |
| 246 | + wantError: `invalid type \*ast.ParenExpr in build attribute |
| 247 | +`, |
| 248 | + wantTagCalls: map[string]bool{ |
| 249 | + "foo": true, |
| 250 | + "bar": true, |
| 251 | + "baz": true, |
| 252 | + }, |
| 253 | + wantAttr: "@if(foo || (!bar && baz))", |
| 254 | +}} |
| 255 | + |
| 256 | +func TestShouldBuildFile(t *testing.T) { |
| 257 | + for _, test := range shouldBuildFileTests { |
| 258 | + t.Run(test.testName, func(t *testing.T) { |
| 259 | + f, err := parser.ParseFile("testfile.cue", test.syntax) |
| 260 | + qt.Assert(t, qt.IsNil(err)) |
| 261 | + tagsUsed := make(map[string]bool) |
| 262 | + ok, attr, err := ShouldBuildFile(f, func(tag string) bool { |
| 263 | + tagsUsed[tag] = true |
| 264 | + return test.tags[tag] |
| 265 | + }) |
| 266 | + qt.Check(t, qt.Equals(ok, test.wantOK)) |
| 267 | + if test.wantAttr == "" { |
| 268 | + qt.Assert(t, qt.IsNil(attr)) |
| 269 | + } else { |
| 270 | + qt.Assert(t, qt.Not(qt.IsNil(attr))) |
| 271 | + attrStr, err := format.Node(attr) |
| 272 | + qt.Assert(t, qt.IsNil(err)) |
| 273 | + qt.Assert(t, qt.Equals(string(attrStr), test.wantAttr)) |
| 274 | + } |
| 275 | + if test.wantError != "" { |
| 276 | + qt.Assert(t, qt.Not(qt.IsNil(err))) |
| 277 | + qt.Assert(t, qt.Matches(errors.Details(err, nil), test.wantError)) |
| 278 | + qt.Assert(t, qt.Equals(ok, false)) |
| 279 | + return |
| 280 | + } |
| 281 | + qt.Assert(t, qt.IsNil(err)) |
| 282 | + if len(tagsUsed) == 0 { |
| 283 | + tagsUsed = nil |
| 284 | + } |
| 285 | + qt.Check(t, qt.DeepEquals(tagsUsed, test.wantTagCalls)) |
| 286 | + }) |
| 287 | + } |
| 288 | +} |
0 commit comments