-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathkv-test
executable file
·126 lines (101 loc) · 2.41 KB
/
kv-test
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
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env bash
BASH_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
source $BASH_DIR/kv-bash
OK() { echo -e "[\e[01;32m OK \e[0m]"; }
FAILED() { echo -e "[\e[01;31mFAILED\e[0m]"; }
RESULT() { [ $? == 0 ] && OK || FAILED; }
TEST_COUNT=0
test_dirA="./testingdirA"
test_dirB="./testingdirB"
# Usage: TESTCASE [description string]
function TESTCASE() {
((TEST_COUNT++))
printf "%3s %-50s" "$TEST_COUNT" "$1"
}
rm -rf "$test_dirA" "$test_dirB"
echo
echo RUN ALL TEST CASES:
echo ===================
TESTCASE 'NO KV_USER_DIR: kvclear; kvlist => line count = 0'
kvset cat Tom
kvset mouse Jerry
kvclear
[ $(kvlist | wc -l) == 0 ]
RESULT
KV_USER_DIR="$test_dirA"
TESTCASE 'call kvget for non-exist key should return empty'
[ "$(kvget name)" == "" ]
RESULT
TESTCASE 'kvset then kvget a variable'
kvset name "Tom"
[ "$(kvget name)" == "Tom" ]
RESULT
TESTCASE 'kvset then kvset again with different value'
kvset name "Tom"
kvset name "Jerry"
[ "$(kvget name)" == "Jerry" ]
RESULT
TESTCASE 'deleted variable should be empty'
kvset name "hello world!"
kvdel name
[ "$(kvget name)" == "" ]
RESULT
TESTCASE 'kvdel non exist should be OK'
kvdel name
[ "$(kvdel name 2>&1)" == "" ]
RESULT
TESTCASE 'kvset without param return error'
kvset 2>/dev/null
[ $? != 0 ]
RESULT
TESTCASE 'kvget without param return error'
kvget 2>/dev/null
[ $? != 0 ]
RESULT
TESTCASE 'kvdel without param return error'
kvdel 2>/dev/null
[ $? != 0 ]
RESULT
TESTCASE 'kvset 3 keys/value; kvlist => line count = 3'
kvclear
kvset var1 value1
kvset var2 value2
kvset var3 value3
[ $(kvlist | wc -l) == 3 ]
RESULT
TESTCASE 'non-exist-var => empty value => line count = 1'
[ $(kvget non-exist-var | wc -l) == 1 ]
RESULT
TESTCASE 'kvclear; kvlist => line count = 0'
kvset cat Tom
kvset mouse Jerry
kvclear
[ $(kvlist | wc -l) == 0 ]
RESULT
TESTCASE 'kvget return empty value => error code != 0'
kvset name
$(kvget name)
[ $? != 0 ]
RESULT
TESTCASE 'spaces in value'
kvset name ' phat dam '
[ "$(kvget name)" == ' phat dam ' ]
RESULT
kvdel name
TESTCASE 'can change user dir'
kvclear
kvset Foo Bar
KV_USER_DIR="$test_dirB"
kvset var1 value1
kvset var2 value2
kvset var3 value3
[ $(kvlist | wc -l) == 3 ]
RESULT
kvclear
TESTCASE 'can return to old user dir'
KV_USER_DIR="$test_dirA"
[ $(kvlist | wc -l) == 1 ]
RESULT
kvclear
#more testcase here
echo