| 1 | import os |
| 2 | import time |
| 3 | import v.pref |
| 4 | |
| 5 | struct TestStruct { |
| 6 | test string |
| 7 | } |
| 8 | |
| 9 | fn (mut t TestStruct) test_struct() { |
| 10 | assert @STRUCT == 'TestStruct' |
| 11 | } |
| 12 | |
| 13 | fn (mut t TestStruct) test_struct_w_return() string { |
| 14 | assert @STRUCT == 'TestStruct' |
| 15 | return t.test |
| 16 | } |
| 17 | |
| 18 | fn (mut t TestStruct) test_struct_w_high_order(cb fn (int) string) string { |
| 19 | assert @STRUCT == 'TestStruct' |
| 20 | return 'test' + cb(2) |
| 21 | } |
| 22 | |
| 23 | fn TestStruct.static_method() string { |
| 24 | assert @STRUCT == 'TestStruct' |
| 25 | return @STRUCT |
| 26 | } |
| 27 | |
| 28 | struct Abc { |
| 29 | } |
| 30 | |
| 31 | fn (a Another) method() string { |
| 32 | println(@STRUCT) |
| 33 | return @STRUCT |
| 34 | } |
| 35 | |
| 36 | struct Another { |
| 37 | } |
| 38 | |
| 39 | fn (a Abc) method() string { |
| 40 | println(@STRUCT) |
| 41 | return @STRUCT |
| 42 | } |
| 43 | |
| 44 | fn test_at_struct_ordering() { |
| 45 | a := Abc{} |
| 46 | assert a.method() == 'Abc' |
| 47 | b := Another{} |
| 48 | assert b.method() == 'Another' |
| 49 | } |
| 50 | |
| 51 | struct TestFn { |
| 52 | } |
| 53 | |
| 54 | fn (mut t TestFn) tst_1() { |
| 55 | assert @FN == 'tst_1' |
| 56 | } |
| 57 | |
| 58 | fn (mut t TestFn) tst_2(cb fn (int)) { |
| 59 | assert @FN == 'tst_2' |
| 60 | assert @METHOD == 'TestFn.tst_2' |
| 61 | cb(1) |
| 62 | } |
| 63 | |
| 64 | fn TestFn.static_fn() { |
| 65 | assert @FN == 'static_fn' |
| 66 | assert @METHOD == 'TestFn.static_fn' |
| 67 | assert @STRUCT == 'TestFn' |
| 68 | } |
| 69 | |
| 70 | fn fn_name_mod_level() { |
| 71 | assert @FN == 'fn_name_mod_level' |
| 72 | assert @METHOD == 'fn_name_mod_level' |
| 73 | } |
| 74 | |
| 75 | fn fn_name_mod_level_high_order(cb fn (int)) { |
| 76 | assert @FN == 'fn_name_mod_level_high_order' |
| 77 | cb(1) |
| 78 | } |
| 79 | |
| 80 | fn test_at_file() { |
| 81 | // Test @FILE |
| 82 | f := os.file_name(@FILE) |
| 83 | assert f == 'comptime_at_test.v' |
| 84 | } |
| 85 | |
| 86 | fn test_at_dir() { |
| 87 | // Test @DIR |
| 88 | f := os.file_name(@DIR) |
| 89 | assert f == 'comptime' |
| 90 | assert os.dir(@FILE) == @DIR |
| 91 | d := @DIR |
| 92 | assert d.len > 0 |
| 93 | assert !d.ends_with('.v') |
| 94 | } |
| 95 | |
| 96 | fn test_at_file_len() { |
| 97 | // Test @FILE_LINE |
| 98 | line1, line2 := '${@LINE}', '${@FILE_LINE}' |
| 99 | assert os.file_name(@FILE) + ':' + line1.str() == line2 |
| 100 | } |
| 101 | |
| 102 | fn test_at_fn() { |
| 103 | // Test @FN |
| 104 | assert @FN == 'test_at_fn' |
| 105 | fn_name_mod_level() |
| 106 | fn_name_mod_level_high_order(fn (i int) { |
| 107 | t := i + 1 |
| 108 | assert t == 2 |
| 109 | }) |
| 110 | mut tfn := TestFn{} |
| 111 | tfn.tst_1() |
| 112 | tfn.tst_2(fn (i int) { |
| 113 | t := i + 1 |
| 114 | assert t == 2 |
| 115 | }) |
| 116 | TestFn.static_fn() |
| 117 | } |
| 118 | |
| 119 | fn test_at_mod() { |
| 120 | // Test @MOD |
| 121 | assert @MOD == 'main' |
| 122 | } |
| 123 | |
| 124 | fn test_at_struct() { |
| 125 | // Test @STRUCT |
| 126 | assert @STRUCT == '' |
| 127 | mut ts := TestStruct{ |
| 128 | test: 'test' |
| 129 | } |
| 130 | ts.test_struct() |
| 131 | r1 := ts.test_struct_w_return() |
| 132 | r2 := ts.test_struct_w_high_order(fn (i int) string { |
| 133 | assert @STRUCT == '' |
| 134 | return i.str() |
| 135 | }) |
| 136 | assert r1 == 'test' |
| 137 | assert r2 == 'test2' |
| 138 | assert TestStruct.static_method() == 'TestStruct' |
| 139 | assert @STRUCT == '' |
| 140 | } |
| 141 | |
| 142 | fn test_vmod_file() { |
| 143 | content := @VMOD_FILE |
| 144 | assert content.len > 0 |
| 145 | assert content.contains('Module {') |
| 146 | assert content.contains('name:') |
| 147 | assert content.contains('version:') |
| 148 | assert content.contains('description:') |
| 149 | } |
| 150 | |
| 151 | fn test_comptime_at() { |
| 152 | assert @VEXE == pref.vexe_path() |
| 153 | } |
| 154 | |
| 155 | // Reasons for assertions that are not literal: |
| 156 | // to prevent assertion invalidation due to "line" changes in subsequent code changes |
| 157 | fn test_line_number_last_token() { |
| 158 | line1, line2, line3 := @LINE, @LINE, @LINE |
| 159 | assert line1 == line2 |
| 160 | assert line1 == line3 |
| 161 | } |
| 162 | |
| 163 | fn abc() { |
| 164 | assert @LOCATION.contains('comptime_at_test.v:') |
| 165 | assert @LOCATION.ends_with(', main.abc') |
| 166 | } |
| 167 | |
| 168 | struct MyStruct { |
| 169 | } |
| 170 | |
| 171 | fn MyStruct.new() MyStruct { |
| 172 | assert @LOCATION.ends_with('main.MyStruct.new (static)') |
| 173 | return MyStruct{} |
| 174 | } |
| 175 | |
| 176 | fn (s MyStruct) mymethod() { |
| 177 | assert @LOCATION.contains('comptime_at_test.v:') |
| 178 | assert @LOCATION.ends_with('main.MyStruct{}.mymethod') |
| 179 | } |
| 180 | |
| 181 | fn test_at_location() { |
| 182 | abc() |
| 183 | MyStruct.new().mymethod() |
| 184 | assert @LOCATION.contains('comptime_at_test.v:') |
| 185 | assert @LOCATION.ends_with('main.test_at_location') |
| 186 | } |
| 187 | |
| 188 | fn test_at_build_date_time_timestamp() { |
| 189 | bd := dump(@BUILD_DATE) |
| 190 | bt := dump(@BUILD_TIME) |
| 191 | bts := dump(@BUILD_TIMESTAMP) |
| 192 | assert bd.len > 0 |
| 193 | assert bt.len > 0 |
| 194 | assert bd.count('-') == 2 |
| 195 | assert bt.count(':') == 2 |
| 196 | assert bts.len > 0 |
| 197 | assert bts.i64() > 1_000_000_000 |
| 198 | now_utc := dump(time.utc().unix()) |
| 199 | assert now_utc >= bts.i64() |
| 200 | } |
| 201 | |
| 202 | fn test_at_os() { |
| 203 | println('Current OS is ${@OS}') |
| 204 | assert @OS in ['termux', 'android', 'wasm32_emscripten', 'linux', 'ios', 'macos', 'windows', |
| 205 | 'freebsd', 'openbsd', 'netbsd', 'dragonfly', 'serenity', 'plan9', 'vinix', 'solaris', 'haiku', |
| 206 | 'js_node', 'js_freestanding', 'js_browser'] |
| 207 | } |
| 208 | |
| 209 | fn test_at_ccompiler() { |
| 210 | println('Current C Compiler is ${@CCOMPILER}') |
| 211 | assert @CCOMPILER in ['gcc', 'tinyc', 'clang', 'emcc', 'mingw', 'msvc', 'cplusplus'] |
| 212 | } |
| 213 | |
| 214 | fn test_at_backend() { |
| 215 | println('Current language backend is ${@BACKEND}') |
| 216 | assert @BACKEND in ['c', 'interpret', 'js_node', 'js_browser', 'js_freestanding', 'native', |
| 217 | 'wasm'] |
| 218 | } |
| 219 | |
| 220 | fn test_at_platform() { |
| 221 | println('Current Platform is ${@PLATFORM}') |
| 222 | assert @PLATFORM in ['amd64', 'arm64', 'arm32', 'rv64', 'rv32', 'i386', 's390x', 'ppc64le', |
| 223 | 'loongarch64', 'js_node', 'js_browser', 'js_freestanding', 'wasm32'] |
| 224 | } |
| 225 | |