11module 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 \n string " )
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
0 commit comments