diff --git a/jsonnetunit/matcher.libsonnet b/jsonnetunit/matcher.libsonnet index a31edb3..117cfe0 100644 --- a/jsonnetunit/matcher.libsonnet +++ b/jsonnetunit/matcher.libsonnet @@ -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):: diff --git a/jsonnetunit/std_matchers.libsonnet b/jsonnetunit/std_matchers.libsonnet index 59d1057..56e4969 100644 --- a/jsonnetunit/std_matchers.libsonnet +++ b/jsonnetunit/std_matchers.libsonnet @@ -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' @@ -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, }, } diff --git a/jsonnetunit/test.libsonnet b/jsonnetunit/test.libsonnet index 1448321..18d2c8b 100644 --- a/jsonnetunit/test.libsonnet +++ b/jsonnetunit/test.libsonnet @@ -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, } diff --git a/jsonnetunit/test/failure_test.jsonnet b/jsonnetunit/test/failure_test.jsonnet index 23889de..bf66a99 100644 --- a/jsonnetunit/test/failure_test.jsonnet +++ b/jsonnetunit/test/failure_test.jsonnet @@ -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, + }, + } +) diff --git a/jsonnetunit/test/std_matchers_failure_test.jsonnet b/jsonnetunit/test/std_matchers_failure_test.jsonnet index 357cd89..ad8fd8e 100644 --- a/jsonnetunit/test/std_matchers_failure_test.jsonnet +++ b/jsonnetunit/test/std_matchers_failure_test.jsonnet @@ -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 },