From 221070e76760b70ab8e2d8bfa112133b5f0d04c0 Mon Sep 17 00:00:00 2001 From: Philipp Panzer <philipp.panzer@fau.de> Date: Tue, 5 Mar 2019 17:09:00 +0100 Subject: [PATCH] add testcase for unions and a builtin type for union values --- lib/types.rb | 20 ++++++++++++++++++++ tests/test.rb | 3 ++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/types.rb b/lib/types.rb index 2b2a86a..28a6641 100644 --- a/lib/types.rb +++ b/lib/types.rb @@ -325,6 +325,26 @@ module Dhallish end end + class Union + attr_accessor :init_label + attr_accessor :init_val + attr_accessor :type + + def initialize(init_label, init_val, type) + @init_label = init_label + @init_val = init_val + @type = type + end + + def select(label) + if label == @init_label + @init_val + else + nil + end + end + end + # To be used as Dhallish::Value.val for ruby-defined functions class BuiltinFunction attr_accessor :block diff --git a/tests/test.rb b/tests/test.rb index 9dd57a0..28df652 100755 --- a/tests/test.rb +++ b/tests/test.rb @@ -34,7 +34,8 @@ positive_tests = { "identity with type parameter": ["let f = \\(t : Type) -> \\(x : t) -> x in (f : forall(t : Type) -> t -> t) Natural 2", 2], "function with two type parameters": ["let f = \\(t : Type) -> \\(u : Type) -> \\(x : t) -> \\(y : u) -> x in (f : forall(t : Type) -> forall(u : Type) -> t -> u -> t) Natural Integer 1 -1 ", 1 ], "fallback for file import": ["let x = ./foobarfoobar.dhall as Text ? 1 in x", 1], - "more type annotation": [ "let f = \\(a: Type) -> \\(l: List a) -> \\(x: a) -> x in (f: forall(b: Type) -> forall(l: List b) -> b -> b) Natural [1, 2, 3] 5", 5 ] + "more type annotation": [ "let f = \\(a: Type) -> \\(l: List a) -> \\(x: a) -> x in (f: forall(b: Type) -> forall(l: List b) -> b -> b) Natural [1, 2, 3] 5", 5 ], + "select from union": [ "let union = <a : Natural | b : Text>.a 17 in union.a : Optional Natural", 17 ] } negative_tests = { -- GitLab