/* ----------------------------------------------------------------------------- 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 TestChecker { @category Bool started <- false @category Bool inProgress <- false @category Int testCount <- 0 @category ValueList reports <- ValueList.new() start () { $NoTrace$ if (started) { fail("Testcase already started.") } started <- true inProgress <- true } finish () { $NoTrace$ \ checkTestingStarted() if (inProgress) { inProgress <- false \ checkNow() } } isFailing () { $NoTrace$ \ checkTestingStarted() traverse (reports.defaultOrder() -> TestReportTree report) { if (report.hasError()) { return true } } return false } checkNow () { $NoTrace$ \ checkTestingStarted() if (isFailing()) { \ TestHandler:failAndExit(formatErrors().formatted()) } } newReport (title, context) { $NoTrace$ \ checkTestingAllowed() ValueList contextList <- ValueList.new() traverse (context -> Formatted item) { String stringItem <- item.formatted() // TODO: Get rid of this hard coding. if (stringItem == "From testcase") { break } else { \ contextList.append(stringItem) } } String numberedTitle <- String.builder() .append("Check #") .append((testCount <- testCount+1)) .append(": ") .append(title) .build() TestReportTree report <- TestReportTree.new(numberedTitle, contextList) \ reports.append(report) return report } @category formatErrors () -> (Formatted) formatErrors () { IndentAppend output <- IndentAppend.new(String.builder()) \ output.append("TestChecker failures:") scoped { \ output.pushIndent() } cleanup { \ output.popIndent() } in traverse (reports.defaultOrder() -> TestReportTree report) { \ report.prune()&.writeTo(output)&.addNewline() } return output.build() } @category checkTestingStarted () -> () checkTestingStarted () { if (!started) { fail("Testcase was not started.") } } @category checkTestingAllowed () -> () checkTestingAllowed () { if (!started || !inProgress) { fail("Testcase not started or no longer in progress.") } } }