/
comptime_at_test.v
143 lines (120 loc) · 2.55 KB
/
comptime_at_test.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import os
import v.pref
struct TestStruct {
test string
}
fn (mut t TestStruct) test_struct() {
assert @STRUCT == 'TestStruct'
}
fn (mut t TestStruct) test_struct_w_return() string {
assert @STRUCT == 'TestStruct'
return t.test
}
fn (mut t TestStruct) test_struct_w_high_order(cb fn (int) string) string {
assert @STRUCT == 'TestStruct'
return 'test' + cb(2)
}
fn TestStruct.static_method() string {
assert @STRUCT == 'TestStruct'
return @STRUCT
}
struct Abc {
}
fn (a Another) method() string {
println(@STRUCT)
return @STRUCT
}
struct Another {
}
fn (a Abc) method() string {
println(@STRUCT)
return @STRUCT
}
fn test_at_struct_ordering() {
a := Abc{}
assert a.method() == 'Abc'
b := Another{}
assert b.method() == 'Another'
}
struct TestFn {
}
fn (mut t TestFn) tst_1() {
assert @FN == 'tst_1'
}
fn (mut t TestFn) tst_2(cb fn (int)) {
assert @FN == 'tst_2'
assert @METHOD == 'TestFn.tst_2'
cb(1)
}
fn fn_name_mod_level() {
assert @FN == 'fn_name_mod_level'
assert @METHOD == 'fn_name_mod_level'
}
fn fn_name_mod_level_high_order(cb fn (int)) {
assert @FN == 'fn_name_mod_level_high_order'
cb(1)
}
fn test_at_file() {
// Test @FILE
f := os.file_name(@FILE)
assert f == 'comptime_at_test.v'
}
fn test_at_file_len() {
// Test @FILE_LINE
line1, line2 := '${@LINE}', '${@FILE_LINE}'
assert os.file_name(@FILE) + ':' + line1.str() == line2
}
fn test_at_fn() {
// Test @FN
assert @FN == 'test_at_fn'
fn_name_mod_level()
fn_name_mod_level_high_order(fn (i int) {
t := i + 1
assert t == 2
})
mut tfn := TestFn{}
tfn.tst_1()
tfn.tst_2(fn (i int) {
t := i + 1
assert t == 2
})
}
fn test_at_mod() {
// Test @MOD
assert @MOD == 'main'
}
fn test_at_struct() {
// Test @STRUCT
assert @STRUCT == ''
mut ts := TestStruct{
test: 'test'
}
ts.test_struct()
r1 := ts.test_struct_w_return()
r2 := ts.test_struct_w_high_order(fn (i int) string {
assert @STRUCT == ''
return i.str()
})
assert r1 == 'test'
assert r2 == 'test2'
assert TestStruct.static_method() == 'TestStruct'
assert @STRUCT == ''
}
fn test_vmod_file() {
content := @VMOD_FILE
assert content.len > 0
assert content.contains('Module {')
assert content.contains('name:')
assert content.contains('version:')
assert content.contains('description:')
}
fn test_comptime_at() {
assert @VEXE == pref.vexe_path()
}
// Reasons for assertions that are not literal:
// to prevent assertion invalidation due to "line" changes in subsequent code changes
fn test_line_number_last_token() {
line1, line2, line3 := @LINE, @LINE, @LINE
assert line1 == line2
assert line1 == line3
}