summaryrefslogtreecommitdiffhomepage
path: root/test/t/syntax.rb
diff options
context:
space:
mode:
authorTomoyuki Sahara <[email protected]>2014-01-06 10:10:37 +0900
committerTomoyuki Sahara <[email protected]>2014-01-06 10:10:37 +0900
commit37475f4577886ad85c87967c94c4bcfc6f6949d5 (patch)
treed8fa92c830df2741df0e1106f12e78a8b056f70f /test/t/syntax.rb
parentf5cca078e3193f1fff57545defa36d1f8ee8862f (diff)
downloadmruby-37475f4577886ad85c87967c94c4bcfc6f6949d5.tar.gz
mruby-37475f4577886ad85c87967c94c4bcfc6f6949d5.zip
tests for "case" expression.
Diffstat (limited to 'test/t/syntax.rb')
-rw-r--r--test/t/syntax.rb92
1 files changed, 92 insertions, 0 deletions
diff --git a/test/t/syntax.rb b/test/t/syntax.rb
index f1fc80216..13cd1198e 100644
--- a/test/t/syntax.rb
+++ b/test/t/syntax.rb
@@ -42,6 +42,98 @@ assert('Abbreviated variable assignment', '11.4.2.3.2') do
assert_equal 3, c
end
+assert('case expression', '11.5.2.2.4') do
+ # case-expression-with-expression, one when-clause
+ x = 0
+ case "a"
+ when "a"
+ x = 1
+ end
+ assert_equal 1, x
+
+ # case-expression-with-expression, multiple when-clauses
+ x = 0
+ case "b"
+ when "a"
+ x = 1
+ when "b"
+ x = 2
+ end
+ assert_equal 2, x
+
+ # no matching when-clause
+ x = 0
+ case "c"
+ when "a"
+ x = 1
+ when "b"
+ x = 2
+ end
+ assert_equal 0, x
+
+ # case-expression-with-expression, one when-clause and one else-clause
+ a = 0
+ case "c"
+ when "a"
+ x = 1
+ else
+ x = 3
+ end
+ assert_equal 3, x
+
+ # case-expression-without-expression, one when-clause
+ x = 0
+ case
+ when true
+ x = 1
+ end
+ assert_equal 1, x
+
+ # case-expression-without-expression, multiple when-clauses
+ x = 0
+ case
+ when 0 == 1
+ x = 1
+ when 1 == 1
+ x = 2
+ end
+ assert_equal 2, x
+
+ # case-expression-without-expression, one when-clause and one else-clause
+ x = 0
+ case
+ when 0 == 1
+ x = 1
+ else
+ x = 3
+ end
+ assert_equal 3, x
+
+ # multiple when-arguments
+ x = 0
+ case 4
+ when 1, 3, 5
+ x = 1
+ when 2, 4, 6
+ x = 2
+ end
+ assert_equal 2, x
+
+ # when-argument with splatting argument
+ x = :integer
+ odds = [ 1, 3, 5, 7, 9 ]
+ evens = [ 2, 4, 6, 8 ]
+ case 5
+ when *odds
+ x = :odd
+ when *evens
+ x = :even
+ end
+ assert_equal :odd, x
+
+ true
+end
+
assert('Nested const reference') do
module Syntax4Const
CONST1 = "hello world"