packages feed

zeolite-lang-0.24.0.0: lib/testing/test/helpers.0rx

/* -----------------------------------------------------------------------------
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 TestValue {
  $ReadOnlyExcept[]$

  @value Int number
  @value String message
  @value optional Int error

  new (number, message, error) {
    return delegate -> #self
  }

  default () {
    return new(123, "message", empty)
  }

  testCompare (actual, report) {
    \ MultiChecker.new(report)
        .tryCheck(title: "number", actual.number(), CheckValue:equals(number))
        .tryCheck(title: "message", actual.message(), CheckValue:equals(message))
        .tryCheck(title: "error", actual.error(), CheckValue:equals(error))
  }

  @value number () -> (Int)
  number () { return number }

  @value message () -> (String)
  message () { return message }

  @value error () -> (optional Int)
  error () { return error }
}

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(`Format:autoFormat` x)
          .append(" is not equal to ")
          .append(`Format:autoFormat` y)
          .build())
    }
  }
}