diff options
Diffstat (limited to 'v_windows/v/vlib/v/fmt/tests')
197 files changed, 4841 insertions, 0 deletions
diff --git a/v_windows/v/vlib/v/fmt/tests/anon_fn_as_param_keep.vv b/v_windows/v/vlib/v/fmt/tests/anon_fn_as_param_keep.vv new file mode 100644 index 0000000..4ed7d1a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/anon_fn_as_param_keep.vv @@ -0,0 +1,20 @@ +import v.ast + +struct Data { + a fn (stmt ast.Stmt, vp voidptr) bool +} + +pub fn (a []Data) reduce(iter fn (int, int) int, accum_start int) int { + iter(accum_start) +} + +pub fn test_anon_fn_void(func fn ()) int { + return 0 +} + +fn C.HasAnonFnWithNamedParams(cb fn (c C.bar, d voidptr)) + +// NB: the signature of both anonymus functions should only differs in the param name +fn anon_fn_param_has_no_name(f fn (int) string) {} + +fn anon_fn_with_named_param(func fn (a int) string) {} diff --git a/v_windows/v/vlib/v/fmt/tests/anon_fn_call_keep.vv b/v_windows/v/vlib/v/fmt/tests/anon_fn_call_keep.vv new file mode 100644 index 0000000..e2661ba --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/anon_fn_call_keep.vv @@ -0,0 +1,7 @@ +fn main() { + fn () { + for { + println('Hello V!') + } + }() +} diff --git a/v_windows/v/vlib/v/fmt/tests/anon_fn_expected.vv b/v_windows/v/vlib/v/fmt/tests/anon_fn_expected.vv new file mode 100644 index 0000000..35641ee --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/anon_fn_expected.vv @@ -0,0 +1,24 @@ +fn has_anon_fn() { + an_fn := fn () { + println('Hello there !') + } + an_fn_w_param := fn (s string) { + println('I received $s') + } + an_fn_w_multi_params := fn (s1 string, s2 string, s3 string) { + println('I received $s1, $s2, $s3') + } + an_fn_w_multi_params2 := fn (s string, i int) { + println('I received $s, $i') + } + fn_w_var_args := fn (ss ...string) { + for s in ss { + println('yo $s') + } + } + an_fn() + an_fn_w_param('a gift') + an_fn_w_multi_params('one', 'two', 'three') + an_fn_w_multi_params2('one', 1) + fn_w_var_args('one arg', 'two args', 'three args') +} diff --git a/v_windows/v/vlib/v/fmt/tests/anon_fn_input.vv b/v_windows/v/vlib/v/fmt/tests/anon_fn_input.vv new file mode 100644 index 0000000..8884991 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/anon_fn_input.vv @@ -0,0 +1,23 @@ +fn has_anon_fn() { + an_fn := fn() { + println('Hello there !') + } + an_fn_w_param := fn ( s string ) + { + println('I received $s') + } + an_fn_w_multi_params := fn (s1 string, s2 string, s3 string) { + println('I received $s1, $s2, $s3') + } + an_fn_w_multi_params2 :=fn (s string, i int) { + println('I received $s, $i') + } + fn_w_var_args := fn (ss ...string) { + for s in ss { + println('yo $s') + } + } an_fn() + an_fn_w_param('a gift') an_fn_w_multi_params('one', 'two', 'three') + an_fn_w_multi_params2( 'one', 1) + fn_w_var_args('one arg', 'two args', 'three args') +} diff --git a/v_windows/v/vlib/v/fmt/tests/array_decomposition_keep.vv b/v_windows/v/vlib/v/fmt/tests/array_decomposition_keep.vv new file mode 100644 index 0000000..387997f --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/array_decomposition_keep.vv @@ -0,0 +1,8 @@ +fn varargs(a ...int) { + println(a) +} + +fn main() { + a := [1, 2, 3] + varargs(...a) +} diff --git a/v_windows/v/vlib/v/fmt/tests/array_init_comment_ending_keep.vv b/v_windows/v/vlib/v/fmt/tests/array_init_comment_ending_keep.vv new file mode 100644 index 0000000..fdbf675 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/array_init_comment_ending_keep.vv @@ -0,0 +1,23 @@ +import net.http + +const ( + write_set_cookie_tests = [ + ReadSetCookiesTestCase{ + header: { + 'Set-Cookie': ['special-7=","'] + } + cookies: [ + &http.Cookie{ + name: 'special-7' + value: ',' + raw: 'special-8=","' + }, + ] + } + // (bradfitz): users have reported seeing this in the + // wild, but do browsers handle it? RFC 6265 just says "don't + // do that" (section 3) and then never mentions header folding + // again. + // Header{"Set-Cookie": ["ASP.NET_SessionId=foo; path=/; HttpOnly, .ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly"]}, + ] +) diff --git a/v_windows/v/vlib/v/fmt/tests/array_init_eol_comments_keep.vv b/v_windows/v/vlib/v/fmt/tests/array_init_eol_comments_keep.vv new file mode 100644 index 0000000..a9e4570 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/array_init_eol_comments_keep.vv @@ -0,0 +1,104 @@ +fn abc() { + test_cases_f32 := [ + f32_from_bits1(0x0000_0000), // +0 + f32_from_bits1(0x8000_0000), // -0 + f32_from_bits1(0xFFC0_0001), // sNan + f32_from_bits1(0xFF80_0001), // qNan + f32_from_bits1(0x7F80_0000), // +inf + f32_from_bits1(0xFF80_0000), // -inf + 1, + -1, + 10, + -10, + 0.3, + -0.3, + 1000000, + 123456.7, + 123e35, + -123.45, + 1e23, + f32_from_bits1(0x0080_0000), // smallest float32 + math.max_f32, + 383260575764816448.0, + ] + + exp_result_f32 := [ + '0e+00', + '-0e+00', + 'nan', + 'nan', + '+inf', + '-inf', + '1.e+00', + '-1.e+00', + '1.e+01', + '-1.e+01', + '3.e-01', + '-3.e-01', + '1.e+06', + '1.234567e+05', + '1.23e+37', + '-1.2345e+02', + '1.e+23', + '1.1754944e-38', // aprox from 1.1754943508 × 10−38, + '3.4028235e+38', + '3.8326058e+17', + ] + + test_cases_f64 := [ + f64_from_bits1(0x0000_0000_0000_0000), // +0 + f64_from_bits1(0x8000_0000_0000_0000), // -0 + f64_from_bits1(0x7FF0_0000_0000_0001), // sNan + f64_from_bits1(0x7FF8_0000_0000_0001), // qNan + f64_from_bits1(0x7FF0_0000_0000_0000), // +inf + f64_from_bits1(0xFFF0_0000_0000_0000), // -inf + 1, + -1, + 10, + -10, + 0.3, + -0.3, + 1000000, + 123456.7, + 123e45, + -123.45, + 1e23, + f64_from_bits1(0x0010_0000_0000_0000), // smallest float64 + math.max_f32, + 383260575764816448, + 383260575764816448, + // C failing cases + 123e300, + 123e-300, + 5.e-324, + -5.e-324, + ] + + exp_result_f64 := [ + '0e+00', + '-0e+00', + 'nan', + 'nan', + '+inf', + '-inf', + '1.e+00', + '-1.e+00', + '1.e+01', + '-1.e+01', + '3.e-01', + '-3.e-01', + '1.e+06', + '1.234567e+05', + '1.23e+47', + '-1.2345e+02', + '1.e+23', + '2.2250738585072014e-308', + '3.4028234663852886e+38', + '3.8326057576481645e+17', + '3.8326057576481645e+17', + '1.23e+302', // this test is failed from C sprintf!! + '1.23e-298', + '5.e-324', + '-5.e-324', + ] +} diff --git a/v_windows/v/vlib/v/fmt/tests/array_init_expected.vv b/v_windows/v/vlib/v/fmt/tests/array_init_expected.vv new file mode 100644 index 0000000..44cf30f --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/array_init_expected.vv @@ -0,0 +1,35 @@ +fn main() { + if 1 == 1 { + println('yeah !') + } + if 1 != 1 { + println("oh no :'(") + } +} + +fn wrapping_tests() { + my_arr := ['Lorem ipsum dolor sit amet, consectetur adipiscing', + 'elit. Donec varius purus leo, vel maximus diam', + 'finibus sed. Etiam eu urna ante. Nunc quis vehicula', + 'velit. Sed at mauris et quam ornare tristique.', + ] +} + +fn array_init_without_commas() { + a := [3, -4, 5, -2] + b := [5 - 2, 7, 2 * 3, -4, -7 * 8, 2 * -4] + c := [4, 6 - 7, 8, 3 * 4] + d := [&a, &b, &c] + e := u16(434) + f := u16(23) + g := u16(4324) + h := [e & f, g, g & g] + k := 323 + l := [k, -k, 5] + x := &e + y := &f + z := &g + q := [f, *y, f * g, e] + ch := chan u16{} + w := [f, 12 <- ch] +} diff --git a/v_windows/v/vlib/v/fmt/tests/array_init_inline_comments_keep.vv b/v_windows/v/vlib/v/fmt/tests/array_init_inline_comments_keep.vv new file mode 100644 index 0000000..d1c18e9 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/array_init_inline_comments_keep.vv @@ -0,0 +1,2 @@ +a := [1 /* y */, /* x */ 2, 3] +println(a) diff --git a/v_windows/v/vlib/v/fmt/tests/array_init_input.vv b/v_windows/v/vlib/v/fmt/tests/array_init_input.vv new file mode 100644 index 0000000..4b465d7 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/array_init_input.vv @@ -0,0 +1,31 @@ +fn main() { + if 1 in [1] { + println('yeah !') + } + if 1 !in [1] { + println("oh no :'(") + } +} + +fn wrapping_tests() { + my_arr := ['Lorem ipsum dolor sit amet, consectetur adipiscing', 'elit. Donec varius purus leo, vel maximus diam', 'finibus sed. Etiam eu urna ante. Nunc quis vehicula', 'velit. Sed at mauris et quam ornare tristique.'] +} + +fn array_init_without_commas() { + a := [3 -4 5 -2] + b := [5-2,7,2*3,-4,-7*8,2*-4] + c := [4 6 - 7 8 3 * 4] + d := [&a &b &c] + e := u16(434) + f := u16(23) + g := u16(4324) + h := [e&f g g & g] + k := 323 + l := [k -k 5] + x := &e + y := &f + z := &g + q := [f *y f * g e] + ch := chan u16{} + w := [f 12 <-ch] +} diff --git a/v_windows/v/vlib/v/fmt/tests/array_init_keep.vv b/v_windows/v/vlib/v/fmt/tests/array_init_keep.vv new file mode 100644 index 0000000..8321e2e --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/array_init_keep.vv @@ -0,0 +1,9 @@ +fn main() { + _ := []int{len: 10, cap: 10, init: 7} + _ := []map[string]string{len: 5, cap: 50, init: { + 'a': 'a' + }} + _ := []map[string][]int{len: 7, cap: 100, init: { + 'a': [1, 2] + }} +} diff --git a/v_windows/v/vlib/v/fmt/tests/array_newlines_keep.vv b/v_windows/v/vlib/v/fmt/tests/array_newlines_keep.vv new file mode 100644 index 0000000..11fded1 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/array_newlines_keep.vv @@ -0,0 +1,17 @@ +fn make_flag(a string, b string, c string) string { + return '' +} + +fn main() { + // Set up flags + expected_flags := [ + make_flag('solaris', '-L', '/opt/local/lib'), + make_flag('macos', '-framework', 'Cocoa'), + make_flag('windows', '-l', 'gdi32'), + ] + x := []int{len: 10, cap: 100, init: 1} + _ := expected_flags + buf := [100]byte{} + println(x) + println(buf) +} diff --git a/v_windows/v/vlib/v/fmt/tests/array_slices_expected.vv b/v_windows/v/vlib/v/fmt/tests/array_slices_expected.vv new file mode 100644 index 0000000..b0bcee7 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/array_slices_expected.vv @@ -0,0 +1,12 @@ +fn fn_contains_index_expr() { + arr := [1, 2, 3, 4, 5] + a := 1 in arr[0..] + _ := a + _ := 1 in arr[..2] + _ := 1 in arr[1..3] + d := arr[2] + _ := d + _ := arr[2..] + _ := arr[..2] + _ := arr[1..3] +} diff --git a/v_windows/v/vlib/v/fmt/tests/array_slices_input.vv b/v_windows/v/vlib/v/fmt/tests/array_slices_input.vv new file mode 100644 index 0000000..d70add5 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/array_slices_input.vv @@ -0,0 +1,14 @@ + + +fn fn_contains_index_expr() { + arr := [1, 2, 3, 4, 5] + a := 1 in arr[ 0.. ] +_ := a + _ := 1 in arr[ ..2 ] + _ := 1 in arr[1..3] + d := arr[2] + _ := d + _ := arr[2 ..] + _ := arr[.. 2 ] + _ := arr[ 1 .. 3] +} diff --git a/v_windows/v/vlib/v/fmt/tests/array_static_keep.vv b/v_windows/v/vlib/v/fmt/tests/array_static_keep.vv new file mode 100644 index 0000000..1555780 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/array_static_keep.vv @@ -0,0 +1,14 @@ +struct Board { +mut: + field [10][5]int + points int + shifts int +} + +struct TileLine { + ypos int +mut: + field [5]int + points int + shifts int +} diff --git a/v_windows/v/vlib/v/fmt/tests/assembly/asm_keep.vv b/v_windows/v/vlib/v/fmt/tests/assembly/asm_keep.vv new file mode 100644 index 0000000..a6f9e56 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/assembly/asm_keep.vv @@ -0,0 +1,10 @@ +fn main() { + asm volatile amd64 { + mov ecx, 5 + loop_start: + add j, 3 + loop loop_start + ; +r (j) + ; ; ecx + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/asserts_expected.vv b/v_windows/v/vlib/v/fmt/tests/asserts_expected.vv new file mode 100644 index 0000000..08dd71a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/asserts_expected.vv @@ -0,0 +1,5 @@ +fn f() { + assert 0 == 0 + assert 0 < 1 + assert (1 + 2) == 3 +} diff --git a/v_windows/v/vlib/v/fmt/tests/asserts_input.vv b/v_windows/v/vlib/v/fmt/tests/asserts_input.vv new file mode 100644 index 0000000..ae2a01a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/asserts_input.vv @@ -0,0 +1,5 @@ +fn f() { + assert (0 == 0) + assert (0 < 1) + assert ((1 + 2) == 3) +} diff --git a/v_windows/v/vlib/v/fmt/tests/asserts_keep.vv b/v_windows/v/vlib/v/fmt/tests/asserts_keep.vv new file mode 100644 index 0000000..999c1fe --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/asserts_keep.vv @@ -0,0 +1,6 @@ +fn test_abc() { + assert 2 < 3 + assert 2 == 2 + assert 'abc' == 'abc' + assert 'abc'.len == 3 +} diff --git a/v_windows/v/vlib/v/fmt/tests/attrs_expected.vv b/v_windows/v/vlib/v/fmt/tests/attrs_expected.vv new file mode 100644 index 0000000..1660ee5 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/attrs_expected.vv @@ -0,0 +1,9 @@ +[export: 'JNICALL Java_io_vlang_V_callStaticMethods'] +[tom: 'jerry'] +[direct_array_access; inline; unsafe] +fn heavily_tagged() {} + +// console attribute for easier diagnostics on windows +// also it's not safe to use +[console; unsafe] +fn dangerous_console() {} diff --git a/v_windows/v/vlib/v/fmt/tests/attrs_input.vv b/v_windows/v/vlib/v/fmt/tests/attrs_input.vv new file mode 100644 index 0000000..bfffc55 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/attrs_input.vv @@ -0,0 +1,10 @@ +[inline] +[export: 'JNICALL Java_io_vlang_V_callStaticMethods'] +[direct_array_access] +[unsafe] +[tom: 'jerry'] +fn heavily_tagged() {} + +[console] // console attribute for easier diagnostics on windows +[unsafe] // also it's not safe to use +fn dangerous_console() {} diff --git a/v_windows/v/vlib/v/fmt/tests/attrs_keep.vv b/v_windows/v/vlib/v/fmt/tests/attrs_keep.vv new file mode 100644 index 0000000..cad488c --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/attrs_keep.vv @@ -0,0 +1,32 @@ +struct AttrsWithEscpaedStringArgs { + dollar string [foo: '\$var'] + double_bs string [bar: '\\baz'] +} + +[deprecated: 'use bar() instead'] +[foo: bar] +[if debug; inline] +fn keep_attributes() { + println('hi !') +} + +[bar: 'foo'] +fn attr_with_arg() {} + +['a_string_name'] +fn name_only_attr() {} + +struct User { + age int + nums []int + last_name string [json: lastName] + is_registered bool [json: IsRegistered] + typ int [json: 'type'] + pets string [json: 'pet_animals'; raw] +} + +[_allow_multiple_values] +enum Example { + value1 = 1 + value1_again = 1 +} diff --git a/v_windows/v/vlib/v/fmt/tests/bin2v_keep.vv b/v_windows/v/vlib/v/fmt/tests/bin2v_keep.vv new file mode 100644 index 0000000..5c6f861 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/bin2v_keep.vv @@ -0,0 +1,2 @@ +// This is a placeholder file which will be filled with bin2v output before the test. +// HINT: do NOT delete, move or rename this file! diff --git a/v_windows/v/vlib/v/fmt/tests/blocks_expected.vv b/v_windows/v/vlib/v/fmt/tests/blocks_expected.vv new file mode 100644 index 0000000..988f8a8 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/blocks_expected.vv @@ -0,0 +1,11 @@ +fn unsafe_fn() { + unsafe { + _ = malloc(2) + } +} + +fn fn_with_defer() { + defer { + voidfn() + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/blocks_input.vv b/v_windows/v/vlib/v/fmt/tests/blocks_input.vv new file mode 100644 index 0000000..6efacaa --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/blocks_input.vv @@ -0,0 +1,7 @@ +fn unsafe_fn() { +unsafe { _ = malloc(2) } +} + +fn fn_with_defer() { +defer { voidfn() } +} diff --git a/v_windows/v/vlib/v/fmt/tests/c_struct_init_keep.vv b/v_windows/v/vlib/v/fmt/tests/c_struct_init_keep.vv new file mode 100644 index 0000000..f5c93e1 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/c_struct_init_keep.vv @@ -0,0 +1,30 @@ +module main + +fn abc() { + desc := C.sapp_desc{ + width: a.width + height: a.height + user_data: &a + init_userdata_cb: init + frame_userdata_cb: frame + event_userdata_cb: event + window_title: title.str + html5_canvas_name: title.str + cleanup_userdata_cb: cleanup + } +} + +fn init(user_data voidptr) { + desc := C.sg_desc{ + mtl_device: sapp.metal_get_device() + mtl_renderpass_descriptor_cb: sapp.metal_get_renderpass_descriptor + mtl_drawable_cb: sapp.metal_get_drawable + d3d11_device: sapp.d3d11_get_device() + d3d11_device_context: sapp.d3d11_get_device_context() + d3d11_render_target_view_cb: sapp.d3d11_get_render_target_view + d3d11_depth_stencil_view_cb: sapp.d3d11_get_depth_stencil_view + } + sgl_desc := C.sgl_desc_t{ + max_vertices: 50 * 65536 + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/cast_expected.vv b/v_windows/v/vlib/v/fmt/tests/cast_expected.vv new file mode 100644 index 0000000..169f07c --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/cast_expected.vv @@ -0,0 +1,13 @@ +import v.ast { InfixExpr } +import v.table { Type } + +fn test_as() { + _ := sum_expr() as InfixExpr + _ := sum_expr() as ast.PrefixExpr +} + +fn test_cast() { + _ := f32(0) + _ := Type(0) + _ := ast.Expr(sum_expr()) +} diff --git a/v_windows/v/vlib/v/fmt/tests/cast_input.vv b/v_windows/v/vlib/v/fmt/tests/cast_input.vv new file mode 100644 index 0000000..72b254c --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/cast_input.vv @@ -0,0 +1,14 @@ +import v.ast { InfixExpr } +import v.table { Type } + +fn test_as() { + _ := sum_expr() as ast.InfixExpr + _ := sum_expr() as ast.PrefixExpr +} + +fn test_cast() { + _ := f32(0) + _ := table.Type(0) + _ := ast.Expr(sum_expr()) +} + diff --git a/v_windows/v/vlib/v/fmt/tests/chan_init_keep.vv b/v_windows/v/vlib/v/fmt/tests/chan_init_keep.vv new file mode 100644 index 0000000..47eaac6 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/chan_init_keep.vv @@ -0,0 +1,27 @@ +import some_mod + +struct ChanFieldType { + c chan some_mod.Type +} + +struct FSMEvent { + x int +} + +fn abc(n FSMEvent) { +} + +fn (e FSMEvent) abc(n FSMEvent) { +} + +fn (e FSMEvent) x(ch chan FSMEvent) { +} + +fn produce_events(ch chan FSMEvent) { +} + +fn main() { + ch_fsm_events := chan FSMEvent{cap: 1000} + eprintln('ch_fsm_events.len: $ch_fsm_events.len') + eprintln('ch_fsm_events.cap: $ch_fsm_events.cap') +} diff --git a/v_windows/v/vlib/v/fmt/tests/chan_ops_keep.vv b/v_windows/v/vlib/v/fmt/tests/chan_ops_keep.vv new file mode 100644 index 0000000..57ae616 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/chan_ops_keep.vv @@ -0,0 +1,65 @@ +import sync + +const ( + num_iterations = 10000 +) + +struct St { + a int +} + +fn get_val_from_chan(ch chan i64) ?i64 { + r := <-ch ? + return r +} + +fn get_val_from_chan2(ch chan i64) ?i64 { + r := <-ch or { + println('error') + return err + } + return r +} + +// this function gets an array of channels for `i64` +fn do_rec_calc_send(chs []chan i64, sem sync.Semaphore) { + mut msg := '' + for { + mut s := get_val_from_chan(chs[0]) or { + msg = err.str() + break + } + s++ + chs[1] <- s + } + assert msg == 'channel closed' + sem.post() +} + +fn test_channel_array_mut() { + mut chs := [chan i64{}, chan i64{cap: 10}] + sem := sync.new_semaphore() + go do_rec_calc_send(chs, sem) + mut t := i64(100) + for _ in 0 .. num_iterations { + chs[0] <- t + t = <-chs[1] + } + (&sync.Channel(chs[0])).close() + orr := &sync.Channel(chs[0]) + chs[1].close() + ch := chan int{} + ch.close() + a := ch.cap + b := ch.len + c := ch[1].cap + d := ch[o].len + sem.wait() + assert t == 100 + num_iterations + ch2 := chan mut St{cap: 10} + go g(ch2) +} + +fn g(ch chan mut St) { + return +} diff --git a/v_windows/v/vlib/v/fmt/tests/chan_or_keep.vv b/v_windows/v/vlib/v/fmt/tests/chan_or_keep.vv new file mode 100644 index 0000000..a6e7a90 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/chan_or_keep.vv @@ -0,0 +1,33 @@ +const n = 1000 + +fn f(ch chan int) { + mut s := 0 + for _ in 0 .. n { + s += <-ch or { + println('Something went wrong:') + println('got $err') + } + } + assert s == n * (n + 1) / 2 + ch.close() +} + +fn do_send(ch chan int, val int) ?int { + ch <- val ? + return val + 1 +} + +fn do_send_2(ch chan int, val int) ?int { + ch <- val or { return error('could not send') } + return val + 1 +} + +fn main() { + ch := chan int{} + go f(ch) + mut s := 1 + for { + s = do_send(ch, s) or { break } + } + assert s == n + 1 +} diff --git a/v_windows/v/vlib/v/fmt/tests/char_literal_keep.vv b/v_windows/v/vlib/v/fmt/tests/char_literal_keep.vv new file mode 100644 index 0000000..e00c5e4 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/char_literal_keep.vv @@ -0,0 +1,3 @@ +println(`"`) +printrn(`'`) +println(`\``) diff --git a/v_windows/v/vlib/v/fmt/tests/closure_keep.vv b/v_windows/v/vlib/v/fmt/tests/closure_keep.vv new file mode 100644 index 0000000..ec05f85 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/closure_keep.vv @@ -0,0 +1,10 @@ +my_var := 1 +my_simple_closure := fn [my_var] () { + println(my_var) +} +my_closure_returns := fn [my_var] () int { + return my_var +} +my_closure_has_params := fn [my_var, my_closure_returns] (add int) { + println(my_var + my_closure_returns() + add) +} diff --git a/v_windows/v/vlib/v/fmt/tests/comments_array_keep.vv b/v_windows/v/vlib/v/fmt/tests/comments_array_keep.vv new file mode 100644 index 0000000..74632ed --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/comments_array_keep.vv @@ -0,0 +1,79 @@ +fn main() { + _ := [ + // pre comment + 6, + // and after + ] + _ := [ + 7, + // below expr + ] + _ := [ + 8, /* I don't know why this still is a block comment */ + 9, + ] + arr := [ + // test 0 + 1, + // test 1 + 2, + // test 2 + 3, /* 3 */ + 4, /* 4-1 */ /* 4-2 */ + ] +} + +fn only_comments_array() { + _ := [/* on a single line */ /* too */] + _ := [ + // 1, + // 2, + // 3, + ] + _ := [ + /* whatever */ /* this is */ // 3, + // 4, + ] +} + +fn single_line_array_pre_comments() { + _ := [/* 2, */ 3] + _ := [/* 4, */ /* 5, */ 6] + _ := [/* cmt */ -4] +} + +fn single_line_array_iembed_comments() { + _ := [1, /* betw single line */ 2] + // This caused a bug where the ´-´ was parsed as InfixExpr and not as part of an IntegerLiteral + _ := [1, /* cmt */ -4] +} + +fn mixed_comments() { + _ := [ + 3 /* iembed */, + // keep line comment here + // and here + 5, + ] +} + +fn keep_real_block_comment() { + _ := [ + 'foo', + /* + 'bar', + 'baz', + 'spam', + */ + 'eggs', + ] +} + +fn comment_at_line_start_with_expressions_after() { + arr := [123456789012345, 234567890123456, 678901234567890, 789012345678901, /* at the end */ + 345678901234567, /* in between */ 456789012345678, + /* line start */ 567890123456789, 890123456789012] + arr2 := [/* same line pre comment */ Foo{ + a: 123 + }] +} diff --git a/v_windows/v/vlib/v/fmt/tests/comments_expected.vv b/v_windows/v/vlib/v/fmt/tests/comments_expected.vv new file mode 100644 index 0000000..859908f --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/comments_expected.vv @@ -0,0 +1,105 @@ +import time // foo + +/* +block +comment +*/ + +fn fun() int { + // comment zero + return 0 // another comment +} + +fn mr_fun() (int, int) { + // one comment + // another comment + return 1, 2 +} + +fn single_line_blocks() { + // 1 + println('') + // 2 + println('') + // 3 + // 4 + println('') + // 5 + // 6 +} + +fn main() { + /* + block1 + */ + /* + block2 + */ + /* + block3 + */ + // this is a comment + a := 1 + // and another comment + // just to make it worse + b, c := a, 2 + d := c // and an extra one + e := c + // more comments = more good + arr := [ + // block foo bar + // inline foo bar + 0, + ] + // before arg comment + // after arg comment + println('this is a test') + // before if expr + // after if expr + if true { + println('if') + } + // before else if + // between else if + else if false { + println('else if') + } + // before else + // after else + else { + println('else') + } + // empty return + return +} + +fn insert_space() { + // abc +} + +fn linebreaks_in_block_comments() { + /* + foo + comment goes here! + bar + */ + /* + spam + spaces make no difference there + eggs + */ +} + +fn between_if_branches() { + if spam { + } + // remove the empty line above + else if eggs { + } + + if spam2 { + } + // remove the empty line below + else { + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/comments_input.vv b/v_windows/v/vlib/v/fmt/tests/comments_input.vv new file mode 100644 index 0000000..e67d070 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/comments_input.vv @@ -0,0 +1,89 @@ +import time // foo +/* +block +comment +*/ + +fn fun() int { + return /* comment zero */ 0 // another comment +} + +fn mr_fun() (int, int) { + return /* one comment */ 1, /* another comment */ 2 +} + +fn single_line_blocks() { + /* 1 */ + println('') + /* 2 */ + println('') + /* 3 */ + /* 4 */ + println('') + // 5 + /* 6 */ +} + +fn main() { + /* block1 + */ + /* + block2 */ + /* + + block3 + + */ + a := /* this is a comment */ 1 + b, c := /* and another comment */ a, /* just to make it worse */ 2 + d := c // and an extra one + e := c + // more comments = more good + arr := [ + /* block foo bar */ + // inline foo bar + 0, + ] + println(/* before arg comment */ 'this is a test' /* after arg comment */) + if /* before if expr */ true /* after if expr */ { + println('if') + } + // before else if + else /* between else if */ if false { + println('else if') + } + // before else + else /* after else */ { + println('else') + } + return // empty return +} + +fn insert_space() { + //abc +} + +fn linebreaks_in_block_comments() { + /*foo + comment goes here! + bar*/ + /* spam + spaces make no difference there + eggs */ +} + +fn between_if_branches() { + if spam { + } + + // remove the empty line above + else if eggs { + } + + if spam2 { + } + // remove the empty line below + + else { + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/comments_keep.vv b/v_windows/v/vlib/v/fmt/tests/comments_keep.vv new file mode 100644 index 0000000..5af7280 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/comments_keep.vv @@ -0,0 +1,101 @@ +import semver // as sv + +enum Abc { + a + b // after a value + c + // between values + d +} + +struct User { + name string // name + // middle comment + age int + // last comment + // last comment2 +} + +fn main() { + u := User{ + name: 'Peter' + } + n := sizeof(User) + // else + // else { + // } + _ := User{ + name: 'Henry' // comment after name + // on the next line + age: 42 + // after age line + // after line2 + } + _ := User{ + // Just a comment + } + ////// + // / + // 123 + match 0 { + 0 { + 0 // comment after an expression inside match + } + else {} + } +} + +fn assign_comments() { + a := 123 // comment after assign + b := 'foo' // also comment after assign + c := true + // Between two assigns + d := false + // at the end +} + +fn linebreaks_in_ascii_art_block_comments() { + /* + +++ + */ + /***** + +++ + *****/ + /**** + +++ + */ + /* + +++ + ****/ +} + +fn map_comments() { + mymap := { + // pre + `:`: 1 + `!`: 2 // after + // and between + `%`: 3 + // between + // between second + `$`: 4 + `&`: 5 + // post + } +} + +fn ifs_comments_and_empty_lines() { + if true { + } + // some comment direct after an if without else + if false { + } else { + } + // some comment direct after an else + if false { + } + + // this is parsed as post_comment of the if but does not really belong there + // thereore keep the empty line + something_else() +} diff --git a/v_windows/v/vlib/v/fmt/tests/comptime_field_selector_keep.vv b/v_windows/v/vlib/v/fmt/tests/comptime_field_selector_keep.vv new file mode 100644 index 0000000..e9752de --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/comptime_field_selector_keep.vv @@ -0,0 +1,23 @@ +struct Foo { +mut: + test string + name string +} + +fn (f Foo) print() { + println('test') +} + +fn test<T>() { + mut t := T{} + t.name = '2' + $for f in T.fields { + $if f.typ is string { + println(t.$(f.name)) + } + } +} + +fn main() { + test<Foo>() +} diff --git a/v_windows/v/vlib/v/fmt/tests/comptime_field_selector_parentheses_keep.vv b/v_windows/v/vlib/v/fmt/tests/comptime_field_selector_parentheses_keep.vv new file mode 100644 index 0000000..e9752de --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/comptime_field_selector_parentheses_keep.vv @@ -0,0 +1,23 @@ +struct Foo { +mut: + test string + name string +} + +fn (f Foo) print() { + println('test') +} + +fn test<T>() { + mut t := T{} + t.name = '2' + $for f in T.fields { + $if f.typ is string { + println(t.$(f.name)) + } + } +} + +fn main() { + test<Foo>() +} diff --git a/v_windows/v/vlib/v/fmt/tests/comptime_keep.vv b/v_windows/v/vlib/v/fmt/tests/comptime_keep.vv new file mode 100644 index 0000000..987e513 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/comptime_keep.vv @@ -0,0 +1,91 @@ +import vweb + +struct App { + a string + b string +mut: + c int + d f32 +pub: + e f32 + f u64 +pub mut: + g string + h byte +} + +fn comptime_for() { + println(@FN) + $for method in App.methods { + println(' method: $method.name | $method') + } +} + +fn comptime_for_with_if() { + println(@FN) + $for method in App.methods { + println(' method: $method') + $if method.typ is fn () { + assert method.name in ['run', 'method2'] + } + $if method.return_type is int { + assert method.name in ['int_method1', 'int_method2'] + } + $if method.args[0].typ is string { + assert method.name == 'my_method' + } + } +} + +fn comptime_for_fields() { + println(@FN) + $for field in App.fields { + println(' field: $field.name | $field') + $if field.typ is string { + assert field.name in ['a', 'b', 'g'] + } + $if field.typ is f32 { + assert field.name in ['d', 'e'] + } + if field.is_mut { + assert field.name in ['c', 'd', 'g', 'h'] + } + if field.is_pub { + assert field.name in ['e', 'f', 'g', 'h'] + } + if field.is_pub && field.is_mut { + assert field.name in ['g', 'h'] + } + } +} + +struct Result {} + +fn (mut a App) my_method(p string) Result { + println('>>>> ${@FN} | p: $p') + return Result{} +} + +fn handle_conn<T>(mut app T) { + $for method in T.methods { + $if method.return_type is Result { + app.$method('abc', 'def') + } + } +} + +fn comptime_call_dollar_method() { + mut app := App{} + handle_conn<App>(mut app) +} + +fn (mut app App) create() vweb.Result { + return $vweb.html() +} + +fn main() { + comptime_for() + comptime_for_with_if() + comptime_for_fields() + comptime_call_dollar_method() +} diff --git a/v_windows/v/vlib/v/fmt/tests/concat_expr_expected.vv b/v_windows/v/vlib/v/fmt/tests/concat_expr_expected.vv new file mode 100644 index 0000000..47d5872 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/concat_expr_expected.vv @@ -0,0 +1,24 @@ +fn concatenation_of_strings() { + _ := 'Simple' + 'Concat' + _ := 'Hello' + ' ' + 'World' + '!' + _ := 'There' + ' ' + 'so' + ' ' + 'many' + ' ' + 'words' + ' ' + 'they' + ' ' + "don't" + ' ' + + 'fit' + ' ' + 'in' + ' ' + 'one' + ' ' + 'line' +} + +fn concat_inside_ternary() { + { // This block is needed to force line wrapping + cline := if iline == pos.line_nr { + sline[..start_column] + color(kind, sline[start_column..end_column]) + + sline[end_column..] + } else { + sline + } + } +} + +fn concat_two_or_more_ternaries() { + x := if some_condition { 'very long string part' } else { 'def' } + + if y == 'asd' { 'qwe' } else { 'something else' } + var := if other_condition { 'concat three long ternary ifs' } else { 'def' } + + if true { 'shorter' } else { 'quite short' } + if true { 'small' } else { 'tiny' } +} diff --git a/v_windows/v/vlib/v/fmt/tests/concat_expr_input.vv b/v_windows/v/vlib/v/fmt/tests/concat_expr_input.vv new file mode 100644 index 0000000..383c52e --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/concat_expr_input.vv @@ -0,0 +1,22 @@ +fn concatenation_of_strings() { +_ := 'Simple' + 'Concat' + _ := 'Hello' + + ' ' + + 'World' + '!' + _ := 'There' + ' ' + 'so' + ' ' + 'many' + ' ' + 'words' + ' ' + 'they' + ' ' + "don't" + ' ' + 'fit' + ' ' + 'in' + ' ' + 'one' + ' ' + 'line' +} + +fn concat_inside_ternary() { + { // This block is needed to force line wrapping + cline := if iline == pos.line_nr { + sline[..start_column] + color(kind, sline[start_column..end_column]) + sline[end_column..] + } else { + sline + } + } +} + +fn concat_two_or_more_ternaries() { + x := if some_condition { 'very long string part' } else { 'def' } + if y == 'asd' { 'qwe' } else {'something else'} + var := if other_condition { 'concat three long ternary ifs' } else { 'def' } + if true {'shorter'} else {'quite short'} + if true { 'small' } else {'tiny'} +} diff --git a/v_windows/v/vlib/v/fmt/tests/conditional_compilation_keep.vv b/v_windows/v/vlib/v/fmt/tests/conditional_compilation_keep.vv new file mode 100644 index 0000000..3d7e1d2 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/conditional_compilation_keep.vv @@ -0,0 +1,19 @@ +// __global g_my_global_variable int +fn main() { + $if tinyc { + println('This will be compiled only when the compiler is tcc') + } + $if !windows { + println('This will get compiled on non-windows platforms.') + } + // + $if !linux { + println('Only non linux platforms will get this') + } $else { + println('This part is for linux only.') + } + // + $if network ? { + println('This will be run, only when the program is compiled with `-d network`') + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/conditions_expected.vv b/v_windows/v/vlib/v/fmt/tests/conditions_expected.vv new file mode 100644 index 0000000..cee2823 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/conditions_expected.vv @@ -0,0 +1,12 @@ +fn fn_with_if_else() { + if true { + a = 10 + a++ + } else { + println('false') + } +} + +fn fn_with_if_else_oneline() { + _ := if true { 1 } else { 2 } +} diff --git a/v_windows/v/vlib/v/fmt/tests/conditions_input.vv b/v_windows/v/vlib/v/fmt/tests/conditions_input.vv new file mode 100644 index 0000000..e38b836 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/conditions_input.vv @@ -0,0 +1,10 @@ +fn fn_with_if_else() { + if true { + a = 10 + a++ + } else { println('false') } +} + +fn fn_with_if_else_oneline() { +_ := if true { 1 } else { 2 } +} diff --git a/v_windows/v/vlib/v/fmt/tests/consts_expected.vv b/v_windows/v/vlib/v/fmt/tests/consts_expected.vv new file mode 100644 index 0000000..b97e49d --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/consts_expected.vv @@ -0,0 +1,60 @@ +const ( + // pi + // pi + pi = 3.14 + // phi + // phi + // phi + phi = 1.618 + // Euler's constant + eulers = 2.7182 + supported_platforms = ['windows', 'macos', 'linux', 'freebsd', 'openbsd', 'netbsd', 'dragonfly', + 'android', 'js', 'solaris', 'haiku'] + one_line_supported = ['windows', 'macos', 'linux', 'freebsd', 'openbsd', 'netbsd', 'dragonfly', + 'android', 'js', 'solaris', 'haiku'] + another_const = ['a', 'b', 'c', 'd', 'e', 'f'] + multiline_const = [ + 'first line', + 'second line', + 'third line', + 'fourth line', + ] +) + +const ( + i_am_a_very_long_constant_name_so_i_stand_alone_and_my_length_is_over_90_characters = [ + 'testforit', + ] +) + +pub const ( + i_am_pub_const = true +) + +fn main() { + a := [ + [3, 5, 6], + [7, 9, 2], + ] + b := [[ + [2, 5, 8], + [5, 1, 3], + [2, 6, 0], + ], [ + [9, 4, 5], + [7, 2, 3], + [1, 2, 3], + ]] + c := [ + [ + [2, 5, 8], + [5, 1, 3], + [2, 6, 0], + ], + [ + [9, 4, 5], + [7, 2, 3], + [1, 2, 3], + ], + ] +} diff --git a/v_windows/v/vlib/v/fmt/tests/consts_input.vv b/v_windows/v/vlib/v/fmt/tests/consts_input.vv new file mode 100644 index 0000000..e16942a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/consts_input.vv @@ -0,0 +1,54 @@ +const ( +// pi +// pi +pi=3.14 +// phi +// phi +// phi +phi=1.618 + //Euler's constant +eulers=2.7182 +supported_platforms = ['windows', 'macos', 'linux', 'freebsd', 'openbsd', + 'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku'] +one_line_supported = ['windows', 'macos', 'linux', 'freebsd', 'openbsd', 'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku'] +another_const = ['a', 'b' + 'c', 'd', 'e' + 'f' + ] +multiline_const = [ + 'first line', 'second line','third line', + 'fourth line'] +) + +const ( + i_am_a_very_long_constant_name_so_i_stand_alone_and_my_length_is_over_90_characters = ['testforit'] +) + +pub const ( +i_am_pub_const=true +) + +fn main() { + a := [ + [3, +5, + 6],[7, 9, 2]] +b := [[ +[2, +5,8],[ 5, 1, +3],[ 2, 6, 0]],[ +[9, +4,5],[ +7,2,3], +[1, +2,3]]] +c := [ +[ +[2, +5,8],[ 5, 1, +3],[ 2, 6, 0]],[[ +9, +4,5],[7,2,3], +[1, +2,3]]] +} diff --git a/v_windows/v/vlib/v/fmt/tests/consts_keep.vv b/v_windows/v/vlib/v/fmt/tests/consts_keep.vv new file mode 100644 index 0000000..c505ad7 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/consts_keep.vv @@ -0,0 +1,27 @@ +const font = $embed_file('../assets/fonts/RobotoMono-Regular.ttf') + +const ( + test_alignment = 123 + foo = Foo{ + f: 'foo' + } + spam = 456 + egg = 'lorem ipsum' + spameggs = true +) + +const ( + bar = 'A string +spanning multiple +lines' + baz = 'short' + some_long_name = MyStruct{ + x: 42 + } +) + +const ( + a = 123 + abc = 123 + b = 123 +) diff --git a/v_windows/v/vlib/v/fmt/tests/consts_with_comments_keep.vv b/v_windows/v/vlib/v/fmt/tests/consts_with_comments_keep.vv new file mode 100644 index 0000000..2f8a221 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/consts_with_comments_keep.vv @@ -0,0 +1,4 @@ +const ( + fsm_state_array = ['init', 'state_a', 'state_b', 'state_c', 'exit'] // use as a first half key for map see fsm_state_ev_fn, the same order as in enum FSM_state + fsm_event_array = ['ev1', 'ev2', 'ev3', 'ev4', 'ev5'] // use as a second half key for map see fsm_state_ev_fn, the same order as in enum FSM_event +) diff --git a/v_windows/v/vlib/v/fmt/tests/embed_file_keep.vv b/v_windows/v/vlib/v/fmt/tests/embed_file_keep.vv new file mode 100644 index 0000000..2234bd7 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/embed_file_keep.vv @@ -0,0 +1,8 @@ +fn main() { + mut the_png := $embed_file('v.png') + println(the_png) + content := the_png.data() + eprintln('content: ${ptr_str(content)}') + eprintln(unsafe { the_png.data().vbytes(the_png.len) }.hex()) + println(the_png) +} diff --git a/v_windows/v/vlib/v/fmt/tests/empty_curlies_and_parens_keep.vv b/v_windows/v/vlib/v/fmt/tests/empty_curlies_and_parens_keep.vv new file mode 100644 index 0000000..30e4420 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/empty_curlies_and_parens_keep.vv @@ -0,0 +1,44 @@ +const () + +const ( +) + +struct Bar {} + +struct Bar2 { +} + +interface Spam {} + +interface Spam2 { +} + +enum Baz {} + +enum Baz2 { +} + +fn foo() {} + +fn foo2() { +} + +fn main() { + arr := []int{} + x := 1 + for s in arr {} + for s in arr { + } + for i := 0; i < 5; i++ {} + for j := 0; j < 5; j++ { + } + for false {} + for false { + } + defer {} + defer { + } + unsafe {} + unsafe { + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/empty_lines_expected.vv b/v_windows/v/vlib/v/fmt/tests/empty_lines_expected.vv new file mode 100644 index 0000000..605c552 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/empty_lines_expected.vv @@ -0,0 +1,28 @@ +type MyInt = int +type EmptyLineAfterLastType = fn () int + +struct EmptyLineAfterStructs {} + +fn empty_line_after_functions() {} + +fn squash_multiple_empty_lines() { + println('a') + + println('b') + + c := 0 + + d := 0 +} + +fn remove_leading_and_trailing_empty_lines() { + println('a') + + println('b') + + if test { + c := 0 + } else { + d := 0 + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/empty_lines_input.vv b/v_windows/v/vlib/v/fmt/tests/empty_lines_input.vv new file mode 100644 index 0000000..f8eee66 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/empty_lines_input.vv @@ -0,0 +1,34 @@ +type MyInt = int +type EmptyLineAfterLastType = fn() int +struct EmptyLineAfterStructs {} +fn empty_line_after_functions() {} +fn squash_multiple_empty_lines() { + println('a') + + + println('b') + + + c := 0 + + + d := 0 +} + +fn remove_leading_and_trailing_empty_lines() { + + println('a') + + println('b') + + if test { + + c := 0 + + } else { + + d := 0 + + } + +} diff --git a/v_windows/v/vlib/v/fmt/tests/empty_lines_keep.vv b/v_windows/v/vlib/v/fmt/tests/empty_lines_keep.vv new file mode 100644 index 0000000..7e22486 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/empty_lines_keep.vv @@ -0,0 +1,98 @@ +// Keep empty lines between types +type Expr = IfExpr | IntegerLiteral + +type Node2 = Expr | string +type MyInt = int + +// Keep empty lines in const blocks +const ( + _ = SomeStruct{ + val: 'Multiline field exprs should cause no problems' + } + _ = 1 + + _ = 'Keep this empty line before' + // Comment before a field + _ = 2 + + // The empty line above should stay too + _ = 3 + + // This comment doesn't really belong anywhere + + _ = 'A multiline string with a StringInterLiteral... + $foo + ...and the ending brace on a newline had a wrong pos.last_line. + ' + _ = 4 +) + +fn keep_single_empty_line() { + println('a') + println('b') + + println('c') + + d := 0 + + if true { + println('e') + } + + f := 0 + arr << MyStruct{} + + arr2 := [1, 2] +} + +fn prevent_empty_line_after_multi_line_statements() { + // line1 + /* + block1 + */ + /* + block2 + */ + if test { + println('a') + } + println('b') + for test { + println('c') + } + c := fn (s string) { + println('s') + } +} + +fn between_orm_blocks() { + sql db { + insert upper_1 into Upper + } + + upper_s := sql db { + select from Upper where id == 1 + } +} + +fn no_empty_lines() { + _ := $embed_file('testy.v') + mut files := map[string][]FileData{} + code := 'foo +bar' + params[0] = IfExpr{ + ...params[0] + typ: params[0].typ.set_nr_muls(1) + } + env_value = environ()[env_lit] or { + return error('the environment variable "$env_lit" does not exist.') + } + assert '$mr_one_two()' == "(One{ + value: 'one' +}, Two{ + value: 'two' +})" + r := m[key] ? + compile_time_env := $env('ENV_VAR') + func() +} diff --git a/v_windows/v/vlib/v/fmt/tests/enum_comments_keep.vv b/v_windows/v/vlib/v/fmt/tests/enum_comments_keep.vv new file mode 100644 index 0000000..f46208b --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/enum_comments_keep.vv @@ -0,0 +1,17 @@ +module main + +// Top level comment +enum EnumType { + // comment for the enum + abc + xyz // comment for the enum field +} + +// Another top level comment +enum PacketType { + // Regular data packet + data // abc def + // xyz + // another comment + abcd // jklmn +} diff --git a/v_windows/v/vlib/v/fmt/tests/enums_expected.vv b/v_windows/v/vlib/v/fmt/tests/enums_expected.vv new file mode 100644 index 0000000..8c30ec2 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/enums_expected.vv @@ -0,0 +1,9 @@ +pub enum PubEnum { + foo + bar +} + +enum PrivateEnum { + foo + bar +} diff --git a/v_windows/v/vlib/v/fmt/tests/enums_input.vv b/v_windows/v/vlib/v/fmt/tests/enums_input.vv new file mode 100644 index 0000000..c7af1d6 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/enums_input.vv @@ -0,0 +1,5 @@ +pub enum PubEnum{ + foo + bar +} +enum PrivateEnum { foo bar } diff --git a/v_windows/v/vlib/v/fmt/tests/expressions_expected.vv b/v_windows/v/vlib/v/fmt/tests/expressions_expected.vv new file mode 100644 index 0000000..1498e2e --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/expressions_expected.vv @@ -0,0 +1,103 @@ +import v.checker +import v.ast +import v.table +import v.gen +import v.token + +fn string_inter_lit(mut c checker.Checker, mut node ast.StringInterLiteral) table.Type { + for i, expr in node.exprs { + ftyp := c.expr(expr) + node.expr_types << ftyp + typ := c.table.unalias_num_type(ftyp) + mut fmt := node.fmts[i] + // analyze and validate format specifier + if fmt !in [`E`, `F`, `G`, `e`, `f`, `g`, `d`, `u`, `x`, `X`, `o`, `c`, `s`, `p`, `_`] { + c.error('unknown format specifier `${fmt:c}`', node.fmt_poss[i]) + } + if node.precisions[i] != 987698 && !typ.is_float() { + c.error('precision specification only valid for float types', node.fmt_poss[i]) + } + if node.pluss[i] && !typ.is_number() { + c.error('plus prefix only allowd for numbers', node.fmt_poss[i]) + } + if (typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`]) + || (typ.is_signed() && fmt !in [`d`, `x`, `X`, `o`, `c`]) + || (typ.is_int_literal() && fmt !in [`d`, `c`, `x`, `X`, `o`, `u`, `x`, `X`, `o`]) + || (typ.is_float() && fmt !in [`E`, `F`, `G`, `e`, `f`, `g`]) + || (typ.is_pointer() && fmt !in [`p`, `x`, `X`]) + || (typ.is_string() && fmt != `s`) + || (typ.idx() in [table.i64_type_idx, table.f64_type_idx] && fmt == `c`) { + c.error('illegal format specifier `${fmt:c}` for type `${c.table.get_type_name(ftyp)}`', + node.fmt_poss[i]) + } + node.need_fmts[i] = fmt != c.get_default_fmt(ftyp, typ) + } + + return table.string_type +} + +fn get_some_val(a_test f64, b_test f64, c_test f64, d_test f64, e_test f64, f_test f64) f64 { + return a_test * b_test * c_test * d_test + e_test * f_test * a_test * d_test + + a_test * b_test * c_test +} + +fn main() { + a, b, r, d := 5.3, 7.5, 4.4, 6.6 + if a + b + r * d + a + b + r * d > a + b + r * d + a * b + r { + println('ok') + } + v_str := 'v' + s := []string{} + s << ' `$v_str`' + println(s) + println('this is quite a long string' + + ' that is followd by an even longer part that should go to another line') + if (a == b && b > r) || (d > r) || (a < b) || (b < d && a + b > r) + || (a + b + d >= 0 && r < 0) || (a > b && d - r < b) { + println('ok') + } +} + +fn gen_str_for_multi_return(mut g gen.Gen, info table.MultiReturn, styp string, str_fn_name string) { + for i, _ in info.types { + println('\tstrings__Builder_write(&sb, _STR("\'%.*s\\000\'", 2, a.arg$i));') + } +} + +struct Parser { + peek_tok token.Token + peek_tok2 token.Token + peek_tok3 token.Token +} + +fn (mut p Parser) name_expr() { + if p.peek_tok.kind == .lpar + || (p.peek_tok.kind == .lt && p.peek_tok2.kind == .name && p.peek_tok3.kind == .gt) { + println(p.peek_tok.lit) + } +} + +fn set_nr_muls(t table.Type, nr_muls int) table.Type { + return int(t) & 0xff00ffff | (nr_muls << 16) +} + +// Test what exprs are treated as multiline. The ternary if only functions as a wrapper. +// When one expr in a branch doesn't fit a single line, the whole if will be unwrapped. +fn multiline_exprs() { + // StructInit with at least one field + _ := if true { + Foo{} + } else { + Foo{ + val: 123 + } + } + // ConcatExpr with a multiline child expr + _, _ := if true { + 1, Foo{ + val: 123 + } + } else { + 2, Foo{} + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/expressions_input.vv b/v_windows/v/vlib/v/fmt/tests/expressions_input.vv new file mode 100644 index 0000000..dec30ce --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/expressions_input.vv @@ -0,0 +1,102 @@ +import v.checker +import v.ast +import v.table +import v.gen +import v.token + +fn string_inter_lit(mut c checker.Checker, mut node ast.StringInterLiteral) table.Type { + for i, expr in node.exprs { + ftyp := c.expr(expr) + node.expr_types << ftyp + typ := c.table.unalias_num_type(ftyp) + mut fmt := node.fmts[i] + // analyze and validate format specifier + if fmt !in [`E`, `F`, `G`, `e`, `f`, `g`, + `d`, `u`, `x`, `X`, `o`, `c`, `s`, `p`, `_`] { + c.error('unknown format specifier `${fmt:c}`', + node.fmt_poss[i]) + } + if node.precisions[i] != 987698 && + !typ.is_float() { + c.error('precision specification only valid for float types', + node.fmt_poss[i]) + } + if node.pluss[i] && !typ.is_number() { + c.error('plus prefix only allowd for numbers', node.fmt_poss[i]) + } + if (typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`]) || (typ.is_signed() && + fmt !in [`d`, `x`, `X`, `o`, `c`]) || (typ.is_int_literal() + && fmt !in [`d`, `c`, `x`, `X`, `o`, + `u`, `x`, `X`, `o`]) || (typ.is_float() && fmt !in [`E`, `F`, + `G`, `e`, `f`, `g`]) || (typ.is_pointer() && + fmt !in [`p`, `x`, `X`]) || (typ.is_string() && fmt != `s`) + || (typ.idx() in [table.i64_type_idx, + table.f64_type_idx + ] && fmt == `c`) { + c.error('illegal format specifier `${fmt:c}` for type `${c.table.get_type_name(ftyp)}`', + node.fmt_poss[i]) + } + node.need_fmts[i] = fmt != c.get_default_fmt(ftyp, typ) + } + + return table.string_type +} + +fn get_some_val(a_test, b_test, c_test, d_test, e_test, f_test f64) f64 { + return a_test*b_test*c_test* + d_test+e_test*f_test*a_test*d_test+a_test* + b_test*c_test +} + +fn main() { + a, b, r, d := 5.3, 7.5, 4.4, 6.6 + if a+b+ + r*d+a+b+r* + d > a+b+r*d+ + a*b+r { + println('ok') + } + v_str := 'v' + s := []string{} + s << ' `$v_str`' + println(s) + println('this is quite a long string' + ' that is followd by an even longer part that should go to another line') + if (a == b && b > r) || (d > r) || (a < b) || (b< d && a+b > r) || (a+b+d >= 0 && r < 0) || (a > b && d-r < b) { + println('ok') + } +} + +fn gen_str_for_multi_return(mut g gen.Gen, + info table.MultiReturn, styp, str_fn_name string) { +for i, _ in info.types { + println('\tstrings__Builder_write(&sb, _STR("\'%.*s\\000\'", 2, a.arg$i));') + } +} + +struct Parser { + peek_tok token.Token + peek_tok2 token.Token + peek_tok3 token.Token +} + +fn (mut p Parser) name_expr() { + if p.peek_tok.kind == + .lpar || (p.peek_tok.kind == .lt && p.peek_tok2.kind == .name && + p.peek_tok3.kind == .gt) { + println(p.peek_tok.lit) + } +} + +fn set_nr_muls(t table.Type, nr_muls int) table.Type { + return int(t) & + 0xff00ffff | (nr_muls << 16) +} + +// Test what exprs are treated as multiline. The ternary if only functions as a wrapper. +// When one expr in a branch doesn't fit a single line, the whole if will be unwrapped. +fn multiline_exprs() { + // StructInit with at least one field + _ := if true { Foo{} } else { Foo{ val: 123} } + // ConcatExpr with a multiline child expr + _, _ := if true { 1, Foo{val: 123} } else { 2, Foo{} } +} diff --git a/v_windows/v/vlib/v/fmt/tests/file_with_just_imports_keep.vv b/v_windows/v/vlib/v/fmt/tests/file_with_just_imports_keep.vv new file mode 100644 index 0000000..122510a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/file_with_just_imports_keep.vv @@ -0,0 +1,3 @@ +module proto +import emily33901.vproto + diff --git a/v_windows/v/vlib/v/fmt/tests/fixed_size_array_type_keep.vv b/v_windows/v/vlib/v/fmt/tests/fixed_size_array_type_keep.vv new file mode 100644 index 0000000..fbe0001 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/fixed_size_array_type_keep.vv @@ -0,0 +1,28 @@ +const size = 5 + +struct Foo { + bar [size]int + baz [5]int +} + +fn foo() [1]f32 { + return [f32(0.0)]! +} + +fn main() { + _ := [5]string{init: 'abc'} +} + +// NB: secret_key_size is missing here on purpose +// vfmt should leave it as is, assuming it is comming +// from another .v file + +struct VerifyKey { + public_key [public_key_size]byte +} + +struct SigningKey { + secret_key [secret_key_size]byte +pub: + verify_key VerifyKey +} diff --git a/v_windows/v/vlib/v/fmt/tests/float_literals_expected.vv b/v_windows/v/vlib/v/fmt/tests/float_literals_expected.vv new file mode 100644 index 0000000..ec535d0 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/float_literals_expected.vv @@ -0,0 +1,4 @@ +fn main() { + a := 1.0 + println(a) +} diff --git a/v_windows/v/vlib/v/fmt/tests/float_literals_input.vv b/v_windows/v/vlib/v/fmt/tests/float_literals_input.vv new file mode 100644 index 0000000..e1f2d51 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/float_literals_input.vv @@ -0,0 +1,4 @@ +fn main() { + a := 1. + println(a) +} diff --git a/v_windows/v/vlib/v/fmt/tests/fn_headers_with_no_bodies_keep.vv b/v_windows/v/vlib/v/fmt/tests/fn_headers_with_no_bodies_keep.vv new file mode 100644 index 0000000..98d0476 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/fn_headers_with_no_bodies_keep.vv @@ -0,0 +1,5 @@ +fn proc_pidpath(int, voidptr, int) int + +fn C.realpath(&char, &char) &char + +fn C.chmod(&byte, int) int diff --git a/v_windows/v/vlib/v/fmt/tests/fn_multi_return_keep.vv b/v_windows/v/vlib/v/fmt/tests/fn_multi_return_keep.vv new file mode 100644 index 0000000..c7f343a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/fn_multi_return_keep.vv @@ -0,0 +1,13 @@ +import v.ast + +fn return_multiple_values() (int, int) { + return 0, 1 +} + +fn return_multiple_values_opt() ?(int, int) { + return none +} + +fn return_multiple_values_with_type_from_other_module() (ast.File, int) { + return ast.File{}, 0 +} diff --git a/v_windows/v/vlib/v/fmt/tests/fn_parameter_the_same_as_a_module_const_keep.vv b/v_windows/v/vlib/v/fmt/tests/fn_parameter_the_same_as_a_module_const_keep.vv new file mode 100644 index 0000000..fb725a9 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/fn_parameter_the_same_as_a_module_const_keep.vv @@ -0,0 +1,12 @@ +module xyz + +const params = []string{} + +pub fn abc(params []string) { + if params.len > 5 { + println('more') + } + for x in params { + println(x) + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/fn_return_generic_struct_keep.vv b/v_windows/v/vlib/v/fmt/tests/fn_return_generic_struct_keep.vv new file mode 100644 index 0000000..abbc861 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/fn_return_generic_struct_keep.vv @@ -0,0 +1,39 @@ +pub struct Optional<T> { +mut: + value T + some bool +} + +pub struct Foo { + foo int +} + +pub fn (f Foo) new_some<T>(value T) Optional<T> { + return Optional{ + value: value + some: true + } +} + +pub fn (f Foo) some<T>(opt Optional<T>) bool { + return opt.some +} + +pub fn (f Foo) get<T>(opt Optional<T>) T { + return opt.value +} + +pub fn (f Foo) set<T>(mut opt Optional<T>, value T) { + opt.value = value + opt.some = true +} + +fn main() { + foo := Foo{} + mut o := foo.new_some<int>(23) + println(foo.some<int>(o)) + assert foo.some<int>(o) == true + foo.set<int>(mut o, 42) + println(foo.get<int>(o)) + assert foo.get<int>(o) == 42 +} diff --git a/v_windows/v/vlib/v/fmt/tests/fn_trailing_arg_syntax_expected.vv b/v_windows/v/vlib/v/fmt/tests/fn_trailing_arg_syntax_expected.vv new file mode 100644 index 0000000..f082c13 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/fn_trailing_arg_syntax_expected.vv @@ -0,0 +1,50 @@ +struct Bar { + x string + y int + b Baz +} + +struct Baz { + x int + y int +} + +fn main() { + bar_func(x: 'this line is short enough', y: 13) + bar_func( + x: 'a very long content should cause vfmt to use multiple lines instead of one.' + y: 123456789 + ) + bar_func( + x: 'some string' + b: Baz{ + x: 0 + y: 0 + } + ) + bar2_func() + bar2_func(Bar{ x: 's' }, + x: 's' + ) + baz_func('foo', 'bar', + x: 0 + y: 0 + ) + ui.row( + // stretch: true + margin: Margin{ + top: 10 + left: 10 + right: 10 + bottom: 10 + } + ) +} + +fn bar_func(bar Bar) { +} + +fn bar2_func(bar1 Bar, bar2 Bar) { +} + +fn baz_func(a string, b string, baz Baz) {} diff --git a/v_windows/v/vlib/v/fmt/tests/fn_trailing_arg_syntax_input.vv b/v_windows/v/vlib/v/fmt/tests/fn_trailing_arg_syntax_input.vv new file mode 100644 index 0000000..a42259f --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/fn_trailing_arg_syntax_input.vv @@ -0,0 +1,36 @@ +struct Bar { + x string + y int + b Baz +} + +struct Baz { + x int + y int +} + +fn main() { + bar_func(x: 'this line is short enough', y: 13) + bar_func(x: 'a very long content should cause vfmt to use multiple lines instead of one.', y: 123456789) + bar_func(x: 'some string', b: Baz{ + x: 0 + y: 0 + }) + bar2_func() + bar2_func(Bar{x: 's'}, x: 's') + baz_func('foo', 'bar', x: 0 + y: 0 + ) + ui.row( + //stretch: true + margin: Margin{top:10,left:10,right:10,bottom:10} + ) +} + +fn bar_func(bar Bar) { +} + +fn bar2_func(bar1 Bar, bar2 Bar) { +} + +fn baz_func(a string, b string, baz Baz) {} diff --git a/v_windows/v/vlib/v/fmt/tests/fn_trailing_arg_syntax_keep.vv b/v_windows/v/vlib/v/fmt/tests/fn_trailing_arg_syntax_keep.vv new file mode 100644 index 0000000..dc76db4 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/fn_trailing_arg_syntax_keep.vv @@ -0,0 +1,56 @@ +type Foo = Bar | Baz + +struct Bar { + x string + y int + z int + a int +} + +struct Baz { + x string +} + +fn bar_func(bar Bar) {} + +fn foo_func(f Foo) {} + +fn main() { + bar_func(x: 'bar', y: 13, z: 42) + bar_func( + x: 'bar' + y: 13 + z: 42 + ) + foo_func(Baz{ + x: 'Baz as Foo sumtype' + }) + func_from_other_file(val: 'something') + bar_func( + // pre comment + x: 'struct has a pre comment' + ) + bar_func( + x: 'first field' + // comment between fields + y: 100 + ) + bar_func( + x: 'Look! A comment to my right.' // comment after field + ) + func_from_other_file( + xyz: AnotherStruct{ + f: 'here' + } + ) + ui.button( + width: 70 + onclick: fn (a voidptr, b voidptr) { + webview.new_window(url: 'https://vlang.io', title: 'The V programming language') + } + ) +} + +fn trailing_struct_with_update_expr() { + c.error('duplicate const `$field.name`', Position{ ...field.pos, len: name_len }) +} diff --git a/v_windows/v/vlib/v/fmt/tests/fn_with_anon_params_keep.vv b/v_windows/v/vlib/v/fmt/tests/fn_with_anon_params_keep.vv new file mode 100644 index 0000000..ab6a13a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/fn_with_anon_params_keep.vv @@ -0,0 +1 @@ +fn C.PQgetvalue(voidptr, int, int) &byte diff --git a/v_windows/v/vlib/v/fmt/tests/fntype_alias_array_keep.vv b/v_windows/v/vlib/v/fmt/tests/fntype_alias_array_keep.vv new file mode 100644 index 0000000..080fe9a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/fntype_alias_array_keep.vv @@ -0,0 +1,4 @@ +type MyFn = fn (int) int + +mut arr := []MyFn{} +arr << MyFn(test) diff --git a/v_windows/v/vlib/v/fmt/tests/fntype_alias_keep.vv b/v_windows/v/vlib/v/fmt/tests/fntype_alias_keep.vv new file mode 100644 index 0000000..8e530a7 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/fntype_alias_keep.vv @@ -0,0 +1,65 @@ +module ui + +import gg +import gx +import eventbus + +pub type DrawFn = fn (ctx &gg.Context, state voidptr) + +pub type ClickFn = fn (e MouseEvent, func voidptr) + +pub type KeyFn = fn (e KeyEvent, func voidptr) + +pub type ScrollFn = fn (e ScrollEvent, func voidptr) + +pub type MouseMoveFn = fn (e MouseMoveEvent, func voidptr) + +[heap] +pub struct Window { +pub mut: + ui &UI = voidptr(0) + children []Widget + child_window &Window = voidptr(0) + parent_window &Window = voidptr(0) + has_textbox bool // for initial focus + tab_index int + just_tabbed bool + state voidptr + draw_fn DrawFn + title string + mx f64 + my f64 + width int + height int + bg_color gx.Color + click_fn ClickFn + mouse_down_fn ClickFn + mouse_up_fn ClickFn + scroll_fn ScrollFn + key_down_fn KeyFn + char_fn KeyFn + mouse_move_fn MouseMoveFn + eventbus &eventbus.EventBus = eventbus.new() +} + +pub struct WindowConfig { +pub: + width int + height int + resizable bool + title string + always_on_top bool + state voidptr + draw_fn DrawFn + bg_color gx.Color = default_window_color + on_click ClickFn + on_mouse_down ClickFn + on_mouse_up ClickFn + on_key_down KeyFn + on_scroll ScrollFn + on_mouse_move MouseMoveFn + children []Widget + font_path string + // pub mut: + // parent_window &Window +} diff --git a/v_windows/v/vlib/v/fmt/tests/fntype_mut_args_with_optional_keep.vv b/v_windows/v/vlib/v/fmt/tests/fntype_mut_args_with_optional_keep.vv new file mode 100644 index 0000000..efb2685 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/fntype_mut_args_with_optional_keep.vv @@ -0,0 +1 @@ +pub type SocketMessageFn = fn (mut c Client, msg &Message) ? diff --git a/v_windows/v/vlib/v/fmt/tests/fntype_return_optional_keep.vv b/v_windows/v/vlib/v/fmt/tests/fntype_return_optional_keep.vv new file mode 100644 index 0000000..35c829e --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/fntype_return_optional_keep.vv @@ -0,0 +1,3 @@ +type Foo = fn (a int) ? + +type Foo2 = fn (num int) ?int diff --git a/v_windows/v/vlib/v/fmt/tests/functions_expected.vv b/v_windows/v/vlib/v/fmt/tests/functions_expected.vv new file mode 100644 index 0000000..1d88e63 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/functions_expected.vv @@ -0,0 +1,68 @@ +fn C.func(arg int) int + +fn fn_variadic(arg int, args ...string) { + println('Do nothing') +} + +fn fn_with_assign_stmts() { + _, _ := fn_with_multi_return() +} + +fn fn_with_multi_return() (int, string) { + return 0, 'test' +} + +fn voidfn() { + println('this is a function that does not return anything') +} + +fn fn_with_1_arg(arg int) int { + return 0 +} + +fn fn_with_2a_args(arg1 int, arg2 int) int { + return 0 +} + +fn fn_with_2_args_to_be_shorten(arg1 int, arg2 int) int { + return 0 +} + +fn fn_with_2b_args(arg1 string, arg2 int) int { + return 0 +} + +fn fn_with_3_args(arg1 string, arg2 int, arg3 User) int { + return 0 +} + +fn (this User) fn_with_receiver() { + println('') +} + +fn fn_with_optional() ?int { + if true { + return error('true') + } + return 30 +} + +fn (f Foo) fn_with_optional() ?int { + if true { + return error('true') + } + return 40 +} + +fn mut_array(mut a []int) { + println(1) +} + +fn fn_with_ref_return() &Foo { + return &Foo{} +} + +[inline] +fn fn_with_flag() { + println('flag') +} diff --git a/v_windows/v/vlib/v/fmt/tests/functions_input.vv b/v_windows/v/vlib/v/fmt/tests/functions_input.vv new file mode 100644 index 0000000..cbd9865 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/functions_input.vv @@ -0,0 +1,63 @@ +fn C.func(arg int) int + +fn fn_variadic(arg int, args... string) { + println('Do nothing') +} + +fn fn_with_assign_stmts() { + _,_ := fn_with_multi_return() +} + +fn fn_with_multi_return() (int,string) { + return 0,'test' +} + +fn voidfn(){ + println('this is a function that does not return anything') + } + +fn fn_with_1_arg(arg int) int { +return 0 +} + +fn fn_with_2a_args(arg1 int, arg2 int) int { +return 0 +} + +fn fn_with_2_args_to_be_shorten(arg1 int, arg2 int) int { +return 0 +} + +fn fn_with_2b_args(arg1 string, arg2 int) int { +return 0 +} + +fn fn_with_3_args(arg1 string, arg2 int, arg3 User) int { +return 0 +} + +fn (this User) fn_with_receiver() { +println('') +} + +fn fn_with_optional() ?int { + if true { return error('true') } + return 30 +} + +fn (f Foo) fn_with_optional() ?int { + if true { return error('true') } + return 40 +} + +fn mut_array(mut a []int) { + println(1) +} + +fn fn_with_ref_return() &Foo { + return &Foo{} +} + +[inline] fn fn_with_flag() { +println('flag') +} diff --git a/v_windows/v/vlib/v/fmt/tests/generic_recursive_structs_keep.vv b/v_windows/v/vlib/v/fmt/tests/generic_recursive_structs_keep.vv new file mode 100644 index 0000000..a8704c5 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/generic_recursive_structs_keep.vv @@ -0,0 +1,28 @@ +pub struct Node<T> { + value T + points_to []&Node<T> +} + +fn main() { + mid := &Node<string>{ + value: 'Middle' + } + finish := &Node<string>{ + value: 'Finish' + } + + graph := &Node<string>{ + value: 'Start' + points_to: [ + &Node<string>{ + value: 'TopLeft' + points_to: [ + finish, + mid, + ] + }, + ] + } + + println(graph.points_to[0].value) // 'TopLeft' +} diff --git a/v_windows/v/vlib/v/fmt/tests/generic_structs_keep.vv b/v_windows/v/vlib/v/fmt/tests/generic_structs_keep.vv new file mode 100644 index 0000000..c021c7c --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/generic_structs_keep.vv @@ -0,0 +1,45 @@ +struct Foo<T> { +pub: + data T +} + +fn (f Foo<int>) value() string { + return f.data.str() +} + +type DB = string + +struct Repo<T, U> { + db DB +pub mut: + model T + permission U +} + +struct User { +mut: + name string +} + +struct Permission { +pub mut: + name string +} + +fn main() { + foo_int := Foo<int>{2} + assert foo_int.value() == '2' + println(foo_int) + // + x := Repo<int, f64>{'abc', 3, 1.5} + println(x.db) + println(x.model) + println(x.permission) + // + mut a := Repo<User,Permission>{ + model: User{ + name: 'joe' + } + } + println(a.model.name) +} diff --git a/v_windows/v/vlib/v/fmt/tests/generics_cascade_types_keep.vv b/v_windows/v/vlib/v/fmt/tests/generics_cascade_types_keep.vv new file mode 100644 index 0000000..82fd8dd --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/generics_cascade_types_keep.vv @@ -0,0 +1,27 @@ +struct Foo<T> { +pub: + data T +} + +struct Foo1 {} + +struct Foo2 {} + +fn multi_generic_args<T, V>(t T, v V) bool { + return true +} + +fn main() { + v1, v2 := -1, 1 + + // not generic + a1, a2 := v1 < v2, v2 > v1 + assert a1 && a2 + b1, b2 := v1 < simplemodule.zero, v2 > v1 + assert b1 && b2 + + // generic + assert multi_generic_args<int, string>(0, 's') + assert multi_generic_args<Foo1, Foo2>(Foo1{}, Foo2{}) + assert multi_generic_args<Foo<int>, Foo<int> >(Foo<int>{}, Foo<int>{}) +} diff --git a/v_windows/v/vlib/v/fmt/tests/generics_keep.vv b/v_windows/v/vlib/v/fmt/tests/generics_keep.vv new file mode 100644 index 0000000..4fe55e7 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/generics_keep.vv @@ -0,0 +1,32 @@ +import mymod { ImpNode } + +fn foobar_mymod<U>(inode ImpNode<U>) ImpNode<U> { + return ImpNode{} +} + +fn simple<T>() T { + return T{} +} + +struct Foo {} + +fn (_ Foo) simple<T>() T { + return T{} +} + +struct NonGenericStruct {} + +fn use_as_generic(ngs NonGenericStruct<V>) NonGenericStruct<V> { + return NonGenericStruct{} +} + +struct GenericStruct<A, B> {} + +fn proper_generics(gs GenericStruct<A, B>) GenericStruct<A, B> { + return gs +} + +fn main() { + simple<int>() + Foo{}.simple<int>() +} diff --git a/v_windows/v/vlib/v/fmt/tests/global_keep.vv b/v_windows/v/vlib/v/fmt/tests/global_keep.vv new file mode 100644 index 0000000..e809f3d --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/global_keep.vv @@ -0,0 +1,10 @@ +__global x = bool(true) +__global y f32 +__global () +__global ( + spam string + foo = int(5) +) +__global ( + a int +) diff --git a/v_windows/v/vlib/v/fmt/tests/go_stmt_expected.vv b/v_windows/v/vlib/v/fmt/tests/go_stmt_expected.vv new file mode 100644 index 0000000..5e26722 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/go_stmt_expected.vv @@ -0,0 +1,14 @@ +fn my_thread() { + println('yo') +} + +fn my_thread_with_params(s string) { + println(s) +} + +fn my_fn_calling_threads() { + go my_thread() + go my_thread_with_params('yay') + + go my_thread_with_params('nono') +} diff --git a/v_windows/v/vlib/v/fmt/tests/go_stmt_input.vv b/v_windows/v/vlib/v/fmt/tests/go_stmt_input.vv new file mode 100644 index 0000000..3ccaf8f --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/go_stmt_input.vv @@ -0,0 +1,16 @@ +fn my_thread() { + println('yo') +} + +fn my_thread_with_params(s string) +{ + println(s) +} + +fn my_fn_calling_threads () { +go my_thread() + go my_thread_with_params('yay') + + go + my_thread_with_params('nono') +} diff --git a/v_windows/v/vlib/v/fmt/tests/go_stmt_keep.vv b/v_windows/v/vlib/v/fmt/tests/go_stmt_keep.vv new file mode 100644 index 0000000..e404156 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/go_stmt_keep.vv @@ -0,0 +1,9 @@ +import sync + +fn go_with_anon_fn() { + wg.add(1) + go fn (mut wg sync.WaitGroup) { + wg.done() + }(mut wg) + wg.wait() +} diff --git a/v_windows/v/vlib/v/fmt/tests/goto_expected.vv b/v_windows/v/vlib/v/fmt/tests/goto_expected.vv new file mode 100644 index 0000000..144a3ad --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/goto_expected.vv @@ -0,0 +1,4 @@ +fn test_goto() { + goto label + label: +} diff --git a/v_windows/v/vlib/v/fmt/tests/goto_input.vv b/v_windows/v/vlib/v/fmt/tests/goto_input.vv new file mode 100644 index 0000000..c0740dd --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/goto_input.vv @@ -0,0 +1,4 @@ +fn test_goto() { + goto label +label: +} diff --git a/v_windows/v/vlib/v/fmt/tests/hashstmt_expected.vv b/v_windows/v/vlib/v/fmt/tests/hashstmt_expected.vv new file mode 100644 index 0000000..057d14a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/hashstmt_expected.vv @@ -0,0 +1,3 @@ +#include "header.h" + +struct Foo {} diff --git a/v_windows/v/vlib/v/fmt/tests/hashstmt_input.vv b/v_windows/v/vlib/v/fmt/tests/hashstmt_input.vv new file mode 100644 index 0000000..406f30a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/hashstmt_input.vv @@ -0,0 +1,2 @@ +#include "header.h" +struct Foo {} diff --git a/v_windows/v/vlib/v/fmt/tests/hashstmt_keep.vv b/v_windows/v/vlib/v/fmt/tests/hashstmt_keep.vv new file mode 100644 index 0000000..dd994f5 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/hashstmt_keep.vv @@ -0,0 +1,12 @@ +// comment above HashStmt +#flag -I @VROOT/c +#flag @VROOT/c/implementation.o + +#include "@VROOT/c/implementation.h" + +// comment between with newlines around + +#include "header.h" +// comment between without newlines +#include "sqlite3.h" +// comment directly after the HsahStmt diff --git a/v_windows/v/vlib/v/fmt/tests/if_brace_on_newline_expected.vv b/v_windows/v/vlib/v/fmt/tests/if_brace_on_newline_expected.vv new file mode 100644 index 0000000..6613108 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/if_brace_on_newline_expected.vv @@ -0,0 +1,12 @@ +fn get_typ() Type { + { + { + // The opening brace should be put on a new line here for readability + if typ := c.resolve_generic_type(method.return_type, method.generic_names, + call_expr.generic_types) + { + return typ + } + } + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/if_brace_on_newline_input.vv b/v_windows/v/vlib/v/fmt/tests/if_brace_on_newline_input.vv new file mode 100644 index 0000000..52b9b2a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/if_brace_on_newline_input.vv @@ -0,0 +1,10 @@ +fn get_typ() Type { + { + { + // The opening brace should be put on a new line here for readability + if typ := c.resolve_generic_type(method.return_type, method.generic_names, call_expr.generic_types) { + return typ + } + } + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/if_expected.vv b/v_windows/v/vlib/v/fmt/tests/if_expected.vv new file mode 100644 index 0000000..e691055 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/if_expected.vv @@ -0,0 +1,9 @@ +fn non_ternary_return() string { + return if some_cond { + 'foo' + } else if false { + 'bar' + } else { + 'baz' + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/if_input.vv b/v_windows/v/vlib/v/fmt/tests/if_input.vv new file mode 100644 index 0000000..5853876 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/if_input.vv @@ -0,0 +1,3 @@ +fn non_ternary_return() string { + return if some_cond { 'foo' } else if false { 'bar' } else { 'baz' } +} diff --git a/v_windows/v/vlib/v/fmt/tests/if_keep.vv b/v_windows/v/vlib/v/fmt/tests/if_keep.vv new file mode 100644 index 0000000..7e8166a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/if_keep.vv @@ -0,0 +1,11 @@ +fn void_type_array_cond() { + if fg == [] { + stack.ctx.reset_color() + } +} + +fn multi_dimensional_array_cond() { + if t.data == [][]string{} { + return error('Table.data should not be empty.') + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/if_not_in_is_expected.vv b/v_windows/v/vlib/v/fmt/tests/if_not_in_is_expected.vv new file mode 100644 index 0000000..fefa1e1 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/if_not_in_is_expected.vv @@ -0,0 +1,17 @@ +struct Foo1 {} + +struct Foo2 {} + +type Foo = Foo1 | Foo2 + +fn main() { + a := [1, 2, 3] + if 4 !in a { + println('4 not in a') + } + + foo := Foo(Foo1{}) + if foo !is Foo2 { + println('foo is not Foo2') + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/if_not_in_is_input.vv b/v_windows/v/vlib/v/fmt/tests/if_not_in_is_input.vv new file mode 100644 index 0000000..5527b6b --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/if_not_in_is_input.vv @@ -0,0 +1,17 @@ +struct Foo1{} + +struct Foo2{} + +type Foo = Foo1 | Foo2 + +fn main() { + a := [1, 2, 3] + if !(4 in a) { + println('4 not in a') + } + + foo := Foo(Foo1{}) + if !(foo is Foo2) { + println('foo is not Foo2') + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/if_ternary_expected.vv b/v_windows/v/vlib/v/fmt/tests/if_ternary_expected.vv new file mode 100644 index 0000000..e100a4a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/if_ternary_expected.vv @@ -0,0 +1,48 @@ +fn main() { + // This line is too long + sprogress := if b.no_cstep { + 'TMP1/${b.nexpected_steps:1d}' + } else { + '${b.cstep:1d}/${b.nexpected_steps:1d}' + } + // Normal struct inits + _ := if true { + Foo{} + } else { + Foo{ + x: 5 + } + } + _ := if some_cond { + Bar{ + a: 'bar' + b: 'also bar' + } + } else { + Bar{} + } +} + +fn condition_is_very_long_infix() { + val := if the_first_condition && this_is_required_too + && (another_cond || foobar_to_exceed_the_max_len) { + 'true' + } else { + 'false' + } +} + +fn branches_are_long_fn_calls() { + _ := if nr_dims == 1 { + t.find_or_register_array(elem_type) + } else { + t.find_or_register_arra(t.find_or_register_array_with_dims(elem_type, nr_dims - 1)) + } + // With another arg to make fn call exceed the max_len after if unwrapping + _ := if nr_dims == 1 { + t.find_or_register_array(elem_type) + } else { + t.find_or_register_arra(t.find_or_register_array_with_dims(elem_type, nr_dims - 1, + 'some string')) + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/if_ternary_input.vv b/v_windows/v/vlib/v/fmt/tests/if_ternary_input.vv new file mode 100644 index 0000000..c76a8ff --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/if_ternary_input.vv @@ -0,0 +1,19 @@ +fn main() { + // This line is too long + sprogress := if b.no_cstep { 'TMP1/${b.nexpected_steps:1d}' } else { '${b.cstep:1d}/${b.nexpected_steps:1d}' } + // Normal struct inits + _ := if true { Foo{} } else { Foo{ + x: 5 + } } + _ := if some_cond { Bar{ a: 'bar', b: 'also bar'} } else { Bar{} } +} + +fn condition_is_very_long_infix() { + val := if the_first_condition && this_is_required_too && (another_cond || foobar_to_exceed_the_max_len) { 'true' } else { 'false' } +} + +fn branches_are_long_fn_calls() { + _ := if nr_dims == 1 { t.find_or_register_array(elem_type) } else { t.find_or_register_arra(t.find_or_register_array_with_dims(elem_type, nr_dims - 1)) } + // With another arg to make fn call exceed the max_len after if unwrapping + _ := if nr_dims == 1 { t.find_or_register_array(elem_type) } else { t.find_or_register_arra(t.find_or_register_array_with_dims(elem_type, nr_dims - 1, 'some string')) } +} diff --git a/v_windows/v/vlib/v/fmt/tests/if_ternary_keep.vv b/v_windows/v/vlib/v/fmt/tests/if_ternary_keep.vv new file mode 100644 index 0000000..8352cc5 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/if_ternary_keep.vv @@ -0,0 +1,44 @@ +struct Foo { + n int + s string +} + +fn valid_single_line() { + // Variable initialization + a, b := if true { 'a', 'b' } else { 'b', 'a' } + // Variable assignment + mut x := 'abc' + x = if x == 'def' { 'ghi' } else { 'def' } + // Array pushes + [0, 1] << if true { 2 } else { 3 } + // Empty or literal syntax struct inits + _ := if false { Foo{} } else { Foo{5, 6} } + // As argument for a function call + some_func(if cond { 'param1' } else { 'param2' }) + // struct init + foo := Foo{ + n: if true { 1 } else { 0 } + s: if false { 'false' } else { 'true' } + } +} + +fn requires_multiple_lines() { + b := if bar { + // with comments inside + 'some str' + } else { + 'other str' + } +} + +fn return_ternary(cond bool) int { + return if cond { 5 } else { 12 } +} + +fn long_return_ternary() string { + return if false { + 'spam and eggs' + } else { + 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.' + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/import_auto_added_expected.vv b/v_windows/v/vlib/v/fmt/tests/import_auto_added_expected.vv new file mode 100644 index 0000000..2b9dc52 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/import_auto_added_expected.vv @@ -0,0 +1,7 @@ +module main + +import time + +fn main() { + curr_time := time.now() +} diff --git a/v_windows/v/vlib/v/fmt/tests/import_auto_added_input.vv b/v_windows/v/vlib/v/fmt/tests/import_auto_added_input.vv new file mode 100644 index 0000000..4c6a559 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/import_auto_added_input.vv @@ -0,0 +1,5 @@ +module main + +fn main() { + curr_time := time.now() +} diff --git a/v_windows/v/vlib/v/fmt/tests/import_comments_keep.vv b/v_windows/v/vlib/v/fmt/tests/import_comments_keep.vv new file mode 100644 index 0000000..1f64b0b --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/import_comments_keep.vv @@ -0,0 +1,11 @@ +// import time This should be commented out +import os +// between +import rand +// first between +// second between +import semver +// after the last import +// also after the last import + +// this comment does not belong to the imports anymore diff --git a/v_windows/v/vlib/v/fmt/tests/import_duplicate_expected.vv b/v_windows/v/vlib/v/fmt/tests/import_duplicate_expected.vv new file mode 100644 index 0000000..5769710 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/import_duplicate_expected.vv @@ -0,0 +1,10 @@ +import math +import os + +const ( + mypi = math.pi +) + +fn main() { + println(os.path_separator) +} diff --git a/v_windows/v/vlib/v/fmt/tests/import_duplicate_input.vv b/v_windows/v/vlib/v/fmt/tests/import_duplicate_input.vv new file mode 100644 index 0000000..0c11afe --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/import_duplicate_input.vv @@ -0,0 +1,11 @@ +import math +import os +import math + +const ( + mypi = math.pi +) + +fn main() { + println(os.path_separator) +} diff --git a/v_windows/v/vlib/v/fmt/tests/import_multiple_with_alias_expected.vv b/v_windows/v/vlib/v/fmt/tests/import_multiple_with_alias_expected.vv new file mode 100644 index 0000000..e2ee322 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/import_multiple_with_alias_expected.vv @@ -0,0 +1,11 @@ +import os +import math + +fn main() { + // println(m.pi) + println(os.path_separator) + println(math.pi) + // math as m + // import math.complex as c + // num := c.Complex{} TODO +} diff --git a/v_windows/v/vlib/v/fmt/tests/import_multiple_with_alias_input.vv b/v_windows/v/vlib/v/fmt/tests/import_multiple_with_alias_input.vv new file mode 100644 index 0000000..e2ee322 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/import_multiple_with_alias_input.vv @@ -0,0 +1,11 @@ +import os +import math + +fn main() { + // println(m.pi) + println(os.path_separator) + println(math.pi) + // math as m + // import math.complex as c + // num := c.Complex{} TODO +} diff --git a/v_windows/v/vlib/v/fmt/tests/import_selective_expected.vv b/v_windows/v/vlib/v/fmt/tests/import_selective_expected.vv new file mode 100644 index 0000000..fa72af7 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/import_selective_expected.vv @@ -0,0 +1,67 @@ +import math { max, min } +import cli { Command } +import math.complex { Complex, complex } +import os { + file_ext, + user_os, +} +import mod { + Enum, + FnArg, + FnRet, + InterfaceField, + InterfaceMethodArg, + InterfaceMethodRet, + RightOfAs, + RightOfIs, + StructEmbed, + StructField, + StructMethodArg, + StructMethodRet, + StructRefField, +} + +struct Struct { + StructEmbed + v StructField + ref &StructRefField +} + +fn (s Struct) method(v StructMethodArg) StructMethodRet { + return StructMethodRet{} +} + +interface Interface { + v InterfaceField + f(InterfaceMethodArg) InterfaceMethodRet +} + +fn f(v FnArg) FnRet { + if v is RightOfIs { + } + _ = v as RightOfAs + + println(Enum.val) + + return FnRet{} +} + +struct App { + command &Command +} + +struct MyCommand { + Command +} + +fn imaginary(im f64) Complex { + return complex(0, im) +} + +fn main() { + println(max(0.1, 0.2)) + println(min(0.1, 0.2)) + println(user_os()) + println(file_ext('main.v')) + println(imaginary(1)) +} diff --git a/v_windows/v/vlib/v/fmt/tests/import_selective_input.vv b/v_windows/v/vlib/v/fmt/tests/import_selective_input.vv new file mode 100644 index 0000000..534b72e --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/import_selective_input.vv @@ -0,0 +1,71 @@ +import math { max, + min, +} + +import cli { Command } +import math.complex { complex, Complex } +import os { + input, user_os, file_ext } + +import mod { + Unused, + StructEmbed, StructField, StructRefField + StructMethodArg, + StructMethodRet + + InterfaceField, + InterfaceMethodArg, + InterfaceMethodRet, + + FnArg, + FnRet, + + RightOfIs, + RightOfAs, + + Enum +} + +struct Struct { + StructEmbed + v StructField + ref &StructRefField +} + +fn (s Struct) method(v StructMethodArg) StructMethodRet { + return StructMethodRet{} +} + +interface Interface { + v InterfaceField + f(InterfaceMethodArg) InterfaceMethodRet +} + +fn f(v FnArg) FnRet { + if v is RightOfIs {} + _ = v as RightOfAs + + println(Enum.val) + + return FnRet{} +} + +struct App { + command &Command +} + +struct MyCommand { + Command +} + +fn imaginary(im f64) Complex { + return complex(0, im) +} + +fn main() { + println(max(0.1, 0.2)) + println(min(0.1, 0.2)) + println(user_os()) + println(file_ext('main.v')) + println(imaginary(1)) +} diff --git a/v_windows/v/vlib/v/fmt/tests/import_selective_keep.vv b/v_windows/v/vlib/v/fmt/tests/import_selective_keep.vv new file mode 100644 index 0000000..592a635 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/import_selective_keep.vv @@ -0,0 +1,12 @@ +import math.complex { Complex } +import gg { MouseButton } +import time { Duration } + +fn keep_imported_enum_map_key() { + bm := map[MouseButton]string{} +} + +fn main() { + _ := Duration(10) // keep cast type + assert *(&f64(&byte(&num) + __offsetof(Complex, re))) == 1.0 +} diff --git a/v_windows/v/vlib/v/fmt/tests/import_single_keep.vv b/v_windows/v/vlib/v/fmt/tests/import_single_keep.vv new file mode 100644 index 0000000..29d4d36 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/import_single_keep.vv @@ -0,0 +1,5 @@ +import os + +fn main() { + os.system('echo hi') +} diff --git a/v_windows/v/vlib/v/fmt/tests/import_with_alias_keep.vv b/v_windows/v/vlib/v/fmt/tests/import_with_alias_keep.vv new file mode 100644 index 0000000..f883f0e --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/import_with_alias_keep.vv @@ -0,0 +1,25 @@ +import os +import time as t +import some.library as slib +import crypto.sha256 +import mymod.sha256 as mysha256 + +type my_alias = fn (t slib.MyType) + +struct Foo { + bar t.Time + bars []t.Time + barref &t.Time + barrefs []&t.Time + c_type C.some_struct + js_type JS.other_struct +} + +fn main() { + println('start') + t.sleep_ms(500) + println('end') + os.system('date') + v_hash := sha256.sum('hi'.bytes()).hex() + my_hash := mysha256.sum('hi'.bytes()).hex() +} diff --git a/v_windows/v/vlib/v/fmt/tests/infix_expr_expected.vv b/v_windows/v/vlib/v/fmt/tests/infix_expr_expected.vv new file mode 100644 index 0000000..4dfa012 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/infix_expr_expected.vv @@ -0,0 +1,33 @@ +fn grouped_cond_single_line() { + // fmt tries to keep grouped conditions together... + _ := one_condition_before && another_condition + && (inside_paren || is_kept_together || if_possible) && end_cond +} + +fn unwrap_grouped_conds() { + // ...but sometimes they have to be splitted + _ := one_condition && before_condition && (conds_inside_paren + || are_kept_together || if_possible || but_this_is_really_too_much + || for_one_line) + _ := (also_inside_parens || just_as_above || but_this_is_also_more + || than_a_single_line_could_fit) && end_cond + fields = fields.filter((it.typ in [string_type, int_type, bool_type] + || c.table.types[int(it.typ)].kind == .struct_) && !it.attrs.contains('skip')) +} + +fn main() { + clean_struct_v_type_name = + clean_struct_v_type_name.replace('_Array', '_array').replace('_T_', '<').replace('_', ', ') + + '>' + { + { + { + // Indent this much to force a break after the assign (`:=`). + // Check that the trailing space is removed + should_cast := + (g.table.type_kind(stmt.left_types.first()) in js.shallow_equatables) + && (g.cast_stack.len <= 0 || stmt.left_types.first() != g.cast_stack.last()) + } + } + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/infix_expr_input.vv b/v_windows/v/vlib/v/fmt/tests/infix_expr_input.vv new file mode 100644 index 0000000..7bfec62 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/infix_expr_input.vv @@ -0,0 +1,24 @@ +fn grouped_cond_single_line() { + // fmt tries to keep grouped conditions together... + _ := one_condition_before && another_condition && (inside_paren || is_kept_together || if_possible) && end_cond +} + +fn unwrap_grouped_conds() { + // ...but sometimes they have to be splitted + _ := one_condition && before_condition && (conds_inside_paren || are_kept_together || if_possible || but_this_is_really_too_much || for_one_line) + _ := (also_inside_parens || just_as_above || but_this_is_also_more || than_a_single_line_could_fit) && end_cond + fields = fields.filter((it.typ in [string_type, int_type, bool_type] || c.table.types[int(it.typ)].kind == .struct_) && !it.attrs.contains('skip')) +} + +fn main() { + clean_struct_v_type_name = clean_struct_v_type_name.replace('_Array', '_array').replace('_T_', '<').replace('_', ', ') + '>' + { + { + { + // Indent this much to force a break after the assign (`:=`). + // Check that the trailing space is removed + should_cast := (g.table.type_kind(stmt.left_types.first()) in js.shallow_equatables) && (g.cast_stack.len <= 0 || stmt.left_types.first() != g.cast_stack.last()) + } + } + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/infix_expr_keep.vv b/v_windows/v/vlib/v/fmt/tests/infix_expr_keep.vv new file mode 100644 index 0000000..5eecc2c --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/infix_expr_keep.vv @@ -0,0 +1,15 @@ +fn infix_in_multi_assign() { + child_width, child_height = child.adj_width + child.margin(.left) + child.margin(.right), + child.adj_height + child.margin(.top) + child.margin(.bottom) +} + +fn impostor_infix() { + /* + String concatiation is an InfixExpr. The second part is so long + that it overflows a single line. Therefore the wrapping logic is run. + The problem was that `&&` and `||` inside the string were detected as infix operators. + Thus causing a totally wrong wrapping and sometimes runtime panics. + */ + x := 'foo' + + 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || (eeeeeeeeeeeeeeeeee && ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) && bbbbbbbbbbbbbbbbbbbbbbbbbbbb' +} diff --git a/v_windows/v/vlib/v/fmt/tests/integer_literal_keep.vv b/v_windows/v/vlib/v/fmt/tests/integer_literal_keep.vv new file mode 100644 index 0000000..f26d917 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/integer_literal_keep.vv @@ -0,0 +1,19 @@ +struct IfExpr { +} + +struct MatchExpr { +} + +type Expr = IfExpr | MatchExpr + +fn sum_types(a []Expr) { +} + +fn main() { + x := 0xdead_beef + u := 9_978_654_321 + o := 0o66_4 + eprintln(' hex constant in decimal: $x') + eprintln(' u constant in decimal: $u') + eprintln('octal constant in decimal: $o') +} diff --git a/v_windows/v/vlib/v/fmt/tests/interface_declaration_comments_keep.vv b/v_windows/v/vlib/v/fmt/tests/interface_declaration_comments_keep.vv new file mode 100644 index 0000000..60c8312 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/interface_declaration_comments_keep.vv @@ -0,0 +1,31 @@ +pub interface ReaderWriter { + read(mut buf []byte) ?int // from Reader + write(buf []byte) ?int // from Writer +} + +interface Speaker { + // first + speak() string + // between + foo() string + foo2() string + // last +} + +interface Baz { + // first + speak() string + // comment + // more between + foo() string + foo2() string + // last +} + +interface Bar { + speak() string // after + foo() string + speak2() string // also after + // and between + foo2() string +} diff --git a/v_windows/v/vlib/v/fmt/tests/interface_variadic_keep.vv b/v_windows/v/vlib/v/fmt/tests/interface_variadic_keep.vv new file mode 100644 index 0000000..ed288d3 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/interface_variadic_keep.vv @@ -0,0 +1,4 @@ +interface Element { + unnamed_method(...f64) + named_method(params ...f64) +} diff --git a/v_windows/v/vlib/v/fmt/tests/interface_with_mut_fields_keep.vv b/v_windows/v/vlib/v/fmt/tests/interface_with_mut_fields_keep.vv new file mode 100644 index 0000000..868b09e --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/interface_with_mut_fields_keep.vv @@ -0,0 +1,7 @@ +interface Toto { + a int + d() +mut: + b int + f() +} diff --git a/v_windows/v/vlib/v/fmt/tests/labelled_break_continue_keep.vv b/v_windows/v/vlib/v/fmt/tests/labelled_break_continue_keep.vv new file mode 100644 index 0000000..89fb1d2 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/labelled_break_continue_keep.vv @@ -0,0 +1,38 @@ +fn test_labelled_for() { + mut i := 4 + goto L1 + L1: for { + i++ + for { + if i < 7 { + continue L1 + } else { + break L1 + } + } + } + assert i == 7 + goto L2 + L2: for ; true; i++ { + for { + if i < 17 { + continue L2 + } else { + break L2 + } + } + } + assert i == 17 + goto L3 + L3: for e in [1, 2, 3, 4] { + i = e + for { + if i < 3 { + continue L3 + } else { + break L3 + } + } + } + assert i == 3 +} diff --git a/v_windows/v/vlib/v/fmt/tests/language_prefixes_keep.vv b/v_windows/v/vlib/v/fmt/tests/language_prefixes_keep.vv new file mode 100644 index 0000000..a0a25c5 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/language_prefixes_keep.vv @@ -0,0 +1,13 @@ +fn (v JS.String) toString() JS.String + +fn (v JS.String) toMultiRet() (JS.String, int) + +fn JS.Math.abs(f64) f64 + +fn main() { + JS.Math.abs(0) +} + +fn object_ref_optional() ?&C.File { + return error('') +} diff --git a/v_windows/v/vlib/v/fmt/tests/loops_expected.vv b/v_windows/v/vlib/v/fmt/tests/loops_expected.vv new file mode 100644 index 0000000..17d3f66 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/loops_expected.vv @@ -0,0 +1,18 @@ +fn for_in_loop() { + for item in arr { + println(item) + } +} + +fn for_in_loop_with_counter() { + for i, item in arr { + println(i) + println(item) + } +} + +fn for_in_loop_with_index_expr() { + for i in 0 .. 10 { + println(i) + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/loops_input.vv b/v_windows/v/vlib/v/fmt/tests/loops_input.vv new file mode 100644 index 0000000..1b271a3 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/loops_input.vv @@ -0,0 +1,18 @@ +fn for_in_loop() { + for item in arr { + println(item) + } +} + +fn for_in_loop_with_counter() { + for i, item in arr { + println(i) + println(item) + } +} + +fn for_in_loop_with_index_expr() { + for i in 0..10 { + println(i) + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/manualfree_keep.v b/v_windows/v/vlib/v/fmt/tests/manualfree_keep.v new file mode 100644 index 0000000..98de3f1 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/manualfree_keep.v @@ -0,0 +1,18 @@ +[manualfree] +module main + +fn abc() { + x := 'abc should be autofreed' + println(x) +} + +[manualfree] +fn xyz() { + x := 'xyz should do its own memory management' + println(x) +} + +fn main() { + abc() + xyz() +} diff --git a/v_windows/v/vlib/v/fmt/tests/maps_expected.vv b/v_windows/v/vlib/v/fmt/tests/maps_expected.vv new file mode 100644 index 0000000..122114a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/maps_expected.vv @@ -0,0 +1,16 @@ +const ( + reserved_types = { + 'i8': true + 'i16': true + 'int': true + 'i64': true + 'i128': true + } +) + +numbers := { + 'one': 1 + 'two': 2 + 'sevenhundredseventyseven': 777 + 'fivethousandthreehundredtwentyseven': 5327 +} diff --git a/v_windows/v/vlib/v/fmt/tests/maps_in_fn_args__keep.vv b/v_windows/v/vlib/v/fmt/tests/maps_in_fn_args__keep.vv new file mode 100644 index 0000000..5968438 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/maps_in_fn_args__keep.vv @@ -0,0 +1,15 @@ +import v.ast + +fn abc_xxx(xobj ast.ScopeObject) { +} + +fn abc_map(xmap map[string]ast.ScopeObject) { +} + +fn (t Tree) objects(so map[string]ast.ScopeObject) &C.cJSON { + obj := create_object() + for key, val in so { + to_object(obj, key, t.scope_object(val)) + } + return obj +} diff --git a/v_windows/v/vlib/v/fmt/tests/maps_input.vv b/v_windows/v/vlib/v/fmt/tests/maps_input.vv new file mode 100644 index 0000000..6eb783e --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/maps_input.vv @@ -0,0 +1,16 @@ +const ( +reserved_types = { + 'i8': true + 'i16': true + 'int': true + 'i64': true + 'i128': true +} +) + +numbers := { + 'one': 1 + 'two': 2 + 'sevenhundredseventyseven': 777 +'fivethousandthreehundredtwentyseven': 5327 +} diff --git a/v_windows/v/vlib/v/fmt/tests/maps_keep.vv b/v_windows/v/vlib/v/fmt/tests/maps_keep.vv new file mode 100644 index 0000000..b5737f7 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/maps_keep.vv @@ -0,0 +1,17 @@ +fn workaround() { + a := map[string]string{} + println(a) +} + +fn main() { + mut ams := []map[string]string{} + ams << { + 'a': 'b' + 'c': 'd' + } + ams << { + 'e': 'f' + 'g': 'h' + } + println(ams) +} diff --git a/v_windows/v/vlib/v/fmt/tests/maps_of_fns_with_string_keys_keep.vv b/v_windows/v/vlib/v/fmt/tests/maps_of_fns_with_string_keys_keep.vv new file mode 100644 index 0000000..30829de --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/maps_of_fns_with_string_keys_keep.vv @@ -0,0 +1,13 @@ +fn sqr(n int) int { + return n * n +} + +fn main() { + fns := [sqr] + println(fns[0](10)) + fns_map := { + 'sqr': sqr + } + println(fns_map['sqr']) + println(fns_map['sqr'](2)) +} diff --git a/v_windows/v/vlib/v/fmt/tests/match_expected.vv b/v_windows/v/vlib/v/fmt/tests/match_expected.vv new file mode 100644 index 0000000..556768f --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/match_expected.vv @@ -0,0 +1,38 @@ +fn match_expr_assignment() { + a := 20 + _ := match a { + 10 { 10 } + 5 { 5 } + else { 2 } + } +} + +fn match_branch_comment() { + a := 1 + match a { + 1 { + println('1') + } + 2 { + println('2') + } + else { + // do nothing + } + } +} + +fn really_long_branch_exprs() { + match x { + NodeError, ArrayDecompose, ArrayInit, AsCast, Assoc, AtExpr, BoolLiteral, CallExpr, + MapInit, MatchExpr, None, OffsetOf, OrExpr, ParExpr, PostfixExpr, PrefixExpr, RangeExpr, + SelectExpr, SelectorExpr, SizeOf, SqlExpr, StringInterLiteral, StringLiteral, StructInit { + return expr.pos + } + InfixExpr { + Foo{ + x: 3 + } + } + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/match_input.vv b/v_windows/v/vlib/v/fmt/tests/match_input.vv new file mode 100644 index 0000000..3f7cbae --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/match_input.vv @@ -0,0 +1,36 @@ +fn match_expr_assignment() { + a := 20 + _ := match a { + 10 { 10 } + else { 2 } + 5 { 5 } + } +} + +fn match_branch_comment() { + a := 1 + match a { + 1 { println('1') } + 2 { + println('2') + } + else { + + +// do nothing + } + } +} + +fn really_long_branch_exprs() { + match x { + NodeError, ArrayDecompose, ArrayInit, AsCast, Assoc, AtExpr, BoolLiteral, CallExpr, MapInit, MatchExpr, None, OffsetOf, OrExpr, ParExpr, PostfixExpr, PrefixExpr, RangeExpr, SelectExpr, SelectorExpr, SizeOf, SqlExpr, StringInterLiteral, StringLiteral, StructInit { + return expr.pos + } + InfixExpr { + Foo{ + x: 3 + } + } + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/match_keep.vv b/v_windows/v/vlib/v/fmt/tests/match_keep.vv new file mode 100644 index 0000000..73326e4 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/match_keep.vv @@ -0,0 +1,76 @@ +fn nested_match() { + match 100 { + 0...1 { + println('0 to 1') + } + else { + match 200 { + 0...1 { println('0 to 1') } + else { println('unknown value') } + } + } + } +} + +fn branches_are_struct_inits() { + match 'a' { + 'b' { SpamStruct{} } + } + match 'a' { + 'b' { + SpamStruct{ + x: 42 + } + } + } + match 'a' { + 'b' { + SpamStruct{ + // comment inside init + } + } + } +} + +fn branches_are_call_exprs_with_or_blocks() { + match 'a' { + 'b' { foo() or { panic(err.msg) } } + } + match 'a' { + 'b' { + foo() or { + // do stuff + panic(err.msg) + } + } + } + match 'a' { + 'b' { + foo() or { + another_stmt() + panic(err.msg) + } + } + } +} + +fn keep_branch_linebreaks() { + a := 10 + match a { + // first comment + 10 { + println('10') + } + 20 { + println('20') + } + else {} + } + match a { + // first comment + 10 { println('10') } + // post_comment of the first branch + 20 { println('20') } + else {} + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/match_range_expression_branches_keep.vv b/v_windows/v/vlib/v/fmt/tests/match_range_expression_branches_keep.vv new file mode 100644 index 0000000..ae99b0f --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/match_range_expression_branches_keep.vv @@ -0,0 +1,15 @@ +pub fn str_escaped(b byte) string { + str := match b { + 0 { '`\\' + '0`' } // Bug is preventing \\0 in a literal + 7 { '`\\a`' } + 8 { '`\\b`' } + 9 { '`\\t`' } + 10 { '`\\n`' } + 11 { '`\\v`' } + 12 { '`\\f`' } + 13 { '`\\r`' } + 32...126 { b.str() } + else { '0x' + b.hex() } + } + return str +} diff --git a/v_windows/v/vlib/v/fmt/tests/match_with_commented_branches_keep.vv b/v_windows/v/vlib/v/fmt/tests/match_with_commented_branches_keep.vv new file mode 100644 index 0000000..3240eb5 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/match_with_commented_branches_keep.vv @@ -0,0 +1,57 @@ +module ast + +pub fn (stmt Stmt) position() Position { + match stmt { + AssertStmt { return stmt.pos } + AssignStmt { return stmt.pos } + /* + // Attr { + // } + // Block { + // } + // BranchStmt { + // } + */ + Comment { return stmt.pos } + ConstDecl { return stmt.pos } + /* + // DeferStmt { + // } + */ + EnumDecl { return stmt.pos } + ExprStmt { return stmt.pos } + FnDecl { return stmt.pos } + ForCStmt { return stmt.pos } + ForInStmt { return stmt.pos } + ForStmt { return stmt.pos } + /* + // GlobalDecl { + // } + // GoStmt { + // } + // GotoLabel { + // } + // GotoStmt { + // } + // HashStmt { + // } + */ + Import { return stmt.pos } + /* + // InterfaceDecl { + // } + // Module { + // } + */ + Return { return stmt.pos } + StructDecl { return stmt.pos } + /* + // TypeDecl { + // } + // UnsafeStmt { + // } + */ + // + else { return Position{} } + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/missing_import_expected.vv b/v_windows/v/vlib/v/fmt/tests/missing_import_expected.vv new file mode 100644 index 0000000..c463392 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/missing_import_expected.vv @@ -0,0 +1,5 @@ +import time + +fn main() { + println(time.now()) +} diff --git a/v_windows/v/vlib/v/fmt/tests/missing_import_input.vv b/v_windows/v/vlib/v/fmt/tests/missing_import_input.vv new file mode 100644 index 0000000..d173cb2 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/missing_import_input.vv @@ -0,0 +1,3 @@ +fn main() { + println(time.now()) +} diff --git a/v_windows/v/vlib/v/fmt/tests/module_alias_keep.vv b/v_windows/v/vlib/v/fmt/tests/module_alias_keep.vv new file mode 100644 index 0000000..f6f77c0 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/module_alias_keep.vv @@ -0,0 +1,36 @@ +import time +import semver as sv +import term.ui as tui +import v.ast + +interface Inter { + code tui.KeyCode +} + +struct TuiStruct { + code tui.KeyCode +} + +fn foo(f time.Time) time.Time { + f2 := time.Time{} + return f +} + +fn bar(b sv.Version) sv.Version { + b2 := sv.Version{} + return b +} + +fn bar_multi_return(b sv.Version) (sv.Version, int) { + b2 := sv.Version{} + return b, 0 +} + +struct SomeStruct { + a fn (ast.Stmt, voidptr) bool +} + +fn main() { + if x is ast.FnDecl { + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/module_interface_keep.vv b/v_windows/v/vlib/v/fmt/tests/module_interface_keep.vv new file mode 100644 index 0000000..f07e5bf --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/module_interface_keep.vv @@ -0,0 +1,5 @@ +module module_fmt + +pub interface MyInterface { + fun() +} diff --git a/v_windows/v/vlib/v/fmt/tests/module_struct_keep.vv b/v_windows/v/vlib/v/fmt/tests/module_struct_keep.vv new file mode 100644 index 0000000..87c6ff8 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/module_struct_keep.vv @@ -0,0 +1,11 @@ +module module_fmt + +pub struct MyStruct { +mut: + value int + foo mod.Foo +} + +pub fn (m MyStruct) foo() bool { + return true +} diff --git a/v_windows/v/vlib/v/fmt/tests/multi_generic_test_keep.vv b/v_windows/v/vlib/v/fmt/tests/multi_generic_test_keep.vv new file mode 100644 index 0000000..f8da457 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/multi_generic_test_keep.vv @@ -0,0 +1,2 @@ +fn test<A, B, D, E>() { +} diff --git a/v_windows/v/vlib/v/fmt/tests/multiline_comment_keep.vv b/v_windows/v/vlib/v/fmt/tests/multiline_comment_keep.vv new file mode 100644 index 0000000..d3d1536 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/multiline_comment_keep.vv @@ -0,0 +1,22 @@ +/* +this is a very long comment +that is on multiple lines + and has some formatting in it + that should be + preserved. +*/ +fn main() { + println('hello') + /* + this comment also + has mutliple lines + but it's difference + is that it is indented ! + */ + if true { + /* + this one is even more + indented ! + */ + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/nested_map_type_keep.vv b/v_windows/v/vlib/v/fmt/tests/nested_map_type_keep.vv new file mode 100644 index 0000000..37efebc --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/nested_map_type_keep.vv @@ -0,0 +1,5 @@ +import v.ast + +fn foo(my_map map[string]map[string]int) int { + return 0 +} diff --git a/v_windows/v/vlib/v/fmt/tests/newlines_keep.vv b/v_windows/v/vlib/v/fmt/tests/newlines_keep.vv new file mode 100644 index 0000000..7d70d56 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/newlines_keep.vv @@ -0,0 +1,32 @@ +// Module with attribute +[manualfree] +module websocket + +fn C.no_body_function() +fn C.another_one(x int) + +fn C.separated_from_my_body_and_the_above() + +fn main() {} + +// This should stay between both functions + +fn x() {} + +// doc comment above an attributed function +[inline] +fn y_with_attr() { +} + +// doc comment above an attributed struct +[typedef] +struct FooWithAttr { +} + +fn between_assembly_blocks() { + asm amd64 { + } + + asm i386 { + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/no_main_expected.vv b/v_windows/v/vlib/v/fmt/tests/no_main_expected.vv new file mode 100644 index 0000000..2a082f9 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/no_main_expected.vv @@ -0,0 +1 @@ +println('hello world') diff --git a/v_windows/v/vlib/v/fmt/tests/no_main_input.vv b/v_windows/v/vlib/v/fmt/tests/no_main_input.vv new file mode 100644 index 0000000..d774314 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/no_main_input.vv @@ -0,0 +1 @@ +println( "hello world" ) diff --git a/v_windows/v/vlib/v/fmt/tests/offset_keep.vv b/v_windows/v/vlib/v/fmt/tests/offset_keep.vv new file mode 100644 index 0000000..df391b9 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/offset_keep.vv @@ -0,0 +1,8 @@ +struct Animal { + breed string + age u64 +} + +fn main() { + println(__offsetof(Animal, breed) + __offsetof(Animal, age)) +} diff --git a/v_windows/v/vlib/v/fmt/tests/operator_overload_keep.vv b/v_windows/v/vlib/v/fmt/tests/operator_overload_keep.vv new file mode 100644 index 0000000..381224a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/operator_overload_keep.vv @@ -0,0 +1,11 @@ +struct Foo { + x int +} + +fn (a Foo) + (b Foo) Foo { + return Foo{a.x + b.x} +} + +fn (a Foo) % (b Foo) Foo { + return Foo{a.x % b.x} +} diff --git a/v_windows/v/vlib/v/fmt/tests/optional_keep.vv b/v_windows/v/vlib/v/fmt/tests/optional_keep.vv new file mode 100644 index 0000000..407db36 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/optional_keep.vv @@ -0,0 +1,5 @@ +pub fn test() ?&SomeType { +} + +struct SomeType { +} diff --git a/v_windows/v/vlib/v/fmt/tests/optional_propagate_keep.vv b/v_windows/v/vlib/v/fmt/tests/optional_propagate_keep.vv new file mode 100644 index 0000000..0cd4524 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/optional_propagate_keep.vv @@ -0,0 +1,3 @@ +fn opt_propagate() ?int { + eventual_wrong_int() ? +} diff --git a/v_windows/v/vlib/v/fmt/tests/or_keep.vv b/v_windows/v/vlib/v/fmt/tests/or_keep.vv new file mode 100644 index 0000000..a1d1f65 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/or_keep.vv @@ -0,0 +1,40 @@ +fn main() { + empty_or_block() or {} + empty_or_block() or { + } +} + +fn fn_with_or() int { + fn_with_optional() or { return 10 } + return 20 +} + +fn (f Foo) method_with_or() int { + f.fn_with_optional() or { return 10 } + return 20 +} + +fn unwrapped_single_line_if() { + namefound := publisher.name_fix_check(name_to_find, state.site.id, ispage) or { + if err.contains('Could not find') { + state.error('cannot find link: $name_to_find') + } else { + state.error('cannot find link: $name_to_find\n$err') + } + println('Another stmt') + } +} + +fn or_with_one_multi_line_stmt() { + b := or_func() or { + MyStruct{ + val: 'xyz' + } + } +} + +fn channel_pop() { + var_init := <-ch or { -1.25 } + var_assign = <-ch or { -2.5 } + arr_push << <-ch or { -3.75 } +} diff --git a/v_windows/v/vlib/v/fmt/tests/orm_keep.vv b/v_windows/v/vlib/v/fmt/tests/orm_keep.vv new file mode 100644 index 0000000..26513bd --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/orm_keep.vv @@ -0,0 +1,67 @@ +import sqlite +import mymodule { ModDbStruct } + +struct Customer { + id int + name string + nr_orders int + country string +} + +fn find_all_customers(db sqlite.DB) []Customer { + return sql db { + select from Customer + } +} + +fn main() { + db := sqlite.connect('customers.db') ? + // select count(*) from Customer + nr_customers := sql db { + select count from Customer + } + println('number of all customers: $nr_customers') + // V syntax can be used to build queries + // db.select returns an array + uk_customers := sql db { + select from Customer where country == 'uk' && nr_orders > 0 + } + println(uk_customers.len) + for customer in uk_customers { + println('$customer.id - $customer.name') + } + // by adding `limit 1` we tell V that there will be only one object + customer := sql db { + select from Customer where id == 1 limit 1 + } + best_customer := sql db { + select from Customer order by nr_orders desc limit 1 + } + second_best := sql db { + select from UCustomerser order by nr_orders desc limit 1 offset 1 + } + println('$customer.id - $customer.name') + // insert a new customer + new_customer := Customer{ + name: 'Bob' + nr_orders: 10 + } + sql db { + insert new_customer into Customer + } + // delete a row + sql db { + delete from Customer where nr_orders == 10 && name == 'Bob' + } + sql db { + update Customer set name = 'Queen Elizabeth II', age = 150, nr_orders = 42, country = 'Great Britain' + where id == 5 + } + // DB is a selective import + sql db { + delete from ModDbStruct where id == 1 + } + _ := sql db { + select from ModDbStruct + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/par_expr_expected.vv b/v_windows/v/vlib/v/fmt/tests/par_expr_expected.vv new file mode 100644 index 0000000..360c528 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/par_expr_expected.vv @@ -0,0 +1,3 @@ +fn main() { + _ := (cond1 && cond2) || single_ident +} diff --git a/v_windows/v/vlib/v/fmt/tests/par_expr_input.vv b/v_windows/v/vlib/v/fmt/tests/par_expr_input.vv new file mode 100644 index 0000000..d3c8653 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/par_expr_input.vv @@ -0,0 +1,3 @@ +fn main() { + _ := (cond1 && cond2) || (single_ident) +} diff --git a/v_windows/v/vlib/v/fmt/tests/pointer_casts_keep.vv b/v_windows/v/vlib/v/fmt/tests/pointer_casts_keep.vv new file mode 100644 index 0000000..57b5905 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/pointer_casts_keep.vv @@ -0,0 +1,50 @@ +struct Struct { + name string + x int +} + +fn main() { + unsafe { + pb := &byte(0) + ppb := &&byte(0) + pppb := &&&byte(0) + ppppb := &&&&byte(0) + dump(voidptr(pb)) + dump(voidptr(ppb)) + dump(voidptr(pppb)) + dump(voidptr(ppppb)) + pc := &char(0) + ppc := &&char(0) + pppc := &&&char(0) + ppppc := &&&&char(0) + dump(voidptr(pc)) + dump(voidptr(ppc)) + dump(voidptr(pppc)) + dump(voidptr(ppppc)) + ps := &Struct(0) + pps := &&Struct(0) + ppps := &&&Struct(0) + pppps := &&&&Struct(0) + dump(voidptr(ps)) + dump(voidptr(pps)) + dump(voidptr(ppps)) + dump(voidptr(pppps)) + } + ss := &Struct{ + name: 'abc' + x: 123 + } + dump(ss) + pss := voidptr(ss) + if &Struct(pss).name == 'abc' { + println('ok') + } + if &Struct(pss).x == 123 { + // &Struct cast and selecting .x + println('ok') + } + if &&Struct(pss) != 0 { + // &&Struct + println('ok') + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/proto_module_importing_vproto_keep.vv b/v_windows/v/vlib/v/fmt/tests/proto_module_importing_vproto_keep.vv new file mode 100644 index 0000000..2bca234 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/proto_module_importing_vproto_keep.vv @@ -0,0 +1,35 @@ +module proto + +import vproto + +struct Xyz { + x int +} + +struct Abcde { + f1 vproto.Xyz + f2 &vproto.Xyz + f3 []vproto.Xyz + f4 []&vproto.Xyz + f5 map[string]vproto.Xyz + f6 map[string]&vproto.Xyz + // + p1 Xyz + p2 &Xyz + p3 []Xyz + p4 []&Xyz + p5 map[string]Xyz + p6 map[string]&Xyz + p7 map[string]map[string]map[string]&Xyz + // + p8 map[string]map[string]map[string]map[string]&Xyz + p9 map[string]map[string]map[string]map[string]&vproto.Xyz +} + +fn abc() { + x := vproto.Xyz{2} + mut a := []vproto.Xyz{} + a << x + a << vproto.Xyz{3} + z := map[string]map[string]map[string]map[string]&vproto.Xyz{} +} diff --git a/v_windows/v/vlib/v/fmt/tests/ref_type_cast_keep.vv b/v_windows/v/vlib/v/fmt/tests/ref_type_cast_keep.vv new file mode 100644 index 0000000..aba9e54 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/ref_type_cast_keep.vv @@ -0,0 +1,9 @@ +module main + +fn cleanup(user_data voidptr) { + abc := App(user_data) + xyz := &App(user_data) + // + mut app := App(user_data) + mut ref := &App(user_data) +} diff --git a/v_windows/v/vlib/v/fmt/tests/select_keep.vv b/v_windows/v/vlib/v/fmt/tests/select_keep.vv new file mode 100644 index 0000000..4a5abf7 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/select_keep.vv @@ -0,0 +1,128 @@ +import time +import sync + +struct St { + a int +} + +fn getint() int { + return 8 +} + +fn f1(ch1 chan int, ch2 chan St, ch3 chan int, ch4 chan int, ch5 chan int, sem sync.Semaphore) { + mut a := 5 + select { + // pre comment + a = <-ch3 { + a = 0 + } + b := <-ch2 { + a = b.a + } + ch3 <- 5 { + a = 1 + } + ch2 <- St{ + a: 37 + } { + a = 2 + } + // another comment + ch4 <- (6 + 7 * 9) { + a = 8 + } + ch5 <- getint() { + a = 9 + } + 300 * time.millisecond { + a = 3 + } + // post comment + } + assert a == 3 + sem.post() +} + +fn f2(ch1 chan St, ch2 chan int, sem sync.Semaphore) { + mut r := 23 + for i in 0 .. 2 { + select { + b := <-ch1 { + r = b.a + } + ch2 <- r { + r = 17 + } + } + if i == 0 { + assert r == 17 + } else { + assert r == 13 + } + } + sem.post() +} + +fn test_select_blocks() { + ch1 := chan int{cap: 1} + ch2 := chan St{} + ch3 := chan int{} + ch4 := chan int{} + ch5 := chan int{} + sem := sync.new_semaphore() + mut r := false + t := select { + b := <-ch1 { + println(b) + } + else { + // no channel ready + r = true + } + } + assert r == true + assert t == true + go f2(ch2, ch3, sem) + n := <-ch3 + assert n == 23 + ch2 <- St{ + a: 13 + } + sem.wait() + stopwatch := time.new_stopwatch() + go f1(ch1, ch2, ch3, ch4, ch5, sem) + sem.wait() + elapsed_ms := f64(stopwatch.elapsed()) / time.millisecond + assert elapsed_ms >= 295.0 + ch1.close() + ch2.close() + mut h := 7 + mut is_open := true + if select { + b := <-ch2 { + h = 0 + } + ch1 <- h { + h = 1 + } + else { + h = 2 + } + } { + panic('channel is still open') + } else { + is_open = false + } + // no branch should have run + for select { + b := <-ch2 { + h = 0 + } + } { + println('ch2 open') + } + println('ch2 closed') + assert h == 7 + // since all channels are closed `select` should return `false` + assert is_open == false +} diff --git a/v_windows/v/vlib/v/fmt/tests/shared_expected.vv b/v_windows/v/vlib/v/fmt/tests/shared_expected.vv new file mode 100644 index 0000000..d077f88 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/shared_expected.vv @@ -0,0 +1,85 @@ +import time + +struct St { +mut: + a int +} + +fn (shared x St) f(shared z St) { + for _ in 0 .. reads_per_thread { + rlock x { // other instances may read at the same time + time.sleep(time.millisecond) + assert x.a == 7 || x.a == 5 + } + } + lock z { + z.a-- + } +} + +fn g() shared St { + shared x := St{ + a: 12 + } + return x +} + +fn h() ?shared St { + return error('no value') +} + +fn k() { + shared x := g() + shared y := h() or { + shared f := St{} + f + } + shared z := h() ? + shared p := v + v := rlock z { + z.a + } + lock y, z; rlock x, p { + z.a = x.a + y.a + } + println(v) +} + +const ( + reads_per_thread = 30 + read_threads = 10 + writes = 5 +) + +fn test_shared_lock() { + // object with separate read/write lock + shared x := &St{ + a: 5 + } + shared z := &St{ + a: read_threads + } + for _ in 0 .. read_threads { + go x.f(shared z) + } + 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(50 * time.millisecond) + x.a = if (i & 1) == 0 { 7 } else { 5 } + } // now new reads are possible again + time.sleep(20 * time.millisecond) + } + // wait until all read threads are finished + for finished := false; true; { + mut rr := 0 + rlock z { + rr = z.a + finished = z.a == 0 + } + if finished { + break + } + time.sleep(100 * time.millisecond) + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/shared_input.vv b/v_windows/v/vlib/v/fmt/tests/shared_input.vv new file mode 100644 index 0000000..dccb34a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/shared_input.vv @@ -0,0 +1,84 @@ +import sync +import time + +struct St { +mut: + a int +} + +fn (shared x St) f(shared z St) { + for _ in 0 .. reads_per_thread { + rlock x { // other instances may read at the same time + time.sleep(time.millisecond) + assert x.a == 7 || x.a == 5 + } + } + lock z { + z.a-- + } +} + +fn g() shared St { +shared x := St{a: 12 } + return x +} + +fn h() ?shared St { + return error('no value') +} + +fn k() { + shared x := g() + shared y := h() or { + shared f := St{} + f + } + shared z := h() ? + shared p := v + v := rlock z { z.a } + rlock x; lock y, z; rlock p { + z.a = x.a + y.a + } + println(v) +} + +const ( + reads_per_thread = 30 + read_threads = 10 + writes = 5 +) + +fn test_shared_lock() { + // object with separate read/write lock +shared x := &St { +a: 5 +} + shared z := +&St{ + a: read_threads + } + for _ in 0.. read_threads { + go x.f(shared z) + } + 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(50* time.millisecond) + x.a = if (i & 1) == 0 { +7} else {5} + } // now new reads are possible again +time.sleep(20*time.millisecond) + } + // wait until all read threads are finished +for finished:=false;; { + mut rr := 0 + rlock z { + rr = z.a + finished = z.a == 0 +} +if finished { + break + } + time.sleep(100*time.millisecond) + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/star__amp_int__cast_keep.vv b/v_windows/v/vlib/v/fmt/tests/star__amp_int__cast_keep.vv new file mode 100644 index 0000000..73312cd --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/star__amp_int__cast_keep.vv @@ -0,0 +1,5 @@ +fn main() { + body := [1, 2, 3] + size := *&int(body.data) + eprintln('size: $size') +} diff --git a/v_windows/v/vlib/v/fmt/tests/static_mut_keep.vv b/v_windows/v/vlib/v/fmt/tests/static_mut_keep.vv new file mode 100644 index 0000000..4d4d61b --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/static_mut_keep.vv @@ -0,0 +1,12 @@ +[unsafe] +fn foo() int { + mut static x := 42 + x++ + return x +} + +[unsafe] +fn foo() int { + static x := 42 // a immutable static is not very useful, but vfmt should support that too + return x +} diff --git a/v_windows/v/vlib/v/fmt/tests/stmt_keep.vv b/v_windows/v/vlib/v/fmt/tests/stmt_keep.vv new file mode 100644 index 0000000..1dcb23d --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/stmt_keep.vv @@ -0,0 +1,8 @@ +fn single_line_stmts() { + // Wouldn't be the or-block's stmt be single line, the block would be written as multi line + foo() or { assert false } + for { + foo() or { break } + } + foo() or { return } +} diff --git a/v_windows/v/vlib/v/fmt/tests/string_interpolation_complex_keep.vv b/v_windows/v/vlib/v/fmt/tests/string_interpolation_complex_keep.vv new file mode 100644 index 0000000..470ee54 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/string_interpolation_complex_keep.vv @@ -0,0 +1,9 @@ +struct Container { + id string +} + +container := Container{} +docker_pubkey := '1234657890' + +cmd := "docker exec $container.id sh -c 'echo \"$docker_pubkey\" >> ~/.ssh/authorized_keys'" +println(cmd) diff --git a/v_windows/v/vlib/v/fmt/tests/string_interpolation_expected.vv b/v_windows/v/vlib/v/fmt/tests/string_interpolation_expected.vv new file mode 100644 index 0000000..4b0944b --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/string_interpolation_expected.vv @@ -0,0 +1,32 @@ +struct Aa { + xy int +} + +struct Bb { + a Aa +} + +struct Cc { + a []Aa +} + +fn (c &Cc) f() int { + return c.a[0].xy +} + +fn (c &Cc) g(k int, l int) int { + return c.a[k].xy + l +} + +fn main() { + st := Bb{Aa{5}} + ar := Cc{[Aa{3}, Aa{-4}, Aa{12}]} + aa := Aa{-13} + z := -14.75 + println('$st.a.xy ${ar.a[2].xy} $aa.xy $z') + println('$st.a.xy${ar.a[2].xy}$aa.xy$z') + println('${st.a.xy}ya ${ar.a[2].xy}X2 ${aa.xy}.b ${z}3') + println('${z:-5} ${z:+5.3} ${z:+09.3f} ${z:-7.2} ${z:+09} ${z:08.3f}') + println('$ar.f() ${ar.g(1, 2)} ${ar.a}() ${z}(') + println('${z > 12.3 * z - 3} ${@VEXE} ${4 * 5}') +} diff --git a/v_windows/v/vlib/v/fmt/tests/string_interpolation_input.vv b/v_windows/v/vlib/v/fmt/tests/string_interpolation_input.vv new file mode 100644 index 0000000..891e011 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/string_interpolation_input.vv @@ -0,0 +1,32 @@ +struct Aa { + xy int +} + +struct Bb { + a Aa +} + +struct Cc { + a []Aa +} + +fn (c &Cc) f() int { + return c.a[0].xy +} + +fn (c &Cc) g(k int, l int) int { + return c.a[k].xy+l +} + +fn main() { + st := Bb{Aa{5}} + ar := Cc{[Aa{3}, Aa{-4}, Aa{12}]} + aa := Aa{-13} + z := -14.75 + println('${st.a.xy} ${ar.a[2].xy} ${aa.xy} ${z}') + println('${st.a.xy}${ar.a[2].xy}${aa.xy}${z}') + println('${st.a.xy}ya ${ar.a[2].xy}X2 ${aa.xy}.b ${z}3') + println('${z:-5} ${z:+5.3} ${z:+09.3f} ${z:-07.2} ${z:+009} ${z:008.3f}') + println('${ar.f()} ${ar.g(1, 2)} ${ar.a}() ${z}(') + println('${z > 12.3 * z - 3} ${@VEXE} ${4 * 5}') +} diff --git a/v_windows/v/vlib/v/fmt/tests/string_interpolation_keep.vv b/v_windows/v/vlib/v/fmt/tests/string_interpolation_keep.vv new file mode 100644 index 0000000..97a1ff5 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/string_interpolation_keep.vv @@ -0,0 +1,17 @@ +import os + +fn main() { + println('Hello world, args: $os.args') + i := 123 + a := 'abc' + b := 'xyz' + e := 'a: $a b: $b i: $i' + d := 'a: ${a:5s} b: ${b:-5s} i: ${i:20d}' + f := 'a byte string'.bytes() + println('a: $a $b xxx') + eprintln('e: $e') + _ = ' ${foo.method(bar).str()} ' + println('(${some_struct.@type}, $some_struct.y)') + _ := 'CastExpr ${int(d.e).str()}' + println('${f[0..4].bytestr()}') +} diff --git a/v_windows/v/vlib/v/fmt/tests/string_quotes_expected.vv b/v_windows/v/vlib/v/fmt/tests/string_quotes_expected.vv new file mode 100644 index 0000000..5f61490 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/string_quotes_expected.vv @@ -0,0 +1,10 @@ +fn main() { + println('Hello world !') + println('This is correct !') + println("It's okay") + println('This is "too"') + println("I'm correctly formatted") + println('"Everything on the internet is true" - Albert Einstein, 1965') + println('I\'m out of idea "_"') + println('Definitely out ":\'("') +} diff --git a/v_windows/v/vlib/v/fmt/tests/string_quotes_input.vv b/v_windows/v/vlib/v/fmt/tests/string_quotes_input.vv new file mode 100644 index 0000000..c8cedb5 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/string_quotes_input.vv @@ -0,0 +1,10 @@ +fn main() { + println("Hello world !") + println('This is correct !') + println("It's okay") + println('This is "too"') + println('I\'m correctly formatted') + println("\"Everything on the internet is true\" - Albert Einstein, 1965") + println('I\'m out of idea "_"') + println("Definitely out \":'(\"") +} diff --git a/v_windows/v/vlib/v/fmt/tests/string_raw_and_cstr_keep.vv b/v_windows/v/vlib/v/fmt/tests/string_raw_and_cstr_keep.vv new file mode 100644 index 0000000..89e3010 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/string_raw_and_cstr_keep.vv @@ -0,0 +1,6 @@ +fn main() { + raw := r'\x00' + cstr := c'foo' + println(raw) + println(cstr) +} diff --git a/v_windows/v/vlib/v/fmt/tests/struct_decl_keep.vv b/v_windows/v/vlib/v/fmt/tests/struct_decl_keep.vv new file mode 100644 index 0000000..cb2de8c --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/struct_decl_keep.vv @@ -0,0 +1,5 @@ +struct Foo { + a int +__global: + g string +} diff --git a/v_windows/v/vlib/v/fmt/tests/struct_default_field_expressions_keep.vv b/v_windows/v/vlib/v/fmt/tests/struct_default_field_expressions_keep.vv new file mode 100644 index 0000000..0b8239e --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/struct_default_field_expressions_keep.vv @@ -0,0 +1,14 @@ +struct Foo { + i int = 1 // A comment +} + +struct Bar { + f Foo = &Foo(0) + z int [skip] = -1 +} + +struct Baz { + x int = 1 // It's one + y string = 'one' // It's one written out + z bool = true // Also one +} diff --git a/v_windows/v/vlib/v/fmt/tests/struct_embed_keep.vv b/v_windows/v/vlib/v/fmt/tests/struct_embed_keep.vv new file mode 100644 index 0000000..9213bf7 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/struct_embed_keep.vv @@ -0,0 +1,18 @@ +struct Foo { + x int +} + +struct Test {} + +struct Bar { + Foo // comment for Foo + Test // comment for Test + // another comment for Test + y int + z string +} + +struct Baz { + Foo // Another comment for Foo + Test +} diff --git a/v_windows/v/vlib/v/fmt/tests/struct_init_keep.vv b/v_windows/v/vlib/v/fmt/tests/struct_init_keep.vv new file mode 100644 index 0000000..d4ec06a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/struct_init_keep.vv @@ -0,0 +1,14 @@ +struct User { + age int + name string +} + +fn main() { + u := User{ + age: 54 + } + _ = User{ + ...u + name: 'hi' + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/struct_init_with_comments_keep.vv b/v_windows/v/vlib/v/fmt/tests/struct_init_with_comments_keep.vv new file mode 100644 index 0000000..60576b2 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/struct_init_with_comments_keep.vv @@ -0,0 +1,20 @@ +module abcde + +pub struct Builder { +pub mut: + // inline before field + buf []byte + str_calls int + len int + initial_size int = 1 +} + +pub fn new_builder(initial_size int) Builder { + return Builder{ + // buf: make(0, initial_size) + buf: []byte{cap: initial_size} + str_calls: 0 // after str_calls + len: 0 // after len + initial_size: initial_size // final + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/struct_init_with_custom_len_keep.vv b/v_windows/v/vlib/v/fmt/tests/struct_init_with_custom_len_keep.vv new file mode 100644 index 0000000..57d916d --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/struct_init_with_custom_len_keep.vv @@ -0,0 +1,17 @@ +struct Foo { + i int + a []int +} + +struct Bar { + f &Foo = &Foo(0) + d Foo = Foo{0} +} + +fn main() { + size := 5 + f := &Foo{ + a: []int{len: int(size)} + } + println('f.a: $f.a') +} diff --git a/v_windows/v/vlib/v/fmt/tests/struct_init_with_ref_cast_keep.vv b/v_windows/v/vlib/v/fmt/tests/struct_init_with_ref_cast_keep.vv new file mode 100644 index 0000000..006e4f7 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/struct_init_with_ref_cast_keep.vv @@ -0,0 +1,22 @@ +struct Foo { + f int = 123 +} + +struct Bar { + f &Foo = &Foo(0) +} + +struct Zar { + f Foo +} + +fn main() { + b := &Bar{ + f: &Foo(32) + } + c := &Zar{ + f: Foo{456} + } + assert ptr_str(b.f) == '20' + assert c.f.f == 456 +} diff --git a/v_windows/v/vlib/v/fmt/tests/struct_keep.vv b/v_windows/v/vlib/v/fmt/tests/struct_keep.vv new file mode 100644 index 0000000..68ed58b --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/struct_keep.vv @@ -0,0 +1,46 @@ +import os + +struct KeepAnyLanguagePrefixVariation { + x C.bar + y &C.bar + z []C.bar + z2 []&C.bar +} + +fn foo(a []os.File) { +} + +struct User { + age int + name string +} + +fn handle_users(users []User) { + println(users.len) +} + +fn (u &User) foo(u2 &User) { +} + +type Expr = IfExpr | IntegerLiteral + +fn exprs(e []Expr) { + println(e.len) +} + +struct KeepStructEmbed { + User +pub: + a int + b int +} + +struct KeepMultiLineDefaultExprsIndent { + buttons []PeriodButton = [PeriodButton{ + period: pr.Period.m1 + text: 'M1' + }, PeriodButton{ + period: pr.Period.m5 + text: 'M5' + }] +} diff --git a/v_windows/v/vlib/v/fmt/tests/struct_no_extra_attr_keep.vv b/v_windows/v/vlib/v/fmt/tests/struct_no_extra_attr_keep.vv new file mode 100644 index 0000000..8278308 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/struct_no_extra_attr_keep.vv @@ -0,0 +1,27 @@ +[typedef] +struct Foo { +} + +[typedef] +struct Bar { + x string + y int +} + +[heap] +struct Baz { + x string + y int +} + +[inline] +struct Spam { + x string + y int +} + +[deprecated] +struct Eggs { + y_y int [json: yY] + x string [deprecated] +} diff --git a/v_windows/v/vlib/v/fmt/tests/struct_update_comment_keep.vv b/v_windows/v/vlib/v/fmt/tests/struct_update_comment_keep.vv new file mode 100644 index 0000000..1a257d8 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/struct_update_comment_keep.vv @@ -0,0 +1,18 @@ +struct Foo { + name string + age int +} + +struct Foo2 {} + +fn main() { + f := Foo{ + name: 'test' + age: 18 + } + f2 := Foo{ + // before + ...f // after + name: 'f2' + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/struct_update_keep.vv b/v_windows/v/vlib/v/fmt/tests/struct_update_keep.vv new file mode 100644 index 0000000..e550ac1 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/struct_update_keep.vv @@ -0,0 +1,17 @@ +struct Foo { + name string + age int +} + +struct Foo2 {} + +fn main() { + f := Foo{ + name: 'test' + age: 18 + } + f2 := Foo{ + ...f + name: 'f2' + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/struct_with_fn_fields_keep.vv b/v_windows/v/vlib/v/fmt/tests/struct_with_fn_fields_keep.vv new file mode 100644 index 0000000..2f1bd98 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/struct_with_fn_fields_keep.vv @@ -0,0 +1,4 @@ +struct FieldsWithOptionalVoidReturnType { + f fn () ? + g fn () ? +} diff --git a/v_windows/v/vlib/v/fmt/tests/structs_expected.vv b/v_windows/v/vlib/v/fmt/tests/structs_expected.vv new file mode 100644 index 0000000..ca3fb0c --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/structs_expected.vv @@ -0,0 +1,62 @@ +struct User { + name string // name + name2 []rune // name2 + very_long_field bool + age int // age + very_long_type_field1 very_looooog_type // long + very_long_type_field2 very_loooooooong_type // long +} + +struct FamousUser { + User +pub: + aka string +} + +struct Foo { + field1 int // f1 + field2 string // f2 +pub: + public_field1 int // f1 + public_field2 f64 // f2 +mut: + mut_field string +pub mut: + pub_mut_field string +} + +struct Bar { + Foo +} + +fn new_user() User { + return User{ + name: 'Serious Sam' + age: 19 + } +} + +struct SomeStruct { +mut: + // 1 + // 2 + // 3 + somefield /* 4 */ /* 5 */ int // 6 + // 7 + // 8 + /* + 9 +10 + */ + somefield2 /* 11 */ int // 12 +pub: + somefield3 int + + somefield4 int + /* + 13 +14 + */ +} + +struct C.Foo {} diff --git a/v_windows/v/vlib/v/fmt/tests/structs_input.vv b/v_windows/v/vlib/v/fmt/tests/structs_input.vv new file mode 100644 index 0000000..c17259a --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/structs_input.vv @@ -0,0 +1,65 @@ +struct User { + name string // name + name2 []rune // name2 + very_long_field bool + age int // age + very_long_type_field1 very_looooog_type // long + very_long_type_field2 very_loooooooong_type // long +} + +struct FamousUser { +pub: + User + aka string +} + +struct Foo { + field1 int // f1 + field2 string // f2 + pub: + public_field1 int // f1 + public_field2 f64 // f2 + mut: + mut_field string + pub mut: + pub_mut_field string +} + +struct Bar { Foo } + +fn new_user() +User +{ + return User{ + name: 'Serious Sam' + age: 19 + } +} + +struct SomeStruct { +// 1 +mut: +// 2 +// 3 +somefield /*4*/ /*5*/ int /*6*/ /*7*/ /*8*/ /* +9 +10 +*/ +somefield2 /*11*/ int // 12 +pub: + + + + +somefield3 int + + + +somefield4 int +/* +13 +14 +*/ +} + +struct C.Foo diff --git a/v_windows/v/vlib/v/fmt/tests/sum_smartcast_keep.vv b/v_windows/v/vlib/v/fmt/tests/sum_smartcast_keep.vv new file mode 100644 index 0000000..eacdcbd --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/sum_smartcast_keep.vv @@ -0,0 +1,20 @@ +struct S1 { +mut: + i int +} + +struct S2 { +} + +type Sum = S1 | S2 + +fn f(sum Sum) { + if mut sum is S1 { + sum.i++ + } + if sum is S1 { + } + a := [sum] + if a[0] is S2 { + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/thread_in_a_module_keep.vv b/v_windows/v/vlib/v/fmt/tests/thread_in_a_module_keep.vv new file mode 100644 index 0000000..41e7ea6 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/thread_in_a_module_keep.vv @@ -0,0 +1,17 @@ +module my_module + +import another +import other as ooo + +pub fn do_something() { + mut simples := []MyStruct{} + // + mut threads := []thread MyStruct{} + threads.wait() + // + mut another_threads := []thread another.MyStruct{} + another_threads.wait() + // + mut other_threads := []thread ooo.MyStruct{} + other_threads.wait() +} diff --git a/v_windows/v/vlib/v/fmt/tests/to_string_2_forms_keep.vv b/v_windows/v/vlib/v/fmt/tests/to_string_2_forms_keep.vv new file mode 100644 index 0000000..adb49bd --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/to_string_2_forms_keep.vv @@ -0,0 +1,33 @@ +fn abc() string { + unsafe { + mut fullpath := vcalloc(4) + fullpath[0] = `a` + fullpath[1] = `b` + fullpath[2] = `c` + fullpath[3] = 0 + return fullpath.vstring() + } + return '' +} + +fn def() string { + unsafe { + mut fullpath := vcalloc(4) + fullpath[0] = `a` + fullpath[1] = `b` + fullpath[2] = `c` + fullpath[3] = 0 + return fullpath.vstring_with_len(3) + } + return '' +} + +fn main() { + assert 'abc' == abc() + assert 'abc' == def() + abc_str1 := ptr_str(abc().str) + abc_str2 := ptr_str(abc().str) + println('abc_str1: $abc_str1') + println('abc_str2: $abc_str2') + assert abc_str1 != abc_str2 +} diff --git a/v_windows/v/vlib/v/fmt/tests/trailing_space_expected.vv b/v_windows/v/vlib/v/fmt/tests/trailing_space_expected.vv new file mode 100644 index 0000000..690540c --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/trailing_space_expected.vv @@ -0,0 +1,10 @@ +// NB: The input file has and *should* have trailing spaces. +// When making changes, please ensure these spaces are not removed. + +fn comments_with_trailing_space() { + // two spaces on the right + /* + spaces after here + and everywhere :) + */ +} diff --git a/v_windows/v/vlib/v/fmt/tests/trailing_space_input.vv b/v_windows/v/vlib/v/fmt/tests/trailing_space_input.vv new file mode 100644 index 0000000..2b5b448 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/trailing_space_input.vv @@ -0,0 +1,10 @@ +// NB: The input file has and *should* have trailing spaces. +// When making changes, please ensure these spaces are not removed. + +fn comments_with_trailing_space() { + // two spaces on the right + /* + spaces after here + and everywhere :) + */ +}
\ No newline at end of file diff --git a/v_windows/v/vlib/v/fmt/tests/type_ptr_keep.vv b/v_windows/v/vlib/v/fmt/tests/type_ptr_keep.vv new file mode 100644 index 0000000..338b54e --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/type_ptr_keep.vv @@ -0,0 +1,16 @@ +const ( + x = &Test{} + y = []&Test{} + xx = &&Test{} + yy = []&&Test{} +) + +fn test_type_ptr() { + _ := &Test{} + _ := []&Test{} + _ := &&Test{} + _ := []&&Test{} +} + +struct Test { +} diff --git a/v_windows/v/vlib/v/fmt/tests/typeof_keep.vv b/v_windows/v/vlib/v/fmt/tests/typeof_keep.vv new file mode 100644 index 0000000..93d6cdc --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/typeof_keep.vv @@ -0,0 +1,3 @@ +fn test_typeof() { + println(typeof(x).name) +} diff --git a/v_windows/v/vlib/v/fmt/tests/types_expected.vv b/v_windows/v/vlib/v/fmt/tests/types_expected.vv new file mode 100644 index 0000000..1eed436 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/types_expected.vv @@ -0,0 +1,27 @@ +// Sumtype +type FooBar = Bar | Foo +pub type PublicBar = Bar | Foo | FooBar + +type Uint = byte | u16 | u32 | u64 // This should stay on the same line +type Float = f32 | f64 + +// Alias type +type MyInt = int + +pub type Abc = f32 + +// Fn type decl + +type EmptyFn = fn () + +type OneArgFn = fn (i int) + +type TwoDiffArgs = fn (i int, s string) bool + +type TwoSameArgs = fn (i int, j int) string // And a comment + +type VarArgs = fn (s ...string) int + +type NOVarArgs = fn (i int, s ...string) f64 + +type NoNameArgs = fn (int, string, ...string) diff --git a/v_windows/v/vlib/v/fmt/tests/types_input.vv b/v_windows/v/vlib/v/fmt/tests/types_input.vv new file mode 100644 index 0000000..6a5f947 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/types_input.vv @@ -0,0 +1,37 @@ + + + // Sumtype + type FooBar= Foo | Bar + pub type PublicBar = Foo | Bar | FooBar + +type Uint = u16 | u64 + | u32 + | byte // This should stay on the same line +type +Float = + f32 | + f64 + + // Alias type + type MyInt = int + + pub type Abc = f32 + + +// Fn type decl + + type EmptyFn = fn() +type OneArgFn = + fn (i int) +type TwoDiffArgs += fn (i int, s string) bool + + + type TwoSameArgs = fn(i int, j int) string // And a comment + +type VarArgs = fn +(s ...string) int + +type NOVarArgs = fn(i int, s ...string) f64 + +type NoNameArgs = fn( int, string , ...string) diff --git a/v_windows/v/vlib/v/fmt/tests/union_keep.vv b/v_windows/v/vlib/v/fmt/tests/union_keep.vv new file mode 100644 index 0000000..5872d03 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/union_keep.vv @@ -0,0 +1,4 @@ +union Un { + i int + b byte +} diff --git a/v_windows/v/vlib/v/fmt/tests/unsafe_keep.vv b/v_windows/v/vlib/v/fmt/tests/unsafe_keep.vv new file mode 100644 index 0000000..d1d6702 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/unsafe_keep.vv @@ -0,0 +1,17 @@ +fn main() { + unsafe { + println('hi') + println('hi2') + } + unsafe { + println('qwer') + } + unsafe { 6 } + x := unsafe { + 5 + } + y := unsafe { 7 } + unsafe {} + unsafe { + } +} diff --git a/v_windows/v/vlib/v/fmt/tests/vargs_reference_param_keep.vv b/v_windows/v/vlib/v/fmt/tests/vargs_reference_param_keep.vv new file mode 100644 index 0000000..cef84b3 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/vargs_reference_param_keep.vv @@ -0,0 +1,27 @@ +[heap] +struct Foo { + name string +} + +fn agg_stuff(stuffs ...&Foo) []&Foo { + stuffs2 := stuffs.clone() + return stuffs2 +} + +fn arr_stuff(stuffs []&Foo) []&Foo { + stuffs2 := stuffs.clone() + return stuffs2 +} + +fn main() { + foo1 := &Foo{'foo'} + foo2 := &Foo{'bar'} + + foo11 := agg_stuff(foo1, foo2) + println(foo11) + + foo22 := arr_stuff([foo1, foo2]) + println(foo22) + + assert '$foo11' == '$foo22' +} diff --git a/v_windows/v/vlib/v/fmt/tests/void_optional_keep.vv b/v_windows/v/vlib/v/fmt/tests/void_optional_keep.vv new file mode 100644 index 0000000..97ed7db --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/void_optional_keep.vv @@ -0,0 +1,7 @@ +fn tt() ? { + return error('error') +} + +fn main() { + tt() or { panic('$err') } +} diff --git a/v_windows/v/vlib/v/fmt/tests/vscript_keep.vv b/v_windows/v/vlib/v/fmt/tests/vscript_keep.vv new file mode 100644 index 0000000..3f4dfe2 --- /dev/null +++ b/v_windows/v/vlib/v/fmt/tests/vscript_keep.vv @@ -0,0 +1,9 @@ +#!/usr/local/bin/v run + +x := 5 +println(x) // comment after +println('b') +// comment between +println('c') +mut numbers := [1, 3, 2] +numbers.sort() // 1, 2, 3 |