v4 / vlib / v3 / tests / checker_print_test.v
109 lines · 95 sloc · 3.08 KB · 6c4d26f1f98c5464b012a375667ab345e462f7e0
Raw
1import os
2import v3.parser
3import v3.pref
4import v3.types
5
6const checker_print_tests_dir = os.dir(@FILE)
7const checker_print_v3_dir = os.dir(checker_print_tests_dir)
8const checker_print_v3_src = os.join_path(checker_print_v3_dir, 'v3.v')
9const checker_print_vexe = @VEXE
10
11// check_print_sources validates check print sources state for v3 tests.
12fn check_print_sources(name string, files map[string]string) []types.TypeError {
13 root := os.join_path(os.temp_dir(), 'v3_checker_print_${name}')
14 if os.exists(root) {
15 os.rmdir_all(root) or { panic(err) }
16 }
17 os.mkdir_all(root) or { panic(err) }
18 mut paths := []string{}
19 for rel, src in files {
20 path := os.join_path(root, rel)
21 os.mkdir_all(os.dir(path)) or { panic(err) }
22 os.write_file(path, src) or { panic(err) }
23 paths << path
24 }
25 prefs := pref.new_preferences()
26 mut p := parser.Parser.new(prefs)
27 mut a := p.parse_files(paths)
28 mut tc := types.TypeChecker.new(a)
29 tc.collect(a)
30 for path in paths {
31 tc.diagnostic_files[path] = true
32 }
33 tc.diagnose_unknown_calls = true
34 tc.check_semantics()
35 return tc.errors
36}
37
38// build_checker_print_v3_bin builds checker print v3 bin data for v3 tests.
39fn build_checker_print_v3_bin(name string) string {
40 v3_bin := os.join_path(os.temp_dir(), 'v3_checker_print_${name}')
41 build := os.execute('${checker_print_vexe} -o ${v3_bin} ${checker_print_v3_src}')
42 assert build.exit_code == 0, build.output
43 return v3_bin
44}
45
46// test_bare_println_allows_non_string_for_stringify_lowering validates this v3 regression case.
47fn test_bare_println_allows_non_string_for_stringify_lowering() {
48 errors := check_print_sources('bare_println', {
49 'main.v': 'module main
50
51fn println(s string) {}
52
53fn main() {
54 println(123)
55}
56'
57 })
58 assert errors.len == 0, errors.str()
59}
60
61// test_local_println_non_string_param_keeps_declared_arg_type validates this v3 regression case.
62fn test_local_println_non_string_param_keeps_declared_arg_type() {
63 errors := check_print_sources('local_println_int_param', {
64 'main.v': 'module main
65
66fn println(i int) {}
67
68fn main() {
69 println("x")
70}
71'
72 })
73 assert errors.len == 1, errors.str()
74 assert errors[0].msg == 'cannot use `string` as argument 1 to `println`; expected `int`'
75}
76
77// test_qualified_println_keeps_declared_arg_type validates this v3 regression case.
78fn test_qualified_println_keeps_declared_arg_type() {
79 errors := check_print_sources('qualified_println', {
80 'main.v': 'module main
81
82import log
83
84fn main() {
85 log.println(123)
86}
87'
88 'log/log.v': 'module log
89
90pub fn println(s string) {}
91'
92 })
93 assert errors.len == 1, errors.str()
94 assert errors[0].msg == 'cannot use `int` as argument 1 to `log.println`; expected `string`'
95}
96
97// test_eprint_bool_compile_uses_stringification validates this v3 regression case.
98fn test_eprint_bool_compile_uses_stringification() {
99 v3_bin := build_checker_print_v3_bin('eprint_bool')
100 src := os.join_path(os.temp_dir(), 'v3_checker_print_eprint_bool.v')
101 bin := os.join_path(os.temp_dir(), 'v3_checker_print_eprint_bool')
102 os.write_file(src, '
103fn main() {
104 eprint(true)
105}
106') or { panic(err) }
107 compile := os.execute('${v3_bin} -o ${bin} ${src}')
108 assert compile.exit_code == 0, compile.output
109}
110