packages feed

zeolite-lang-0.16.0.0: tests/member-init.0rt

/* -----------------------------------------------------------------------------
Copyright 2020-2021 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 "@type member not allowed" {
  error
  require "not allowed"
}

define Test {
  @type Bool value <- false
}

concrete Test {}


testcase "@category member from @type" {
  success
}

unittest test {
  // NOTE: Executing the test ensures that C++ compilation works.
}

concrete Test {}

define Test {
  @category Bool value <- true

  @type call () -> ()
  call () {
    \ value
  }
}


testcase "@category member from @value" {
  success
}

unittest test {
  // NOTE: Executing the test ensures that C++ compilation works.
}

concrete Test {}

define Test {
  @category Bool value <- true

  @value call () -> ()
  call () {
    \ value
  }
}


testcase "@category to @category" {
  error
  require "get"
}

concrete Test {}

define Test {
  @category Bool value <- get()

  @category get () -> (Bool)
  get () {
    return true
  }
}


testcase "@category member refers to @category member" {
  success
}

unittest test {
  \ Testing.checkEquals<?>(Test:get(),2)
}

concrete Test {
  @category get () -> (Int)
}

define Test {
  @category Int value1 <- 1
  @category Int value2 <- value1+1

  get () {
    return value2
  }
}


testcase "@category member is lazy" {
  success
}

unittest test {
  // Constructing Test ensures that the instance is actually created.
  \ typename<Test>()
}

concrete Util {
  @type doNotUse () -> (Bool)
}

define Util {
  doNotUse () {
    fail("do not use")
  }
}

concrete Test {}

define Test {
  @category Bool value <- Util.doNotUse()
}


testcase "@category member init when read" {
  crash
  require "do not use"
}

unittest test {
  \ Test.run()
}

concrete Util {
  @type doNotUse () -> (Bool)
}

define Util {
  doNotUse () {
    fail("do not use")
  }
}

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

define Test {
  @category Bool value <- Util.doNotUse()

  run () {
    Bool value2 <- value
  }
}


testcase "@category member init when assigned" {
  crash
  require "do not use"
}

unittest test {
  \ Test.run()
}

concrete Util {
  @type doNotUse () -> (Bool)
}

define Util {
  doNotUse () {
    fail("do not use")
  }
}

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

define Test {
  @category Bool value <- Util.doNotUse()

  run () {
    value <- false
  }
}


testcase "@category member init when ignored" {
  crash
  require "do not use"
}

unittest test {
  \ Test.run()
}

concrete Util {
  @type doNotUse () -> (Bool)
}

define Util {
  doNotUse () {
    fail("do not use")
  }
}

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

define Test {
  @category Bool value <- Util.doNotUse()

  run () {
    \ value
  }
}


testcase "@category member inline assignment" {
  success
}

unittest test {
  \ Test.run()
}

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

define Test {
  @category Bool value <- true

  @type call () -> (Bool)
  call () {
    return (value <- false)
  }

  run () {
    if (Test.call() || value) {
      fail("Failed")
    }
  }
}


testcase "@category init cycle" {
  crash
  require "Value1|Value2"
}

unittest test {
  \ Value1.get()
}

concrete Value1 {
  @type get () -> (Bool)
}

concrete Value2 {
  @type get () -> (Bool)
}

define Value1 {
  @category Bool value <- Value2.get()

  get () {
    return value
  }
}

define Value2 {
  @category Bool value <- Value1.get()

  get () {
    return value
  }
}


testcase "self in @category init" {
  error
  require "self"
}

concrete Test {}

define Test {
  @category Test value <- self
}


testcase "cycle in @category init" {
  error
  require "disallowed"
}

concrete Test {}

define Test {
  @category Bool value <- get()

  @category get () -> (Bool)
  get () {
    return value
  }
}


testcase "@category members read-only during others' init" {
  error
  require "value1.+read-only"
}

concrete Test {}

define Test {
  @category Int value1 <- 1
  @category Int value2 <- (value1 <- 2)
}


testcase "bad type in @category member" {
  error
  require "Foo not found"
}

concrete Test {}

define Test {
  @category optional Foo value <- empty
}


testcase "bad type in @value member" {
  error
  require "Foo not found"
}

concrete Test {}

define Test {
  @value Foo value
}


testcase "param disallowed in @category member" {
  error
  require "#x not found"
}

concrete Test<#x> {}

define Test {
  @category optional #x value <- empty
}