On May 25, 2011, at 10:07 AM, Gustavo Delfino wrote:
> Hello all. Thanks to subjects, custom matchers and fluent chaining I was able
> to greatly simplify my spec. But now I want to DRY my custom matchers.
>
> I have two custom matchers: 'have_text' and 'have_number' and both contain
> exactly the same chains 'on_column' and 'on_columns'.
>
> Is there a way to DRY this up?
>
>
> module CustomColumnsMatchers
>
> RSpec::Matchers.define(:have_text) do |expected_string|
> chain(:on_column) do |colnumber|
> @index = colnumber - 1
> @width = 1
> end
> chain(:on_columns) do |range|
> a = range.to_a
> @index = Range.new( a[0]-1, a[-1]-1 )
> @width = a[-1] - a[0] + 1
> end
> match do |line|
> line[@index] == expected_string.ljust(@width)
> end
> end
>
> RSpec::Matchers.define(:have_number) do |expected_number_string|
> chain(:on_column) do |colnumber|
> @index = colnumber - 1
> @width = 1
> end
> chain(:on_columns) do |range|
> a = range.to_a
> @index = Range.new( a[0]-1, a[-1]-1 )
> @width = a[-1] - a[0] + 1
> end
> match do |line|
> line[@index] == expected_number_string.rjust(@width,'0')
> end
> end
>
> end
>
>
> Regards,
>
> Gustavo Delfino
I'd probably do something like:
RSpec::Matchers.define(:have_text) do |expected_string|
chain(:on_column, &HaveXXXHelpers.on_column)
chain(:on_columns, &HaveXXXHelpers.on_columns)
match do |line|
line[@index] == expected_string.ljust(@width)
end
end
HTH,
David
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users