Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -296,7 +310,17 @@ In the default console reporter, the **test output** - on both _stdout_ and _std
should-not-contain ro
```

- `throws <block>`: 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 <block>`: 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
Expand Down
12 changes: 9 additions & 3 deletions assertions/throws.elv
Original file line number Diff line number Diff line change
@@ -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!'
}
Expand Down
35 changes: 35 additions & 0 deletions assertions/throws.test.elv
Original file line number Diff line number Diff line change
Expand Up @@ -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!'
}
}
}
8 changes: 2 additions & 6 deletions sandbox-result.elv
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
}
}
var merge~ = (operator:multi-value $empty $-merge-two~)
11 changes: 2 additions & 9 deletions section.elv
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use github.com/giancosta86/ethereal/v1/map
use github.com/giancosta86/ethereal/v1/operator
use ./outcomes
use ./test-result

Expand Down Expand Up @@ -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 = (
Expand Down
3 changes: 2 additions & 1 deletion section.test.elv
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ var failed-test = [
]

>> 'with 0 operands' {
section:merge |
all [] |
section:merge |
should-be $section:empty
}

Expand Down
11 changes: 2 additions & 9 deletions stats.elv
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use github.com/giancosta86/ethereal/v1/operator
use ./outcomes
use ./section

Expand All @@ -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~
Expand Down
19 changes: 18 additions & 1 deletion test-script.elv
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
45 changes: 39 additions & 6 deletions test-script.test.elv
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand All @@ -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' {
Expand Down
1 change: 1 addition & 0 deletions tests/script-gallery.elv
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
9 changes: 9 additions & 0 deletions tests/single-scripts/test-drafts.test.elv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
>> Alpha

>> 'In subsection' {
>> Beta

>> Gamma

>> 'Longer title'
}