-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.libsonnet
110 lines (97 loc) · 2.64 KB
/
main.libsonnet
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
local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
{
'#':
d.pkg(
name='testonnet',
url='github.com/jsonnet-libs/testonnet',
filename=std.thisFile,
help=|||
Testonnet is a unit test framework for [Jsonnet](http://jsonnet.org/).
|||,
)
+ d.package.withUsageTemplate(|||
local test = import "%(import)s";
local fact(n) = if n == 0 then 1 else n * fact(n - 1);
test.new(std.thisFile)
+ test.case.new(
name='Fact',
test=test.expect.eq(
actual=fact(10),
expected=3628800
)
)
|||),
'#new': d.fn(
help=|||
`new` initializes Testonnet with a new test suite with a `name`.
The `name` will be reported during execution, in case of failures it will help find
the culprit, using `std.thisFile` in the name might be useful to identify the test
suite.
Output on success:
```
$ jsonnet -J vendor/ test.jsonnet
TRACE: testonnet/main.libsonnet:74 Testing suite test.jsonnet
{
"verify": "Passed 3 test cases"
}
```
Output on failure:
```
$ jsonnet -J vendor/ test.jsonnet
TRACE: testonnet/main.libsonnet:74 Testing suite test.jsonnet
RUNTIME ERROR: Failed 3/3 test cases:
testFoo: Expected 1 to be 2
testBar: Expected 1 to satisfy the function
testBaz: Expected 1 to satisfy the condition that the value is between 2 and 3
testonnet/main.libsonnet:(78:11)-(84:13) thunk from <object <anonymous>>
testonnet/main.libsonnet:(74:7)-(87:8) object <anonymous>
Field "verify"
During manifestation
```
|||,
args=[
d.arg('name', d.T.string),
],
),
new(name): {
cases:: [],
verify:
local failures = [
case
for case in self.cases
if !case.test.verify
];
std.trace(
'Testing suite ' + name,
if std.length(failures) > 0
then
error 'Failed %d/%d test cases:\n' % [
std.length(failures),
std.length(self.cases),
] + std.join('\n', [
'%s: %s' % [f.name, f.test.message]
for f in failures
])
else
'Passed %d test cases' % std.length(self.cases),
),
},
case: {
'#new':: d.fn(
help=|||
`new` creates a new test case.
|||,
args=[
d.arg('name', d.T.string),
d.arg('test', d.T.object),
]
),
new(name, test): {
cases+: [{
name: name,
test: test,
}],
},
},
expect: import 'expect.libsonnet',
}