/* ----------------------------------------------------------------------------- Copyright 2020-2021,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 Testing { checkEquals (x, y) { $NoTrace$ Bool failed <- false if (`present` x && `present` y && !#x.equals(`require` x, `require` y)) { failed <- true } elif (`present` x != `present` y) { failed <- true } if (failed) { fail(String.builder() .append(autoFormat(x)) .append(" is not equal to ") .append(autoFormat(y)) .build()) } } checkNotEquals (x, y) { $NoTrace$ Bool failed <- false if (`present` x && `present` y && #x.equals(`require` x, `require` y)) { failed <- true } elif (! `present` x && ! `present` y) { failed <- true } if (failed) { fail(String.builder() .append(autoFormat(x)) .append(" is equal to ") .append(autoFormat(y)) .build()) } } checkEmpty (x) { $NoTrace$ if (present(x)) { fail(String.builder() .append(autoFormat(x)) .append(" is not empty") .build()) } } checkPresent (x) { $NoTrace$ if (!present(x)) { fail(String.builder() .append("expected non-empty value") .build()) } } checkTrue (x) { $NoTrace$ if (! `present` x) { fail("expected true but got empty") } if (!require(x).asBool()) { fail("expected true but got false") } } checkFalse (x) { $NoTrace$ if (! `present` x) { fail("expected false but got empty") } if (require(x).asBool()) { fail("expected false but got true") } } checkBetween (x, l, h) { $NoTrace$ if (!lessEquals(l, h)) { fail(String.builder() .append("Upper limit ") .append(autoFormat(l)) .append(" is not at or above lower limit ") .append(autoFormat(h)) .build()) } Bool failed <- false if (! `present` x) { failed <- true } elif (!lessEquals(l, `require` x) || !lessEquals(`require` x, h)) { failed <- true } if (failed) { fail(String.builder() .append(autoFormat(x)) .append(" is not between ") .append(autoFormat(l)) .append(" and ") .append(autoFormat(h)) .build()) } } checkGreaterThan (x, l) { $NoTrace$ Bool failed <- false if (! `present` x) { failed <- true } elif (!lessEquals(l, `require` x)) { failed <- true } if (failed) { fail(String.builder() .append(autoFormat(x)) .append(" is not greater than ") .append(autoFormat(l)) .build()) } } checkLessThan (x, h) { $NoTrace$ Bool failed <- false if (! `present` x) { failed <- true } elif (!lessEquals(`require` x, h)) { failed <- true } if (failed) { fail(String.builder() .append(autoFormat(x)) .append(" is not less than ") .append(autoFormat(h)) .build()) } } @type autoFormat<#x> (optional #x) -> (Formatted) autoFormat (x) { if (! `present` x) { return "empty" } scoped { optional Formatted formatted <- reduce<#x, Formatted>(x) } in if (`present` formatted) { return `require` formatted } else { return "(unknown)" } } @type lessEquals<#x> #x defines Equals<#x> #x defines LessThan<#x> (#x, #x) -> (Bool) lessEquals (x, y) { $NoTrace$ // Using !#x.lessThan(y, x) wouldn't account for NaNs. return #x.lessThan(x, y) || #x.equals(x, y) } }