2
0

tests.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. JSON_SCRIPT=tests.json
  2. JSON_SCRIPT_BIN=${TEST_JSON_SCRIPT=:-./json_script-example}
  3. FILE_STDOUT=tests.stdout
  4. FILE_STDERR=tests.stderr
  5. FILE_EXPECTED=tests.expected
  6. call_json_script() {
  7. #export LD_PRELOAD=../libjson_script.so
  8. $JSON_SCRIPT_BIN "$@" "$JSON_SCRIPT" >"$FILE_STDOUT" 2>"$FILE_STDERR"
  9. }
  10. assertStdioEquals() {
  11. local expected="$1"
  12. local file_stdio="$2"
  13. echo "$expected" >"$FILE_EXPECTED"
  14. if [ -z "$expected" ]; then
  15. # we are expecting empty output, but we deliberately added a newline
  16. # with echo above, so adding another echo to compensate for that
  17. echo >>"$file_stdio"
  18. fi
  19. diff -up "$FILE_EXPECTED" "$file_stdio" >/dev/null 2>&1 || {
  20. cat >&2 <<EOF
  21. |--- expecting
  22. $expected<
  23. |--- actual
  24. $(cat $file_stdio)<
  25. |--- END
  26. EOF
  27. exit 1
  28. }
  29. }
  30. assertStdoutEquals() {
  31. assertStdioEquals "$1" "$FILE_STDOUT"
  32. }
  33. assertStderrEquals() {
  34. assertStdioEquals "$1" "$FILE_STDERR"
  35. }
  36. test_bad_json() {
  37. cat >"$JSON_SCRIPT" <<-EOF
  38. [
  39. [ ]
  40. [ ]
  41. ]
  42. EOF
  43. call_json_script
  44. assertStderrEquals "load JSON data from $JSON_SCRIPT failed."
  45. }
  46. test_expr_eq() {
  47. cat >"$JSON_SCRIPT" <<-EOF
  48. [
  49. [ "if",
  50. [ "eq", "VAR", "foo" ],
  51. [ "echo", "bar" ],
  52. [ "echo", "baz" ]
  53. ]
  54. ]
  55. EOF
  56. call_json_script "VAR=foo"
  57. assertStdoutEquals "echo bar"
  58. call_json_script "VAR=xxx"
  59. assertStdoutEquals "echo baz"
  60. }
  61. test_expr_has() {
  62. cat >"$JSON_SCRIPT" <<-EOF
  63. [
  64. [ "if",
  65. [ "has", "VAR" ],
  66. [ "echo", "bar" ],
  67. [ "echo", "baz" ]
  68. ]
  69. ]
  70. EOF
  71. call_json_script "VAR=foo"
  72. assertStdoutEquals "echo bar"
  73. call_json_script
  74. assertStdoutEquals "echo baz"
  75. }
  76. test_expr_regex_single() {
  77. cat >"$JSON_SCRIPT" <<-EOF
  78. [
  79. [ "if",
  80. [ "regex", "VAR", ".ell." ],
  81. [ "echo", "bar" ],
  82. [ "echo", "baz" ]
  83. ]
  84. ]
  85. EOF
  86. call_json_script "VAR=hello"
  87. assertStdoutEquals "echo bar"
  88. call_json_script "VAR=.ell."
  89. assertStdoutEquals "echo bar"
  90. call_json_script
  91. assertStdoutEquals "echo baz"
  92. call_json_script "VAR="
  93. assertStdoutEquals "echo baz"
  94. call_json_script "VAR=hell"
  95. assertStdoutEquals "echo baz"
  96. }
  97. test_expr_regex_multi() {
  98. cat >"$JSON_SCRIPT" <<-EOF
  99. [
  100. [ "if",
  101. [ "regex", "VAR", [ ".ell.", "w.rld" ] ],
  102. [ "echo", "bar" ],
  103. [ "echo", "baz" ]
  104. ]
  105. ]
  106. EOF
  107. call_json_script "VAR=hello"
  108. assertStdoutEquals "echo bar"
  109. call_json_script "VAR=world"
  110. assertStdoutEquals "echo bar"
  111. call_json_script "VAR=.ell."
  112. assertStdoutEquals "echo bar"
  113. call_json_script "VAR=w.rld"
  114. assertStdoutEquals "echo bar"
  115. call_json_script
  116. assertStdoutEquals "echo baz"
  117. call_json_script "VAR="
  118. assertStdoutEquals "echo baz"
  119. call_json_script "VAR=hell"
  120. assertStdoutEquals "echo baz"
  121. }
  122. test_expr_not() {
  123. cat >"$JSON_SCRIPT" <<-EOF
  124. [
  125. [ "if",
  126. [ "not", [ "has", "VAR" ] ],
  127. [ "echo", "bar" ],
  128. [ "echo", "baz" ]
  129. ]
  130. ]
  131. EOF
  132. call_json_script "VAR=foo"
  133. assertStdoutEquals "echo baz"
  134. call_json_script
  135. assertStdoutEquals "echo bar"
  136. }
  137. test_expr_and() {
  138. cat >"$JSON_SCRIPT" <<-EOF
  139. [
  140. [ "if",
  141. [ "and", [ "eq", "EQVAR", "eqval" ],
  142. [ "regex", "REGEXVAR", "regex..." ]
  143. ],
  144. [ "echo", "bar" ],
  145. [ "echo", "baz" ]
  146. ]
  147. ]
  148. EOF
  149. call_json_script "EQVAR=eqval" "REGEXVAR=regexval"
  150. assertStdoutEquals "echo bar"
  151. call_json_script "EQVAR=foo"
  152. assertStdoutEquals "echo baz"
  153. call_json_script "REGEXVAR=regex***"
  154. assertStdoutEquals "echo baz"
  155. call_json_script
  156. assertStdoutEquals "echo baz"
  157. }
  158. test_expr_or() {
  159. cat >"$JSON_SCRIPT" <<-EOF
  160. [
  161. [ "if",
  162. [ "or", [ "not", [ "eq", "EQVAR", "eqval" ] ],
  163. [ "regex", "REGEXVAR", [ "regexva.[0-9]", "regexva.[a-z]" ] ]
  164. ],
  165. [ "echo", "bar" ],
  166. [ "echo", "baz" ]
  167. ]
  168. ]
  169. EOF
  170. call_json_script "EQVAR=eqval" "REGEXVAR=regexval1"
  171. assertStdoutEquals "echo bar"
  172. call_json_script "EQVAR=neq" "REGEXVAR=sxc"
  173. assertStdoutEquals "echo bar"
  174. call_json_script "REGEXVAR=sxc"
  175. assertStdoutEquals "echo bar"
  176. call_json_script "EQVAR=foo"
  177. assertStdoutEquals "echo bar"
  178. call_json_script
  179. assertStdoutEquals "echo bar"
  180. call_json_script "EQVAR=eqval" "REGEXVAR=regexval"
  181. assertStdoutEquals "echo baz"
  182. }
  183. test_expr_isdir() {
  184. cat >"$JSON_SCRIPT" <<-EOF
  185. [
  186. [ "if",
  187. [ "isdir", "%VAR%" ],
  188. [ "echo", "bar" ],
  189. [ "echo", "baz" ]
  190. ]
  191. ]
  192. EOF
  193. call_json_script "VAR=/"
  194. assertStdoutEquals "echo bar"
  195. call_json_script "VAR=$(mktemp -u)"
  196. assertStdoutEquals "echo baz"
  197. call_json_script
  198. assertStdoutEquals "echo baz"
  199. }
  200. test_cmd_case() {
  201. cat >"$JSON_SCRIPT" <<-EOF
  202. [
  203. [ "case", "CASEVAR", {
  204. "0": [ "echo", "foo" ],
  205. "1": [
  206. [ "echo", "bar" ],
  207. [ "echo", "baz" ]
  208. ],
  209. "%VAR%": [ "echo", "quz" ]
  210. } ]
  211. ]
  212. EOF
  213. call_json_script "CASEVAR=0"
  214. assertStdoutEquals "echo foo"
  215. call_json_script "CASEVAR=1"
  216. assertStdoutEquals "echo bar
  217. echo baz"
  218. call_json_script "CASEVAR=%VAR%"
  219. assertStdoutEquals "echo quz"
  220. call_json_script "CASEVAR="
  221. assertStdoutEquals ""
  222. call_json_script
  223. assertStdoutEquals ""
  224. call_json_script "CASEVAR=xxx" "VAR=xxx"
  225. assertStdoutEquals ""
  226. }
  227. test_cmd_if() {
  228. cat >"$JSON_SCRIPT" <<-EOF
  229. [
  230. [ "if",
  231. [ "eq", "VAR", "foo" ],
  232. [ "echo", "bar" ],
  233. [ "echo", "baz" ]
  234. ]
  235. ]
  236. EOF
  237. call_json_script "VAR=foo"
  238. assertStdoutEquals "echo bar"
  239. call_json_script "VAR=xxx"
  240. assertStdoutEquals "echo baz"
  241. }
  242. test_cmd_cb() {
  243. cat >"$JSON_SCRIPT" <<-EOF
  244. [
  245. [ "exec", "%VAR%", "/%VAS%%%/" ]
  246. ]
  247. EOF
  248. call_json_script
  249. assertStdoutEquals "exec /%/"
  250. call_json_script "VAR="
  251. assertStdoutEquals "exec /%/"
  252. call_json_script "VAR=qux" "VAS=3"
  253. assertStdoutEquals "exec qux /3%/"
  254. }
  255. test_cmd_return() {
  256. cat >"$JSON_SCRIPT" <<-EOF
  257. [
  258. [ "heh", "%HEHVAR%" ],
  259. [ "%VAR%", "%VAR%" ],
  260. [ "return" ],
  261. [ "exec_non_reachable", "Arghhh" ]
  262. ]
  263. EOF
  264. call_json_script "HEHVAR=dude" "VAR=ow"
  265. assertStdoutEquals "heh dude
  266. %VAR% ow"
  267. }
  268. . ./shunit2/shunit2