| 🛟 Updated. 28d5985 k33g 16h ago | 1 | package syntax |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // yamlClassOf returns the class covering the first occurrence of a piece of |
| 9 | // text in a YAML document. |
| 10 | func yamlClassOf(t *testing.T, src, want string) Class { |
| 11 | t.Helper() |
| 12 | |
| 13 | index := strings.Index(src, want) |
| 14 | if index < 0 { |
| 15 | t.Fatalf("%q does not appear in the source", want) |
| 16 | } |
| 17 | line := strings.Count(src[:index], "\n") |
| 18 | col := index - (strings.LastIndex(src[:index], "\n") + 1) |
| 19 | |
| 20 | class, ok := classAt(Highlight(LanguageYAML, src), line, col) |
| 21 | if !ok { |
| 22 | t.Fatalf("no span covers %q at line %d column %d", want, line, col) |
| 23 | } |
| 24 | return class |
| 25 | } |
| 26 | |
| 27 | func TestYAMLColoursACompseFilesParts(t *testing.T) { |
| 28 | const src = `# a compose file |
| 29 | services: |
| 30 | web: |
| 31 | image: "nginx:1.27" |
| 32 | replicas: 3 |
| 33 | enabled: true |
| 34 | ` |
| 35 | |
| 36 | tests := map[string]Class{ |
| 37 | "# a compose file": ClassComment, |
| 38 | "services": ClassIdentifier, |
| 39 | `"nginx:1.27"`: ClassString, |
| 40 | "3": ClassNumber, |
| 41 | "true": ClassConstant, |
| 42 | } |
| 43 | |
| 44 | for text, want := range tests { |
| 45 | t.Run(text, func(t *testing.T) { |
| 46 | if got := yamlClassOf(t, src, text); got != want { |
| 47 | t.Errorf("%q is %v, want %v", text, got, want) |
| 48 | } |
| 49 | }) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestYAMLColoursTheColonAsPunctuation(t *testing.T) { |
| 54 | spans := Highlight(LanguageYAML, "image: nginx") |
| 55 | |
| 56 | class, ok := classAt(spans, 0, len("image")) |
| 57 | if !ok || class != ClassPunctuation { |
| 58 | t.Errorf("the colon after a key is %v (covered: %v), want punctuation", class, ok) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestYAMLColoursAListMarker(t *testing.T) { |
| 63 | const src = "ports:\n - \"8080:80\"\n" |
| 64 | |
| 65 | spans := Highlight(LanguageYAML, src) |
| 66 | class, ok := classAt(spans, 1, 2) // the "-" |
| 67 | if !ok || class != ClassPunctuation { |
| 68 | t.Errorf("the list marker is %v (covered: %v), want punctuation", class, ok) |
| 69 | } |
| 70 | if got := yamlClassOf(t, src, `"8080:80"`); got != ClassString { |
| 71 | t.Errorf("the quoted entry is %v, want string", got) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestAColonWithNoSpaceAfterItStaysInsideTheValue(t *testing.T) { |
| 76 | // `image: nginx:1.27` and `url: http://x` are one value each. A colon is a |
| 77 | // separator only when a space follows it — otherwise every image tag and |
| 78 | // every URL in every compose file comes out in three colours. |
| 79 | tests := map[string]string{ |
| 80 | "image: nginx:1.27": "nginx:1.27", |
| 81 | "url: http://example.com/x": "http://example.com/x", |
| 82 | } |
| 83 | |
| 84 | for src, value := range tests { |
| 85 | t.Run(value, func(t *testing.T) { |
| 86 | spans := Highlight(LanguageYAML, src) |
| 87 | start := strings.Index(src, value) |
| 88 | |
| 89 | for col := start; col < start+len(value); col++ { |
| 90 | class, ok := classAt(spans, 0, col) |
| 91 | if !ok || class != ClassIdentifier { |
| 92 | t.Fatalf("column %d of %q is %v (covered: %v); the value was split", col-start, value, class, ok) |
| 93 | } |
| 94 | } |
| 95 | }) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func TestAColonWithASpaceAfterItIsStillASeparator(t *testing.T) { |
| 100 | // The other half of the rule, in flow style where it is the only marker. |
| 101 | spans := Highlight(LanguageYAML, "a: {b: 1}") |
| 102 | |
| 103 | class, ok := classAt(spans, 0, len("a: {b")) |
| 104 | if !ok || class != ClassPunctuation { |
| 105 | t.Errorf("the colon in a flow mapping is %v (covered: %v), want punctuation", class, ok) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestYAMLColoursAnAnchorAndItsAlias(t *testing.T) { |
| 110 | const src = "defaults: &base\n a: 1\nuse:\n <<: *base\n" |
| 111 | |
| 112 | if got := yamlClassOf(t, src, "&base"); got != ClassBuiltin { |
| 113 | t.Errorf("the anchor is %v, want builtin", got) |
| 114 | } |
| 115 | if got := yamlClassOf(t, src, "*base"); got != ClassBuiltin { |
| 116 | t.Errorf("the alias is %v, want builtin", got) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func TestYAMLColoursATag(t *testing.T) { |
| 121 | if got := yamlClassOf(t, "value: !!str 3\n", "!!str"); got != ClassType { |
| 122 | t.Errorf("a tag is %v, want type", got) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | func TestYAMLColoursDocumentMarkers(t *testing.T) { |
| 127 | for _, marker := range []string{"---", "..."} { |
| 128 | t.Run(marker, func(t *testing.T) { |
| 129 | spans := Highlight(LanguageYAML, marker+"\na: 1\n") |
| 130 | if class, ok := classAt(spans, 0, 0); !ok || class != ClassPunctuation { |
| 131 | t.Errorf("%q is %v (covered: %v), want punctuation", marker, class, ok) |
| 132 | } |
| 133 | }) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | func TestYAMLTreatsTheGenerousBooleanSpellings(t *testing.T) { |
| 138 | // YAML 1.1 reads all of these as booleans, and most parsers in the wild |
| 139 | // still do. A reader wants to see that `no` is not the string "no". |
| 140 | for _, word := range []string{"true", "false", "yes", "no", "on", "off", "null"} { |
| 141 | t.Run(word, func(t *testing.T) { |
| 142 | if got := yamlClassOf(t, "key: "+word+"\n", word); got != ClassConstant { |
| 143 | t.Errorf("%q is %v, want constant", word, got) |
| 144 | } |
| 145 | }) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func TestYAMLColoursABlockScalarAcrossLines(t *testing.T) { |
| 150 | // The one construct whose extent is decided by indentation rather than by |
| 151 | // a delimiter, and the one every CI file uses. |
| 152 | const src = "run: |\n set -e\n echo hello\nnext: 1\n" |
| 153 | spans := Highlight(LanguageYAML, src) |
| 154 | |
| 155 | for _, line := range []int{1, 2} { |
| 156 | if class, ok := classAt(spans, line, 2); !ok || class != ClassString { |
| 157 | t.Errorf("line %d of the block is %v (covered: %v), want string", line, class, ok) |
| 158 | } |
| 159 | } |
| 160 | if class, ok := classAt(spans, 3, 0); !ok || class != ClassIdentifier { |
| 161 | t.Errorf("the key after the block is %v (covered: %v), want it read as code again", class, ok) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | func TestABlankLineDoesNotEndABlockScalar(t *testing.T) { |
| 166 | // A literal scalar keeps its empty lines, so ending the block at the first |
| 167 | // paragraph break would cut a shell script in a CI file in half. |
| 168 | const src = "run: |\n first\n\n second\nnext: 1\n" |
| 169 | spans := Highlight(LanguageYAML, src) |
| 170 | |
| 171 | if class, ok := classAt(spans, 3, 2); !ok || class != ClassString { |
| 172 | t.Errorf("the line after the blank one is %v (covered: %v), want it still in the block", class, ok) |
| 173 | } |
| 174 | if class, ok := classAt(spans, 4, 0); !ok || class != ClassIdentifier { |
| 175 | t.Errorf("the key after the block is %v (covered: %v), want code", class, ok) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | func TestABlockScalarEndsAtTheFirstLessIndentedLine(t *testing.T) { |
| 180 | const src = "a:\n run: |\n deep\n other: 1\n" |
| 181 | spans := Highlight(LanguageYAML, src) |
| 182 | |
| 183 | if class, ok := classAt(spans, 2, 4); !ok || class != ClassString { |
| 184 | t.Errorf("the block's body is %v (covered: %v), want string", class, ok) |
| 185 | } |
| 186 | if class, ok := classAt(spans, 3, 2); !ok || class != ClassIdentifier { |
| 187 | t.Errorf("the sibling key is %v (covered: %v), want it out of the block", class, ok) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | func TestTheFoldedBlockScalarWorksTheSameWay(t *testing.T) { |
| 192 | spans := Highlight(LanguageYAML, "note: >-\n folded text\nnext: 1\n") |
| 193 | |
| 194 | if class, ok := classAt(spans, 1, 2); !ok || class != ClassString { |
| 195 | t.Errorf("a folded block's body is %v (covered: %v), want string", class, ok) |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | func TestYAMLColoursAComment(t *testing.T) { |
| 200 | if got := yamlClassOf(t, "a: 1 # why\n", "# why"); got != ClassComment { |
| 201 | t.Errorf("a trailing comment is %v, want comment", got) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | func TestAHashInsideAValueIsNotAComment(t *testing.T) { |
| 206 | // YAML needs a space before a `#` for it to start a comment, so a colour |
| 207 | // or a fragment stays part of the scalar. |
| 208 | spans := Highlight(LanguageYAML, "colour: ff#00aa\n") |
| 209 | |
| 210 | if class, ok := classAt(spans, 0, len("colour: ff#")); ok && class == ClassComment { |
| 211 | t.Error("a # with no space before it was read as a comment") |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | func TestYAMLColoursAQuotedKey(t *testing.T) { |
| 216 | if got := yamlClassOf(t, `"a key": 1`, `"a key"`); got != ClassIdentifier { |
| 217 | t.Errorf("a quoted key is %v, want identifier", got) |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | func TestYAMLColoursFlowCollections(t *testing.T) { |
| 222 | const src = `test: ["CMD", "curl"]` |
| 223 | |
| 224 | if got := yamlClassOf(t, src, "["); got != ClassPunctuation { |
| 225 | t.Errorf("a flow sequence bracket is %v, want punctuation", got) |
| 226 | } |
| 227 | if got := yamlClassOf(t, src, `"CMD"`); got != ClassString { |
| 228 | t.Errorf("an entry is %v, want string", got) |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | func TestAComposeFileIsRecognisedByItsExtension(t *testing.T) { |
| 233 | for _, name := range []string{ |
| 234 | "compose.yaml", "compose.yml", "docker-compose.yml", "docker-compose.yaml", |
| 235 | "k8s/deployment.yaml", ".github/workflows/ci.yml", |
| 236 | } { |
| 237 | t.Run(name, func(t *testing.T) { |
| 238 | if got := LanguageOf(name, ""); got != LanguageYAML { |
| 239 | t.Errorf("LanguageOf(%q) = %q, want yaml", name, got) |
| 240 | } |
| 241 | }) |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | func TestYAMLReturnsOneEntryPerLine(t *testing.T) { |
| 246 | tests := map[string]int{ |
| 247 | "": 1, |
| 248 | "a: 1": 1, |
| 249 | "a: 1\n": 2, |
| 250 | "a: |\n b\n c\n": 4, |
| 251 | } |
| 252 | |
| 253 | for src, want := range tests { |
| 254 | if got := len(Highlight(LanguageYAML, src)); got != want { |
| 255 | t.Errorf("Highlight(%q) returned %d lines, want %d", src, got, want) |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | func TestBrokenYAMLIsStillColoured(t *testing.T) { |
| 261 | for _, src := range []string{"a: \"unterminated", " - ", "!", "&", "a: |"} { |
| 262 | t.Run(src, func(t *testing.T) { |
| 263 | if got := len(Highlight(LanguageYAML, src)); got != 1 { |
| 264 | t.Errorf("Highlight(%q) returned %d lines, want 1", src, got) |
| 265 | } |
| 266 | }) |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | func TestYAMLSpansAreOrderedAndDoNotOverlap(t *testing.T) { |
| 271 | const src = "services:\n web:\n image: \"nginx\" # pinned\n ports: [80, 443]\n" |
| 272 | |
| 273 | for line, onLine := range Highlight(LanguageYAML, src) { |
| 274 | previousEnd := 0 |
| 275 | for _, span := range onLine { |
| 276 | if span.Start < previousEnd { |
| 277 | t.Errorf("line %d: span %+v starts before the previous one ended at %d", line, span, previousEnd) |
| 278 | } |
| 279 | previousEnd = span.End |
| 280 | } |
| 281 | } |
| 282 | } |