@@ -46,9 +46,75 @@ testOutput() {
4646 assertEquals ' should preserve unescaped backslashes' ' Foo \ bar' " ${stdout} "
4747}
4848
49+ testKeyValue () {
50+ local store=$( mktemp)
51+
52+ kv_create $store
53+
54+ kv_set $store key value
55+ kv_set $store foo bar
56+ kv_set $store key other_value
57+ kv_set $store bar baz
58+
59+ assertEquals " other_value" " $( kv_get $store key) "
60+ assertEquals " bar" " $( kv_get $store foo) "
61+ assertEquals " baz" " $( kv_get $store bar) "
62+
63+ # if the key isn't there it should return an empty string
64+ assertEquals " " " $( kv_get $store not_there) "
65+
66+ # kv_keys returns each key on a new line
67+ assertEquals " $( printf " %s\n" bar foo key) " " $( kv_keys $store ) "
68+
69+ # kv_list returns key=value on individual lines
70+ assertEquals " $( printf " %s\n" bar=baz foo=bar key=other_value) " " $( kv_list $store ) "
71+
72+ # calling create on an existing store doesn't erase it
73+ kv_create $store
74+ assertEquals " $( printf " %s\n" bar=baz foo=bar key=other_value) " " $( kv_list $store ) "
75+
76+ # now clear the store
77+ kv_clear $store
78+
79+ assertEquals " " " $( kv_get $store key) "
80+ assertEquals " " " $( kv_keys $store ) "
81+ assertEquals " " " $( kv_list $store ) "
82+ }
83+
84+ # if the file doesn't exist, everything should be a no-op
85+ testKeyValueNoFile () {
86+ # empty file argument
87+ local empty=" "
88+
89+ kv_set $empty key value
90+
91+ assertEquals " $( kv_get $empty key) " " "
92+ assertEquals " $( kv_keys $empty ) " " "
93+ assertEquals " $( kv_list $empty ) " " "
94+
95+ local store=" /tmp/does-not-exist"
96+
97+ kv_set $store key value
98+
99+ assertEquals " " " $( kv_get $store key) "
100+ assertEquals " " " $( kv_keys $store ) "
101+ assertEquals " " " $( kv_list $store ) "
102+
103+ # running these commands has not created this file
104+ assertTrue " [[ ! -e $store ]]"
105+
106+ local space=" "
107+ kv_set $space key value
108+
109+ assertEquals " $( kv_get $space key) " " "
110+ assertEquals " $( kv_keys $space ) " " "
111+ assertEquals " $( kv_list $space ) " " "
112+ }
113+
49114# the modules to be tested
50115source " $( pwd) " /lib/monitor.sh
51116source " $( pwd) " /lib/output.sh
117+ source " $( pwd) " /lib/kvstore.sh
52118
53119# import the testing framework
54- source " $( pwd) " /test/shunit2
120+ source " $( pwd) " /test/shunit2
0 commit comments