Skip to content

Commit

Permalink
add 'be_normalized_to' matcher for testing params normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
shime committed Jun 7, 2013
1 parent 0455a06 commit 6f0bd98
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 35 deletions.
13 changes: 6 additions & 7 deletions spec/griddler/adapters/cloudmailin_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

describe Griddler::Adapters::CloudmailinAdapter, '.normalize_params' do
it 'normalizes parameters' do
params = default_params

normalized_params = Griddler::Adapters::CloudmailinAdapter.normalize_params(params)
normalized_params[:to].should eq ['Some Identifier <[email protected]>']
normalized_params[:from].should eq 'Joe User <[email protected]>'
normalized_params[:subject].should eq 'Re: [ThisApp] That thing'
normalized_params[:text].should include('Dear bob')
Griddler::Adapters::CloudmailinAdapter.normalize_params(default_params).should be_normalized_to({
to: ['Some Identifier <[email protected]>'],
from: 'Joe User <[email protected]>',
subject: 'Re: [ThisApp] That thing',
text: /Dear bob/
})
end

it 'passes the received array of files' do
Expand Down
19 changes: 9 additions & 10 deletions spec/griddler/adapters/mandrill_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

describe Griddler::Adapters::MandrillAdapter, '.normalize_params' do
it 'normalizes parameters' do
params = default_params

normalized_params = Griddler::Adapters::MandrillAdapter.normalize_params(params)
normalized_params.each do |params|
params[:to].should eq ['The Token <[email protected]>']
params[:from].should eq '[email protected]'
params[:subject].should eq 'hello'
params[:text].should include('Dear bob')
params[:html].should include('<p>Dear bob</p>')
params[:raw_body].should include('raw')
Griddler::Adapters::MandrillAdapter.normalize_params(default_params).each do |params|
params.should be_normalized_to({
to: ['The Token <[email protected]>'],
from: '[email protected]',
subject: 'hello',
text: %r{Dear bob},
html: %r{<p>Dear bob</p>},
raw_body: %r{raw}
})
end
end

Expand Down
33 changes: 15 additions & 18 deletions spec/griddler/adapters/postmark_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

describe Griddler::Adapters::PostmarkAdapter, '.normalize_params' do
it 'normalizes parameters' do
params = default_params

normalized_params = Griddler::Adapters::PostmarkAdapter.normalize_params(params)
normalized_params[:to].should eq ['Robert Paulson <[email protected]>']
normalized_params[:from].should eq '[email protected]'
normalized_params[:subject].should eq 'Reminder: First and Second Rule'
normalized_params[:text].should include('Dear bob')
normalized_params[:html].should include('<p>Dear bob</p>')
Griddler::Adapters::PostmarkAdapter.normalize_params(default_params).should be_normalized_to({
to: ['Robert Paulson <[email protected]>'],
from: '[email protected]',
subject: 'Reminder: First and Second Rule',
text: /Dear bob/,
html: %r{<p>Dear bob</p>}
})
end

it 'passes the received array of files' do
Expand All @@ -35,16 +34,14 @@
end

it 'gets rid of the original postmark params' do
params = default_params

normalized_params = Griddler::Adapters::PostmarkAdapter.normalize_params(params)

normalized_params[:ToFull].should be_nil
normalized_params[:FromFull].should be_nil
normalized_params[:Subject].should be_nil
normalized_params[:TextBody].should be_nil
normalized_params[:HtmlBody].should be_nil
normalized_params[:Attachments].should be_nil
Griddler::Adapters::PostmarkAdapter.normalize_params(default_params).should be_normalized_to({
ToFull: nil,
FromFull: nil,
Subject: nil,
TextBody: nil,
HtmlBody: nil,
Attachments: nil
})
end

def default_params
Expand Down
24 changes: 24 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,27 @@
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
end

RSpec::Matchers.define :be_normalized_to do |expected|
failure_message_for_should do |actual|
message = ""
expected.each do |k, v|
message << "expected :#{k} to be normalized to #{expected[k].inspect}, "\
"but received #{actual[k].inspect}\n" unless actual[k] == expected[k]
end
message
end

description do
"be normalized to #{expected}"
end

match do |actual|
expected.each do |k, v|
case v
when Regexp then actual[k].should =~ v
else actual[k].should === v
end
end
end
end

0 comments on commit 6f0bd98

Please sign in to comment.