From 81963b51ab6b9881b5842afc2aece95212db435a Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 25 Apr 2022 11:58:26 +0300 Subject: [PATCH] tests: add ability to check the output of commands for .starts_with, .ends_with and .contains strings, in `v test-all` --- cmd/tools/vtest-all.v | 78 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 14 deletions(-) diff --git a/cmd/tools/vtest-all.v b/cmd/tools/vtest-all.v index d2f704c6df..56765e1f84 100644 --- a/cmd/tools/vtest-all.v +++ b/cmd/tools/vtest-all.v @@ -49,17 +49,26 @@ enum RunCommandKind { const expect_nothing = '' +const starts_with_nothing = '' + +const ends_with_nothing = '' + +const contains_nothing = '' + struct Command { mut: - line string - label string // when set, the label will be printed *before* cmd.line is executed - ecode int - okmsg string - errmsg string - rmfile string - runcmd RunCommandKind = .system - expect string = expect_nothing - output string + line string + label string // when set, the label will be printed *before* cmd.line is executed + ecode int + okmsg string + errmsg string + rmfile string + runcmd RunCommandKind = .system + expect string = expect_nothing + starts_with string = starts_with_nothing + ends_with string = ends_with_nothing + contains string = contains_nothing + output string } fn get_all_commands() []Command { @@ -99,6 +108,14 @@ fn get_all_commands() []Command { runcmd: .execute expect: 'Hello, World!\n' } + res << Command{ + line: '$vexe interpret examples/hanoi.v' + okmsg: 'V can interpret hanoi.v' + runcmd: .execute + starts_with: 'Disc 1 from A to C...\n' + ends_with: 'Disc 1 from A to C...\n' + contains: 'Disc 7 from A to C...\n' + } res << Command{ line: '$vexe -o - examples/hello_world.v | grep "#define V_COMMIT_HASH" > /dev/null' okmsg: 'V prints the generated source code to stdout with `-o -` .' @@ -252,23 +269,56 @@ fn (mut cmd Command) run() { spent := sw.elapsed().milliseconds() // mut is_failed := false + mut is_failed_expected := false + mut is_failed_starts_with := false + mut is_failed_ends_with := false + mut is_failed_contains := false if cmd.ecode != 0 { is_failed = true } if cmd.expect != expect_nothing { if cmd.output != cmd.expect { is_failed = true + is_failed_expected = true + } + } + if cmd.starts_with != starts_with_nothing { + if !cmd.output.starts_with(cmd.starts_with) { + is_failed = true + is_failed_starts_with = true + } + } + if cmd.ends_with != ends_with_nothing { + if !cmd.output.ends_with(cmd.ends_with) { + is_failed = true + is_failed_ends_with = true + } + } + if cmd.contains != contains_nothing { + if !cmd.output.contains(cmd.contains) { + is_failed = true + is_failed_contains = true } } // run_label := if is_failed { term.failed('FAILED') } else { term_highlight('OK') } println('> Running: "$cmd.line" took: $spent ms ... $run_label') // - if is_failed && cmd.expect != expect_nothing { - if cmd.output != cmd.expect { - eprintln('> expected:\n$cmd.expect') - eprintln('> output:\n$cmd.output') - } + if is_failed && is_failed_expected { + eprintln('> expected:\n$cmd.expect') + eprintln('> output:\n$cmd.output') + } + if is_failed && is_failed_starts_with { + eprintln('> expected to start with:\n$cmd.starts_with') + eprintln('> output:\n${cmd.output#[..cmd.starts_with.len]}') + } + if is_failed && is_failed_ends_with { + eprintln('> expected to end with:\n$cmd.ends_with') + eprintln('> output:\n${cmd.output#[-cmd.starts_with.len..]}') + } + if is_failed && is_failed_contains { + eprintln('> expected to contain:\n$cmd.contains') + eprintln('> output:\n$cmd.output') } if vtest_nocleanup { return