Skip to content

Commit

Permalink
Resolve Pattern Matching syntax errors in old Ruby
Browse files Browse the repository at this point in the history
Resolves syntax errors introduced by pattern matching in older versions
of Ruby through the use of `eval` to prevent immediate evaluation of
invalid syntax.
  • Loading branch information
baweaver committed Jan 19, 2021
1 parent 8532bb6 commit d771e35
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 13 deletions.
4 changes: 3 additions & 1 deletion spec/lib/http/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -521,13 +521,15 @@ def on_error(request, error)
let(:client) { described_class.new }

it 'can perform a pattern match' do
value =
# Cursed hack to ignore syntax errors to test Pattern Matching.
value = eval <<~RUBY
case client
in state: :clean
true
else
false
end
RUBY

expect(value).to eq(true)
end
Expand Down
4 changes: 3 additions & 1 deletion spec/lib/http/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,15 @@

describe 'Pattern Matching' do
it 'can perform a pattern match' do
value =
# Cursed hack to ignore syntax errors to test Pattern Matching.
value = eval <<~RUBY
case connection
in status_code: 0, pending_request: false
true
else
false
end
RUBY

expect(value).to eq(true)
end
Expand Down
4 changes: 3 additions & 1 deletion spec/lib/http/content_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@

describe 'Pattern Matching' do
it 'can perform a pattern match' do
value =
# Cursed hack to ignore syntax errors to test Pattern Matching.
value = eval <<~RUBY
case described_class.new('text/plain', 'utf-8')
in mime_type: /text/
true
else
false
end
RUBY

expect(value).to eq(true)
end
Expand Down
4 changes: 3 additions & 1 deletion spec/lib/http/headers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,15 @@
end

it 'can perform a pattern match' do
value =
# Cursed hack to ignore syntax errors to test Pattern Matching.
value = eval <<~RUBY
case headers
in content_type: /json/, set_cookie: [/hoo/, *]
true
else
false
end
RUBY

expect(value).to eq(true)
end
Expand Down
4 changes: 3 additions & 1 deletion spec/lib/http/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@

describe 'Pattern Matching' do
it 'can perform a pattern match' do
value =
# Cursed hack to ignore syntax errors to test Pattern Matching.
value = eval <<~RUBY
case subject
in keep_alive_timeout: 5..10
true
else
false
end
RUBY

expect(value).to eq(true)
end
Expand Down
4 changes: 3 additions & 1 deletion spec/lib/http/request/body_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,15 @@

describe 'Pattern Matching' do
it 'can perform a pattern match' do
value =
# Cursed hack to ignore syntax errors to test Pattern Matching.
value = eval <<~RUBY
case subject
in size: 0
true
else
false
end
RUBY

expect(value).to eq(true)
end
Expand Down
4 changes: 3 additions & 1 deletion spec/lib/http/request/writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,15 @@

describe 'Pattern Matching' do
it 'can perform a pattern match' do
value =
# Cursed hack to ignore syntax errors to test Pattern Matching.
value = eval <<~RUBY
case subject
in request_header: [/GET .*/]
true
else
false
end
RUBY

expect(value).to eq(true)
end
Expand Down
4 changes: 3 additions & 1 deletion spec/lib/http/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,15 @@

describe 'Pattern Matching' do
it 'can perform a pattern match' do
value =
# Cursed hack to ignore syntax errors to test Pattern Matching.
value = eval <<~RUBY
case request
in port: 80, verb: :get, version: /^1\.\d/
true
else
false
end
RUBY

expect(value).to eq(true)
end
Expand Down
4 changes: 3 additions & 1 deletion spec/lib/http/response/body_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,15 @@

describe 'Pattern Matching' do
it 'can perform a pattern match' do
value =
# Cursed hack to ignore syntax errors to test Pattern Matching.
value = eval <<~RUBY
case subject
in contents: nil
true
else
false
end
RUBY

expect(value).to eq(true)
end
Expand Down
4 changes: 3 additions & 1 deletion spec/lib/http/response/status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,15 @@
subject { described_class.new("200.0") }

it 'can perform a pattern match' do
value =
# Cursed hack to ignore syntax errors to test Pattern Matching.
value = eval <<~RUBY
case subject
in code: 200..299
true
else
false
end
RUBY

expect(value).to eq(true)
end
Expand Down
8 changes: 6 additions & 2 deletions spec/lib/http/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,25 +190,29 @@

describe 'Pattern Matching' do
it 'can perform a pattern match' do
value =
# Cursed hack to ignore syntax errors to test Pattern Matching.
value = eval <<~RUBY
case response
in body: /Hello/, version: /^1.\d/
true
else
false
end
RUBY

expect(value).to eq(true)
end

it 'can perform an array pattern match' do
value =
# Cursed hack to ignore syntax errors to test Pattern Matching.
value = eval <<~RUBY
case response
in [200, *]
true
else
false
end
RUBY

expect(value).to eq(true)
end
Expand Down
4 changes: 3 additions & 1 deletion spec/lib/http/uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@

describe 'Pattern Matching' do
it 'can perform a pattern match' do
value =
# Cursed hack to ignore syntax errors to test Pattern Matching.
value = eval <<~RUBY
case http_uri
in host: /example/, port: 50..100, scheme: 'http'
true
else
false
end
RUBY

expect(value).to eq(true)
end
Expand Down

0 comments on commit d771e35

Please sign in to comment.