summaryrefslogtreecommitdiff
path: root/test/ripper/test_sexp.rb
diff options
context:
space:
mode:
authorktsj <ktsj@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-04-17 06:48:03 +0000
committerktsj <ktsj@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-04-17 06:48:03 +0000
commit9738f96fcfe50b2a605e350bdd40bd7a85665f54 (patch)
treea8495fa0a315ef4015f01db4d158b74987d18277 /test/ripper/test_sexp.rb
parentb077654a2c89485c086e77c337d30a11ff3781c3 (diff)
Introduce pattern matching [EXPERIMENTAL]
[ruby-core:87945] [Feature #14912] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67586 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ripper/test_sexp.rb')
-rw-r--r--test/ripper/test_sexp.rb302
1 files changed, 302 insertions, 0 deletions
diff --git a/test/ripper/test_sexp.rb b/test/ripper/test_sexp.rb
index d63464d..93e40f3 100644
--- a/test/ripper/test_sexp.rb
+++ b/test/ripper/test_sexp.rb
@@ -140,4 +140,306 @@ eot
s,
bug15670)
end
+
+ pattern_matching_data = {
+ %q{ case 0; in 0; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in, [:@int, "0", [1, 11]], [[:void_stmt]], nil]],
+
+ %q{ case 0; in 0 if a; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:if_mod, [:vcall, [:@ident, "a", [1, 16]]], [:@int, "0", [1, 11]]],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in 0 unless a; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:unless_mod, [:vcall, [:@ident, "a", [1, 20]]], [:@int, "0", [1, 11]]],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in a; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in, [:var_field, [:@ident, "a", [1, 11]]], [[:void_stmt]], nil]],
+
+ %q{ case 0; in a,; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:aryptn,
+ nil,
+ [[:var_field, [:@ident, "a", [1, 11]]]],
+ [:var_field, nil],
+ nil],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in a,b; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:aryptn,
+ nil,
+ [[:var_field, [:@ident, "a", [1, 11]]],
+ [:var_field, [:@ident, "b", [1, 13]]]],
+ nil,
+ nil],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in *a; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:aryptn, nil, nil, [:var_field, [:@ident, "a", [1, 12]]], nil],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in *a,b; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:aryptn,
+ nil,
+ nil,
+ [:var_field, [:@ident, "a", [1, 12]]],
+ [[:var_field, [:@ident, "b", [1, 14]]]]],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in *a,b,c; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:aryptn,
+ nil,
+ nil,
+ [:var_field, [:@ident, "a", [1, 12]]],
+ [[:var_field, [:@ident, "b", [1, 14]]],
+ [:var_field, [:@ident, "c", [1, 16]]]]],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in *; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in, [:aryptn, nil, nil, [:var_field, nil], nil], [[:void_stmt]], nil]],
+
+ %q{ case 0; in *,a; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:aryptn,
+ nil,
+ nil,
+ [:var_field, nil],
+ [[:var_field, [:@ident, "a", [1, 13]]]]],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in a:,**b; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:hshptn,
+ nil,
+ [[[:@label, "a:", [1, 11]], nil]],
+ [:var_field, [:@ident, "b", [1, 16]]]],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in **a; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:hshptn, nil, [], [:var_field, [:@ident, "a", [1, 13]]]],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in **; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in, [:hshptn, nil, [], nil], [[:void_stmt]], nil]],
+
+ %q{ case 0; in a: 0; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:hshptn, nil, [[[:@label, "a:", [1, 11]], [:@int, "0", [1, 14]]]], nil],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in a:; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:hshptn, nil, [[[:@label, "a:", [1, 11]], nil]], nil],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in "a": 0; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:hshptn,
+ nil,
+ [[[:string_content, [:@tstring_content, "a", [1, 12]]],
+ [:@int, "0", [1, 16]]]],
+ nil],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in "a":; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:hshptn,
+ nil,
+ [[[:string_content, [:@tstring_content, "a", [1, 12]]], nil]],
+ nil],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in a: 0, b: 0; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:hshptn,
+ nil,
+ [[[:@label, "a:", [1, 11]], [:@int, "0", [1, 14]]],
+ [[:@label, "b:", [1, 17]], [:@int, "0", [1, 20]]]],
+ nil],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in 0 => a; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:binary,
+ [:@int, "0", [1, 11]],
+ :"=>",
+ [:var_field, [:@ident, "a", [1, 16]]]],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in 0 | 1; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:binary, [:@int, "0", [1, 11]], :|, [:@int, "1", [1, 15]]],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in A(0); end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:aryptn,
+ [:var_ref, [:@const, "A", [1, 11]]],
+ [[:@int, "0", [1, 13]]],
+ nil,
+ nil],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in A(a:); end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:hshptn,
+ [:var_ref, [:@const, "A", [1, 11]]],
+ [[[:@label, "a:", [1, 13]], nil]],
+ nil],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in A(); end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:aryptn, [:var_ref, [:@const, "A", [1, 11]]], nil, nil, nil],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in A[a]; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:aryptn,
+ [:var_ref, [:@const, "A", [1, 11]]],
+ [[:var_field, [:@ident, "a", [1, 13]]]],
+ nil,
+ nil],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in A[a:]; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:hshptn,
+ [:var_ref, [:@const, "A", [1, 11]]],
+ [[[:@label, "a:", [1, 13]], nil]],
+ nil],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in A[]; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:aryptn, [:var_ref, [:@const, "A", [1, 11]]], nil, nil, nil],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in [a]; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:aryptn, nil, [[:var_field, [:@ident, "a", [1, 12]]]], nil, nil],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in []; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in, [:aryptn, nil, nil, nil, nil], [[:void_stmt]], nil]],
+
+ %q{ case 0; in {a: 0}; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in,
+ [:hshptn, nil, [[[:@label, "a:", [1, 12]], [:@int, "0", [1, 15]]]], nil],
+ [[:void_stmt]],
+ nil]],
+
+ %q{ case 0; in {}; end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in, [:hshptn, nil, nil, nil], [[:void_stmt]], nil]],
+
+ %q{ case 0; in (0); end } =>
+ [:case,
+ [:@int, "0", [1, 5]],
+ [:in, [:@int, "0", [1, 12]], [[:void_stmt]], nil]],
+
+ %q{ case 0; in a:, a:; end } =>
+ nil,
+
+ %q{ case 0; in a?:; end } =>
+ nil,
+ }
+ pattern_matching_data.each_with_index do |(src, expected), i|
+ define_method(:"test_pattern_matching_#{i}") do
+ sexp = Ripper.sexp(src.strip)
+ assert_equal expected, sexp && sexp[1][0], src
+ end
+ end
end if ripper_test