/* ----------------------------------------------------------------------------- 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 "optional persists" { success } unittest test { Test value <- Test.create() \ value.set() \ value.check() } concrete Test { @type create () -> (Test) @value set () -> () @value check () -> () } define Test { @value optional Test self2 create () { return Test{ empty } } set () { scoped { Test value <- create() } in self2 <- value } check () { \ require(self2) } } testcase "weak is weak" { success } unittest test { Test value <- Test.create() \ value.set() \ value.check() } concrete Test { @type create () -> (Test) @value set () -> () @value check () -> () } define Test { @value weak Test self2 create () { return Test{ empty } } set () { scoped { Test value <- create() } in self2 <- value } check () { scoped { optional Test self3 <- strong(self2) } in if (present(self3)) { fail("Failed") } } } testcase "present weak" { success } unittest test { Test value <- Test.create() \ value.check() } concrete Test { @type create () -> (Test) @value check () -> () } define Test { create () { return Test{ } } check () { weak Test value <- create() if (present(strong(value))) { // value should be nullptr here fail("Failed") } } } testcase "weak variable to weak variable" { success } unittest test { \ Test.run() } define Test { @category weak Test one <- empty run () { weak Test two <- one one <- two } } concrete Test { @type run () -> () } testcase "optional variable to weak variable" { success } unittest test { \ Test.run() } define Test { @category optional Test one <- empty run () { weak Test two <- one two <- one } } concrete Test { @type run () -> () } testcase "weak in multi assign" { success } unittest test { // value1 ensures value2 is present. Value value1, weak Value value2 <- Test.get() if (!present(strong(value2))) { fail("Failed") } } concrete Value { @type create () -> (Value) } define Value { create () { return Value{ } } } concrete Test { @type get () -> (Value, Value) } define Test { get () { Value value <- Value.create() return value, value } } testcase "weak in inline assign" { success } unittest test { Value value1 <- Value.create() weak Value value2 <- empty if (!present(strong((value2 <- value1)))) { fail("Failed") } } concrete Value { @type create () -> (Value) } define Value { create () { return Value{ } } } testcase "present required" { success } unittest test { Test value <- Test.create() if (!present(value)) { fail("Failed") } } concrete Test { @type create () -> (Test) } define Test { create () { return Test{ } } } testcase "require required" { success } unittest test { Test value <- Test.create() \ require(value).call() } concrete Test { @type create () -> (Test) @value call () -> () } define Test { create () { return Test{ } } call () { } } testcase "ValidateRefs succeeds" { success } unittest boxed { String value <- "message" weak String value2 <- value $ValidateRefs[value, value2]$ } unittest int { Int value <- 1 weak Int value2 <- value $ValidateRefs[value, value2]$ } unittest char { Char value <- 'a' weak Char value2 <- value $ValidateRefs[value, value2]$ } unittest bool { Bool value <- false weak Bool value2 <- value $ValidateRefs[value, value2]$ } unittest float { Float value <- 1.0 weak Float value2 <- value $ValidateRefs[value, value2]$ } unittest emptyVal { optional all value <- empty weak all value2 <- value $ValidateRefs[value, value2]$ } testcase "call from empty" { error require "optional" } unittest test { \ empty.foo() } testcase "inline assignment to weak" { success } unittest unboxed { weak Int value <- empty Int value2 <- (value <- 123) \ Testing.checkEquals(value2, 123) \ Testing.checkEquals(strong(value), 123) } unittest boxedNoRefs { weak String value <- empty \ Testing.checkEquals((value <- "message"), "message") \ Testing.checkEquals(strong(value), empty) } unittest boxedRefs { weak String value <- empty String original <- "message" String value2 <- (value <- original) \ Testing.checkEquals(value2, original) \ Testing.checkEquals(strong(value), original) } unittest weakToWeak { weak String value <- empty String original <- "message" weak String value2 <- original \ Testing.checkEquals(`strong` (value <- value2), original) \ Testing.checkEquals(strong(value), original) } testcase "conditional inline assignment" { success } concrete Helper { @type notCalled () -> (all) } define Helper { notCalled () { fail("should not be called") } } unittest shortCircuit { optional Int value <- 123 Int value2 <- (value <-| Helper.notCalled()) \ Testing.checkEquals(value2, 123) \ Testing.checkEquals(value, 123) } unittest unboxedToUnboxed { optional Int value <- empty Int value2 <- (value <-| 123) \ Testing.checkEquals(value2, 123) \ Testing.checkEquals(value, 123) Int value3 <- (value <-| 456) \ Testing.checkEquals(value3, 123) \ Testing.checkEquals(value, 123) } unittest boxedToBoxed { optional String value <- empty String value2 <- (value <-| "message") \ Testing.checkEquals(value2, "message") \ Testing.checkEquals(value, "message") String value3 <- (value <-| "other") \ Testing.checkEquals(value3, "message") \ Testing.checkEquals(value, "message") } testcase "optional or" { success } concrete Helper { @type notCalled () -> (all) @type getOnce () -> (optional Int) } define Helper { @category Bool used <- false notCalled () { fail("should not be called") } getOnce () { if (used) { fail("already called") } used <- true return 123 } } unittest bothBoxedEmpty { optional String value <- empty \ Testing.checkEquals(value <|| "message", "message") } unittest bothBoxedNotEmpty { optional String value <- "other" \ Testing.checkEquals(value <|| "message", "other") } unittest leftUnboxedEmpty { optional Int value <- empty \ Testing.checkEquals((value <|| "message").formatted(), "message") } unittest leftUnboxedNotEmpty { optional Int value <- 123 \ Testing.checkEquals((value <|| "message").formatted(), "123") } unittest rightUnboxedEmpty { optional String value <- empty \ Testing.checkEquals((value <|| 123).formatted(), "123") } unittest rightUnboxedNotEmpty { optional String value <- "message" \ Testing.checkEquals((value <|| 123).formatted(), "message") } unittest leftEvaluatedOnce { \ Testing.checkEquals(Helper.getOnce() <|| 456, 123) } unittest shortCircuit { optional String value <- "other" \ Testing.checkEquals(value <|| Helper.notCalled(), "other") } testcase "optional-or required return type" { error require "\[Int\|String\]" exclude "optional" } unittest test { Int value <- empty?Int <|| "message" } testcase "optional-or optional return type" { error require "modifier" exclude "\[Int\|String\]" } unittest test { Formatted value <- empty?Int <|| empty?String } testcase "optional-or not allowed with left required" { error require "optional.+Int" } unittest test { \ 123 <|| 456 } testcase "optional-or not allowed with right weak" { error require "weak Int" } unittest test { weak Int value <- empty \ empty?Int <|| value } testcase "conditional assignment" { success } concrete Helper { @type notCalled () -> (all) } define Helper { notCalled () { fail("should not be called") } } unittest shortCircuit { optional Int value <- 123 value <-| Helper.notCalled() \ Testing.checkEquals(value, 123) } unittest unboxedToUnboxed { optional Int value <- empty value <-| 123 \ Testing.checkEquals(value, 123) value <-| 456 \ Testing.checkEquals(value, 123) } unittest boxedToBoxed { optional String value <- empty value <-| "message" \ Testing.checkEquals(value, "message") value <-| "other" \ Testing.checkEquals(value, "message") } testcase "conditional inline assignment wrong return type" { error require "Formatted" require "Int" } unittest test { optional Formatted value <- empty Int value2 <- (value <-| 123) } testcase "conditional inline assignment wrong return storage" { error require "storage modifier" } unittest test { optional Int value <- 123 Int value2 <- (value <-| empty) } testcase "conditional inline assignment deferred" { error require "initialized" require "value" } unittest test { optional Int value <- defer Int value2 <- (value <-| 123) } testcase "conditional inline assignment required" { error require "optional" } unittest test { Int value <- 456 Int value2 <- (value <-| 123) } testcase "conditional inline assignment weak" { error require "optional" } unittest test { weak Int value <- 456 Int value2 <- (value <-| 123) } testcase "conditional assignment wrong return count" { error require "return" } concrete Helper { @type get () -> (Int, Int) } define Helper { get () { return 1, 2 } } unittest test { optional Int value <- 456 value <-| Helper.get() } testcase "optional value calls" { success } concrete Helper { @type notCalled () -> (all) } define Helper { notCalled () { fail("should not be called") } } unittest nonEmpty { optional Int value <- 123 \ Testing.checkEquals(value&.formatted(), "123") } unittest prefixEmpty { optional Int value <- empty \ Testing.checkEquals(`value&.formatted()&.readAt` 0, empty) } unittest prefixNonEmpty { optional Int value <- 123 \ Testing.checkEquals(`value&.formatted()&.readAt` 0, '1') } unittest chainedCalls { optional Int value <- 123 \ Testing.checkEquals(value&.formatted()&.readAt(0), '1') } unittest emptyValue { optional Int value <- empty \ Testing.checkEquals(value&.formatted(), empty) } unittest shortCircuit { optional String value <- empty \ value&.readAt(Helper.notCalled()) } testcase "conditional call return types become optional" { error require "storage modifier" } unittest test { optional Int value <- empty String value2 <- value&.formatted() } testcase "conditional call not allowed for required" { error require "[Oo]ptional" } unittest test { \ "123"&.readAt(0) } testcase "conditional call not allowed for weak" { error require "[Oo]ptional" } unittest test { weak String value <- empty \ value&.readAt(0) } testcase "conditional call with multi returns" { success } concrete Value { @type new () -> (Value) @value get () -> (optional Int, Int) } define Value { new () { return Value{ } } get () { return 1, 2 } } unittest chained { optional Value value <- Value.new() \ Testing.checkEquals(value&.get(){1}&.formatted(), "2") } unittest notCalled { optional Value value <- empty optional Int result1, optional Int result2 <- value&.get() \ Testing.checkEmpty(result1) \ Testing.checkEmpty(result2) } testcase "conditional call only happens once" { success } concrete Value { @type new () -> (optional Value) @value get () -> (Int) } define Value { @category Bool used <- false new () { if (used) { fail("already called") } used <- true return Value{ } } get () { return 1 } } unittest test { \ Testing.checkEquals(Value.new()&.get()&.formatted()&.readAt(0), '1') } testcase "conditional call bad function name" { error require "foo" } unittest test { optional String value <- empty \ value&.foo() } testcase "conditional call from empty" { error require "formatted" require "all" } unittest test { \ empty&.formatted() } testcase "conditional call from converted empty" { success } unittest test { \ Testing.checkEmpty(empty?Formatted&.formatted()) }