/* ----------------------------------------------------------------------------- Copyright 2023 Kevin P. Barry Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ----------------------------------------------------------------------------- */ // Author: Kevin P. Barry [ta0kira@gmail.com] define MultiChecker { $ReadOnlyExcept[]$ @value TestReport report new (report) { return delegate -> #self } check (title, value, matcher) { if (delegate -> `executeMatch`) { return empty } else { return self } } tryCheck (title, value, matcher) { \ delegate -> `executeMatch` return self } @value executeMatch<#x> (Formatted title:, optional #x, ValueMatcher<#x>) -> (Bool) executeMatch (title, value, matcher) { String fullTitle <- String.builder() .append(title) .append(": ") .append(matcher.summary()) .build() TestReport newReport <- report.newSection(title: fullTitle) $Hidden[report]$ \ value `matcher.check` newReport return newReport.hasError() } } define MatcherCompose { or (left, right) { return delegate -> `OrMatcher:new` } not (matcher) { return delegate -> `NotMatcher:new` } and (left, right) { return delegate -> `AndMatcher:new` } anyOf (matchers) { return delegate -> `AnyOfMatcher:new` } allOf (matchers) { return delegate -> `AllOfMatcher:new` } } concrete OrMatcher<#x|> { refines ValueMatcher<#x> @category new<#x> (ValueMatcher<#x>, ValueMatcher<#x>) -> (ValueMatcher<#x>) } define OrMatcher { @value ValueMatcher<#x> left @value ValueMatcher<#x> right new (left, right) { return delegate -> OrMatcher<#x> } check (actual, report) { TestReport leftReport <- report.newSection(title: left.summary()) TestReport rightReport <- report.newSection(title: right.summary()) \ actual `left.check` leftReport if (!leftReport.hasError()) { return _ } $Hidden[left]$ \ actual `right.check` rightReport if (!rightReport.hasError()) { \ leftReport.discardReport() } } summary () { return String.builder() .append("either (") .append(left.summary()) .append(") or (") .append(right.summary()) .append(")") .build() } } concrete NotMatcher<#x|> { refines ValueMatcher<#x> @category new<#x> (ValueMatcher<#x>) -> (ValueMatcher<#x>) } define NotMatcher { @value ValueMatcher<#x> matcher new (matcher) { return delegate -> NotMatcher<#x> } check (actual, report) { \ delegate -> `matcher.check` if (report.hasError()) { \ report.discardReport() } else { \ report.addError(message: String.builder() .append("expected failure to match with ") .append(matcher.summary()) .build()) } } summary () { return String.builder() .append("not (") .append(matcher.summary()) .append(")") .build() } } concrete AndMatcher<#x|> { refines ValueMatcher<#x> @category new<#x> (ValueMatcher<#x>, ValueMatcher<#x>) -> (ValueMatcher<#x>) } define AndMatcher { @value ValueMatcher<#x> left @value ValueMatcher<#x> right new (left, right) { return delegate -> AndMatcher<#x> } check (actual, report) { TestReport leftReport <- report.newSection(title: left.summary()) TestReport rightReport <- report.newSection(title: right.summary()) \ actual `left.check` leftReport \ actual `right.check` rightReport } summary () { return String.builder() .append("both (") .append(left.summary()) .append(") and (") .append(right.summary()) .append(")") .build() } } concrete AnyOfMatcher<#x|> { refines ValueMatcher<#x> @category new<#x> (DefaultOrder>) -> (ValueMatcher<#x>) } define AnyOfMatcher { @value DefaultOrder> matchers new (matchers) { return delegate -> AnyOfMatcher<#x> } check (actual, report) { ValueList reports <- ValueList.new() traverse (matchers.defaultOrder() -> ValueMatcher<#x> matcher) { TestReport subReport <- report.newSection(title: matcher.summary()) $Hidden[report]$ \ actual `matcher.check` subReport if (subReport.hasError()) { \ reports.append(subReport) } else { traverse (reports.defaultOrder() -> TestReport failed) { \ failed.discardReport() } break } } } summary () { return "match any of" } } concrete AllOfMatcher<#x|> { refines ValueMatcher<#x> @category new<#x> (DefaultOrder>) -> (ValueMatcher<#x>) } define AllOfMatcher { @value DefaultOrder> matchers new (matchers) { return delegate -> AllOfMatcher<#x> } check (actual, report) { traverse (matchers.defaultOrder() -> ValueMatcher<#x> matcher) { TestReport subReport <- report.newSection(title: matcher.summary()) $Hidden[report]$ \ actual `matcher.check` subReport } } summary () { return "match all of" } }