From 366767cb72180100cb33a23206df25a50e31f7cf Mon Sep 17 00:00:00 2001 From: Brandon Weaver Date: Tue, 19 Jan 2021 01:00:37 -0600 Subject: [PATCH] Resolve Pattern Matching syntax errors in old Ruby Resolves syntax errors introduced by pattern matching in older versions of Ruby through the use of `eval` to prevent immediate evaluation of invalid syntax. --- spec/lib/http/client_spec.rb | 4 +++- spec/lib/http/connection_spec.rb | 4 +++- spec/lib/http/content_type_spec.rb | 4 +++- spec/lib/http/headers_spec.rb | 4 +++- spec/lib/http/options_spec.rb | 4 +++- spec/lib/http/request/body_spec.rb | 4 +++- spec/lib/http/request/writer_spec.rb | 4 +++- spec/lib/http/request_spec.rb | 6 ++++-- spec/lib/http/response/body_spec.rb | 4 +++- spec/lib/http/response/status_spec.rb | 4 +++- spec/lib/http/response_spec.rb | 10 +++++++--- spec/lib/http/uri_spec.rb | 4 +++- 12 files changed, 41 insertions(+), 15 deletions(-) diff --git a/spec/lib/http/client_spec.rb b/spec/lib/http/client_spec.rb index 893c0a40..f696f98e 100644 --- a/spec/lib/http/client_spec.rb +++ b/spec/lib/http/client_spec.rb @@ -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 diff --git a/spec/lib/http/connection_spec.rb b/spec/lib/http/connection_spec.rb index 974312e4..7bc9839e 100644 --- a/spec/lib/http/connection_spec.rb +++ b/spec/lib/http/connection_spec.rb @@ -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 diff --git a/spec/lib/http/content_type_spec.rb b/spec/lib/http/content_type_spec.rb index 368e8c9f..82f3012c 100644 --- a/spec/lib/http/content_type_spec.rb +++ b/spec/lib/http/content_type_spec.rb @@ -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 diff --git a/spec/lib/http/headers_spec.rb b/spec/lib/http/headers_spec.rb index 4c705c48..cc583a57 100644 --- a/spec/lib/http/headers_spec.rb +++ b/spec/lib/http/headers_spec.rb @@ -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 diff --git a/spec/lib/http/options_spec.rb b/spec/lib/http/options_spec.rb index 0f4fa00b..15541754 100644 --- a/spec/lib/http/options_spec.rb +++ b/spec/lib/http/options_spec.rb @@ -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 diff --git a/spec/lib/http/request/body_spec.rb b/spec/lib/http/request/body_spec.rb index f04105ae..77b55928 100644 --- a/spec/lib/http/request/body_spec.rb +++ b/spec/lib/http/request/body_spec.rb @@ -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 diff --git a/spec/lib/http/request/writer_spec.rb b/spec/lib/http/request/writer_spec.rb index c90ac079..a162d2f9 100644 --- a/spec/lib/http/request/writer_spec.rb +++ b/spec/lib/http/request/writer_spec.rb @@ -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 diff --git a/spec/lib/http/request_spec.rb b/spec/lib/http/request_spec.rb index 5b87800f..522777cf 100644 --- a/spec/lib/http/request_spec.rb +++ b/spec/lib/http/request_spec.rb @@ -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/ + in port: 80, verb: :get, version: /^1\\.\\d/ true else false end + RUBY expect(value).to eq(true) end diff --git a/spec/lib/http/response/body_spec.rb b/spec/lib/http/response/body_spec.rb index a17a3983..edc46a1a 100644 --- a/spec/lib/http/response/body_spec.rb +++ b/spec/lib/http/response/body_spec.rb @@ -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 diff --git a/spec/lib/http/response/status_spec.rb b/spec/lib/http/response/status_spec.rb index 04d8a064..fc145aed 100644 --- a/spec/lib/http/response/status_spec.rb +++ b/spec/lib/http/response/status_spec.rb @@ -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 diff --git a/spec/lib/http/response_spec.rb b/spec/lib/http/response_spec.rb index fb03010b..0391d2fd 100644 --- a/spec/lib/http/response_spec.rb +++ b/spec/lib/http/response_spec.rb @@ -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/ + 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 diff --git a/spec/lib/http/uri_spec.rb b/spec/lib/http/uri_spec.rb index d2eeb41c..55120073 100644 --- a/spec/lib/http/uri_spec.rb +++ b/spec/lib/http/uri_spec.rb @@ -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