v / vlib / v2 / parser / parser_test.v
105 lines · 86 sloc · 1.75 KB · e7738c112c787d477501fa4a87edd0e1d72159bd
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: !windows
5module parser
6
7import os
8import v2.pref
9import v2.token
10
11fn parse_code_for_test(code string) {
12 tmp_file := '/tmp/v2_parser_test_${os.getpid()}.v'
13 os.write_file(tmp_file, code) or { panic('failed to write temp file') }
14 defer {
15 os.rm(tmp_file) or {}
16 }
17 prefs := &pref.Preferences{}
18 mut file_set := token.FileSet.new()
19 mut par := Parser.new(prefs)
20 files := par.parse_files([tmp_file], mut file_set)
21 assert files.len == 1
22}
23
24fn test_if_condition_parses_selector_struct_init_before_infix() {
25 parse_code_for_test('
26module main
27
28fn main() {
29 mut pos := token.Pos{}
30 if pos == token.Pos{} && true {
31 x := 1
32 }
33}
34')
35}
36
37fn test_if_condition_keeps_uppercase_selector_followed_by_block() {
38 parse_code_for_test('
39module main
40
41fn main() int {
42 ch := 0
43 if ch == C.EOF {
44 return -1
45 }
46 return ch
47}
48')
49}
50
51fn test_match_branch_keeps_empty_uppercase_branch_block() {
52 parse_code_for_test('
53module main
54
55type Obj = EmptyScopeObject | AsmRegister
56
57struct AsmRegister {}
58struct EmptyScopeObject {}
59
60fn main() {
61 node := Obj(EmptyScopeObject{})
62 match node {
63 AsmRegister, EmptyScopeObject {}
64 }
65}
66')
67}
68
69fn test_global_unsafe_block_parses_leading_array_literal_stmt() {
70 parse_code_for_test('
71module main
72
73struct File {}
74
75__global files = unsafe { []&File{} }
76
77fn main() {}
78')
79}
80
81fn test_array_literal_parses_backslash_char_literal() {
82 parse_code_for_test('
83module main
84
85fn main() {
86 x := [u8(`\\\\`)]
87 _ = x
88}
89')
90}
91
92fn test_call_argument_parses_untyped_backslash_char_array_literal() {
93 parse_code_for_test('
94module main
95
96fn b(bytes []u8) []u8 {
97 return bytes
98}
99
100fn main() {
101 x := b([`\\\\`])
102 _ = x
103}
104')
105}
106