| 1 | package internal |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // Tests for converting between KDL v1 and v2 syntax. |
| 9 | // These verify that: |
| 10 | // 1. v1 input produces the same AST as equivalent v2 input |
| 11 | // 2. Generating from either AST produces valid v2 output |
| 12 | // 3. Round-tripping through parse→generate→parse preserves semantics |
| 13 | |
| 14 | // --- v1 ↔ v2 equivalent AST tests --- |
| 15 | |
| 16 | func TestConvert_BooleansSameAST(t *testing.T) { |
| 17 | // v1: true/false, v2: #true/#false — same AST |
| 18 | docV1, err := Parse("node true false") |
| 19 | if err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | docV2, err := Parse("node #true #false") |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | |
| 27 | assertSameArgValues(t, docV1.Nodes[0], docV2.Nodes[0]) |
| 28 | } |
| 29 | |
| 30 | func TestConvert_NullSameAST(t *testing.T) { |
| 31 | docV1, err := Parse("node null") |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | docV2, err := Parse("node #null") |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | |
| 40 | assertSameArgValues(t, docV1.Nodes[0], docV2.Nodes[0]) |
| 41 | } |
| 42 | |
| 43 | func TestConvert_BooleanPropSameAST(t *testing.T) { |
| 44 | docV1, err := Parse("node active=true disabled=false") |
| 45 | if err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | docV2, err := Parse("node active=#true disabled=#false") |
| 49 | if err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | |
| 53 | assertSamePropValues(t, docV1.Nodes[0], docV2.Nodes[0]) |
| 54 | } |
| 55 | |
| 56 | func TestConvert_NullPropSameAST(t *testing.T) { |
| 57 | docV1, err := Parse("node val=null") |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | docV2, err := Parse("node val=#null") |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | |
| 66 | assertSamePropValues(t, docV1.Nodes[0], docV2.Nodes[0]) |
| 67 | } |
| 68 | |
| 69 | func TestConvert_RawStringSameAST(t *testing.T) { |
| 70 | // v1: r"hello", v2: #"hello"# |
| 71 | docV1, err := Parse(`node r"hello"`) |
| 72 | if err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | docV2, err := Parse(`node #"hello"#`) |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | |
| 80 | assertSameArgValues(t, docV1.Nodes[0], docV2.Nodes[0]) |
| 81 | } |
| 82 | |
| 83 | func TestConvert_RawStringWithHashesSameAST(t *testing.T) { |
| 84 | // v1: r#"contains "quotes""#, v2: ##"contains "quotes""## |
| 85 | docV1, err := Parse(`node r#"contains "quotes""#`) |
| 86 | if err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | docV2, err := Parse(`node ##"contains "quotes""##`) |
| 90 | if err != nil { |
| 91 | t.Fatal(err) |
| 92 | } |
| 93 | |
| 94 | assertSameArgValues(t, docV1.Nodes[0], docV2.Nodes[0]) |
| 95 | } |
| 96 | |
| 97 | func TestConvert_RawStringNoEscapes(t *testing.T) { |
| 98 | // Both v1 and v2 raw strings should NOT process backslash escapes |
| 99 | docV1, err := Parse(`node r"hello\nworld"`) |
| 100 | if err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | docV2, err := Parse(`node #"hello\nworld"#`) |
| 104 | if err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | |
| 108 | // Both should have the literal text "hello\nworld" (no actual newline) |
| 109 | assertArgString(t, docV1.Nodes[0], 0, "hello\\nworld") |
| 110 | assertArgString(t, docV2.Nodes[0], 0, "hello\\nworld") |
| 111 | assertSameArgValues(t, docV1.Nodes[0], docV2.Nodes[0]) |
| 112 | } |
| 113 | |
| 114 | // --- Shared syntax produces identical AST --- |
| 115 | |
| 116 | func TestConvert_SharedQuotedStringsSameAST(t *testing.T) { |
| 117 | input := `node "hello world" "with\nnewline"` |
| 118 | doc1, err := Parse(input) |
| 119 | if err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | doc2, err := Parse(input) |
| 123 | if err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
| 126 | assertSameArgValues(t, doc1.Nodes[0], doc2.Nodes[0]) |
| 127 | } |
| 128 | |
| 129 | func TestConvert_SharedNumbersSameAST(t *testing.T) { |
| 130 | input := "node 42 3.14 -10 0xff 0o77 0b1010 1_000" |
| 131 | doc1, err := Parse(input) |
| 132 | if err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | doc2, err := Parse(input) |
| 136 | if err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | assertSameArgValues(t, doc1.Nodes[0], doc2.Nodes[0]) |
| 140 | } |
| 141 | |
| 142 | func TestConvert_SharedChildrenSameAST(t *testing.T) { |
| 143 | input := "parent {\n child1 \"arg\"\n child2 42\n}" |
| 144 | doc1, err := Parse(input) |
| 145 | if err != nil { |
| 146 | t.Fatal(err) |
| 147 | } |
| 148 | doc2, err := Parse(input) |
| 149 | if err != nil { |
| 150 | t.Fatal(err) |
| 151 | } |
| 152 | if len(doc1.Nodes[0].Children) != len(doc2.Nodes[0].Children) { |
| 153 | t.Fatalf("children count mismatch") |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // --- v1 input → v2 output (generate always outputs v2) --- |
| 158 | |
| 159 | func TestConvert_V1BooleansToV2Output(t *testing.T) { |
| 160 | doc, err := Parse("node true false") |
| 161 | if err != nil { |
| 162 | t.Fatal(err) |
| 163 | } |
| 164 | out, err := GenerateString(doc, DefaultGenerateOptions()) |
| 165 | if err != nil { |
| 166 | t.Fatal(err) |
| 167 | } |
| 168 | // Output should use v2 syntax |
| 169 | if !strings.Contains(out, "#true") || !strings.Contains(out, "#false") { |
| 170 | t.Errorf("expected v2 booleans in output, got:\n%s", out) |
| 171 | } |
| 172 | if strings.Contains(out, " true") && !strings.Contains(out, "#true") { |
| 173 | t.Errorf("bare 'true' in output (should be #true)") |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | func TestConvert_V1NullToV2Output(t *testing.T) { |
| 178 | doc, err := Parse("node null") |
| 179 | if err != nil { |
| 180 | t.Fatal(err) |
| 181 | } |
| 182 | out, err := GenerateString(doc, DefaultGenerateOptions()) |
| 183 | if err != nil { |
| 184 | t.Fatal(err) |
| 185 | } |
| 186 | if !strings.Contains(out, "#null") { |
| 187 | t.Errorf("expected #null in output, got:\n%s", out) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | func TestConvert_V1RawStringToV2QuotedOutput(t *testing.T) { |
| 192 | doc, err := Parse(`node r"hello world"`) |
| 193 | if err != nil { |
| 194 | t.Fatal(err) |
| 195 | } |
| 196 | out, err := GenerateString(doc, DefaultGenerateOptions()) |
| 197 | if err != nil { |
| 198 | t.Fatal(err) |
| 199 | } |
| 200 | // Generator outputs regular quoted strings, not raw |
| 201 | if !strings.Contains(out, `"hello world"`) { |
| 202 | t.Errorf("expected quoted string in output, got:\n%s", out) |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // --- Full v1→v2 round-trip --- |
| 207 | |
| 208 | func TestConvert_V1FullDocumentToV2(t *testing.T) { |
| 209 | v1Input := `// A v1 document |
| 210 | name "Bob" |
| 211 | age 76 |
| 212 | active true |
| 213 | empty null |
| 214 | server { |
| 215 | host "localhost" |
| 216 | port 8080 |
| 217 | tls false |
| 218 | }` |
| 219 | |
| 220 | // Parse as v1 |
| 221 | doc, err := Parse(v1Input) |
| 222 | if err != nil { |
| 223 | t.Fatal(err) |
| 224 | } |
| 225 | |
| 226 | // Generate as v2 |
| 227 | v2Output, err := GenerateString(doc, DefaultGenerateOptions()) |
| 228 | if err != nil { |
| 229 | t.Fatal(err) |
| 230 | } |
| 231 | |
| 232 | // v2 output should use #true/#false/#null |
| 233 | if strings.Contains(v2Output, " true\n") && !strings.Contains(v2Output, "#true") { |
| 234 | t.Errorf("expected #true in v2 output") |
| 235 | } |
| 236 | if strings.Contains(v2Output, " false\n") && !strings.Contains(v2Output, "#false") { |
| 237 | t.Errorf("expected #false in v2 output") |
| 238 | } |
| 239 | if strings.Contains(v2Output, " null\n") && !strings.Contains(v2Output, "#null") { |
| 240 | t.Errorf("expected #null in v2 output") |
| 241 | } |
| 242 | |
| 243 | // Re-parse the v2 output and verify same data |
| 244 | doc2, err := Parse(v2Output) |
| 245 | if err != nil { |
| 246 | t.Fatalf("failed to re-parse v2 output: %v\nOutput:\n%s", err, v2Output) |
| 247 | } |
| 248 | |
| 249 | // Verify structure preserved |
| 250 | if len(doc.Nodes) != len(doc2.Nodes) { |
| 251 | t.Fatalf("node count mismatch: %d vs %d", len(doc.Nodes), len(doc2.Nodes)) |
| 252 | } |
| 253 | |
| 254 | for i, n := range doc.Nodes { |
| 255 | if n.Name != doc2.Nodes[i].Name { |
| 256 | t.Errorf("node %d: name mismatch: %q vs %q", i, n.Name, doc2.Nodes[i].Name) |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // --- v2→v1 equivalent: parsing v2 output with v1 keywords --- |
| 262 | |
| 263 | func TestConvert_V2OutputReparsesCorrectly(t *testing.T) { |
| 264 | inputs := []string{ |
| 265 | "node #true #false #null", |
| 266 | "node 42 3.14 0xff", |
| 267 | `node "hello" key="val"`, |
| 268 | "parent {\n child 1\n}", |
| 269 | "a\nb\nc", |
| 270 | } |
| 271 | |
| 272 | for _, input := range inputs { |
| 273 | doc, err := Parse(input) |
| 274 | if err != nil { |
| 275 | t.Fatalf("parse %q: %v", input, err) |
| 276 | } |
| 277 | |
| 278 | out, err := GenerateString(doc, DefaultGenerateOptions()) |
| 279 | if err != nil { |
| 280 | t.Fatalf("generate %q: %v", input, err) |
| 281 | } |
| 282 | |
| 283 | doc2, err := Parse(out) |
| 284 | if err != nil { |
| 285 | t.Fatalf("reparse %q (output: %q): %v", input, out, err) |
| 286 | } |
| 287 | |
| 288 | if len(doc.Nodes) != len(doc2.Nodes) { |
| 289 | t.Errorf("round-trip %q: node count %d vs %d", input, len(doc.Nodes), len(doc2.Nodes)) |
| 290 | } |
| 291 | |
| 292 | for i := range doc.Nodes { |
| 293 | if i >= len(doc2.Nodes) { |
| 294 | break |
| 295 | } |
| 296 | assertSameArgValues(t, doc.Nodes[i], doc2.Nodes[i]) |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | // --- Mixed v1/v2 features in same document --- |
| 302 | |
| 303 | func TestConvert_MixedV1V2SyntaxShared(t *testing.T) { |
| 304 | // Common syntax that works in both versions |
| 305 | input := `node "string" 42 3.14 key="val" { |
| 306 | child |
| 307 | }` |
| 308 | doc, err := Parse(input) |
| 309 | if err != nil { |
| 310 | t.Fatal(err) |
| 311 | } |
| 312 | |
| 313 | assertNodeName(t, doc.Nodes[0], "node") |
| 314 | assertArgString(t, doc.Nodes[0], 0, "string") |
| 315 | assertArgInt(t, doc.Nodes[0], 1, 42) |
| 316 | assertArgFloat(t, doc.Nodes[0], 2, 3.14) |
| 317 | assertPropString(t, doc.Nodes[0], "key", "val") |
| 318 | assertChildCount(t, doc.Nodes[0], 1) |
| 319 | } |
| 320 | |
| 321 | // --- Real-world config conversion tests --- |
| 322 | |
| 323 | func TestConvert_ZellijConfigV1ToV2(t *testing.T) { |
| 324 | // Simplified Zellij-like config in v1 style |
| 325 | v1Config := `keybinds { |
| 326 | normal { |
| 327 | bind "Alt" "h" { |
| 328 | action "MoveFocusOrTab" "Left" |
| 329 | } |
| 330 | bind "Alt" "l" { |
| 331 | action "MoveFocusOrTab" "Right" |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | theme "dracula" |
| 336 | default_shell "zsh" |
| 337 | pane_frames true` |
| 338 | |
| 339 | doc, err := Parse(v1Config) |
| 340 | if err != nil { |
| 341 | t.Fatal(err) |
| 342 | } |
| 343 | |
| 344 | // Generate v2 |
| 345 | v2Output, err := GenerateString(doc, DefaultGenerateOptions()) |
| 346 | if err != nil { |
| 347 | t.Fatal(err) |
| 348 | } |
| 349 | |
| 350 | // Verify v2 output uses #true |
| 351 | if !strings.Contains(v2Output, "#true") { |
| 352 | t.Errorf("expected #true in v2 output for pane_frames") |
| 353 | } |
| 354 | |
| 355 | // Re-parse and verify round-trip |
| 356 | doc2, err := Parse(v2Output) |
| 357 | if err != nil { |
| 358 | t.Fatalf("re-parse failed: %v\nOutput:\n%s", err, v2Output) |
| 359 | } |
| 360 | |
| 361 | if len(doc.Nodes) != len(doc2.Nodes) { |
| 362 | t.Fatalf("node count mismatch: %d vs %d", len(doc.Nodes), len(doc2.Nodes)) |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | func TestConvert_PackageConfigV1ToV2(t *testing.T) { |
| 367 | v1Config := `package { |
| 368 | name "my-app" |
| 369 | version "1.0.0" |
| 370 | description "A great app" |
| 371 | } |
| 372 | dependencies { |
| 373 | dep "react" "^18.0.0" |
| 374 | dep "typescript" "^5.0.0" |
| 375 | } |
| 376 | build { |
| 377 | minify true |
| 378 | sourcemap false |
| 379 | target "es2022" |
| 380 | }` |
| 381 | |
| 382 | doc, err := Parse(v1Config) |
| 383 | if err != nil { |
| 384 | t.Fatal(err) |
| 385 | } |
| 386 | |
| 387 | v2Output, err := GenerateString(doc, DefaultGenerateOptions()) |
| 388 | if err != nil { |
| 389 | t.Fatal(err) |
| 390 | } |
| 391 | |
| 392 | // Should contain v2 booleans |
| 393 | if !strings.Contains(v2Output, "#true") || !strings.Contains(v2Output, "#false") { |
| 394 | t.Errorf("expected v2 booleans, got:\n%s", v2Output) |
| 395 | } |
| 396 | |
| 397 | // Round-trip |
| 398 | doc2, err := Parse(v2Output) |
| 399 | if err != nil { |
| 400 | t.Fatalf("re-parse failed: %v", err) |
| 401 | } |
| 402 | |
| 403 | assertNodeCount(t, &Document{Nodes: doc2.Nodes}, 3) |
| 404 | } |
| 405 | |
| 406 | // --- Version marker conversion --- |
| 407 | |
| 408 | func TestConvert_V1MarkerDocumentToV2(t *testing.T) { |
| 409 | v1Input := "/- kdl-version 1\nnode true false null" |
| 410 | doc, err := Parse(v1Input) |
| 411 | if err != nil { |
| 412 | t.Fatal(err) |
| 413 | } |
| 414 | |
| 415 | v2Output, err := GenerateString(doc, DefaultGenerateOptions()) |
| 416 | if err != nil { |
| 417 | t.Fatal(err) |
| 418 | } |
| 419 | |
| 420 | // Verify v2 keywords in output |
| 421 | if !strings.Contains(v2Output, "#true") { |
| 422 | t.Errorf("expected #true in converted output") |
| 423 | } |
| 424 | if !strings.Contains(v2Output, "#false") { |
| 425 | t.Errorf("expected #false in converted output") |
| 426 | } |
| 427 | if !strings.Contains(v2Output, "#null") { |
| 428 | t.Errorf("expected #null in converted output") |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | func TestConvert_V2MarkerDocumentRoundTrip(t *testing.T) { |
| 433 | v2Input := "/- kdl-version 2\nnode #true #false #null" |
| 434 | doc, err := Parse(v2Input) |
| 435 | if err != nil { |
| 436 | t.Fatal(err) |
| 437 | } |
| 438 | |
| 439 | v2Output, err := GenerateString(doc, DefaultGenerateOptions()) |
| 440 | if err != nil { |
| 441 | t.Fatal(err) |
| 442 | } |
| 443 | |
| 444 | doc2, err := Parse(v2Output) |
| 445 | if err != nil { |
| 446 | t.Fatal(err) |
| 447 | } |
| 448 | |
| 449 | assertSameArgValues(t, doc.Nodes[0], doc2.Nodes[0]) |
| 450 | } |
| 451 | |
| 452 | // --- Helpers --- |
| 453 | |
| 454 | func assertSameArgValues(t *testing.T, n1, n2 *Node) { |
| 455 | t.Helper() |
| 456 | if len(n1.Arguments) != len(n2.Arguments) { |
| 457 | t.Fatalf("arg count mismatch: %d vs %d (node %q vs %q)", |
| 458 | len(n1.Arguments), len(n2.Arguments), n1.Name, n2.Name) |
| 459 | } |
| 460 | for i := range n1.Arguments { |
| 461 | v1 := n1.Arguments[i].Value |
| 462 | v2 := n2.Arguments[i].Value |
| 463 | if v1 == nil && v2 == nil { |
| 464 | continue |
| 465 | } |
| 466 | if v1 == nil || v2 == nil { |
| 467 | t.Errorf("arg %d: value mismatch: %v vs %v", i, v1, v2) |
| 468 | continue |
| 469 | } |
| 470 | // Compare stringified values for simplicity with float edge cases |
| 471 | if !valuesEqual(v1, v2) { |
| 472 | t.Errorf("arg %d: value mismatch: %v (%T) vs %v (%T)", i, v1, v1, v2, v2) |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | func assertSamePropValues(t *testing.T, n1, n2 *Node) { |
| 478 | t.Helper() |
| 479 | if len(n1.Properties) != len(n2.Properties) { |
| 480 | t.Fatalf("prop count mismatch: %d vs %d", len(n1.Properties), len(n2.Properties)) |
| 481 | } |
| 482 | for i := range n1.Properties { |
| 483 | p1 := n1.Properties[i] |
| 484 | p2 := n2.Properties[i] |
| 485 | if p1.Key != p2.Key { |
| 486 | t.Errorf("prop %d: key mismatch: %q vs %q", i, p1.Key, p2.Key) |
| 487 | continue |
| 488 | } |
| 489 | if !valuesEqual(p1.Value.Value, p2.Value.Value) { |
| 490 | t.Errorf("prop %q: value mismatch: %v vs %v", p1.Key, p1.Value.Value, p2.Value.Value) |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | func valuesEqual(a, b interface{}) bool { |
| 496 | if a == nil && b == nil { |
| 497 | return true |
| 498 | } |
| 499 | if a == nil || b == nil { |
| 500 | return false |
| 501 | } |
| 502 | // Direct comparison handles string, int64, bool |
| 503 | return a == b |
| 504 | } |