OILS
/
stdlib
/
testing.ysh
1 |
# testing.ysh
|
2 |
#
|
3 |
# Usage:
|
4 |
# source --builtin testing.sh
|
5 |
#
|
6 |
# func f(x) { return (x + 1) }
|
7 |
#
|
8 |
# describe foo {
|
9 |
# assert (43 === f(42))
|
10 |
# }
|
11 |
#
|
12 |
# if is-main {
|
13 |
# run-tests @ARGV # --filter
|
14 |
# }
|
15 |
|
16 |
module stdlib/testing || return 0
|
17 |
|
18 |
source --builtin args.ysh
|
19 |
|
20 |
# Opt in to lazy evaluation
|
21 |
|
22 |
proc assert ( ; cond LAZY ) {
|
23 |
echo hi
|
24 |
|
25 |
# TODO: evalExpr() builtin
|
26 |
var val = evalExpr(cond)
|
27 |
if (not val) {
|
28 |
# TODO: if it's an expr.Binary
|
29 |
# Then
|
30 |
#
|
31 |
# Then print $left != $right
|
32 |
#
|
33 |
# I think you need to introspect on the source code
|
34 |
#
|
35 |
# Or print '5 != 3'
|
36 |
#
|
37 |
# Or you can evaluate left and right separately, and then compare them
|
38 |
|
39 |
echo
|
40 |
}
|
41 |
}
|
42 |
|
43 |
# What happens when there are duplicate test IDs?
|
44 |
#
|
45 |
# Also I think filter by "$test_id/$case_id"
|
46 |
|
47 |
proc __it (case_id ; ; ; block) {
|
48 |
# This uses a clean directory
|
49 |
echo TODO
|
50 |
}
|
51 |
|
52 |
# is this accessible to users?
|
53 |
# It can contain a global list of things to run
|
54 |
|
55 |
# Naming convention: a proc named 'describe' mutates a global named _describe?
|
56 |
# Or maybe _describe_list ?
|
57 |
|
58 |
var _describe_list = []
|
59 |
|
60 |
proc describe (test_id ; ; ; block) {
|
61 |
echo describe
|
62 |
= desc
|
63 |
|
64 |
# TODO:
|
65 |
# - need append
|
66 |
# - need ::
|
67 |
# _ _describe->append(cmd)
|
68 |
#
|
69 |
# Need to clean this up
|
70 |
# append (_describe, cmd) # does NOT work!
|
71 |
|
72 |
_ _describe_list->append(block)
|
73 |
}
|
74 |
|
75 |
# Problem: this creates a global variable?
|
76 |
Args :spec {
|
77 |
flag --filter 'Regex of test descriptions'
|
78 |
}
|
79 |
|
80 |
proc run-tests {
|
81 |
|
82 |
# TODO: fix this crap
|
83 |
var opt = null
|
84 |
var i = null
|
85 |
setvar opt, i = parseArgs(spec, ARGV)
|
86 |
|
87 |
# TODO:
|
88 |
# - parse --filter foo, which you can use eggex for!
|
89 |
|
90 |
for cmd in (_describe) {
|
91 |
# TODO: print filename and 'describe' name?
|
92 |
try {
|
93 |
eval (cmd)
|
94 |
}
|
95 |
if (_status !== 0) {
|
96 |
echo 'failed'
|
97 |
}
|
98 |
}
|
99 |
}
|