Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions jsonnetunit/matcher.libsonnet
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
satisfied: error 'must implement satisfied in concrete matcher',
positiveMessage: error 'must implement positiveMessage in concrete matcher',
negativeMessage: error 'must implement negativeMessage in concreteMatcher',
satisfied:: error 'must implement satisfied in concrete matcher',
positiveMessage:: error 'must implement positiveMessage in concrete matcher',
negativeMessage:: error 'must implement negativeMessage in concreteMatcher',

matches(expectationType):: self.satisfied == expectationType,
message(expectationType)::
Expand Down
53 changes: 36 additions & 17 deletions jsonnetunit/std_matchers.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,60 @@ local baseMatcher = import 'matcher.libsonnet';

local equalMatcher(actual, expectation) = baseMatcher {
satisfied: actual == expectation,
positiveMessage: 'Expected ' + actual + ' to be ' + expectation,
negativeMessage: 'Expected ' + actual + ' not to be ' + expectation,
positiveMessage: {
Expected: actual,
'to be': expectation,
},
negativeMessage: {
expected: actual,
'not to be': expectation,
},
};

local ltMatcher(actual, expectation) = baseMatcher {
satisfied: actual < expectation,
positiveMessage: 'Expected ' + actual + ' to be less than ' + expectation,
positiveMessage: {
Expected: actual,
'to be less than': expectation,
},
};

local leMatcher(actual, expectation) = baseMatcher {
satisfied: actual <= expectation,
positiveMessage: 'Expected ' + actual +
' to be less than or equal to ' + expectation,
positiveMessage: {
Expected: actual,
'to be less than or equal to': expectation,
},
};

local gtMatcher(actual, expectation) = baseMatcher {
satisfied: actual > expectation,
positiveMessage: 'Expected ' + actual +
' to be greater than ' + expectation,
positiveMessage: {
Expected: actual,
'to be greater than': expectation,
},
};

local geMatcher(actual, expectation) = baseMatcher {
satisfied: actual >= expectation,
positiveMessage: 'Expected ' + actual +
' to be greater than or equal to ' + expectation,
positiveMessage: {
Expected: actual,
'to be greater than or equal to': expectation,
},
};

local thatMatcher(actual, expectation) = baseMatcher {
local this = self,
satisfied: (
if std.type(expectation) == 'function' then
expectation(actual)
else
(expectation { actual: actual }).result
),
positiveMessage: 'Expected ' + actual + ' to satisfy ' + self.description,
positiveMessage: {
Expected: actual,
'to satisfy': this.description,
},
description:: (
if std.type(expectation) == 'function' then
'the function'
Expand All @@ -51,31 +70,31 @@ local thatMatcher(actual, expectation) = baseMatcher {

{
expect: {
matcher: equalMatcher,
matcher:: equalMatcher,
expectationType: true,
},
expectNot: {
matcher: equalMatcher,
matcher:: equalMatcher,
expectationType: false,
},
expectLt: {
matcher: ltMatcher,
matcher:: ltMatcher,
expectationType: true,
},
expectLe: {
matcher: leMatcher,
matcher:: leMatcher,
expectationType: true,
},
expectGt: {
matcher: gtMatcher,
matcher:: gtMatcher,
expectationType: true,
},
expectGe: {
matcher: geMatcher,
matcher:: geMatcher,
expectationType: true,
},
expectThat: {
matcher: thatMatcher,
matcher:: thatMatcher,
expectationType: true,
},
}
25 changes: 16 additions & 9 deletions jsonnetunit/test.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,26 @@ local suite(tests) = {
if !tc.matcher.matches(tc.expectationType)
];
if std.length(failures) > 0 then
local message = 'Failed %d/%d test cases:\n' % [
std.length(failures),
std.length(self.result),
] + std.join('\n', [
'%s: %s' % [tc.name, tc.matcher.message(tc.expectationType)]
for tc in failures
]);
error message
(
local message = 'Failed %d/%d test cases' % [
std.length(failures),
std.length(self.result),
];
local failureMessages =
[
{ [tc.name]: tc.matcher.message(tc.expectationType) }
for tc in failures
];
{
failures: failureMessages,
result: message,
}
)
else
'Passed %d test cases' % std.length(self.result)
),
};

{
suite: suite,
suite:: suite,
}
11 changes: 8 additions & 3 deletions jsonnetunit/test/failure_test.jsonnet
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
local test = import 'jsonnetunit/test.libsonnet';

test.suite({
testFailure: { actual: 1 + 1, expect: 3 },
})
test.suite(
{
testFailure: {
actual: 1 + 1,
expect: 3,
},
}
)
5 changes: 4 additions & 1 deletion jsonnetunit/test/std_matchers_failure_test.jsonnet
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
local test = import 'jsonnetunit/test.libsonnet';

test.suite({
testEq: { actual: 1, expect: 2 },
testEq: {
actual: 1,
expect: 2,
},
testNe: { actual: 1, expectNot: 1 },

testLt: { actual: 2, expectLt: 1 },
Expand Down