From 5339853075dec35b54356fa7bb78c9259a662d60 Mon Sep 17 00:00:00 2001 From: Brandon Weaver Date: Tue, 5 Oct 2021 22:12:12 -0700 Subject: [PATCH] Adds specs for Feature#16828 - Find Pattern Refers to #823 Adds specs for Feature#16828, the find pattern, (`[1, 2, 3] in [*pre, 2, *post]`) in Ruby 3.0+. --- language/pattern_matching_spec.rb | 57 +++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/language/pattern_matching_spec.rb b/language/pattern_matching_spec.rb index c6a3008458..7abb9b8741 100644 --- a/language/pattern_matching_spec.rb +++ b/language/pattern_matching_spec.rb @@ -18,6 +18,63 @@ RUBY end end + + describe "find pattern" do + it "captures preceding elements to the pattern" do + eval(<<~RUBY).should == [0, 1] + case [0, 1, 2, 3] + in [*pre, 2, 3] + pre + else + false + end + RUBY + end + + it "captures following elements to the pattern" do + eval(<<~RUBY).should == [2, 3] + case [0, 1, 2, 3] + in [0, 1, *post] + post + else + false + end + RUBY + end + + it "captures both predecing and following elements to the pattern" do + eval(<<~RUBY).should == [[0, 1], [3, 4]] + case [0, 1, 2, 3, 4] + in [*pre, 2, *post] + [pre, post] + else + false + end + RUBY + end + + it "can capture the entirety of the pattern" do + eval(<<~RUBY).should == [0, 1, 2, 3, 4] + case [0, 1, 2, 3, 4] + in [*everything] + everything + else + false + end + RUBY + end + + it "will match an empty Array-like structure" do + eval(<<~RUBY).should == [] + case [] + in [*everything] + everything + else + false + end + RUBY + end + end end it "extends case expression with case/in construction" do