/* ----------------------------------------------------------------------------- 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] testcase "identifier comparisons" { success } unittest boxed { String value1 <- "value" String value2 <- "value" Identifier id1 <- `identify` value1 Identifier id2 <- `identify` value2 $Hidden[value1, value2]$ \ Testing.checkTrue((id1 < id2) ^ (id2 < id1)) \ Testing.checkTrue((id1 > id2) ^ (id2 > id1)) \ Testing.checkTrue((id1 <= id2) ^ (id2 <= id1)) \ Testing.checkTrue((id1 >= id2) ^ (id2 >= id1)) \ Testing.checkTrue(id1 <= id1) \ Testing.checkTrue(id1 >= id1) \ Testing.checkTrue(id1 == id1) \ Testing.checkFalse(id1 == id2) \ Testing.checkTrue(id1 != id2) } unittest unboxed { Int value1 <- 1 Int value2 <- 2 Int value3 <- 1 Identifier id1 <- identify(value1) Identifier id2 <- identify(value2) Identifier id3 <- identify(value3) $Hidden[value1, value2, value3]$ \ Testing.checkTrue((id1 < id2) ^ (id2 < id1)) \ Testing.checkTrue((id1 > id2) ^ (id2 > id1)) \ Testing.checkTrue((id1 <= id2) ^ (id2 <= id1)) \ Testing.checkTrue((id1 >= id2) ^ (id2 >= id1)) \ Testing.checkTrue(id1 <= id1) \ Testing.checkTrue(id1 >= id1) \ Testing.checkTrue(id1 == id1) \ Testing.checkFalse(id1 == id2) \ Testing.checkTrue(id1 != id2) \ Testing.checkTrue(id1 == id3) } unittest differentTypes { String value1 <- "value" Int value2 <- 2 Identifier id1 <- identify(value1) Identifier id2 <- identify(value2) $Hidden[value1, value2]$ \ Testing.checkTrue((id1 < id2) ^ (id2 < id1)) \ Testing.checkTrue((id1 > id2) ^ (id2 > id1)) \ Testing.checkTrue((id1 <= id2) ^ (id2 <= id1)) \ Testing.checkTrue((id1 >= id2) ^ (id2 >= id1)) \ Testing.checkFalse(id1 == id2) \ Testing.checkTrue(id1 != id2) } unittest hashing { String value1 <- "value" String value2 <- "value" Identifier id1 <- `identify` value1 Identifier id2 <- `identify` value2 $Hidden[value1, value2]$ \ Testing.checkEquals(id1.hashed(), id1.hashed()) \ Testing.checkNotEquals(id1.hashed(), id2.hashed()) } unittest meta { String value1 <- "value" Int value2 <- 2 Identifier id1 <- identify(value1) Identifier id2 <- identify(value2) $Hidden[value1, value2]$ \ Testing.checkTrue(id1 == `identify` id1) \ Testing.checkTrue(id2 == `identify` id2) \ Testing.checkFalse(`identify` id1 == `identify` id2) } unittest formatted { String value1 <- "value" String value2 <- "value" Identifier id1 <- `identify` value1 Identifier id2 <- `identify` value2 $Hidden[value1, value2]$ \ Testing.checkEquals(id1.formatted(), id1.formatted()) \ Testing.checkNotEquals(id1.formatted(), id2.formatted()) \ Testing.checkEquals((`identify` empty).formatted(), "0000000000000000") } unittest equals { String value1 <- "value" String value2 <- "value" Identifier id1 <- `identify` value1 Identifier id2 <- `identify` value2 $Hidden[value1, value2]$ \ Testing.checkTrue(id1 `Identifier.equals` id1) \ Testing.checkFalse(id1 `Identifier.equals` id2) } unittest lessThan { String value1 <- "value" String value2 <- "value" Identifier id1 <- `identify` value1 Identifier id2 <- `identify` value2 $Hidden[value1, value2]$ \ Testing.checkFalse(id1 `Identifier.lessThan` id1) \ Testing.checkTrue((id1 `Identifier.lessThan` id2) ^ (id2 `Identifier.lessThan` id1)) } unittest swap { String value1 <- "value" String value2 <- "value" Identifier id1 <- `identify` value1 Identifier id2 <- `identify` value2 $Hidden[value1, value2]$ Identifier id3 <- id1 \ Testing.checkTrue(id1 == id3) \ Testing.checkFalse(id2 == id3) id1 <-> id2 \ Testing.checkFalse(id1 == id3) \ Testing.checkTrue(id2 == id3) } unittest reduceTest { Identifier id <- `identify` "value" \ Testing.checkEquals>(reduce, Identifier>(id), id) \ Testing.checkEquals>(reduce, Identifier>(id), empty) \ Testing.checkEquals>(reduce, Identifier>(id), empty) } testcase "bad identifier conversion" { error require "Identifier" } unittest test { Identifier value <- identify("value") } testcase "identifier variable storage" { success } unittest test { \ Test.new().run() } concrete Test { @type new () -> (Test) @value run () -> () } define Test { @value Identifier valueId @value optional Identifier optionalValueId @value weak Identifier weakValueId @category Identifier categoryId <- `identify` "value" @category optional Identifier optionalCategoryId <- categoryId @category weak Identifier weakCategoryId <- categoryId new () { Identifier valueId <- `identify` "value" return Test{ valueId, valueId, valueId } } run () { // @value vs. local scoped { Identifier localId <- valueId optional Identifier optionalLocalId <- optionalValueId weak Identifier weakLocalId <- weakValueId } in { \ Testing.checkEquals>(localId, valueId) \ Testing.checkEquals>(optionalLocalId, optionalValueId) \ Testing.checkEquals>(`strong` weakLocalId, `strong` weakLocalId) } // @category vs. local scoped { Identifier localId <- categoryId optional Identifier optionalLocalId <- optionalCategoryId weak Identifier weakLocalId <- weakCategoryId } in { \ Testing.checkEquals>(localId, categoryId) \ Testing.checkEquals>(optionalLocalId, optionalCategoryId) \ Testing.checkEquals>(`strong` weakLocalId, `strong` weakLocalId) } } } testcase "disallow identify on weak" { error require "[Ww]eak" } unittest test { weak String value <- empty \ identify(value) } testcase "disallow identify with params" { error require "param" } unittest test { \ identify("value") } testcase "too many args to identify" { error require "arg" } unittest test { \ identify("value", 123) } testcase "too many returns passed to to identify" { error require "return" require "arg" } unittest test { \ identify(Helper.foo()) } concrete Helper { @type foo () -> (Int, Int) } define Helper { foo () { return 1, 2 } }