packages feed

zeolite-lang-0.22.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 map <- TypeMap.default()

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

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

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

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

  \ map.remove(keyString)

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

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

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

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

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

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

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

  TypeMap copy <- map.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(map.get(keyString),"a")
}

unittest getValues {
  TypeMap map <- 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 <- map.getAll(SortedSet<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)
}

unittest keyHashed {
  TypeKey<Int> key1 <- TypeKey<Int>.new()
  TypeKey<Int> key2 <- TypeKey<Int>.new()
  \ Testing.checkNotEquals(key1.hashed(),key2.hashed())
}

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())
}