From fc1a3e89b3e4910dc51c728db387af1218bc6394 Mon Sep 17 00:00:00 2001 From: aaronp Date: Tue, 1 Apr 2008 01:17:55 +0000 Subject: [PATCH] converting switch statement to ecma git-svn-id: svn+ssh://rubyforge.org/var/svn/johnson/trunk@111 54575175-8111-4fdf-a583-07ff49f40e23 --- lib/johnson/visitors/ecma_visitor.rb | 12 ++++++++++++ test/johnson/nodes/semi_test.rb | 1 + test/johnson/nodes/switch_test.rb | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/lib/johnson/visitors/ecma_visitor.rb b/lib/johnson/visitors/ecma_visitor.rb index d953de2..a5fcddf 100644 --- a/lib/johnson/visitors/ecma_visitor.rb +++ b/lib/johnson/visitors/ecma_visitor.rb @@ -97,6 +97,18 @@ def visit_Return(o) "return#{o.value && ' '}#{o.value && o.value.accept(self)}" end + def visit_Switch(o) + "switch(#{o.left.accept(self)}) #{o.right.accept(self)}" + end + + def visit_Case(o) + "case #{o.left.accept(self)}: #{o.right.accept(self)}" + end + + def visit_Default(o) + "default: #{o.right.accept(self)}" + end + def visit_Label(o) "#{o.left.accept(self)}: #{o.right.accept(self)}" end diff --git a/test/johnson/nodes/semi_test.rb b/test/johnson/nodes/semi_test.rb index 7743feb..1ae8a07 100644 --- a/test/johnson/nodes/semi_test.rb +++ b/test/johnson/nodes/semi_test.rb @@ -3,5 +3,6 @@ class SemiTest < Johnson::NodeTestCase def test_null_semi assert_sexp([], @parser.parse(';')) + assert_ecma('', @parser.parse(';')) end end diff --git a/test/johnson/nodes/switch_test.rb b/test/johnson/nodes/switch_test.rb index 3d2d83c..25aa9c6 100644 --- a/test/johnson/nodes/switch_test.rb +++ b/test/johnson/nodes/switch_test.rb @@ -5,6 +5,9 @@ def test_empty_switch assert_sexp([[:switch, [:name, "o"], []]], @parser.parse('switch(o) { }') ) + assert_ecma('switch(o) { }', + @parser.parse('switch(o) { }') + ) end def test_switch_with_body @@ -14,6 +17,15 @@ def test_switch_with_body ] ]], @parser.parse('switch(o) { case j: foo; }') ) + assert_ecma("switch(o) {\n case j: {\n foo;\n}\n}", + @parser.parse('switch(o) { case j: foo; }') + ) + end + + def test_switch_empty_case + assert_ecma("switch(o) {\n case j: { }\n}", + @parser.parse('switch(o) { case j: }') + ) end def test_switch_with_body_2_case @@ -30,8 +42,14 @@ def test_switch_with_default assert_sexp([[:switch, [:name, "o"], [[:default, nil, [[:name, "bar"]]]]]], @parser.parse('switch(o) { default: bar; }') ) + assert_ecma("switch(o) {\n default: {\n bar;\n}\n}", + @parser.parse('switch(o) { default: bar; }') + ) assert_sexp([[:switch, [:name, "o"], [[:default, nil, []]]]], @parser.parse('switch(o) { default: }') ) + assert_ecma("switch(o) {\n default: { }\n}", + @parser.parse('switch(o) { default: }') + ) end end