-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbashunit.sh
More file actions
116 lines (106 loc) · 3.38 KB
/
bashunit.sh
File metadata and controls
116 lines (106 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/sh
testsuccess=0
testfail=0
redbg=$(tput setab 1)
greenbg=$(tput setab 2)
tinverse=$(tput rev) # Start reverse video
reset=`tput sgr0`
blackfg=$(tput setaf 0)
defaultfg=$(tput setaf 9)
defaultbg=$(tput setab 9)
standardout=true
function testHeadLine() {
headline="$1"
ncols=$(tput cols)
if [[ ${#headline} -gt $ncols ]]; then
headline="${headline::$(($ncols-3))}..."
fi
printf "${tinverse}%-${ncols}s${reset}" "$headline"
}
function printGreen () {
headline="$1"
echo " ${greenbg}${blackfg}$headline${reset}"
}
function printRed () {
headline="$1"
echo " ${redbg}${blackfg}$headline${reset}"
}
# check standard out for pattern
function assertContains () {
commands="$1"
expectedoutregexp="$2"
testHeadLine "Test assert contains - command: $commands - assertion: $expectedoutregexp"
results=$(eval $commands 2>&1)
if $standardout; then echo -e "$results"; fi
if echo "$results" | grep -q "$expectedoutregexp"; then
printGreen "SUCCESS: found \"$expectedoutregexp\" in command output"
((testsuccess++))
else
printRed "FAIL: not found \"$expectedoutregexp\" in command output"
((testfail++))
fi
}
# check standard out for pattern not expected
function assertNotContains () {
commands="$1"
expectedoutregexp="$2"
testHeadLine "Test assert contains - command: $commands - assertion: not $expectedoutregexp"
results=$(eval $commands 2>&1)
if $standardout; then echo -e "$results"; fi
if ! echo "$results" | grep -q "$expectedoutregexp"; then
printGreen "SUCCESS: not found \"$expectedoutregexp\" in command output"
((testsuccess++))
else
printRed "FAIL: found \"$expectedoutregexp\" in command output"
((testfail++))
fi
}
# check if standard out of check command contains pattern
function assertCheckContains () {
commands="$1"
expectedtest="$2"
expectedtestoutregexp="$3"
testHeadLine "Test assert check contains - command: $commands - check: $expectedtest - assertion: $expectedtestoutregexp"
if $standardout; then $commands; else $commands >/dev/null; fi
if [[ $expectedtest != "" ]]; then
exptest=$(eval $expectedtest)
if $standardout; then echo -e "$exptest"; fi
if echo "$exptest" | grep -q "$expectedtestoutregexp"; then
printGreen "SUCCESS: found \"$expectedtestoutregexp\" in test output"
((testsuccess++))
else
printRed "FAIL: not found \"$expectedtestoutregexp\" in test output"
((testfail++))
fi
fi
}
# check if standard out of check command contains pattern
function assertCheckNotContains () {
commands="$1"
expectedtest="$2"
expectedtestoutregexp="$3"
testHeadLine "Test assert check NOT contains - command: $commands - check: $expectedtest - assertion: $expectedtestoutregexp"
if $standardout; then $commands; else $commands >/dev/null; fi
if [[ $expectedtest != "" ]]; then
exptest=$( eval $expectedtest)
if $standardout; then echo -e "$exptest"; fi
if echo "$exptest" | grep -q "$expectedtestoutregexp"; then
printRed "FAIL: found \"$expectedtestoutregexp\" in test output"
((testfail++))
else
printGreen "SUCCESS: not found \"$expectedtestoutregexp\" in test output"
((testsuccess++))
fi
fi
}
function testend () {
testname="$1"
echo
echo "Successfull: $testsuccess"
echo "Fails: $testfail"
if [[ $testfail -gt 0 ]]; then
echo "${redbg}${blackfg}Testsuite \"$testname\" red!${reset}"
else
echo "${greenbg}${blackfg}Testsuite \"$testname\" green!${reset}"
fi
}