packages feed

zeolite-lang-0.21.0.0: lib/container/test/type-map.0rt

/* -----------------------------------------------------------------------------
Copyright 2019-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 "TypeMap tests" {
  success
}

unittest basicOperations {
  TypeMap tree <- TypeMap.new()

  TypeKey<Int>    keyInt    <- TypeKey<Int>.new()
  TypeKey<String> keyString <- TypeKey<String>.new()
  TypeKey<Value>  keyValue  <- TypeKey<Value>.new()

  \ Testing.checkOptional(tree.get(keyInt),   empty)
  \ Testing.checkOptional(tree.get(keyString),empty)
  \ Testing.checkOptional(tree.get(keyValue), empty)

  \ tree.set(keyInt,1)
  \ tree.set(keyString,"a")
  \ tree.set(keyValue,Value.new())

  \ Testing.checkOptional(tree.get(keyInt),   1)
  \ Testing.checkOptional(tree.get(keyString),"a")
  \ Testing.checkOptional(tree.get(keyValue), Value.new())

  \ tree.remove(keyString)

  \ Testing.checkOptional(tree.get(keyString),empty)
}

unittest wrongReturnType {
  TypeMap tree <- TypeMap.new()

  TypeKey<String> keyString <- TypeKey<String>.new()

  // Added as Formatted, which means it cannot be retrieved as String later.
  \ tree.set<Formatted>(keyString,"a")
  \ Testing.checkOptional(tree.get<String>(keyString),empty)

  \ tree.set<String>(keyString,"a")
  \ Testing.checkOptional(tree.get<String>(keyString),"a")
}

unittest duplicate {
  TypeKey<Int>    keyInt    <- TypeKey<Int>.new()
  TypeKey<String> keyString <- TypeKey<String>.new()

  TypeMap tree <- TypeMap.new().set(keyInt,1).set(keyString,"a")

  TypeMap copy <- tree.duplicate()
  \ Testing.checkOptional(copy.get(keyInt),1)
  \ Testing.checkOptional(copy.get(keyString),"a")

  \ copy.remove(keyString)
  \ Testing.checkOptional(copy.get(keyInt),1)
  \ Testing.checkOptional(copy.get(keyString),empty)
  \ Testing.checkOptional(tree.get(keyString),"a")
}

unittest getValues {
  TypeMap tree <- TypeMap.new()
      .set(TypeKey<String>.new(),"one")
      .set(TypeKey<Int>.new(),2)
      .set(TypeKey<String>.new(),"two")
      .set(TypeKey<Int>.new(),1)

  [Container&SetReader<String>] actual <- tree.getAll(TreeSet<String>.new())

  \ Testing.checkEquals(actual.size(),2)
  \ Testing.checkTrue(actual.member("one"))
  \ Testing.checkTrue(actual.member("two"))
}

unittest keyEquals {
  TypeKey<Int>    key1 <- TypeKey<Int>.new()
  TypeKey<Int>    key2 <- TypeKey<Int>.new()
  TypeKey<String> key3 <- TypeKey<String>.new()
  \ Testing.checkEquals<TypeKey<Int>>(key1,key1)
  \ Testing.checkFalse(key1 `TypeKey<any>.equals` key2)
  \ Testing.checkFalse(key1 `TypeKey<any>.equals` key3)
}

concrete Value {
  refines Formatted
  defines Equals<Value>

  @type new () -> (Value)
}

define Value {
  new () {
    return Value{ }
  }

  formatted () {
    return "Value"
  }

  equals (_,_) {
    return true
  }
}


testcase "TypeKey formatted" {
  crash
  require "TypeKey<Int>\{[0-9]+\}"
}

unittest test {
  fail(TypeKey<Int>.new())
}