/* ----------------------------------------------------------------------------- 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] $TestsOnly$ define Matches { with (value, matcher) { $NoTrace$ if (delegate -> `executeMatch`) { \ TestChecker.checkNow() } } tryWith (value, matcher) { $NoTrace$ \ delegate -> `executeMatch` } value (actual, expected) { $NoTrace$ if (delegate -> `executeValue`) { \ TestChecker.checkNow() } } tryValue (actual, expected) { $NoTrace$ \ delegate -> `executeValue` } @category executeMatch<#x> (optional #x, ValueMatcher<#x>) -> (Bool) executeMatch (value, matcher) { $NoTrace$ String title <- String.builder() .append("Matches with ") .append(matcher.summary()) .build() TestReport report <- TestChecker.newReport(title: title, context: $CallTrace$) \ value `matcher.check` report return report.hasError() } @category executeValue<#x> #x requires TestCompare<#x> (optional #x, #x) -> (Bool) executeValue (actual, expected) { $NoTrace$ String title <- String.builder() .append("Matches ") .append(typename<#x>()) .build() TestReport report <- TestChecker.newReport(title: title, context: $CallTrace$) if (! `present` actual) { \ report.addError(message: "expected value but got empty") } else { \ expected.testCompare(actual: require(actual), report) } return report.hasError() } } define CheckAlways { $ReadOnlyExcept[]$ refines ValueMatcher @value Bool die @value optional Formatted message match () { return CheckAlways{ false, empty } } error (message) { return CheckAlways{ false, message } } die (message) { return CheckAlways{ true, message } } check (_, report) { if (! `present` message) { } elif (die) { fail(`require` message) } else { \ report.addError(message: `require` message) } } summary () { if (die) { return "always die" } elif (`present` message) { return "always error" } else { return "always pass" } } }