| 1 | // Copyright (c) 2026 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module ast |
| 5 | |
| 6 | // Tests for `FlatBuilder.append_file_stmts`. The helper is the foundational |
| 7 | // primitive behind the transformer's upcoming `post_pass_to_flat` port — |
| 8 | // it appends pre-emitted stmt nodes to a registered file's stmt list and |
| 9 | // re-emits the file root, mirroring the post-pass mutation pattern used by |
| 10 | // `inject_embed_file_helper`, `inject_test_main`, and `generated_fns_parts`. |
| 11 | |
| 12 | fn make_minimal_file_with_stmts(extra_stmts []Stmt) File { |
| 13 | mut stmts := []Stmt{cap: 1 + extra_stmts.len} |
| 14 | stmts << Stmt(ModuleStmt{ |
| 15 | name: 'foo' |
| 16 | }) |
| 17 | for s in extra_stmts { |
| 18 | stmts << s |
| 19 | } |
| 20 | return File{ |
| 21 | name: 'inline_minimal.v' |
| 22 | mod: 'foo' |
| 23 | stmts: stmts |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | fn make_extra_stmt_a() Stmt { |
| 28 | return Stmt(ConstDecl{ |
| 29 | fields: [ |
| 30 | FieldInit{ |
| 31 | name: 'A' |
| 32 | value: Expr(BasicLiteral{ |
| 33 | kind: .number |
| 34 | value: '1' |
| 35 | }) |
| 36 | }, |
| 37 | ] |
| 38 | }) |
| 39 | } |
| 40 | |
| 41 | fn make_extra_stmt_b() Stmt { |
| 42 | return Stmt(ConstDecl{ |
| 43 | fields: [ |
| 44 | FieldInit{ |
| 45 | name: 'B' |
| 46 | value: Expr(BasicLiteral{ |
| 47 | kind: .number |
| 48 | value: '2' |
| 49 | }) |
| 50 | }, |
| 51 | ] |
| 52 | }) |
| 53 | } |
| 54 | |
| 55 | // Reference: build a file directly with both extras at construction time. |
| 56 | fn build_reference_with_extras() FlatAst { |
| 57 | mut b := new_flat_builder() |
| 58 | b.append_file(make_minimal_file_with_stmts([make_extra_stmt_a(), |
| 59 | make_extra_stmt_b()])) |
| 60 | return b.flat |
| 61 | } |
| 62 | |
| 63 | // Subject: build the bare file, emit extras separately into the same builder, |
| 64 | // then splice them in via append_file_stmts. |
| 65 | fn build_subject_with_appended_extras() FlatAst { |
| 66 | mut b := new_flat_builder() |
| 67 | b.append_file(make_minimal_file_with_stmts([])) |
| 68 | a_id := b.emit_stmt(make_extra_stmt_a()) |
| 69 | b_id := b.emit_stmt(make_extra_stmt_b()) |
| 70 | b.append_file_stmts(0, [a_id, b_id]) |
| 71 | return b.flat |
| 72 | } |
| 73 | |
| 74 | fn test_append_file_stmts_signature_matches_reference() { |
| 75 | ref_sig := build_reference_with_extras().signature() |
| 76 | sub_sig := build_subject_with_appended_extras().signature() |
| 77 | assert ref_sig == sub_sig |
| 78 | } |
| 79 | |
| 80 | fn test_append_file_stmts_zero_extras_returns_existing_root() { |
| 81 | mut b := new_flat_builder() |
| 82 | original_id := b.append_file(make_minimal_file_with_stmts([])) |
| 83 | returned_id := b.append_file_stmts(0, []) |
| 84 | assert returned_id == original_id |
| 85 | } |
| 86 | |
| 87 | fn test_append_file_stmts_invalid_idx_returns_invalid() { |
| 88 | mut b := new_flat_builder() |
| 89 | b.append_file(make_minimal_file_with_stmts([])) |
| 90 | returned_id := b.append_file_stmts(-1, [FlatNodeId(0)]) |
| 91 | assert returned_id == invalid_flat_node_id |
| 92 | } |
| 93 | |