Skip to content

Commit a8a50d0

Browse files
authored
Merge pull request #82 from rmm5t/rmm/matcher-custom-value
Add ability to specify a custom value in matcher
2 parents 4cc8923 + ea4ff12 commit a8a50d0

5 files changed

Lines changed: 95 additions & 30 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ describe User do
236236
it { is_expected.to strip_attribute(:name).replace_newlines }
237237
it { is_expected.to strip_attribute :email }
238238
it { is_expected.to strip_attributes(:name, :email) }
239+
it { is_expected.to strip_attributes(:ticker).using("AAPL") }
239240
it { is_expected.not_to strip_attribute :password }
240241
it { is_expected.not_to strip_attributes(:password, :encrypted_password) }
241242
end
@@ -249,6 +250,7 @@ class UserTest < ActiveSupport::TestCase
249250
should strip_attribute(:name).replace_newlines
250251
should strip_attribute :email
251252
should strip_attributes(:name, :email)
253+
should strip_attributes(:ticker).using("AAPL")
252254
should_not strip_attribute :password
253255
should_not strip_attributes(:password, :encrypted_password)
254256
end
@@ -265,6 +267,7 @@ describe User do
265267
must strip_attribute(:name).replace_newlines
266268
must strip_attribute :email
267269
must strip_attributes(:name, :email)
270+
must strip_attributes(:ticker).using("AAPL")
268271
wont strip_attribute :password
269272
wont strip_attributes(:password, :encrypted_password)
270273
end
@@ -281,6 +284,7 @@ describe User do
281284
must { strip_attribute(:name).replace_newlines }
282285
must { strip_attribute :email }
283286
must { strip_attributes(:name, :email) }
287+
must { strip_attributes(:ticker).using("AAPL") }
284288
wont { strip_attribute :password }
285289
wont { strip_attributes(:password, :encrypted_password) }
286290
end

bin/console

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env ruby
2+
3+
require "bundler/setup"
4+
require "strip_attributes"
5+
6+
# You can add fixtures and/or initialization code here to make experimenting
7+
# with your gem easier. You can also use a different console, if you like.
8+
9+
# (If you use this, don't forget to add pry to your Gemfile!)
10+
# require "pry"
11+
# Pry.start
12+
13+
require "irb"
14+
IRB.start(__FILE__)

bin/rake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
#
5+
# This file was generated by Bundler.
6+
#
7+
# The application 'rake' is installed as part of a gem, and
8+
# this file is here to facilitate running it.
9+
#
10+
11+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
12+
13+
require "rubygems"
14+
require "bundler/setup"
15+
16+
load Gem.bin_path("rake", "rake")

lib/strip_attributes/matchers.rb

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module StripAttributes
22
module Matchers
3-
43
# Whitespace is stripped from the beginning and end of the attribute
54
#
65
# RSpec Examples:
@@ -20,7 +19,7 @@ def strip_attribute(*attributes)
2019
StripAttributeMatcher.new(attributes)
2120
end
2221

23-
alias_method :strip_attributes, :strip_attribute
22+
alias strip_attributes strip_attribute
2423

2524
class StripAttributeMatcher
2625
def initialize(attributes)
@@ -31,12 +30,17 @@ def initialize(attributes)
3130
def matches?(subject)
3231
@attributes.all? do |attribute|
3332
@attribute = attribute
34-
subject.send("#{@attribute}=", " string ")
33+
subject.send("#{@attribute}=", " #{value} ")
3534
subject.valid?
36-
subject.send(@attribute) == "string" and collapse_spaces?(subject) and replace_newlines?(subject)
35+
subject.send(@attribute) == value and collapse_spaces?(subject) and replace_newlines?(subject)
3736
end
3837
end
3938

39+
def using(value)
40+
@options[:value] = value
41+
self
42+
end
43+
4044
def collapse_spaces
4145
@options[:collapse_spaces] = true
4246
self
@@ -47,44 +51,51 @@ def replace_newlines
4751
self
4852
end
4953

50-
def failure_message # RSpec 3.x
54+
# RSpec 3.x
55+
def failure_message
5156
"Expected whitespace to be #{expectation} from ##{@attribute}, but it was not"
5257
end
53-
alias_method :failure_message_for_should, :failure_message # RSpec 1.2, 2.x, and minitest-matchers
58+
alias failure_message_for_should failure_message # RSpec 1.2, 2.x, and minitest-matchers
5459

