1 # frozen_string_literal: true
2
3 require "test_helper"
4
5
6 module Umu
7
8 module Test
9
10 module Grammar
11
12 module CoreLanguage
13
14 module Expression
15
16 module Entry
17
18 module CaseExpression
19
20 class ClassTest < Minitest::Test
21 =begin
22 <case-rule-head-class> ::= "&" ID [ <pattern> ] ;
23 =end
24
25 def setup
26 @interp = Api.setup_interpreter
27 end
28
29
30 def test_class
31 script = <<-EOS
32 case x of {
33 | &Int -> @I
34 | &Float -> @F
35 }
36 EOS
37
38 value = Api.eval_expr(
39 @interp, script, x:VC.make_integer(1)
40 )
41 assert_instance_of VCA::Symbol, value
42 assert_equal :I, value.val
43
44 value = Api.eval_expr(
45 @interp, script, x:VC.make_float(1.0)
46 )
47 assert_instance_of VCA::Symbol, value
48 assert_equal :F, value.val
49 end
50
51
52 def test_with_contents
53 script = <<-EOS
54 case x of {
55 | &Some c -> c
56 | &None -> "NONE"
57 }
58 EOS
59
60 value = Api.eval_expr(
61 @interp, script, x:VC.make_some(VC.make_string("Apple"))
62 )
63 assert_instance_of VCA::String, value
64 assert_equal "Apple", value.val
65
66 value = Api.eval_expr(
67 @interp, script, x:VC.make_none
68 )
69 assert_instance_of VCA::String, value
70 assert_equal "NONE", value.val
71 end
72
73
74 def test_should_be_consistent_rule_category
75 script = <<-EOS
76 case x of {
77 | &Int -> @I
78 | Float -> @F
79 }
80 EOS
81
82 assert_raises(X::SyntaxError) do
83 Api.eval_expr(
84 @interp, script, x:VC.make_integer(1)
85 )
86 end
87 end
88
89
90 def test_should_not_be_duplicated_rule
91 script = <<-EOS
92 case x of {
93 | &Int -> @I
94 | &Int -> @F
95 }
96 EOS
97
98 =begin
99 assert_raises(X::SyntaxError) do
100 Api.eval_expr(
101 @interp, script, x:VC.make_integer(1)
102 )
103 end
104 =end
105
106 # [FIXME] Should validate rule duplication!!
107 assert (
108 Api.eval_expr(
109 @interp, script, x:VC.make_integer(1)
110 )
111 )
112 end
113 end
114
115 end # Umu::Test::Grammar::CoreLanguage::Expression::Entry::CaseExpression
116
117 end # Umu::Test::Grammar::CoreLanguage::Expression::Entry
118
119 end # Umu::Test::Grammar::CoreLanguage::Expression
120
121 end # Umu::Test::Grammar::CoreLanguage
122
123 end # Umu::Test::Grammar
124
125 end # Umu::Test
126
127 end # Umu