Skip to content

Commit da01ac9

Browse files
author
Matt Muller
committed
Simplify testing dependencies
1 parent d40f9bb commit da01ac9

8 files changed

Lines changed: 22 additions & 82 deletions

File tree

gems/smithy/spec/support/matchers/cbor_value_matcher.rb renamed to gems/smithy-client/lib/smithy-client/protocol_spec_matcher.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
require 'rspec/expectations'
44

5-
# Provides an rspec matcher for CBOR encoded values
5+
# Provides an RSpec matcher for protocol specs.
66
# rubocop:disable Metrics/BlockLength
7-
RSpec::Matchers.define :match_cbor do |expected|
7+
RSpec::Matchers.define :match_data do |expected|
88
match do |actual|
99
# identical values don't need more comparison
1010
return true if actual == expected
@@ -14,7 +14,7 @@
1414
def match_hash(actual, expected)
1515
expected.each do |key, value|
1616
expect(actual).to include(key)
17-
match_value(actual[key], value)
17+
match_data(actual[key], value)
1818
end
1919

2020
actual.each_key do |key|
@@ -24,7 +24,7 @@ def match_hash(actual, expected)
2424

2525
def match_array(actual, expected)
2626
actual.each_with_index do |value, index|
27-
match_value(value, expected[index])
27+
match_data(value, expected[index])
2828
end
2929
end
3030

@@ -35,7 +35,7 @@ def match_float(actual, expected)
3535
expect(actual).to be_within(0.0001).of(expected)
3636
end
3737

38-
def match_value(actual, expected)
38+
def match_data(actual, expected) # rubocop:disable Metrics/AbcSize
3939
case actual
4040
when Hash
4141
match_hash(actual, expected)
@@ -52,7 +52,7 @@ def match_value(actual, expected)
5252
end
5353
end
5454

55-
match_value(actual, expected)
55+
match_data(actual, expected)
5656
end
5757

5858
diffable

gems/smithy/lib/smithy/templates/client/protocol_spec.erb

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,6 @@ module <%= module_name %>
1111
let(:client_options) { { stub_responses: true } }
1212
let(:client) { Client.new(client_options) }
1313

14-
# temporary
15-
def data_to_hash(obj)
16-
case obj
17-
when Struct
18-
obj.members.each.with_object({}) do |member, hash|
19-
value = obj[member]
20-
hash[member] = data_to_hash(value) unless value.nil?
21-
end
22-
when Hash
23-
obj.each.with_object({}) do |(key, value), hash|
24-
hash[key] = data_to_hash(value)
25-
end
26-
when Array then obj.collect { |value| data_to_hash(value) }
27-
when IO, StringIO then obj.read
28-
when Smithy::Schema::Union then obj.to_h
29-
else obj
30-
end
31-
end
32-
3314
<% all_operation_tests.each do |operation_tests| -%>
3415
describe '#<%= operation_tests.name %>' do
3516
<% unless operation_tests.request_tests.empty? -%>

gems/smithy/lib/smithy/templates/client/spec_helper.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
$LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
66

77
require '<%= gem_name %>'
8+
89
require 'rspec'
10+
require 'smithy-client/protocol_spec_matcher'

gems/smithy/lib/smithy/views/client/protocol_spec.rb

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ class ProtocolSpec < View
1010
def initialize(plan)
1111
@plan = plan
1212
@model = plan.model
13-
@all_operation_tests = Model::ServiceIndex
14-
.new(@model)
15-
.operations_for(@plan.service)
16-
.map { |id, o| OperationTests.new(@model, id, o) }
17-
.reject(&:empty?)
13+
@all_operation_tests =
14+
Model::ServiceIndex
15+
.new(@model)
16+
.operations_for(@plan.service)
17+
.map { |id, o| OperationTests.new(@model, id, o) }
18+
.reject(&:empty?)
1819
super()
1920
end
2021

@@ -120,7 +121,7 @@ def additional_requires
120121
requires +=
121122
case test_case['bodyMediaType']
122123
when 'application/cbor'
123-
%w[base64 cbor_value_matcher]
124+
%w[base64]
124125
when 'application/json'
125126
%w[json]
126127
else
@@ -162,7 +163,7 @@ def body_expect
162163
case test_case['bodyMediaType']
163164
when 'application/cbor'
164165
'expect(Smithy::CBOR.decode(request.body.read)).' \
165-
"to match_cbor(Smithy::CBOR.decode(::Base64.decode64('#{test_case['body']}')))"
166+
"to match_data(Smithy::CBOR.decode(::Base64.decode64('#{test_case['body']}')))"
166167
when 'application/json'
167168
"expect(JSON.parse(request.body.read)).to eq(JSON.parse('#{test_case['body']}'))"
168169
else
@@ -196,12 +197,7 @@ def stub_body
196197
end
197198

198199
def data_expect
199-
case test_case['bodyMediaType']
200-
when 'application/cbor'
201-
"expect(output.data.to_h).to match_cbor(#{params})"
202-
else
203-
"expect(data_to_hash(output.data)).to eq(#{params})"
204-
end
200+
"expect(output.data.to_h).to match_data(#{params})"
205201
end
206202

207203
def streaming_member
@@ -229,12 +225,7 @@ def params
229225
end
230226

231227
def data_expect
232-
case test_case['bodyMediaType']
233-
when 'application/cbor'
234-
"expect(e.data.to_h).to match_cbor(#{params})"
235-
else
236-
"expect(data_to_hash(e.data)).to eq(#{params})"
237-
end
228+
"expect(e.data.to_h).to match_data(#{params})"
238229
end
239230
end
240231
end

projections/shapes/spec/shapes/protocol_spec.rb

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,5 @@ module ShapeService
88
let(:client_options) { { stub_responses: true } }
99
let(:client) { Client.new(client_options) }
1010

11-
# temporary
12-
def data_to_hash(obj)
13-
case obj
14-
when Struct
15-
obj.members.each.with_object({}) do |member, hash|
16-
value = obj[member]
17-
hash[member] = data_to_hash(value) unless value.nil?
18-
end
19-
when Hash
20-
obj.each.with_object({}) do |(key, value), hash|
21-
hash[key] = data_to_hash(value)
22-
end
23-
when Array then obj.collect { |value| data_to_hash(value) }
24-
when IO, StringIO then obj.read
25-
when Smithy::Schema::Union then obj.to_h
26-
else obj
27-
end
28-
end
29-
3011
end
3112
end

projections/shapes/spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
$LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
66

77
require 'shapes'
8+
89
require 'rspec'
10+
require 'smithy-client/protocol_spec_matcher'

projections/weather/spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
$LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
66

77
require 'weather'
8+
89
require 'rspec'
10+
require 'smithy-client/protocol_spec_matcher'

projections/weather/spec/weather/protocol_spec.rb

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,5 @@ module Weather
88
let(:client_options) { { stub_responses: true } }
99
let(:client) { Client.new(client_options) }
1010

11-
# temporary
12-
def data_to_hash(obj)
13-
case obj
14-
when Struct
15-
obj.members.each.with_object({}) do |member, hash|
16-
value = obj[member]
17-
hash[member] = data_to_hash(value) unless value.nil?
18-
end
19-
when Hash
20-
obj.each.with_object({}) do |(key, value), hash|
21-
hash[key] = data_to_hash(value)
22-
end
23-
when Array then obj.collect { |value| data_to_hash(value) }
24-
when IO, StringIO then obj.read
25-
when Smithy::Schema::Union then obj.to_h
26-
else obj
27-
end
28-
end
29-
3011
end
3112
end

0 commit comments

Comments
 (0)