diff --git a/README.md b/README.md index 0a7f433..684a362 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,20 @@ whereas all the other occurrences of `>>` define **sections** - and each section **Please, note**: tests can also reside _in the root of the script_ - of course, when they are not contained in another `>>` block. +**Please, note**: `>>` can also be followed _just by the test title_, like in: + +```elvish +>> 'This is a test draft' +``` + +This will define a **test draft**, whose body simply calls `fail-test` - thus equivalent to: + +```elvish +>> 'This is a test draft' { + fail-test +} +``` + **Please, note**: the `>>` _function_ - called at the beginning of the line, has _no ambiguity_ with the `>>` _redirection operator_, which always appears _after a command_. ## Test outcome @@ -296,7 +310,17 @@ In the default console reporter, the **test output** - on both _stdout_ and _std should-not-contain ro ``` -- `throws `: most general way to assert that `block` _throws an exception_ of any kind - failing if it _completed successfully_. In general, you should use the `fails` assertion, as it focuses on `fail`-based exceptions. +- `throws `: most general way to assert that `block` _throws an exception_ of any kind - failing if the block _completed successfully_. The function emits: + - by default: + - the **exception** itself, as a _value_ + + - the **bytes** emitted by the block, redirected to **stderr** + + - if the `swallow` flag is enabled: the **actual output** of the block - both _bytes_ and _values_ + + **Please, note**: In general, you should use the `fails` assertion, as it focuses on `fail`-based exceptions. + + ##### Example ```elvish # This works fine diff --git a/assertions/throws.elv b/assertions/throws.elv index 059a8be..65dc94b 100644 --- a/assertions/throws.elv +++ b/assertions/throws.elv @@ -1,8 +1,14 @@ -fn throws { |block| +fn throws { |&swallow=$false block| try { - $block | only-bytes >&2 + if $swallow { + $block + } else { + $block | only-bytes >&2 + } } catch e { - put $e + if (not $swallow) { + put $e + } } else { fail 'The given code block did not fail!' } diff --git a/assertions/throws.test.elv b/assertions/throws.test.elv index f452c54..13b0076 100644 --- a/assertions/throws.test.elv +++ b/assertions/throws.test.elv @@ -29,4 +29,39 @@ var throws~ = $throws:throws~ exception:get-fail-content | should-be CIOP } + + >> 'when swallowing the exception' { + >> 'when an exception actually occurs' { + >> 'when emitting values' { + throws &swallow { + put 90 + put (num 92) + fail DODO + } | + should-emit &strict [ + 90 + (num 92) + ] + } + + >> 'when emitting bytes' { + throws &swallow { + echo Hello + fail DODO + } | + should-emit [ + Hello + ] + } + } + + >> 'when no exception occurs' { + fails { + throws &swallow { + put 90 + } + } | + should-be 'The given code block did not fail!' + } + } } \ No newline at end of file diff --git a/sandbox-result.elv b/sandbox-result.elv index 0a0e67b..127f875 100644 --- a/sandbox-result.elv +++ b/sandbox-result.elv @@ -1,5 +1,6 @@ use github.com/giancosta86/ethereal/v1/lang use github.com/giancosta86/ethereal/v1/map +use github.com/giancosta86/ethereal/v1/operator use github.com/giancosta86/ethereal/v1/seq use ./section @@ -21,9 +22,4 @@ fn -merge-two { |left right| ] } -fn merge { |@arguments| - lang:get-inputs $arguments | - seq:reduce $empty { |cumulated-result current-result| - -merge-two $cumulated-result $current-result - } -} \ No newline at end of file +var merge~ = (operator:multi-value $empty $-merge-two~) \ No newline at end of file diff --git a/section.elv b/section.elv index db7fca1..0239dae 100644 --- a/section.elv +++ b/section.elv @@ -1,4 +1,5 @@ use github.com/giancosta86/ethereal/v1/map +use github.com/giancosta86/ethereal/v1/operator use ./outcomes use ./test-result @@ -85,15 +86,7 @@ set -merge-sub-sections~ = { |left right| put $sub-sections } -fn merge { - var result = $empty - - each { |section| - set result = (-merge-two-sections $result $section) - } - - put $result -} +var merge~ = (operator:multi-value $empty $-merge-two-sections~) fn trim-empty { |section| var updated-sub-sections = ( diff --git a/section.test.elv b/section.test.elv index 4a48baf..0507c2c 100644 --- a/section.test.elv +++ b/section.test.elv @@ -313,7 +313,8 @@ var failed-test = [ ] >> 'with 0 operands' { - section:merge | + all [] | + section:merge | should-be $section:empty } diff --git a/stats.elv b/stats.elv index 4ce3dd1..0ce6acb 100644 --- a/stats.elv +++ b/stats.elv @@ -1,3 +1,4 @@ +use github.com/giancosta86/ethereal/v1/operator use ./outcomes use ./section @@ -13,15 +14,7 @@ fn -raw-sum-two { |left right| ] } -fn -raw-sum { - var result = $-raw-empty - - each { |current-stats| - set result = (-raw-sum-two $result $current-stats) - } - - put $result -} +var -raw-sum~ = (operator:multi-value $-raw-empty $-raw-sum-two~) var -raw-from-test-results~ var -raw-from-sub-sections~ diff --git a/test-script.elv b/test-script.elv index 19646e8..acd2da1 100644 --- a/test-script.elv +++ b/test-script.elv @@ -1,5 +1,6 @@ use path use github.com/giancosta86/ethereal/v1/exception +use github.com/giancosta86/ethereal/v1/lang use github.com/giancosta86/ethereal/v1/map use ./assertions use ./ethereal @@ -22,7 +23,23 @@ fn run { |script-path| ] } - fn '>>' { |title block| + fn '>>' { |title @rest| + if (not-eq (kind-of $title) string) { + fail 'The title must be a string!' + } + + var block = ( + var rest-length = (count $rest) + + if (== $rest-length 1) { + put $rest[0] + } elif (== $rest-length 0) { + put $assertions:fail-test~ + } else { + fail 'Only 1 or 2 arguments are allowed!' + } + ) + var this-frame = (test-script-frame:create $abs-script-path $title) var parent-frame = $current-frame diff --git a/test-script.test.elv b/test-script.test.elv index 21e7442..a26e489 100644 --- a/test-script.test.elv +++ b/test-script.test.elv @@ -143,12 +143,10 @@ use ./tests/script-gallery } >> 'root test without title' { - throws { + fails { run-test-script root-test-without-title } | - print (all)[reason] | - str:contains (all) 'arity mismatch' | - should-be $true + should-be 'The title must be a string!' } >> 'section without title' { @@ -166,8 +164,43 @@ use ./tests/script-gallery ] put $section[test-results][Alpha][exception-log] | - str:contains (all) 'arity mismatch' | - should-be $true + should-contain 'The title must be a string!' + } + + >> 'test drafts' { + var section = (run-test-script test-drafts) + + section:simplify $section | + should-be [ + &test-results=[ + &Alpha=[ + &outcome=$outcomes:failed + &output='' + ] + ] + &sub-sections=[ + &'In subsection'=[ + &test-results=[ + &Beta=[ + &outcome=$outcomes:failed + &output='' + ] + &Gamma=[ + &outcome=$outcomes:failed + &output='' + ] + &'Longer title'=[ + &outcome=$outcomes:failed + &output='' + ] + ] + &sub-sections=[&] + ] + ] + ] + + put $section[test-results][Alpha][exception-log] | + should-contain 'TEST SET TO FAIL' } >> 'with section having mixed outcomes' { diff --git a/tests/script-gallery.elv b/tests/script-gallery.elv index 3b11569..7dc3a89 100644 --- a/tests/script-gallery.elv +++ b/tests/script-gallery.elv @@ -15,6 +15,7 @@ var single-scripts = [ single-scripts/root-ok.test.elv single-scripts/root-test-without-title.test.elv single-scripts/sub-section-without-title.test.elv + single-scripts/test-drafts.test.elv ] var aggregator = [ diff --git a/tests/single-scripts/test-drafts.test.elv b/tests/single-scripts/test-drafts.test.elv new file mode 100644 index 0000000..b08da08 --- /dev/null +++ b/tests/single-scripts/test-drafts.test.elv @@ -0,0 +1,9 @@ +>> Alpha + +>> 'In subsection' { + >> Beta + + >> Gamma + + >> 'Longer title' +} \ No newline at end of file