Skip to content

Commit 6526d0e

Browse files
authored
Add more docs (#2)
1 parent e7a4392 commit 6526d0e

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,124 @@ Just `git clone`.
3939
$ git clone https://github.com/yugui/jsonnetunit.git
4040
```
4141

42+
## Getting started
43+
### How to write tests
44+
1. Create a test file
45+
46+
Test files must be `.jsonnet` files which manifestize a result of `test.suite` function.
47+
48+
```jsonnet
49+
local test = import "path/to/jsonnetunit/test.jsonnet";
50+
test.suite({
51+
})
52+
```
53+
2. Add test cases
54+
55+
`test.suite` function takes an object which contains fields prefixed with `test`.
56+
You can add arbitrary number of such fields. `test.suite` does not directly use any other fields.
57+
58+
Individual test fields must have at least two fields:
59+
* `actual` field: There must be a field named `actual`. This is the actual value to be verified.
60+
* expectation field: There must be another field which describes an expectation. This expectation is used to verify the `actual` value.
61+
62+
```jsonnet
63+
test.suite({
64+
testFoo: {
65+
actual: std.length('foo'),
66+
expect: 3,
67+
},
68+
})
69+
```
70+
71+
72+
The interpretation of the expectation depends on the name of the expectation field. The name `expect` in the example means that it expects that `actual` field is equal to the given value.
73+
74+
### Simple expectations
75+
76+
Expectation Field Name | Description | Example
77+
-----------------------|---------------------------|---------------------
78+
`expect` | value equality | `{actual: 1+1, expect: 2}`
79+
`expectNot` | value inequality | `{actual: 1+1, expect: 3}`
80+
`expectLt` | less than | `{actual: 1+1, expect: 3}`
81+
`expectLe` | less than or equal to | `{actual: 1+1, expect: 3}`
82+
`expectGt` | greater than | `{actual: 1+1, expect: 1}`
83+
`expectGe` | greater than or equal to | `{actual: 1+1, expect: 1}`
84+
85+
### expectThat
86+
You can describe an abitrary expectation with `expectThat`.
87+
This expectation field takes a unary function or an object.
88+
89+
The function must take an actual value and it must return if the value satisfies your expectation in a boolean value.
90+
91+
```jsonnet
92+
{
93+
actual: ultimateAnswerToLifeTheUniverseAndEverything(),
94+
expectThat: function(x) x == 42,
95+
}
96+
```
97+
98+
When you pass an object, the object must have two fields `actual` and `result`.
99+
The first field `actual` is overridden with the `actual` value of the test case on evaluation.
100+
The second field `result` must be an boolean which describes whether `actual` field satisfies your expectation.
101+
In this case, you can optionally specifies a custom `description` of the expectation. This is used as a part of error message when the test case fails.
102+
103+
```jsonnet
104+
{
105+
actual: ultimateAnswerToLifeTheUniverseAndEverything(),
106+
expectThat: {
107+
actual: error "to be overridden",
108+
result: self.actual == 42,
109+
description: "Expect %d to be equal to the known value" % self.actual,
110+
},
111+
}
112+
```
113+
114+
### Custom expectation matcher
115+
You can also define your own expectation matcher.
116+
117+
1. Define a binary function which takes `actual` and `expected` values. This function must return an object
118+
derived from `matcher.jsonnet` and must have the following three fields.
119+
* `satisfied`: (boolean) Returns if `self.actual` satisfies your expectation
120+
* `positiveMessage`: (string) Returns an error message to be returned when `self.actual` does not satisfies your expectation.
121+
* `negativeMessage`: (string) Returns an error message to be returned when `self.actual` does not satisfies the negation of the expectation.
122+
123+
e.g.
124+
```jsonnet
125+
local setMatcher(actual, expected) = import "path/to/matcher.jsonnet" {
126+
satisfied: std.set(actual) == std.set(expected),
127+
positiveMessage: "Expected " + actual + " to be equal to " + expected + " as a set",
128+
negativeMessage: "Expected " + actual + " not to be equal to " + expected + " as a set",
129+
};
130+
```
131+
2. Define your expectation field name in the `matchers` field of the test suite.
132+
133+
e.g.
134+
```jsonnet
135+
test.suite({
136+
testEq: {
137+
actual: [6, 7, 2, 3, 7],
138+
expectSetEq: [2, 3, 6, 7],
139+
},
140+
testNe: {
141+
actual: [6, 7, 2, 3, 7],
142+
expectSetNe: [1, 2, 3, 4, 5],
143+
}
144+
}) {
145+
matchers+: {
146+
// Define a new expectation field name "expectSetEq" for set equality
147+
expectSetEq: {
148+
matcher: setMatcher,
149+
expectationType: true,
150+
},
151+
// Define a new expectation field name "expectSetNe" for set inequiality
152+
expectSetNe: {
153+
matcher: setMatcher,
154+
expectationType: false,
155+
},
156+
},
157+
}
158+
```
159+
42160
# Copyright
43161
Copyright 2016 Yuki Yugui Sonoda
44162

0 commit comments

Comments
 (0)