diff --git a/cmd/tools/vdoc/html.v b/cmd/tools/vdoc/html.v index b4e9473ae6..a885db973a 100644 --- a/cmd/tools/vdoc/html.v +++ b/cmd/tools/vdoc/html.v @@ -131,10 +131,10 @@ fn (vd VDoc) render_search_index(out Output) { fn (mut vd VDoc) render_static_html(out Output) { vd.assets = { - 'doc_css': vd.get_resource(main.css_js_assets[0], out) - 'normalize_css': vd.get_resource(main.css_js_assets[1], out) - 'doc_js': vd.get_resource(main.css_js_assets[2], out) - 'dark_mode_js': vd.get_resource(main.css_js_assets[3], out) + 'doc_css': vd.get_resource(css_js_assets[0], out) + 'normalize_css': vd.get_resource(css_js_assets[1], out) + 'doc_js': vd.get_resource(css_js_assets[2], out) + 'dark_mode_js': vd.get_resource(css_js_assets[3], out) 'light_icon': vd.get_resource('light.svg', out) 'dark_icon': vd.get_resource('dark.svg', out) 'menu_icon': vd.get_resource('menu.svg', out) @@ -144,7 +144,7 @@ fn (mut vd VDoc) render_static_html(out Output) { fn (vd VDoc) get_resource(name string, out Output) string { cfg := vd.cfg - path := os.join_path(main.res_path, name) + path := os.join_path(res_path, name) mut res := os.read_file(path) or { panic('vdoc: could not read $path') } /* if minify { @@ -294,7 +294,7 @@ fn (vd VDoc) gen_html(d doc.Doc) string { symbols_toc_str := symbols_toc.str() modules_toc.free() symbols_toc.free() - return main.html_content.replace('{{ title }}', d.head.name).replace('{{ head_name }}', + return html_content.replace('{{ title }}', d.head.name).replace('{{ head_name }}', header_name).replace('{{ version }}', version).replace('{{ light_icon }}', vd.assets['light_icon']).replace('{{ dark_icon }}', vd.assets['dark_icon']).replace('{{ menu_icon }}', vd.assets['menu_icon']).replace('{{ head_assets }}', if cfg.inline_assets { @@ -452,7 +452,7 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool, dnw.write('${tabs[2]}
<$head_tag>$dn.kind $sym_name$hash_link') } if link.len != 0 { - dnw.write('$main.link_svg') + dnw.write('$link_svg') } dnw.write('
') } diff --git a/cmd/tools/vdoc/vdoc.v b/cmd/tools/vdoc/vdoc.v index 4984f9eda0..70c2324215 100644 --- a/cmd/tools/vdoc/vdoc.v +++ b/cmd/tools/vdoc/vdoc.v @@ -247,7 +247,7 @@ fn (mut vd VDoc) generate_docs_from_file() { exit(1) } dir_path := if cfg.is_vlib { - main.vroot + vroot } else if os.is_dir(cfg.input_path) { cfg.input_path } else { @@ -404,8 +404,8 @@ fn parse_arguments(args []string) Config { } '-f' { format := cmdline.option(current_args, '-f', '') - if format !in main.allowed_formats { - allowed_str := main.allowed_formats.join(', ') + if format !in allowed_formats { + allowed_str := allowed_formats.join(', ') eprintln('vdoc: "$format" is not a valid format. Only $allowed_str are allowed.') exit(1) } @@ -472,7 +472,7 @@ fn parse_arguments(args []string) Config { if cfg.input_path.trim_right('/') == 'vlib' { cfg.is_vlib = true cfg.is_multi = true - cfg.input_path = os.join_path(main.vroot, 'vlib') + cfg.input_path = os.join_path(vroot, 'vlib') } else if !is_path { // TODO vd.vprintln('Input "$cfg.input_path" is not a valid path. Looking for modules named "$cfg.input_path"...') mod_path := doc.lookup_module(cfg.input_path) or { @@ -486,7 +486,7 @@ fn parse_arguments(args []string) Config { fn main() { if os.args.len < 2 || '-h' in os.args || '--help' in os.args || os.args[1..] == ['doc', 'help'] { - os.system('$main.vexe help doc') + os.system('$vexe help doc') exit(0) } args := os.args[2..].clone() diff --git a/cmd/tools/vvet/vet_test.v b/cmd/tools/vvet/vet_test.v index 48ddb4988d..6209f9168c 100644 --- a/cmd/tools/vvet/vet_test.v +++ b/cmd/tools/vvet/vet_test.v @@ -48,7 +48,7 @@ fn check_path(vexe string, dir string, tests []string) int { println(found) println('============\n') println('diff:') - println(util.color_compare_strings(main.diff_cmd, found, expected)) + println(util.color_compare_strings(diff_cmd, found, expected)) println('============\n') nb_fail++ } else { diff --git a/cmd/tools/vvet/vvet.v b/cmd/tools/vvet/vvet.v index dd496642cd..e84664d41d 100644 --- a/cmd/tools/vvet/vvet.v +++ b/cmd/tools/vvet/vvet.v @@ -38,12 +38,12 @@ const show_warnings = '-hide-warnings' !in vet_options fn main() { opt := Options{ - is_verbose: main.is_verbose + is_verbose: is_verbose } mut vet := Vet{ opt: opt } - mut paths := cmdline.only_non_options(main.vet_options) + mut paths := cmdline.only_non_options(vet_options) vtmp := os.getenv('VTMP') if vtmp != '' { // `v test-cleancode` passes also `-o tmpfolder` as well as all options in VFLAGS @@ -57,7 +57,7 @@ fn main() { } if path.ends_with('.v') || path.ends_with('.vv') { if path.contains('cmd/tools/vvet/tests/') { - if main.is_force || paths.len == 1 { + if is_force || paths.len == 1 { vet.vet_file(path, true) continue } else { @@ -79,7 +79,7 @@ fn main() { files << vfiles files << vvfiles for file in files { - if !main.is_force && file.ends_with('.vv') && file.contains('cmd/tools/vvet/tests/') { + if !is_force && file.ends_with('.vv') && file.contains('cmd/tools/vvet/tests/') { continue } vet.vet_file(file, false) @@ -90,7 +90,7 @@ fn main() { warnings := vet.errors.filter(it.kind == .warning) errors := vet.errors.filter(it.kind == .error) errors_vfmt := vet.errors.filter(it.kind == .error && it.fix == .vfmt) - if main.show_warnings { + if show_warnings { for err in warnings { eprintln('$err.file_path:$err.pos.line_nr: warning: $err.message') } diff --git a/cmd/v/v.v b/cmd/v/v.v index 29aa431489..2281593cb3 100644 --- a/cmd/v/v.v +++ b/cmd/v/v.v @@ -94,7 +94,7 @@ fn main() { } // Start calling the correct functions/external tools // Note for future contributors: Please add new subcommands in the `match` block below. - if command in main.simple_cmd { + if command in simple_cmd { // External tools util.launch_tool(prefs.is_verbose, 'v' + command, os.args[1..]) return diff --git a/doc/docs.md b/doc/docs.md index bd0485cc6c..bcbba4be07 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -1679,8 +1679,8 @@ const ( world = '世界' ) -println(main.pi) -println(main.world) +println(pi) +println(world) ``` Constants are declared with `const`. They can only be defined @@ -1716,17 +1716,19 @@ const ( blue = rgb(0, 0, 255) ) -println(main.numbers) -println(main.red) -println(main.blue) +println(numbers) +println(red) +println(blue) ``` Global variables are not allowed, so this can be really useful. When naming constants, `snake_case` must be used. In order to distinguish consts from local variables, the full path to consts must be specified. For example, -to access the PI const, full `math.pi` name must be used both outside the `math` module, -and inside it. This can be seen in the example above with `println(main.numbers)`. +to access the PI const, full `math.pi` name must be used both outside the `math` +module, and inside it. That restriction is relaxed only for the `main` module +(the one containing your `fn main()`, where you can use the shorter name of the +constants too, i.e. just `println(numbers)`, not `println(main.numbers)` . vfmt takes care of this rule, so you can type `println(pi)` inside the `math` module, and vffmt will automatically update it to `println(math.pi)`. diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index 332a137a7f..8e00a0af50 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -307,7 +307,7 @@ fn test_fixed() { nums[1] = 7 assert nums[1] == 7 nums2 := [5]int{} // c_n - assert nums2[main.c_n - 1] == 0 + assert nums2[c_n - 1] == 0 } fn modify(mut numbers []int) { @@ -1101,12 +1101,12 @@ const ( ) fn test_multidimensional_array_initialization_with_consts() { - mut data := [][][]int{len: main.grid_size_1, init: [][]int{len: main.grid_size_2, init: []int{len: main.grid_size_3, init: main.cell_value}}} - assert data.len == main.grid_size_1 - assert data[0].len == main.grid_size_2 - assert data[0][0].len == main.grid_size_3 - assert data[0][0][0] == main.cell_value - assert data[1][1][1] == main.cell_value + mut data := [][][]int{len: grid_size_1, init: [][]int{len: grid_size_2, init: []int{len: grid_size_3, init: cell_value}}} + assert data.len == grid_size_1 + assert data[0].len == grid_size_2 + assert data[0][0].len == grid_size_3 + assert data[0][0][0] == cell_value + assert data[1][1][1] == cell_value } fn test_byteptr_vbytes() { diff --git a/vlib/eventbus/README.md b/vlib/eventbus/README.md index 05357da3bb..90d2e3d1d7 100644 --- a/vlib/eventbus/README.md +++ b/vlib/eventbus/README.md @@ -67,7 +67,7 @@ const ( fn main() { // get a mutable reference to the subscriber - mut sub := main.eb.subscriber + mut sub := eb.subscriber // subscribe to the 'error' event sub.subscribe('error', on_error) // start the work diff --git a/vlib/os/inode_test.v b/vlib/os/inode_test.v index 3dbb9be383..c2b5aa781d 100644 --- a/vlib/os/inode_test.v +++ b/vlib/os/inode_test.v @@ -8,18 +8,18 @@ const ( ) fn testsuite_begin() { - eprintln('testsuite_begin, tfolder = $main.tfolder') - os.rmdir_all(main.tfolder) - assert !os.is_dir(main.tfolder) - os.mkdir_all(main.tfolder) - os.chdir(main.tfolder) - assert os.is_dir(main.tfolder) + eprintln('testsuite_begin, tfolder = $tfolder') + os.rmdir_all(tfolder) + assert !os.is_dir(tfolder) + os.mkdir_all(tfolder) + os.chdir(tfolder) + assert os.is_dir(tfolder) } fn testsuite_end() { os.chdir(os.wd_at_startup) - os.rmdir_all(main.tfolder) - assert !os.is_dir(main.tfolder) + os.rmdir_all(tfolder) + assert !os.is_dir(tfolder) } fn test_inode_file_type() { diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index 5585423e73..b17f9e5ebb 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -12,21 +12,21 @@ const ( const args_at_start = os.args.clone() fn testsuite_begin() { - eprintln('testsuite_begin, tfolder = $main.tfolder') - os.rmdir_all(main.tfolder) - assert !os.is_dir(main.tfolder) - os.mkdir_all(main.tfolder) - os.chdir(main.tfolder) - assert os.is_dir(main.tfolder) + eprintln('testsuite_begin, tfolder = $tfolder') + os.rmdir_all(tfolder) + assert !os.is_dir(tfolder) + os.mkdir_all(tfolder) + os.chdir(tfolder) + assert os.is_dir(tfolder) // println('args_at_start: $args_at_start') - assert main.args_at_start.len > 0 - assert main.args_at_start == os.args + assert args_at_start.len > 0 + assert args_at_start == os.args } fn testsuite_end() { os.chdir(os.wd_at_startup) - os.rmdir_all(main.tfolder) - assert !os.is_dir(main.tfolder) + os.rmdir_all(tfolder) + assert !os.is_dir(tfolder) // eprintln('testsuite_end , tfolder = $tfolder removed.') } @@ -434,8 +434,8 @@ struct IntPoint { fn test_write_file_array_bytes() { fpath := './abytes.bin' - mut arr := []byte{len: main.maxn} - for i in 0 .. main.maxn { + mut arr := []byte{len: maxn} + for i in 0 .. maxn { arr[i] = 65 + byte(i) } os.write_file_array(fpath, arr) @@ -447,14 +447,14 @@ fn test_write_file_array_bytes() { fn test_write_file_array_structs() { fpath := './astructs.bin' - mut arr := []IntPoint{len: main.maxn} - for i in 0 .. main.maxn { + mut arr := []IntPoint{len: maxn} + for i in 0 .. maxn { arr[i] = IntPoint{65 + i, 65 + i + 10} } os.write_file_array(fpath, arr) rarr := os.read_file_array(fpath) assert rarr == arr - assert rarr.len == main.maxn + assert rarr.len == maxn // eprintln( rarr.str().replace('\n', ' ').replace('},', '},\n')) } diff --git a/vlib/semver/semver_test.v b/vlib/semver/semver_test.v index d66add3dca..77c8cd7f1e 100644 --- a/vlib/semver/semver_test.v +++ b/vlib/semver/semver_test.v @@ -76,7 +76,7 @@ const ( ) fn test_from() { - for item in main.versions_to_test { + for item in versions_to_test { ver := semver.from(item.raw) or { assert false return @@ -87,7 +87,7 @@ fn test_from() { assert ver.metadata == item.metadata assert ver.prerelease == item.prerelease } - for ver in main.invalid_versions_to_test { + for ver in invalid_versions_to_test { semver.from(ver) or { assert true continue @@ -140,7 +140,7 @@ fn test_compare() { } fn test_satisfies() { - for item in main.ranges_to_test { + for item in ranges_to_test { ver := semver.from(item.raw_version) or { assert false return @@ -155,13 +155,13 @@ fn test_satisfies_invalid() { assert false return } - for item in main.invalid_ranges_to_test { + for item in invalid_ranges_to_test { assert ver.satisfies(item) == false } } fn test_coerce() { - for item in main.coerce_to_test { + for item in coerce_to_test { valid := semver.from(item.valid) or { assert false return @@ -183,10 +183,10 @@ fn test_coerce_invalid() { } fn test_is_valid() { - for item in main.versions_to_test { + for item in versions_to_test { assert semver.is_valid(item.raw) } - for item in main.invalid_versions_to_test { + for item in invalid_versions_to_test { assert semver.is_valid(item) == false } } diff --git a/vlib/strings/builder_test.v b/vlib/strings/builder_test.v index b5f59a1cb8..dd0772b1ae 100644 --- a/vlib/strings/builder_test.v +++ b/vlib/strings/builder_test.v @@ -48,19 +48,19 @@ const ( fn test_big_sb() { mut sb := strings.new_builder(100) mut sb2 := strings.new_builder(10000) - for i in 0 .. main.maxn { + for i in 0 .. maxn { sb.writeln(i.str()) sb2.write('+') } s := sb.str() lines := s.split_into_lines() - assert lines.len == main.maxn + assert lines.len == maxn assert lines[0] == '0' assert lines[1] == '1' assert lines[777] == '777' assert lines[98765] == '98765' println(sb2.len) - assert sb2.len == main.maxn + assert sb2.len == maxn } fn test_byte_write() { diff --git a/vlib/time/time_format_test.v b/vlib/time/time_format_test.v index 175ecc3e18..eae8b0e6a1 100644 --- a/vlib/time/time_format_test.v +++ b/vlib/time/time_format_test.v @@ -19,70 +19,70 @@ fn test_now_format() { } fn test_format() { - assert '11.07.1980 21:23' == main.time_to_test.get_fmt_str(.dot, .hhmm24, .ddmmyyyy) + assert '11.07.1980 21:23' == time_to_test.get_fmt_str(.dot, .hhmm24, .ddmmyyyy) } fn test_hhmm() { - assert '21:23' == main.time_to_test.hhmm() + assert '21:23' == time_to_test.hhmm() } fn test_hhmm12() { - assert '9:23 p.m.' == main.time_to_test.hhmm12() + assert '9:23 p.m.' == time_to_test.hhmm12() } fn test_hhmmss() { - assert '21:23:42' == main.time_to_test.hhmmss() + assert '21:23:42' == time_to_test.hhmmss() } fn test_ymmdd() { - assert '1980-07-11' == main.time_to_test.ymmdd() + assert '1980-07-11' == time_to_test.ymmdd() } fn test_ddmmy() { - assert '11.07.1980' == main.time_to_test.ddmmy() + assert '11.07.1980' == time_to_test.ddmmy() } fn test_md() { - assert 'Jul 11' == main.time_to_test.md() + assert 'Jul 11' == time_to_test.md() } fn test_get_fmt_time_str() { - assert '21:23:42' == main.time_to_test.get_fmt_time_str(.hhmmss24) - assert '21:23' == main.time_to_test.get_fmt_time_str(.hhmm24) - assert '9:23:42 p.m.' == main.time_to_test.get_fmt_time_str(.hhmmss12) - assert '9:23 p.m.' == main.time_to_test.get_fmt_time_str(.hhmm12) + assert '21:23:42' == time_to_test.get_fmt_time_str(.hhmmss24) + assert '21:23' == time_to_test.get_fmt_time_str(.hhmm24) + assert '9:23:42 p.m.' == time_to_test.get_fmt_time_str(.hhmmss12) + assert '9:23 p.m.' == time_to_test.get_fmt_time_str(.hhmm12) } fn test_get_fmt_date_str() { - assert '11.07.1980' == main.time_to_test.get_fmt_date_str(.dot, .ddmmyyyy) - assert '11/07/1980' == main.time_to_test.get_fmt_date_str(.slash, .ddmmyyyy) - assert '11-07-1980' == main.time_to_test.get_fmt_date_str(.hyphen, .ddmmyyyy) - assert '11 07 1980' == main.time_to_test.get_fmt_date_str(.space, .ddmmyyyy) - assert '07.11.1980' == main.time_to_test.get_fmt_date_str(.dot, .mmddyyyy) - assert '07/11/1980' == main.time_to_test.get_fmt_date_str(.slash, .mmddyyyy) - assert '07-11-1980' == main.time_to_test.get_fmt_date_str(.hyphen, .mmddyyyy) - assert '07 11 1980' == main.time_to_test.get_fmt_date_str(.space, .mmddyyyy) - assert '11.07.80' == main.time_to_test.get_fmt_date_str(.dot, .ddmmyy) - assert '11/07/80' == main.time_to_test.get_fmt_date_str(.slash, .ddmmyy) - assert '11-07-80' == main.time_to_test.get_fmt_date_str(.hyphen, .ddmmyy) - assert '11 07 80' == main.time_to_test.get_fmt_date_str(.space, .ddmmyy) - assert '07.11.80' == main.time_to_test.get_fmt_date_str(.dot, .mmddyy) - assert '07/11/80' == main.time_to_test.get_fmt_date_str(.slash, .mmddyy) - assert '07-11-80' == main.time_to_test.get_fmt_date_str(.hyphen, .mmddyy) - assert '07 11 80' == main.time_to_test.get_fmt_date_str(.space, .mmddyy) - assert 'Jul 11' == main.time_to_test.get_fmt_date_str(.space, .mmmd) - assert 'Jul 11' == main.time_to_test.get_fmt_date_str(.space, .mmmdd) - assert 'Jul 11 1980' == main.time_to_test.get_fmt_date_str(.space, .mmmddyyyy) - assert '1980-07-11' == main.time_to_test.get_fmt_date_str(.hyphen, .yyyymmdd) + assert '11.07.1980' == time_to_test.get_fmt_date_str(.dot, .ddmmyyyy) + assert '11/07/1980' == time_to_test.get_fmt_date_str(.slash, .ddmmyyyy) + assert '11-07-1980' == time_to_test.get_fmt_date_str(.hyphen, .ddmmyyyy) + assert '11 07 1980' == time_to_test.get_fmt_date_str(.space, .ddmmyyyy) + assert '07.11.1980' == time_to_test.get_fmt_date_str(.dot, .mmddyyyy) + assert '07/11/1980' == time_to_test.get_fmt_date_str(.slash, .mmddyyyy) + assert '07-11-1980' == time_to_test.get_fmt_date_str(.hyphen, .mmddyyyy) + assert '07 11 1980' == time_to_test.get_fmt_date_str(.space, .mmddyyyy) + assert '11.07.80' == time_to_test.get_fmt_date_str(.dot, .ddmmyy) + assert '11/07/80' == time_to_test.get_fmt_date_str(.slash, .ddmmyy) + assert '11-07-80' == time_to_test.get_fmt_date_str(.hyphen, .ddmmyy) + assert '11 07 80' == time_to_test.get_fmt_date_str(.space, .ddmmyy) + assert '07.11.80' == time_to_test.get_fmt_date_str(.dot, .mmddyy) + assert '07/11/80' == time_to_test.get_fmt_date_str(.slash, .mmddyy) + assert '07-11-80' == time_to_test.get_fmt_date_str(.hyphen, .mmddyy) + assert '07 11 80' == time_to_test.get_fmt_date_str(.space, .mmddyy) + assert 'Jul 11' == time_to_test.get_fmt_date_str(.space, .mmmd) + assert 'Jul 11' == time_to_test.get_fmt_date_str(.space, .mmmdd) + assert 'Jul 11 1980' == time_to_test.get_fmt_date_str(.space, .mmmddyyyy) + assert '1980-07-11' == time_to_test.get_fmt_date_str(.hyphen, .yyyymmdd) } fn test_get_fmt_str() { // Since get_fmt_time_str and get_fmt_date_str do have comprehensive // tests I don't want to exaggerate here with all possible // combinations. - assert '11.07.1980 21:23:42' == main.time_to_test.get_fmt_str(.dot, .hhmmss24, .ddmmyyyy) + assert '11.07.1980 21:23:42' == time_to_test.get_fmt_str(.dot, .hhmmss24, .ddmmyyyy) } fn test_utc_string() { - assert 'Fri, 11 Jul 1980 21:23:42 UTC' == main.time_to_test.utc_string() + assert 'Fri, 11 Jul 1980 21:23:42 UTC' == time_to_test.utc_string() } diff --git a/vlib/time/time_test.v b/vlib/time/time_test.v index 98d9612392..19911c2e88 100644 --- a/vlib/time/time_test.v +++ b/vlib/time/time_test.v @@ -83,17 +83,17 @@ fn test_unix() { } fn test_format_ss() { - assert '11.07.1980 21:23:42' == main.time_to_test.get_fmt_str(.dot, .hhmmss24, .ddmmyyyy) + assert '11.07.1980 21:23:42' == time_to_test.get_fmt_str(.dot, .hhmmss24, .ddmmyyyy) } fn test_format_ss_milli() { - assert '11.07.1980 21:23:42.123' == main.time_to_test.get_fmt_str(.dot, .hhmmss24_milli, .ddmmyyyy) - assert '1980-07-11 21:23:42.123' == main.time_to_test.format_ss_milli() + assert '11.07.1980 21:23:42.123' == time_to_test.get_fmt_str(.dot, .hhmmss24_milli, .ddmmyyyy) + assert '1980-07-11 21:23:42.123' == time_to_test.format_ss_milli() } fn test_format_ss_micro() { - assert '11.07.1980 21:23:42.123456' == main.time_to_test.get_fmt_str(.dot, .hhmmss24_micro, .ddmmyyyy) - assert '1980-07-11 21:23:42.123456' == main.time_to_test.format_ss_micro() + assert '11.07.1980 21:23:42.123456' == time_to_test.get_fmt_str(.dot, .hhmmss24_micro, .ddmmyyyy) + assert '1980-07-11 21:23:42.123456' == time_to_test.format_ss_micro() } fn test_smonth() { @@ -153,12 +153,12 @@ fn test_add() { d_seconds := 3 d_microseconds := 13 duration := time.Duration(d_seconds * time.second + d_microseconds * time.microsecond) - t1 := main.time_to_test - t2 := main.time_to_test.add(duration) + t1 := time_to_test + t2 := time_to_test.add(duration) assert t2.second == t1.second + d_seconds assert t2.microsecond == t1.microsecond + d_microseconds assert t2.unix == t1.unix + u64(d_seconds) - t3 := main.time_to_test.add(-duration) + t3 := time_to_test.add(-duration) assert t3.second == t1.second - d_seconds assert t3.microsecond == t1.microsecond - d_microseconds assert t3.unix == t1.unix - u64(d_seconds) @@ -166,13 +166,13 @@ fn test_add() { fn test_add_days() { num_of_days := 3 - t := main.time_to_test.add_days(num_of_days) - assert t.day == main.time_to_test.day + num_of_days - assert t.unix == main.time_to_test.unix + 86400 * u64(num_of_days) + t := time_to_test.add_days(num_of_days) + assert t.day == time_to_test.day + num_of_days + assert t.unix == time_to_test.unix + 86400 * u64(num_of_days) } fn test_str() { - assert '1980-07-11 21:23:42' == main.time_to_test.str() + assert '1980-07-11 21:23:42' == time_to_test.str() } // not optimal test but will find obvious bugs diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 80f91c8766..cd2df87efe 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1150,16 +1150,24 @@ pub fn (mut f Fmt) ident(node ast.Ident) { // This allows using the variable `minute` inside time's functions // and also makes it clear that a module const is being used // (since V's conts are no longer ALL_CAP). + // ^^^ except for `main`, where consts are allowed to not have a `main.` prefix. if !node.name.contains('.') && !f.inside_const { full_name := f.cur_mod + '.' + node.name if obj := f.file.global_scope.find(full_name) { if obj is ast.ConstField { // "v.fmt.foo" => "fmt.foo" vals := full_name.split('.') - short := vals[vals.len - 2] + '.' + vals[vals.len - 1] + mod_prefix := vals[vals.len - 2] + const_name := vals[vals.len - 1] // f.write(f.short_module(full_name)) - f.write(short) - return + if mod_prefix == 'main' { + f.write(const_name) + return + } else { + short := mod_prefix + '.' + const_name + f.write(short) + return + } } } } diff --git a/vlib/v/fmt/fmt_keep_test.v b/vlib/v/fmt/fmt_keep_test.v index b074fc2938..5634da5a90 100644 --- a/vlib/v/fmt/fmt_keep_test.v +++ b/vlib/v/fmt/fmt_keep_test.v @@ -21,7 +21,7 @@ fn test_fmt() { vexe := os.getenv('VEXE') if vexe.len == 0 || !os.exists(vexe) { eprintln('VEXE must be set') - exit(main.error_missing_vexe) + exit(error_missing_vexe) } vroot := os.dir(vexe) os.chdir(vroot) @@ -59,7 +59,7 @@ fn test_fmt() { if expected_ocontent != result_ocontent { fmt_bench.fail() eprintln(fmt_bench.step_message_fail('file $vrelpath after formatting, does not look as expected.')) - if ipath.ends_with(main.b2v_keep_path) { + if ipath.ends_with(b2v_keep_path) { continue } if diff_cmd == '' { @@ -79,19 +79,19 @@ fn test_fmt() { eprintln(term.h_divider('-')) eprintln(fmt_bench.total_message(fmt_message)) if fmt_bench.nfail > 0 { - exit(main.error_failed_tests) + exit(error_failed_tests) } } fn fill_bin2v_keep() ? { img0 := os.join_path('tutorials', 'img', 'hello.png') img1 := os.join_path('tutorials', 'img', 'time.png') - os.rm(main.b2v_keep_path) ? - os.exec('v bin2v -w $main.b2v_keep_path $img0 $img1') ? + os.rm(b2v_keep_path) ? + os.exec('v bin2v -w $b2v_keep_path $img0 $img1') ? } fn restore_bin2v_placeholder() ? { text := '// This is a placeholder file which will be filled with bin2v output before the test. // HINT: do NOT delete, move or rename this file!\n' - os.write_file(main.b2v_keep_path, text) ? + os.write_file(b2v_keep_path, text) ? } diff --git a/vlib/v/fmt/fmt_test.v b/vlib/v/fmt/fmt_test.v index 474f282685..8cd48e7bdc 100644 --- a/vlib/v/fmt/fmt_test.v +++ b/vlib/v/fmt/fmt_test.v @@ -19,7 +19,7 @@ fn test_fmt() { vexe := os.getenv('VEXE') if vexe.len == 0 || !os.exists(vexe) { eprintln('VEXE must be set') - exit(main.error_missing_vexe) + exit(error_missing_vexe) } vroot := os.dir(vexe) tmpfolder := os.temp_dir() @@ -69,6 +69,6 @@ fn test_fmt() { eprintln(term.h_divider('-')) eprintln(fmt_bench.total_message(fmt_message)) if fmt_bench.nfail > 0 { - exit(main.error_failed_tests) + exit(error_failed_tests) } } diff --git a/vlib/v/fmt/fmt_vlib_test.v b/vlib/v/fmt/fmt_vlib_test.v index 51f89dbe59..252053511a 100644 --- a/vlib/v/fmt/fmt_vlib_test.v +++ b/vlib/v/fmt/fmt_vlib_test.v @@ -22,7 +22,7 @@ fn test_vlib_fmt() { vexe := os.getenv('VEXE') if vexe.len == 0 || !os.exists(vexe) { eprintln('VEXE must be set') - exit(main.error_missing_vexe) + exit(error_missing_vexe) } vroot := os.dir(vexe) tmpfolder := os.temp_dir() @@ -67,6 +67,6 @@ fn test_vlib_fmt() { eprintln(term.h_divider('-')) eprintln(fmt_bench.total_message(fmt_message)) if fmt_bench.nfail > 0 { - exit(main.error_failed_tests) + exit(error_failed_tests) } } diff --git a/vlib/v/fmt/tests/chan_ops_keep.vv b/vlib/v/fmt/tests/chan_ops_keep.vv index 270f3e10b4..df3d3af1da 100644 --- a/vlib/v/fmt/tests/chan_ops_keep.vv +++ b/vlib/v/fmt/tests/chan_ops_keep.vv @@ -41,7 +41,7 @@ fn test_channel_array_mut() { sem := sync.new_semaphore() go do_rec_calc_send(chs, sem) mut t := i64(100) - for _ in 0 .. main.num_iterations { + for _ in 0 .. num_iterations { chs[0] <- t t = <-chs[1] } @@ -55,7 +55,7 @@ fn test_channel_array_mut() { c := ch[1].cap d := ch[o].len sem.wait() - assert t == 100 + main.num_iterations + assert t == 100 + num_iterations ch2 := chan mut St{cap: 10} go g(ch2) } diff --git a/vlib/v/fmt/tests/chan_or_keep.vv b/vlib/v/fmt/tests/chan_or_keep.vv index 4cd531862f..a6e7a90625 100644 --- a/vlib/v/fmt/tests/chan_or_keep.vv +++ b/vlib/v/fmt/tests/chan_or_keep.vv @@ -2,13 +2,13 @@ const n = 1000 fn f(ch chan int) { mut s := 0 - for _ in 0 .. main.n { + for _ in 0 .. n { s += <-ch or { println('Something went wrong:') println('got $err') } } - assert s == main.n * (main.n + 1) / 2 + assert s == n * (n + 1) / 2 ch.close() } @@ -29,5 +29,5 @@ fn main() { for { s = do_send(ch, s) or { break } } - assert s == main.n + 1 + assert s == n + 1 } diff --git a/vlib/v/fmt/tests/shared_expected.vv b/vlib/v/fmt/tests/shared_expected.vv index ada9531fa4..9b9ddc25fe 100644 --- a/vlib/v/fmt/tests/shared_expected.vv +++ b/vlib/v/fmt/tests/shared_expected.vv @@ -6,7 +6,7 @@ mut: } fn (shared x St) f(shared z St) { - for _ in 0 .. main.reads_per_thread { + for _ in 0 .. reads_per_thread { rlock x { // other instances may read at the same time time.sleep_ms(1) assert x.a == 7 || x.a == 5 @@ -29,12 +29,12 @@ fn test_shared_lock() { a: 5 } shared z := &St{ - a: main.read_threads + a: read_threads } - for _ in 0 .. main.read_threads { + for _ in 0 .. read_threads { go x.f(shared z) } - for i in 0 .. main.writes { + for i in 0 .. writes { lock x { // wait for ongoing reads to finish, don't start new ones x.a = 17 // this value should never be read time.sleep_ms(50) diff --git a/vlib/v/gen/cgen_test.v b/vlib/v/gen/cgen_test.v index 18d94801c6..179a5f20ba 100644 --- a/vlib/v/gen/cgen_test.v +++ b/vlib/v/gen/cgen_test.v @@ -16,7 +16,7 @@ fn test_c_files() { println('Running V => C tests') vexe := os.getenv('VEXE') vroot := os.dir(vexe) - for i in 1 .. (main.nr_tests + 1) { + for i in 1 .. (nr_tests + 1) { path := '$vroot/vlib/v/gen/tests/${i}.vv' ctext := os.read_file('$vroot/vlib/v/gen/tests/${i}.c') or { panic(err) } mut b := builder.new_builder(pref.Preferences{}) @@ -28,7 +28,7 @@ fn test_c_files() { res = res[..pos] + res[end + 15..] } if compare_texts(res, ctext, path) { - println('$main.term_ok $i') + println('$term_ok $i') } else { assert false } @@ -57,7 +57,7 @@ fn compare_texts(a string, b string, path string) bool { line_b := lines_b[i] if line_a.trim_space() != line_b.trim_space() { println('$path: Got\n$a') - println('$path:$i: $main.term_fail') + println('$path:$i: $term_fail') println(term.bold(term.bright_yellow('actual : ')) + line_a) println(term.green('expected: ') + line_b) println(lines_b[i + 1]) diff --git a/vlib/v/table/cflags_test.v b/vlib/v/table/cflags_test.v index 16ed78a8a0..8c7b1ed8ba 100644 --- a/vlib/v/table/cflags_test.v +++ b/vlib/v/table/cflags_test.v @@ -14,12 +14,12 @@ fn test_parse_valid_cflags() { expected_flags := [ make_flag('freebsd', '-I', '/usr/local/include/freetype2'), make_flag('linux', '-l', 'glfw'), - make_flag('mingw', main.no_name, '-mwindows'), + make_flag('mingw', no_name, '-mwindows'), make_flag('solaris', '-L', '/opt/local/lib'), make_flag('darwin', '-framework', 'Cocoa'), make_flag('windows', '-l', 'gdi32'), - make_flag(main.no_os, '-l', 'mysqlclient'), - make_flag(main.no_os, main.no_name, '-test'), + make_flag(no_os, '-l', 'mysqlclient'), + make_flag(no_os, no_name, '-test'), ] parse_valid_flag(mut t, '-lmysqlclient') parse_valid_flag(mut t, '-test') @@ -49,22 +49,22 @@ fn test_parse_invalid_cflags() { assert_parse_invalid_flag(mut t, 'solaris') assert_parse_invalid_flag(mut t, 'windows') // Empty flag is not allowed - assert_parse_invalid_flag(mut t, main.no_flag) + assert_parse_invalid_flag(mut t, no_flag) assert t.cflags.len == 0 } fn parse_valid_flag(mut t table.Table, flag string) { - t.parse_cflag(flag, main.module_name, main.cdefines) or { } + t.parse_cflag(flag, module_name, cdefines) or { } } fn assert_parse_invalid_flag(mut t table.Table, flag string) { - t.parse_cflag(flag, main.module_name, main.cdefines) or { return } + t.parse_cflag(flag, module_name, cdefines) or { return } assert false } fn make_flag(os string, name string, value string) cflag.CFlag { return cflag.CFlag{ - mod: main.module_name + mod: module_name os: os name: name value: value diff --git a/vlib/vweb/tests/vweb_test.v b/vlib/vweb/tests/vweb_test.v index c03d07fb85..4d24f68118 100644 --- a/vlib/vweb/tests/vweb_test.v +++ b/vlib/vweb/tests/vweb_test.v @@ -18,18 +18,18 @@ const ( // setup of vweb webserver fn testsuite_begin() { - os.chdir(main.vroot) - if os.exists(main.serverexe) { - os.rm(main.serverexe) + os.chdir(vroot) + if os.exists(serverexe) { + os.rm(serverexe) } } fn test_a_simple_vweb_app_can_be_compiled() { // did_server_compile := os.system('$vexe -g -o $serverexe vlib/vweb/tests/vweb_test_server.v') // TODO: find out why it does not compile with -usecache and -g - did_server_compile := os.system('$main.vexe -o $main.serverexe vlib/vweb/tests/vweb_test_server.v') + did_server_compile := os.system('$vexe -o $serverexe vlib/vweb/tests/vweb_test_server.v') assert did_server_compile == 0 - assert os.exists(main.serverexe) + assert os.exists(serverexe) } fn test_a_simple_vweb_app_runs_in_the_background() { @@ -37,10 +37,10 @@ fn test_a_simple_vweb_app_runs_in_the_background() { $if !windows { suffix = ' > /dev/null &' } - if main.vweb_logfile != '' { - suffix = ' 2>> $main.vweb_logfile >> $main.vweb_logfile &' + if vweb_logfile != '' { + suffix = ' 2>> $vweb_logfile >> $vweb_logfile &' } - server_exec_cmd := '$main.serverexe $main.sport $main.exit_after_time $suffix' + server_exec_cmd := '$serverexe $sport $exit_after_time $suffix' $if debug_net_socket_client ? { eprintln('running:\n$server_exec_cmd') } @@ -102,14 +102,14 @@ fn assert_common_http_headers(x http.Response) { } fn test_http_client_index() { - x := http.get('http://127.0.0.1:$main.sport/') or { panic(err) } + x := http.get('http://127.0.0.1:$sport/') or { panic(err) } assert_common_http_headers(x) assert x.headers['Content-Type'] == 'text/plain' assert x.text == 'Welcome to VWeb' } fn test_http_client_chunk_transfer() { - x := http.get('http://127.0.0.1:$main.sport/chunk') or { panic(err) } + x := http.get('http://127.0.0.1:$sport/chunk') or { panic(err) } assert_common_http_headers(x) assert x.headers['Transfer-Encoding'] == 'chunked' assert x.text == 'Lorem ipsum dolor sit amet, consetetur sadipscing' @@ -117,9 +117,9 @@ fn test_http_client_chunk_transfer() { fn test_http_client_404() { url_404_list := [ - 'http://127.0.0.1:$main.sport/zxcnbnm', - 'http://127.0.0.1:$main.sport/JHKAJA', - 'http://127.0.0.1:$main.sport/unknown', + 'http://127.0.0.1:$sport/zxcnbnm', + 'http://127.0.0.1:$sport/JHKAJA', + 'http://127.0.0.1:$sport/unknown', ] for url in url_404_list { res := http.get(url) or { panic(err) } @@ -128,39 +128,39 @@ fn test_http_client_404() { } fn test_http_client_simple() { - x := http.get('http://127.0.0.1:$main.sport/simple') or { panic(err) } + x := http.get('http://127.0.0.1:$sport/simple') or { panic(err) } assert_common_http_headers(x) assert x.headers['Content-Type'] == 'text/plain' assert x.text == 'A simple result' } fn test_http_client_html_page() { - x := http.get('http://127.0.0.1:$main.sport/html_page') or { panic(err) } + x := http.get('http://127.0.0.1:$sport/html_page') or { panic(err) } assert_common_http_headers(x) assert x.headers['Content-Type'] == 'text/html' assert x.text == '

ok

' } fn test_http_client_settings_page() { - x := http.get('http://127.0.0.1:$main.sport/bilbo/settings') or { panic(err) } + x := http.get('http://127.0.0.1:$sport/bilbo/settings') or { panic(err) } assert_common_http_headers(x) assert x.text == 'username: bilbo' // - y := http.get('http://127.0.0.1:$main.sport/kent/settings') or { panic(err) } + y := http.get('http://127.0.0.1:$sport/kent/settings') or { panic(err) } assert_common_http_headers(y) assert y.text == 'username: kent' } fn test_http_client_user_repo_settings_page() { - x := http.get('http://127.0.0.1:$main.sport/bilbo/gostamp/settings') or { panic(err) } + x := http.get('http://127.0.0.1:$sport/bilbo/gostamp/settings') or { panic(err) } assert_common_http_headers(x) assert x.text == 'username: bilbo | repository: gostamp' // - y := http.get('http://127.0.0.1:$main.sport/kent/golang/settings') or { panic(err) } + y := http.get('http://127.0.0.1:$sport/kent/golang/settings') or { panic(err) } assert_common_http_headers(y) assert y.text == 'username: kent | repository: golang' // - z := http.get('http://127.0.0.1:$main.sport/missing/golang/settings') or { panic(err) } + z := http.get('http://127.0.0.1:$sport/missing/golang/settings') or { panic(err) } assert z.status_code == 404 } @@ -175,9 +175,7 @@ fn test_http_client_json_post() { age: 123 } json_for_ouser := json.encode(ouser) - mut x := http.post_json('http://127.0.0.1:$main.sport/json_echo', json_for_ouser) or { - panic(err) - } + mut x := http.post_json('http://127.0.0.1:$sport/json_echo', json_for_ouser) or { panic(err) } $if debug_net_socket_client ? { eprintln('/json_echo endpoint response: $x') } @@ -186,7 +184,7 @@ fn test_http_client_json_post() { nuser := json.decode(User, x.text) or { User{} } assert '$ouser' == '$nuser' // - x = http.post_json('http://127.0.0.1:$main.sport/json', json_for_ouser) or { panic(err) } + x = http.post_json('http://127.0.0.1:$sport/json', json_for_ouser) or { panic(err) } $if debug_net_socket_client ? { eprintln('/json endpoint response: $x') } @@ -197,7 +195,7 @@ fn test_http_client_json_post() { } fn test_http_client_shutdown_does_not_work_without_a_cookie() { - x := http.get('http://127.0.0.1:$main.sport/shutdown') or { + x := http.get('http://127.0.0.1:$sport/shutdown') or { assert err == '' return } @@ -208,7 +206,7 @@ fn test_http_client_shutdown_does_not_work_without_a_cookie() { fn testsuite_end() { // This test is guaranteed to be called last. // It sends a request to the server to shutdown. - x := http.fetch('http://127.0.0.1:$main.sport/shutdown', + x := http.fetch('http://127.0.0.1:$sport/shutdown', method: .get cookies: { 'skey': 'superman' @@ -236,7 +234,7 @@ fn simple_tcp_client(config SimpleTcpClientConfig) ?string { mut tries := 0 for tries < config.retries { tries++ - client = net.dial_tcp('127.0.0.1:$main.sport') or { + client = net.dial_tcp('127.0.0.1:$sport') or { if tries > config.retries { return error(err) } @@ -245,8 +243,8 @@ fn simple_tcp_client(config SimpleTcpClientConfig) ?string { } break } - client.set_read_timeout(main.tcp_r_timeout) - client.set_write_timeout(main.tcp_w_timeout) + client.set_read_timeout(tcp_r_timeout) + client.set_write_timeout(tcp_w_timeout) defer { client.close() } diff --git a/vlib/vweb/tests/vweb_test_server.v b/vlib/vweb/tests/vweb_test_server.v index e9447bd13f..08d2a907b0 100644 --- a/vlib/vweb/tests/vweb_test_server.v +++ b/vlib/vweb/tests/vweb_test_server.v @@ -64,7 +64,7 @@ pub fn (mut app App) chunk() vweb.Result { // the following serve custom routes ['/:user/settings'] pub fn (mut app App) settings(username string) vweb.Result { - if username !in main.known_users { + if username !in known_users { return app.not_found() } return app.html('username: $username') @@ -72,7 +72,7 @@ pub fn (mut app App) settings(username string) vweb.Result { ['/:user/:repo/settings'] pub fn (mut app App) user_repo_settings(username string, repository string) vweb.Result { - if username !in main.known_users { + if username !in known_users { return app.not_found() } return app.html('username: $username | repository: $repository')