From b2f2f387dc71f3798a3246b84e92b615c6dbff6b Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Mon, 25 Jan 2021 14:22:08 +0100 Subject: [PATCH] fmt: cleanup and expand tests for ternary if (#8333) --- vlib/v/fmt/tests/if_keep.vv | 20 --------------- vlib/v/fmt/tests/if_single_line_keep.vv | 4 --- vlib/v/fmt/tests/if_ternary_expected.vv | 33 +++++++++++++++++++++++++ vlib/v/fmt/tests/if_ternary_input.vv | 13 ++++++++++ vlib/v/fmt/tests/if_ternary_keep.vv | 20 +++++++++++++++ 5 files changed, 66 insertions(+), 24 deletions(-) delete mode 100644 vlib/v/fmt/tests/if_keep.vv delete mode 100644 vlib/v/fmt/tests/if_single_line_keep.vv create mode 100644 vlib/v/fmt/tests/if_ternary_expected.vv create mode 100644 vlib/v/fmt/tests/if_ternary_input.vv create mode 100644 vlib/v/fmt/tests/if_ternary_keep.vv diff --git a/vlib/v/fmt/tests/if_keep.vv b/vlib/v/fmt/tests/if_keep.vv deleted file mode 100644 index 1aad57fe8b..0000000000 --- a/vlib/v/fmt/tests/if_keep.vv +++ /dev/null @@ -1,20 +0,0 @@ -fn main() { - sprogress := if b.no_cstep { - 'TMP1/${b.nexpected_steps:1d}' - } else { - '${b.cstep:1d}/${b.nexpected_steps:1d}' - } - b := if bar { - // comment - 'some str' - } else { - 'other str' - } - _ := if true { - Foo{} - } else { - Foo{ - x: 5 - } - } -} diff --git a/vlib/v/fmt/tests/if_single_line_keep.vv b/vlib/v/fmt/tests/if_single_line_keep.vv deleted file mode 100644 index d9ba70a22e..0000000000 --- a/vlib/v/fmt/tests/if_single_line_keep.vv +++ /dev/null @@ -1,4 +0,0 @@ -a, b := if true { 'a', 'b' } else { 'b', 'a' } -_ := if false { Foo{} } else { Foo{5, 6} } -arr := [0, 1] -arr << if true { 2 } else { 3 } diff --git a/vlib/v/fmt/tests/if_ternary_expected.vv b/vlib/v/fmt/tests/if_ternary_expected.vv new file mode 100644 index 0000000000..9578886e2e --- /dev/null +++ b/vlib/v/fmt/tests/if_ternary_expected.vv @@ -0,0 +1,33 @@ +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' + } +} diff --git a/vlib/v/fmt/tests/if_ternary_input.vv b/vlib/v/fmt/tests/if_ternary_input.vv new file mode 100644 index 0000000000..badcd76dad --- /dev/null +++ b/vlib/v/fmt/tests/if_ternary_input.vv @@ -0,0 +1,13 @@ +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' } +} diff --git a/vlib/v/fmt/tests/if_ternary_keep.vv b/vlib/v/fmt/tests/if_ternary_keep.vv new file mode 100644 index 0000000000..095c8024fb --- /dev/null +++ b/vlib/v/fmt/tests/if_ternary_keep.vv @@ -0,0 +1,20 @@ +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} } +} + +fn requires_multiple_lines() { + b := if bar { + // with comments inside + 'some str' + } else { + 'other str' + } +}