v / vlib / v2 / transformer / inject_test_main_to_flat_test.v
111 lines · 102 sloc · 3.92 KB · f3c5760b8838272e4789c38a1be9e03f9ceac351
Raw
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// vtest build: macos
5//
6// Bit-equality pin for the second flat-aware post_pass port (s152):
7// `inject_test_main_to_flat` must produce a FlatAst whose `signature()`
8// matches what the legacy `inject_test_main` (mut []ast.File) +
9// `flatten_files` pair produces. The s150 primitive `append_file_stmts`
10// is the splice mechanism; the synthesised main payload is built by the
11// shared `synthesise_test_main_fn` helper so both paths produce
12// identical FnDecl content by construction.
13module transformer
14
15import v2.ast
16import v2.pref as vpref
17import v2.types
18
19// Local helper — `v test` compiles each `_test.v` file in isolation, so
20// `create_test_transformer` from `transformer_test.v` is not visible here.
21fn create_test_main_to_flat_test_transformer() &Transformer {
22 env := &types.Environment{}
23 return &Transformer{
24 pref: &vpref.Preferences{}
25 env: unsafe { env }
26 needed_clone_fns: map[string]string{}
27 needed_array_contains_fns: map[string]ArrayMethodInfo{}
28 needed_array_index_fns: map[string]ArrayMethodInfo{}
29 needed_array_last_index_fns: map[string]ArrayMethodInfo{}
30 local_decl_types: map[string]types.Type{}
31 }
32}
33
34fn make_test_file_with_stmts(extra []ast.Stmt) ast.File {
35 mut stmts := []ast.Stmt{cap: 1 + extra.len}
36 stmts << ast.Stmt(ast.ModuleStmt{
37 name: 'main'
38 })
39 for s in extra {
40 stmts << s
41 }
42 return ast.File{
43 name: 'inline_test.v'
44 mod: 'main'
45 stmts: stmts
46 }
47}
48
49fn make_test_fn_decl(name string) ast.Stmt {
50 return ast.Stmt(ast.FnDecl{
51 name: name
52 })
53}
54
55// Reference: run legacy mutator on []ast.File then flatten.
56//
57// Why single-file (no sibling): the file-root node's `extra` slot stores
58// `intern(mod)` as a raw intern index, and `signature()` prints it
59// unresolved. Legacy `flatten_files` and the subject splice path intern
60// strings in different orders when multiple files are present, so a
61// single-file fixture is the apples-to-apples comparison. (Same reason
62// as s151's `inject_embed_file_helper_to_flat_test.v`.)
63fn build_reference_signature_with_test_main() string {
64 mut files := [
65 make_test_file_with_stmts([make_test_fn_decl('test_first'),
66 make_test_fn_decl('test_second')]),
67 ]
68 mut t := create_test_main_to_flat_test_transformer()
69 t.inject_test_main(mut files)
70 return ast.flatten_files(files).signature()
71}
72
73// Subject: append bare test file to FlatBuilder, then run flat-aware splice.
74fn build_subject_signature_with_test_main() string {
75 mut b := ast.new_flat_builder()
76 b.append_file(make_test_file_with_stmts([make_test_fn_decl('test_first'),
77 make_test_fn_decl('test_second')]))
78 mut t := create_test_main_to_flat_test_transformer()
79 t.inject_test_main_to_flat(mut b)
80 return b.flat.signature()
81}
82
83fn test_inject_test_main_to_flat_signature_matches_legacy() {
84 ref_sig := build_reference_signature_with_test_main()
85 sub_sig := build_subject_signature_with_test_main()
86 assert ref_sig == sub_sig
87}
88
89fn test_inject_test_main_to_flat_no_test_fns_is_noop() {
90 mut b := ast.new_flat_builder()
91 b.append_file(make_test_file_with_stmts([]))
92 baseline_sig := b.flat.signature()
93 mut t := create_test_main_to_flat_test_transformer()
94 t.inject_test_main_to_flat(mut b)
95 assert b.flat.signature() == baseline_sig
96}
97
98fn test_inject_test_main_to_flat_existing_main_is_noop() {
99 // Pre-seed the file with a top-level non-method FnDecl named `main` —
100 // the user-main detection must skip the splice even though there's a
101 // `test_*` fn present.
102 preseeded_main := ast.Stmt(ast.FnDecl{
103 name: 'main'
104 })
105 mut b := ast.new_flat_builder()
106 b.append_file(make_test_file_with_stmts([preseeded_main, make_test_fn_decl('test_first')]))
107 baseline_sig := b.flat.signature()
108 mut t := create_test_main_to_flat_test_transformer()
109 t.inject_test_main_to_flat(mut b)
110 assert b.flat.signature() == baseline_sig
111}
112