packages feed

zeolite-lang-0.24.0.0: tests/simple.0rt

/* -----------------------------------------------------------------------------
Copyright 2020,2022-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]

testcase "basic compiles test" {
  compiles
  require compiler "unreachable"
}

unittest test {
  return _
  \ empty
}


testcase "basic error test" {
  error
  require compiler "category.+undefined"
  require compiler "type.+undefined"
  exclude stderr "."
  exclude stdout "."
}

concrete Test {
  @type execute () -> ()
}

define Test {
  execute () {
    \ undefined()
  }
}


testcase "basic crash test" {
  failure
  require stderr "testcase:"
  require stderr "failure message!!!"
  exclude stdout "failure message!!!"
}

unittest test {
  // NOTE: "!!!" is important here because it also tests mixing of escaped and
  // unescaped characters when handling string literals.
  fail("failure message!!!")
}


testcase "basic success test" {
  success
}

unittest test {
  return _
}


testcase "minimal linking works properly" {
  success
}

unittest test {
  \ empty
}


testcase "attempt to define outside of this module" {
  error
  require "String"
  require "module"
  exclude "procedure definition"
}

// The declaration for String is in scope, but it lives outside of this module.
define String { }


testcase "attempt to define interface" {
  error
  require "Type"
  require "concrete"
}

@value interface Type { }

define Type { }


testcase "no deadlock with recursive type" {
  success
}

unittest test {
  \ Type<Type<Type<Int>>>.call()
}

concrete Type<#x> {
  @type call () -> ()
}

define Type {
  call () { }
}


testcase "ModuleOnly is visible to tests" {
  success
}

unittest test {
  optional ModuleOnly value <- empty
}


testcase "custom timeout works as expected" {
  failure
  require "signal 14"
  timeout 1
}

unittest test {
  while (true) { }
}


testcase "args escaped properly" {
  success
  // If the compiler uses quotes without escaping individual characters then
  // main will exit with an error without executing the test.
  args "\" }; exit(1); const char fake[] = { \""
}

unittest test { }


testcase "inline assignment return type" {
  success
}

unittest unboxedToUnboxed {
  Int value <- 456
  Int value2 <- (value <- 123)
  \ Testing.checkEquals(value2, 123)
  \ Testing.checkEquals(value, 123)
}

unittest unboxedToBoxed {
  optional Formatted value <- empty
  Int value2 <- (value <- 123)
  \ Testing.checkEquals(value2, 123)
  \ Testing.checkPresent(value)
  \ Testing.checkEquals(require(value).formatted(), "123")
}

unittest boxedToBoxed {
  optional Formatted value <- empty
  String value2 <- (value <- "message")
  \ Testing.checkEquals(value2, "message")
  \ Testing.checkPresent(value)
  \ Testing.checkEquals(require(value).formatted(), "message")
}


testcase "inline assignment wrong return type" {
  error
  require "String"
  require "Int"
}

unittest test {
  optional Formatted value <- empty
  String value2 <- (value <- 123)
}


testcase "custom testcase is started and finished" {
  failure Helper
  require "started"
  exclude "not started"
}

concrete Helper {
  defines Testcase
  @type started () -> (Bool)
}

define Helper {
  @category Bool started <- false

  started () {
    return started
  }

  start () {
    started <- true
  }

  finish () {
    if (started) {
      fail("started")
    } else {
      fail("not started")
    }
  }
}

unittest test {
  \ Testing.checkTrue(Helper.started())
}


testcase "custom testcase is finished on exit" {
  failure Helper
  require "started"
  exclude "not started"
}

concrete Helper {
  defines Testcase
}

define Helper {
  @category Bool started <- false

  start () {
    started <- true
  }

  finish () {
    if (started) {
      fail("started")
    } else {
      fail("not started")
    }
  }
}

unittest test {
  exit(0)
}


testcase "custom testcase is finished at most once with explicit exit" {
  success Helper
}

concrete Helper {
  defines Testcase
  @type started () -> (Bool)
}

define Helper {
  @category Bool started <- false
  @category Bool called <- false

  started () {
    return started
  }

  start () {
    started <- true
  }

  finish () {
    if (called) {
      fail("called twice")
    }
    called <- true
  }
}

unittest test {
  \ Testing.checkTrue(Helper.started())
  exit(0)
}