55-
def failure_message_when_negated # RSpec 3.x
60+
# RSpec 3.x
61+
def failure_message_when_negated
5662
"Expected whitespace to remain on ##{@attribute}, but it was #{expectation}"
5763
end
58-
alias_method :failure_message_for_should_not, :failure_message_when_negated # RSpec 1.2, 2.x, and minitest-matchers
59-
alias_method :negative_failure_message, :failure_message_when_negated # RSpec 1.1
64+
alias failure_message_for_should_not failure_message_when_negated # RSpec 1.2, 2.x, and minitest-matchers
65+
alias negative_failure_message failure_message_when_negated # RSpec 1.1
6066

6167
def description
62-
"#{expectation(false)} whitespace from #{@attributes.map {|el| "##{el}" }.to_sentence}"
68+
attrs = @attributes.map { |attr| "##{attr}" }.to_sentence
69+
"#{expectation(past: false)} whitespace from #{attrs}"
6370
end
6471

6572
private
6673

74+
def value
75+
@options[:value] || "string"
76+
end
77+
6778
def collapse_spaces?(subject)
68-
return true if !@options[:collapse_spaces]
79+
return true unless @options[:collapse_spaces]
6980

70-
subject.send("#{@attribute}=", " string string ")
81+
subject.send("#{@attribute}=", " #{value} #{value} ")
7182
subject.valid?
72-
subject.send(@attribute) == "string string"
83+
subject.send(@attribute) == "#{value} #{value}"
7384
end
7485

75-
def expectation(past = true)
86+
def expectation(past: true)
7687
expectation = past ? "stripped" : "strip"
7788
expectation += past ? " and collapsed" : " and collapse" if @options[:collapse_spaces]
78-
expectation += past ? " and replaced" : " and replace" if !@options[:replace_newlines]
89+
expectation += past ? " and replaced" : " and replace" unless @options[:replace_newlines]
7990
expectation
8091
end
8192

8293
def replace_newlines?(subject)
83-
return true if !@options[:replace_newlines]
94+
return true unless @options[:replace_newlines]
8495

85-
subject.send("#{@attribute}=", "string\nstring")
96+
subject.send("#{@attribute}=", "#{value}\n#{value}")
8697
subject.valid?
87-
subject.send(@attribute) == "string string"
98+
subject.send(@attribute) == "#{value} #{value}"
8899
end
89100
end
90101
end

test/matchers_test.rb

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ class SampleMockRecord < Tableless
2222
attribute :replaceable
2323
attribute :unreplaceable
2424
strip_attributes only: [:replaceable], replace_newlines: true
25+
26+
attribute :upcased
27+
strip_attributes only: [:upcased]
28+
29+
def upcased=(value)
30+
super((value.upcase if value))
31+
end
2532
end
2633

2734
describe SampleMockRecord do
@@ -57,19 +64,32 @@ class SampleMockRecord < Tableless
5764
it "should not replace on other attributes" do
5865
wont strip_attribute(:unreplaceable).replace_newlines
5966
end
67+
68+
it "should not strip normalized attributes missing a custom value" do
69+
wont strip_attribute(:upcased)
70+
wont strip_attribute(:upcased).using("fOO")
71+
end
72+
73+
it "should strip normalized attributes using a custom value" do
74+
must strip_attribute(:upcased).using("BIG")
75+
end
6076
else
61-
must { strip_attribute :stripped1 }
62-
must { strip_attribute :stripped2 }
63-
must { strip_attribute :stripped3 }
64-
wont { strip_attribute :unstripped1 }
65-
wont { strip_attribute :unstripped2 }
66-
wont { strip_attribute :unstripped3 }
67-
68-
must { strip_attribute(:collapsed).collapse_spaces }
69-
wont { strip_attribute(:uncollapsed).collapse_spaces }
70-
71-
must { strip_attribute(:replaceable).replace_newlines }
72-
wont { strip_attribute(:unreplaceable).replace_newlines }
77+
must { strip_attribute :stripped1 }
78+
must { strip_attribute :stripped2 }
79+
must { strip_attribute :stripped3 }
80+
wont { strip_attribute :unstripped1 }
81+
wont { strip_attribute :unstripped2 }
82+
wont { strip_attribute :unstripped3 }
83+
84+
must { strip_attribute(:collapsed).collapse_spaces }
85+
wont { strip_attribute(:uncollapsed).collapse_spaces }
86+
87+
must { strip_attribute(:replaceable).replace_newlines }
88+
wont { strip_attribute(:unreplaceable).replace_newlines }
89+
90+
wont { strip_attribute(:upcased) }
91+
wont { strip_attribute(:upcased).using("fOO") }
92+
must { strip_attribute(:upcased).using("BIG") }
7393
end
7494

7595
it "should fail when testing for strip on an unstripped attribute" do

0 commit comments

Comments
 (0)