packages feed

zeolite-lang-0.24.0.0: tests/expr-lookup.0rt

/* -----------------------------------------------------------------------------
Copyright 2020,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 "bad macro name" {
  error
  require "UNKNOWN_STRING .+not defined"
}

unittest test {
  String value <- $ExprLookup[UNKNOWN_STRING]$
}


testcase "MODULE_PATH is absolute" {
  failure
  require "Failed condition: /.+/tests"
}

unittest test {
  fail($ExprLookup[MODULE_PATH]$)
}


testcase "MODULE_PATH from regular source" {
  failure
  require "Failed condition: /.+/tests"
}

unittest test {
  fail(ExprLookup.modulePath())
}


testcase "mismatched macro type" {
  error
  require "String.+Int"
}

unittest test {
  Int value <- $ExprLookup[MODULE_PATH]$
}


testcase "macro used inline in expressions" {
  failure
  require "Failed condition: /.+/tests is the path"
}

unittest test {
  fail($ExprLookup[MODULE_PATH]$ + " is the path")
}


testcase "constant defined in .zeolite-module" {
  success
}

unittest test {
  \ Testing.checkEquals($ExprLookup[INT_EXPR]$, 4)
  // Use it a second time to ensure that reserving the macro name doesn't
  // carry over to the next statement.
  \ $ExprLookup[INT_EXPR]$
}


testcase "constant defined in .zeolite-module from regular source" {
  success
}

unittest test {
  \ Testing.checkEquals(ExprLookup.intExpr(), 4)
}


testcase "in context defined in .zeolite-module" {
  success
}

unittest test {
  \ Test.run()
}

define Test {
  @category String macroLocalVar <- "hello"

  run () {
    \ Testing.checkEquals($ExprLookup[LOCAL_VAR]$, "hello")
  }
}

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


testcase "undefined in context defined in .zeolite-module" {
  error
  require "\.zeolite-module"
  require "macroLocalVar"
}

unittest test {
  String value <- $ExprLookup[LOCAL_VAR]$
}


testcase "in context defined in .zeolite-module from regular source" {
  success
}

unittest test {
  \ Testing.checkEquals(ExprLookup.localVar(), 99)
}


testcase "nested expression defined in .zeolite-module" {
  success
}

unittest test {
  \ Testing.checkEquals($ExprLookup[META_VAR]$, 20)
}


testcase "function called on expression" {
  success
}

unittest test {
  \ Testing.checkEquals($ExprLookup[INT_EXPR]$.formatted(), "4")
}


testcase "source context" {
  failure
  require "Failed condition: .*tests/expr-lookup\.0rt"
}

unittest test {
  Formatted context <- $SourceContext$
  fail(context)
}


testcase "recursive expression" {
  error
  exclude compiler "USES_RECURSIVE expanded at"
  require compiler "USES_RECURSIVE"
  require compiler "RECURSIVE_MACRO1 expanded at"
  require compiler "RECURSIVE_MACRO2 expanded at"
  require compiler "RECURSIVE_MACRO3 expanded at"
}

unittest test {
  String value <- $ExprLookup[USES_RECURSIVE]$
}


testcase "call trace" {
  failure
  require "Failed condition: .*tests/expr-lookup\.0rt .*testcase"
}

unittest test {
  optional Order<Formatted> trace <- $CallTrace$
  \ Testing.checkPresent(trace)
  \ Testing.checkPresent(trace&.next())
  String combined <- String.builder()
      .append(require(trace).get())
      .append(" ")
      .append(require(trace&.next()).get())
      .build()
  fail(combined)
}