From 64d1af8cf0796377bf11ef379691bc2e9da53b24 Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Tue, 18 Dec 2018 14:23:49 +0100 Subject: [PATCH] rspec: be_subset_of custom matcher --- spec/custom_matchers.rb | 15 +++++++++++++++ spec/integration/import_openapi_spec.rb | 19 ++++--------------- spec/spec_helper.rb | 1 + 3 files changed, 20 insertions(+), 15 deletions(-) create mode 100644 spec/custom_matchers.rb diff --git a/spec/custom_matchers.rb b/spec/custom_matchers.rb new file mode 100644 index 00000000..4c033b15 --- /dev/null +++ b/spec/custom_matchers.rb @@ -0,0 +1,15 @@ +require 'rspec/expectations' + +RSpec::Matchers.define :be_subset_of do |superset| + match do |subset| + subset.all? do |subset_elem| + superset.find do |super_set_elem| + ThreeScaleToolbox::Helper.compare_hashes(subset_elem, super_set_elem, @keys) + end + end + end + + chain :comparing_keys do |keys| + @keys = keys + end +end diff --git a/spec/integration/import_openapi_spec.rb b/spec/integration/import_openapi_spec.rb index 13258f17..9edb434c 100644 --- a/spec/integration/import_openapi_spec.rb +++ b/spec/integration/import_openapi_spec.rb @@ -22,6 +22,7 @@ let(:external_methods) do { 'methods' => [ + { 'method' => { 'id' => '0', 'friendly_name' => 'old_method', 'system_name' => 'old_method' } }, { 'method' => { 'id' => '1', 'friendly_name' => 'addPet', 'system_name' => 'addpet' } }, { 'method' => { 'id' => '2', 'friendly_name' => 'updatePet', 'system_name' => 'updatepet' } }, { 'method' => { 'id' => '3', 'friendly_name' => 'findPetsByStatus', 'system_name' => 'findpetsbystatus' } } @@ -108,11 +109,7 @@ expect(expected_methods.size).to be > 0 # test Set(service.methods) includes Set(expected_methods) # with a custom identity method for methods - expect( - ThreeScaleToolbox::Helper.array_difference(expected_methods, service.methods) do |el1, el2| - ThreeScaleToolbox::Helper.compare_hashes(el1, el2, method_keys) - end - ).to be_empty + expect(expected_methods).to be_subset_of(service.methods).comparing_keys(method_keys) end it 'mapping rules are created' do @@ -121,16 +118,8 @@ expect(expected_mapping_rules.size).to be > 0 # expect Set(service.mapping_rules) == Set(expected_mapping_rules) # with a custom identity method for mapping_rules - expect( - ThreeScaleToolbox::Helper.array_difference(expected_mapping_rules, service.mapping_rules) do |el1, el2| - ThreeScaleToolbox::Helper.compare_hashes(el1, el2, mapping_rule_keys) - end - ).to be_empty - expect( - ThreeScaleToolbox::Helper.array_difference(service.mapping_rules, expected_mapping_rules) do |el1, el2| - ThreeScaleToolbox::Helper.compare_hashes(el1, el2, mapping_rule_keys) - end - ).to be_empty + expect(expected_mapping_rules).to be_subset_of(service.mapping_rules).comparing_keys(mapping_rule_keys) + expect(service.mapping_rules).to be_subset_of(expected_mapping_rules).comparing_keys(mapping_rule_keys) end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b129c03c..e6a17254 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -17,6 +17,7 @@ require 'webmock/rspec' WebMock.disable_net_connect! require_relative 'shared_contexts' +require_relative 'custom_matchers' # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config|