Skip to content

Instantly share code, notes, and snippets.

@jarjuk
Last active September 20, 2015 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarjuk/53bfb4dbd5f54118f1b1 to your computer and use it in GitHub Desktop.
Save jarjuk/53bfb4dbd5f54118f1b1 to your computer and use it in GitHub Desktop.

This gist contains code for blog post A Case For RPSPEC Custom Matcher

  • spec1.rb : RSPEC validating that implemented_rule includes expected_rule
  • spec1-error.rb : RSPEC falsifying that implemented_rule includes expected_rule
  • spec2.rb : RSPEC validating that at least of of the rules in array of implemented_rules include expected_rule, defines a new matcher include_rule for validation
  • spec2-error.rb : RSPEC falsifying new custom matcher include_rule
implemented_rule =
{
:to_port => -1,
:from_port => -1,
:ip_protocol => "icmp",
:ip_ranges => [ { :cidr_ip => "10.0.0.0/16" } ]
}
expected_rule = {
:to_port => -1,
:ip_protocol => "icmp",
:ip_ranges => [ { :cidr_ip => "0.0.0.0/0" } ]
}
describe "implemented_rule" do
it "#includes expected_rule" do
expect( implemented_rule ).to include( expected_rule )
end
end
implemented_rule =
{
:to_port => -1,
:from_port => -1,
:ip_protocol => "icmp",
:ip_ranges => [ { :cidr_ip => "0.0.0.0/0" } ]
}
expected_rule = {
:to_port => -1,
:ip_protocol => "icmp",
:ip_ranges => [ { :cidr_ip => "0.0.0.0/0" } ]
}
describe "implemented_rule" do
it "#includes expected_rule" do
expect( implemented_rule ).to include( expected_rule )
end
end
implemented_rules = [
{
:to_port => -1,
:from_port => -1,
:ip_protocol => "icmp",
:ip_ranges => [ { :cidr_ip => "10.0.0.0/16" } ]
},
{
:to_port => 80,
:from_port => 80,
:ip_protocol => "tcp",
:ip_ranges => [ { :cidr_ip => "10.0.0.0/16" } ]
},
{
:to_port => 443,
:from_port => 443,
:ip_protocol => "tcp",
:ip_ranges => [ { :cidr_ip => "10.0.0.0/16" } ]
}
]
expected_rule = {
:to_port => -1,
:ip_protocol => "icmp",
:ip_ranges => [ { :cidr_ip => "0.0.0.0/0" } ]
}
RSpec::Matchers.define :include_rule do |expected|
match do |actual_rules|
result = false
actual_rules.each do |rule|
begin
result = expect( rule ).to include( expected )
break if result
rescue Exception => e
end
end
return result
end
failure_message do |actual_rules|
<<-EOS
rule
#{expected}
was not included in actual rules:
#{actual_rules.join( "\n" )}
EOS
end
end
describe "implemented_rules" do
it "#includes expected_rule" do
expect( implemented_rules ).to include_rule( expected_rule )
end
end
implemented_rules = [
{
:to_port => -1,
:from_port => -1,
:ip_protocol => "icmp",
:ip_ranges => [ { :cidr_ip => "0.0.0.0/0" } ]
},
{
:to_port => 80,
:from_port => 80,
:ip_protocol => "tcp",
:ip_ranges => [ { :cidr_ip => "10.0.0.0/16" } ]
},
{
:to_port => 443,
:from_port => 443,
:ip_protocol => "tcp",
:ip_ranges => [ { :cidr_ip => "10.0.0.0/16" } ]
}
]
expected_rule = {
:to_port => -1,
:ip_protocol => "icmp",
:ip_ranges => [ { :cidr_ip => "0.0.0.0/0" } ]
}
RSpec::Matchers.define :include_rule do |expected|
match do |actual_rules|
result = false
actual_rules.each do |rule|
begin
result = expect( rule ).to include( expected )
break if result
rescue Exception => e
end
end
return result
end
failure_message do |actual_rules|
<<-EOS
rule
#{expected}
was not included in actual rules:
#{actual_rules.join( "\n" )}
EOS
end
end
describe "implemented_rules" do
it "#includes expected_rule" do
expect( implemented_rules ).to include_rule( expected_rule )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment