/* ----------------------------------------------------------------------------- Copyright 2019-2021,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 "TypeMap tests" { success TestChecker } unittest basicOperations { TypeMap map <- TypeMap.default() TypeKey keyInt <- TypeKey.new() TypeKey keyString <- TypeKey.new() TypeKey keyValue <- TypeKey.new() \ map.get(keyInt) `Matches:with` CheckValue:equals(empty?Int) \ map.get(keyString) `Matches:with` CheckValue:equals(empty?String) \ map.get(keyValue) `Matches:with` CheckValue:equals(empty?Value) \ map.set(keyInt, 1) \ map.set(keyString, "a") \ map.set(keyValue, Value.new()) \ map.get(keyInt) `Matches:with` CheckValue:equals( 1) \ map.get(keyString) `Matches:with` CheckValue:equals("a") \ map.get(keyValue) `Matches:with` CheckValue:equals( Value.new()) \ map.remove(keyString) \ map.get(keyString) `Matches:with` CheckValue:equals(empty?String) } unittest wrongReturnType { TypeMap map <- TypeMap.new() TypeKey keyString <- TypeKey.new() // Added as Formatted, which means it cannot be retrieved as String later. \ map.set(keyString, "a") \ map.get(keyString) `Matches:with` CheckValue:equals(empty?String) \ map.set(keyString, "a") \ map.get(keyString) `Matches:with` CheckValue:equals("a") } unittest duplicate { TypeKey keyInt <- TypeKey.new() TypeKey keyString <- TypeKey.new() TypeMap map <- TypeMap.new().set(keyInt, 1).set(keyString, "a") TypeMap copy <- map.duplicate() \ copy.get(keyInt) `Matches:with` CheckValue:equals(1) \ copy.get(keyString) `Matches:with` CheckValue:equals("a") \ copy.remove(keyString) \ copy.get(keyInt) `Matches:with` CheckValue:equals(1) \ copy.get(keyString) `Matches:with` CheckValue:equals(empty?String) \ map.get(keyString) `Matches:with` CheckValue:equals("a") } unittest getValues { TypeMap map <- TypeMap.new() .set(TypeKey.new(), "one") .set(TypeKey.new(), 2) .set(TypeKey.new(), "two") .set(TypeKey.new(), 1) [Container & SetReader] actual <- map.getAll(SortedSet.new()) \ actual.size() `Matches:with` CheckValue:equals(2) \ actual.member("one") `Matches:with` CheckValue:equals(true) \ actual.member("two") `Matches:with` CheckValue:equals(true) } unittest keyEquals { TypeKey key1 <- TypeKey.new() TypeKey key2 <- TypeKey.new() TypeKey key3 <- TypeKey.new() \ key1 `Matches:with` CheckValue:equals(key1) \ (key1 `TypeKey.equals` key2) `Matches:with` CheckValue:equals(false) \ (key1 `TypeKey.equals` key2) `Matches:with` CheckValue:equals(false) \ (key1 `TypeKey.equals` key3) `Matches:with` CheckValue:equals(false) } unittest keyHashed { TypeKey key1 <- TypeKey.new() TypeKey key2 <- TypeKey.new() \ key1.hashed() `Matches:with` CheckValue:notEquals(key2.hashed()) } concrete Value { refines Formatted defines Equals @type new () -> (Value) } define Value { new () { return Value{ } } formatted () { return "Value" } equals (_, _) { return true } } testcase "TypeKey formatted" { failure require "TypeKey\{[0-9a-fA-F]+\}" } unittest test { fail(TypeKey.new()) }