File tree Expand file tree Collapse file tree 3 files changed +12
-48
lines changed Expand file tree Collapse file tree 3 files changed +12
-48
lines changed Original file line number Diff line number Diff line change @@ -54,30 +54,24 @@ this test always fails
5454Fail if the given expression evaluates to false.
5555
5656*** Note:*** * The expression must be a simple command. [ Compound
57- commands] ( https://www.gnu.org/software/ bash/manual/bash.html#Compound-Commands ) ,
58- such as ` [[ ` , can be used only when executed with ` bash -c ` .
57+ commands] [ bash-comp-cmd ] , such as ` [[ ` , can be used only when executed
58+ with ` bash -c ` .
5959
6060``` bash
6161@test ' assert()' {
62- run touch ' /var/log/test.log'
62+ touch ' /var/log/test.log'
6363 assert [ -e ' /var/log/test.log' ]
6464}
6565```
6666
67- On failure, the failed expression, ` $status ` and ` $output ` are
68- displayed.
67+ On failure, the failed expression is displayed.
6968
7069```
7170-- assertion failed --
7271expression : [ -e /var/log/test.log ]
73- status : 1
74- output : touch: cannot touch ‘/var/log/test.log’: Permission denied
7572--
7673```
7774
78- If ` $output ` is longer than one line, it is displayed in * multi-line*
79- format.
80-
8175
8276### ` assert_equal `
8377
@@ -641,3 +635,4 @@ exclusive. An error is displayed when used simultaneously.
641635[ bats-core-output ] : https://github.com/ztombol/bats-core#output-formatting
642636[ bats-core ] : https://github.com/ztombol/bats-core
643637[ bats-docs ] : https://github.com/ztombol/bats-docs
638+ [ bash-comp-cmd ] : https://www.gnu.org/software/bash/manual/bash.html#Compound-Commands
Original file line number Diff line number Diff line change @@ -49,15 +49,13 @@ fail() {
4949 return 1
5050}
5151
52- # Fail and display details if the expression evaluates to false. Details
53- # include the expression, `$status' and `$output'.
52+ # Fail and display the expression if it evaluates to false.
5453#
5554# NOTE: The expression must be a simple command. Compound commands, such
5655# as `[[', can be used only when executed with `bash -c'.
5756#
5857# Globals:
59- # status
60- # output
58+ # none
6159# Arguments:
6260# $1 - expression
6361# Returns:
@@ -67,18 +65,8 @@ fail() {
6765# STDERR - details, on failure
6866assert () {
6967 if ! " $@ " ; then
70- { local -ar single=(
71- ' expression' " $* "
72- ' status' " $status "
73- )
74- local -ar may_be_multi=(
75- ' output' " $output "
76- )
77- local -ir width=" $( batslib_get_max_single_line_key_width \
78- " ${single[@]} " " ${may_be_multi[@]} " ) "
79- batslib_print_kv_single " $width " " ${single[@]} "
80- batslib_print_kv_single_or_multi " $width " " ${may_be_multi[@]} "
81- } | batslib_decorate ' assertion failed' \
68+ batslib_print_kv_single 10 ' expression' " $* " \
69+ | batslib_decorate ' assertion failed' \
8270 | fail
8371 fi
8472}
Original file line number Diff line number Diff line change @@ -8,30 +8,11 @@ load test_helper
88 [ " ${# lines[@]} " -eq 0 ]
99}
1010
11- @test ' assert() <expression>: returns 1 and displays details if <expression> evaluates to FALSE' {
12- run bash -c ' echo "a"
13- false'
11+ @test ' assert() <expression>: returns 1 and displays <expression> if it evaluates to FALSE' {
1412 run assert false
1513 [ " $status " -eq 1 ]
16- [ " ${# lines[@]} " -eq 5 ]
14+ [ " ${# lines[@]} " -eq 3 ]
1715 [ " ${lines[0]} " == ' -- assertion failed --' ]
1816 [ " ${lines[1]} " == ' expression : false' ]
19- [ " ${lines[2]} " == ' status : 1' ]
20- [ " ${lines[3]} " == ' output : a' ]
21- [ " ${lines[4]} " == ' --' ]
22- }
23-
24- @test " assert() <expression>: displays \`\$ output' in multi-line format if it is longer than one line" {
25- run bash -c ' printf "a 0\na 1"
26- false'
27- run assert false
28- [ " $status " -eq 1 ]
29- [ " ${# lines[@]} " -eq 7 ]
30- [ " ${lines[0]} " == ' -- assertion failed --' ]
31- [ " ${lines[1]} " == ' expression : false' ]
32- [ " ${lines[2]} " == ' status : 1' ]
33- [ " ${lines[3]} " == ' output (2 lines):' ]
34- [ " ${lines[4]} " == ' a 0' ]
35- [ " ${lines[5]} " == ' a 1' ]
36- [ " ${lines[6]} " == ' --' ]
17+ [ " ${lines[2]} " == ' --' ]
3718}
You can’t perform that action at this time.
0 commit comments