Skip to content

Commit

Permalink
Update matcher to take list of arguments; alias it as #strip_attribut…
Browse files Browse the repository at this point in the history
…es (#52)

* Update matcher to take list of arguments; alias it as #strip_attributes

* Only iterate over the array once
  • Loading branch information
Matt Decuir authored Mar 31, 2020
1 parent 7f2a92e commit b750faf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ end
describe User do
it { is_expected.to strip_attribute(:name).collapse_spaces }
it { is_expected.to strip_attribute :email }
it { is_expected.to strip_attributes(:name, :email) }
it { is_expected.not_to strip_attribute :password }
it { is_expected.not_to strip_attributes(:password, :encrypted_password) }
end
```

Expand All @@ -244,7 +246,9 @@ end
class UserTest < ActiveSupport::TestCase
should strip_attribute(:name).collapse_spaces
should strip_attribute :email
should strip_attributes(:name, :email)
should_not strip_attribute :password
should_not strip_attributes(:password, :encrypted_password)
end
```

Expand All @@ -257,7 +261,9 @@ describe User do
it "should strip attributes" do
must strip_attribute(:name).collapse_spaces
must strip_attribute :email
must strip_attributes(:name, :email)
wont strip_attribute :password
wont strip_attributes(:password, :encrypted_password)
end
end
```
Expand All @@ -270,7 +276,9 @@ describe User do

must { strip_attribute(:name).collapse_spaces }
must { strip_attribute :email }
must { strip_attributes(:name, :email) }
wont { strip_attribute :password }
wont { strip_attributes(:password, :encrypted_password) }
end
```

Expand Down
23 changes: 16 additions & 7 deletions lib/strip_attributes/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,35 @@ module Matchers
# RSpec Examples:
#
# it { is_expected.to strip_attribute(:first_name) }
# it { is_expected.to strip_attributes(:first_name, :last_name) }
# it { is_expected.not_to strip_attribute(:password) }
# it { is_expected.not_to strip_attributes(:password, :encrypted_password) }
#
# Minitest Matchers Examples:
#
# must { strip_attribute :first_name }
# must { strip_attributes(:first_name, :last_name) }
# wont { strip_attribute :password }
def strip_attribute(attribute)
StripAttributeMatcher.new(attribute)
# wont { strip_attributes(:password, :encrypted_password) }
def strip_attribute(*attributes)
StripAttributeMatcher.new(attributes)
end

alias_method :strip_attributes, :strip_attribute

class StripAttributeMatcher
def initialize(attribute)
@attribute = attribute
def initialize(attributes)
@attributes = attributes
@options = {}
end

def matches?(subject)
subject.send("#{@attribute}=", " string ")
subject.valid?
subject.send(@attribute) == "string" and collapse_spaces?(subject)
@attributes.all? do |attribute|
@attribute = attribute
subject.send("#{@attribute}=", " string ")
subject.valid?
subject.send(@attribute) == "string" and collapse_spaces?(subject)
end
end

def collapse_spaces
Expand Down
10 changes: 10 additions & 0 deletions test/matchers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,14 @@ class SampleMockRecord < Tableless
assert true
end
end

it "should take a list of arguments" do
must strip_attribute(:stripped1, :stripped2, :stripped3)
wont strip_attribute(:unstripped1, :unstripped2, :unstripped3)
end

it "should alias strip_attribute to strip_attributes" do
must strip_attributes(:stripped1, :stripped2, :stripped3)
wont strip_attributes(:unstripped1, :unstripped2, :unstripped3)
end
end

0 comments on commit b750faf

Please sign in to comment.