seatbelt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # CHANGELOG
2
+
3
+ ## 0.1.0
4
+
5
+ * include assert_mail
6
+ * include assert_json
7
+ * initial version
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in seatbelt.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Thorsten Böttger
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,112 @@
1
+ # Seatbelt
2
+
3
+ Provide a number of custom assertions for Ruby and Ruby on Rails
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'seatbelt'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install seatbelt
18
+
19
+ ## Usage
20
+
21
+ ### assert_json
22
+
23
+ Test your [JSON](http://www.json.org/) strings
24
+
25
+ ```ruby
26
+ class MyActionTest < ActionController::TestCase
27
+ include Seatbelt::AssertJson
28
+
29
+ def test_my_action
30
+ get :my_action, :format => 'json'
31
+ # => @response.body= '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}'
32
+
33
+ assert_json @response.body do
34
+ has 'key' do
35
+ has 'inner_key1', 'value1'
36
+ has 'inner_key2', /lue2/
37
+ end
38
+ has_not 'key_not_included'
39
+ end
40
+ end
41
+
42
+ end
43
+ ```
44
+
45
+ ### assert_mail
46
+
47
+ Test your Rails [ActionMailer](http://guides.rubyonrails.org/action_mailer_basics.html) deliveries.
48
+
49
+ ```ruby
50
+ class MyTest < Test::Unit::TestCase
51
+
52
+ def test_mail
53
+ this_sends_an_email
54
+ assert_mail :to => 'recipient@email.com', :subject => 'the subject'
55
+ end
56
+
57
+ def test_with_block
58
+ assert_mail :to => 'recipient@email.com', :subject => 'the subject' do
59
+ assert_mail :to => 'another@email.com', :subject => 'used as regular expression' do
60
+ this_sends_two_emails
61
+ end
62
+ end
63
+ end
64
+
65
+ def test_the_mail_body
66
+ assert_mail :to => 'recipient@email.com', :body => ['part 1', 'part 2'] do
67
+ this_sends_an_email
68
+ end
69
+ end
70
+
71
+ def test_the_full_mail
72
+ assert_mail :from => 'sender@email.com',
73
+ :to => 'recipient@email.com',
74
+ :cc => 'cc@email.com',
75
+ :bcc => 'bcc@email.com',
76
+ :subject => 'subject',
77
+ :body => ['part 1', 'part 2'] do
78
+ this_sends_an_email
79
+ end
80
+ end
81
+
82
+ def test_crosscheck
83
+ assert_no_mail do
84
+ this_should_not_send_an_email
85
+ end
86
+ end
87
+
88
+ def test_other_crosscheck
89
+ assert_no_mail :to => 'dontwantemails@email.com' do
90
+ this_should_not_send_an_email
91
+ end
92
+ end
93
+
94
+ end
95
+ ```
96
+
97
+
98
+ ## Changelog ##
99
+
100
+ Look at the [CHANGELOG](https://github.com/xing/assert_json/blob/master/CHANGELOG.md) for details.
101
+
102
+ ## Authors ##
103
+
104
+ * [Thorsten Böttger](http://github.com/alto)
105
+
106
+ ## How to contribute
107
+
108
+ 1. Fork it
109
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
110
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
111
+ 4. Push to the branch (`git push origin my-new-feature`)
112
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+
5
+ # require 'rubygems'
6
+ # require 'rake'
7
+ require 'bundler/gem_tasks'
8
+
9
+ require 'rake/testtask'
10
+ desc 'Test the assertions.'
11
+ Rake::TestTask.new(:test) do |t|
12
+ t.libs << 'lib' << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+ desc 'Default: run unit tests.'
17
+ task :default => :test
@@ -0,0 +1,96 @@
1
+ require 'multi_json'
2
+ module Seatbelt
3
+ module AssertJson
4
+
5
+ def assert_json(json_string, &block)
6
+ if block_given?
7
+ @json = AssertJson::Json.new(json_string)
8
+ # json.instance_exec(json, &block)
9
+ yield @json
10
+ end
11
+ end
12
+
13
+ def has(*args, &block)
14
+ @json.has(*args, &block)
15
+ end
16
+
17
+ def has_not(*args, &block)
18
+ @json.has_not(*args, &block)
19
+ end
20
+
21
+ class Json
22
+
23
+ def initialize(json_string)
24
+ @decoded_json = MultiJson.decode(json_string)
25
+ end
26
+
27
+ def has(*args, &block)
28
+ arg = args.shift
29
+ token = @decoded_json.is_a?(Array) ? @decoded_json.shift : @decoded_json
30
+ case token
31
+ when Hash
32
+ raise_error("element #{arg} not found") unless token.keys.include?(arg)
33
+ if second_arg = args.shift
34
+ case second_arg
35
+ when Regexp
36
+ raise_error("element #{token[arg].inspect} does not match #{second_arg.inspect}") if second_arg !~ token[arg]
37
+ else
38
+ raise_error("element #{token[arg].inspect} does not match #{second_arg.inspect}") if second_arg != token[arg]
39
+ end
40
+ end
41
+ when Array
42
+ raise_error("element #{arg} not found") if token != arg
43
+ when String
44
+ case arg
45
+ when Regexp
46
+ raise_error("element #{arg.inspect} not found") if token !~ arg
47
+ else
48
+ raise_error("element #{arg.inspect} not found") if token != arg
49
+ end
50
+ when NilClass
51
+ raise_error("no element left")
52
+ else
53
+ flunk
54
+ end
55
+
56
+ if block_given?
57
+ begin
58
+ in_scope, @decoded_json = @decoded_json, token.is_a?(Hash) ? token[arg] : token
59
+ yield
60
+ ensure
61
+ @decoded_json = in_scope
62
+ end
63
+ end
64
+
65
+ end
66
+ alias element has
67
+
68
+ def has_not(*args, &block)
69
+ arg = args.shift
70
+ token = @decoded_json
71
+ case token
72
+ when Array
73
+ raise_error("element #{arg} found, but not expected") if token.include?(arg)
74
+ else
75
+ raise_error("element #{arg} found, but not expected") if token.keys.include?(arg)
76
+ end
77
+ end
78
+ alias not_element has_not
79
+
80
+ private
81
+
82
+ def raise_error(message)
83
+ if Object.const_defined?(:MiniTest)
84
+ raise MiniTest::Assertion.new(message)
85
+ else
86
+ raise Test::Unit::AssertionFailedError.new(message)
87
+ end
88
+ end
89
+
90
+ end
91
+ end
92
+ end
93
+
94
+ class Test::Unit::TestCase
95
+ include Seatbelt::AssertJson
96
+ end
@@ -0,0 +1,48 @@
1
+ module Seatbelt
2
+ module AssertMail
3
+
4
+ def assert_mail(options={}, &block)
5
+ assert !find_email(options, &block).nil?, "couldn't find the expected mail (#{options.inspect}) in #{ActionMailer::Base.deliveries.inspect}"
6
+ end
7
+
8
+ def assert_no_mail(options={}, &block)
9
+ assert find_email(options, &block).nil?, "didn't expect mail (#{options.inspect}) in #{ActionMailer::Base.deliveries.inspect}"
10
+ end
11
+
12
+ private
13
+
14
+ def find_email(options, &block)
15
+ if block_given?
16
+ ActionMailer::Base.deliveries.clear
17
+ yield
18
+ end
19
+ ActionMailer::Base.deliveries.detect do |mail|
20
+ got_mail?(mail, options)
21
+ end
22
+ end
23
+
24
+ def got_mail?(mail, options={})
25
+ return false if options[:to] && !mail.to.include?(options[:to])
26
+ return false if options[:from] && !mail.from.include?(options[:from])
27
+ return false if options[:subject] && (mail.subject !~ /#{options[:subject]}/)
28
+ return false if options[:cc] && !mail.cc.include?(options[:cc])
29
+ return false if options[:bcc] && !mail.bcc.include?(options[:bcc])
30
+ if options[:body]
31
+ Array(options[:body]).each do |element|
32
+ if !mail.body.encoded.match(element)
33
+ # puts "#{element} not found in body: #{mail.body.encoded}"
34
+ return false
35
+ end
36
+ end
37
+ end
38
+ true
39
+ end
40
+
41
+ end
42
+ end
43
+
44
+ if Module.const_defined?(:ActionMailer)
45
+ class Test::Unit::TestCase
46
+ include Seatbelt::AssertMail
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module Seatbelt
2
+ VERSION = "0.1.0"
3
+ end
data/lib/seatbelt.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "seatbelt/version"
2
+ require 'seatbelt/assert_json'
3
+ require 'seatbelt/assert_mail'
4
+
5
+ # module Seatbelt
6
+ # # Your code goes here...
7
+ # end
data/seatbelt.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/seatbelt/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Thorsten Böttger"]
6
+ gem.email = ["boettger@mt7.de"]
7
+ gem.description = %q{Provide custom assertions for Ruby and Ruby on Rails}
8
+ gem.summary = %q{Provide custom assertions for Ruby and Ruby on Rails}
9
+ gem.homepage = "http://github.com/also/seatbelt"
10
+ gem.licenses = ["MIT"]
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "seatbelt"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = Seatbelt::VERSION
18
+
19
+ gem.add_dependency 'multi_json'
20
+ gem.add_development_dependency 'activesupport'
21
+ gem.add_development_dependency 'actionmailer'
22
+ end
@@ -0,0 +1,269 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class Seatbelt::AssertJsonTest < Test::Unit::TestCase
4
+ include Seatbelt::AssertJson
5
+
6
+ # def test_string
7
+ # assert_json '"key"' do |json|
8
+ # json.element 'key'
9
+ # end
10
+ # end
11
+ # def test_string_crosscheck
12
+ # assert_raises(MiniTest::Assertion) do
13
+ # assert_json '"key"' do |json|
14
+ # json.element 'wrong_key'
15
+ # end
16
+ # end
17
+ # end
18
+ # def test_regular_expression_for_strings
19
+ # assert_json '"string"' do |json|
20
+ # json.element /tri/
21
+ # end
22
+ # end
23
+
24
+ def test_regular_expression_for_hash_values
25
+ assert_json '{"key":"value"}' do |json|
26
+ json.element 'key', /alu/
27
+ end
28
+ end
29
+
30
+ def test_single_hash
31
+ assert_json '{"key":"value"}' do |json|
32
+ json.element 'key', 'value'
33
+ end
34
+ end
35
+ def test_single_hash_crosscheck_for_key
36
+ assert_raises(MiniTest::Assertion) do
37
+ assert_json '{"key":"value"}' do |json|
38
+ json.element 'wrong_key', 'value'
39
+ end
40
+ end
41
+ end
42
+ def test_single_hash_crosscheck_for_value
43
+ assert_raises(MiniTest::Assertion) do
44
+ assert_json '{"key":"value"}' do |json|
45
+ json.element 'key', 'wrong_value'
46
+ end
47
+ end
48
+ end
49
+
50
+ def test_not_element
51
+ assert_json '{"key":"value"}' do |json|
52
+ json.element 'key', 'value'
53
+ json.not_element 'key_not_included'
54
+ end
55
+ end
56
+ def test_not_element_crosscheck
57
+ assert_raises(MiniTest::Assertion) do
58
+ assert_json '{"key":"value"}' do |json|
59
+ json.not_element 'key'
60
+ end
61
+ end
62
+ end
63
+
64
+ def test_array
65
+ assert_json '["value1","value2","value3"]' do |json|
66
+ json.element 'value1'
67
+ json.element 'value2'
68
+ json.element 'value3'
69
+ end
70
+ end
71
+ def test_not_element_array
72
+ assert_json '["value1","value2"]' do |json|
73
+ json.element 'value1'
74
+ json.element 'value2'
75
+ json.not_element 'value3'
76
+ end
77
+ end
78
+ def test_array_crosscheck_order
79
+ assert_raises(MiniTest::Assertion) do
80
+ assert_json '["value1","value2","value3"]' do |json|
81
+ json.element 'value2'
82
+ end
83
+ end
84
+ end
85
+ def test_array_crosscheck_for_first_item
86
+ assert_raises(MiniTest::Assertion) do
87
+ assert_json '["value1","value2","value3"]' do |json|
88
+ json.element 'wrong_value1'
89
+ end
90
+ end
91
+ end
92
+ def test_array_crosscheck_for_second_item
93
+ assert_raises(MiniTest::Assertion) do
94
+ assert_json '["value1","value2","value3"]' do |json|
95
+ json.element 'value1'
96
+ json.element 'wrong_value2'
97
+ end
98
+ end
99
+ end
100
+
101
+ def test_nested_arrays
102
+ assert_json '[[["deep","another_depp"],["second_deep"]]]' do |json|
103
+ json.element [["deep","another_depp"],["second_deep"]]
104
+ end
105
+ end
106
+ def test_nested_arrays_crosscheck
107
+ assert_raises(MiniTest::Assertion) do
108
+ assert_json '[[["deep","another_depp"],["second_deep"]]]' do |json|
109
+ json.element [["deep","wrong_another_depp"],["second_deep"]]
110
+ end
111
+ end
112
+ assert_raises(MiniTest::Assertion) do
113
+ assert_json '[[["deep","another_depp"],["second_deep"]]]' do |json|
114
+ json.element [["deep","another_depp"],["wrong_second_deep"]]
115
+ end
116
+ end
117
+ end
118
+
119
+ def test_hash_with_value_array
120
+ assert_json '{"key":["value1","value2"]}' do |json|
121
+ json.element 'key', ['value1', 'value2']
122
+ end
123
+ end
124
+ def test_hash_with_value_array_crosscheck_wrong_key
125
+ assert_raises(MiniTest::Assertion) do
126
+ assert_json '{"key":["value1","value2"]}' do |json|
127
+ json.element 'wrong_key', ['value1', 'value2']
128
+ end
129
+ end
130
+ end
131
+ def test_hash_with_value_array_crosscheck_wrong_value1
132
+ assert_raises(MiniTest::Assertion) do
133
+ assert_json '{"key":["value1","value2"]}' do |json|
134
+ json.element 'key', ['wrong_value1', 'value2']
135
+ end
136
+ end
137
+ end
138
+ def test_hash_with_value_array_crosscheck_wrong_value2
139
+ assert_raises(MiniTest::Assertion) do
140
+ assert_json '{"key":["value1","value2"]}' do |json|
141
+ json.element 'key', ['value1', 'wrong_value2']
142
+ end
143
+ end
144
+ end
145
+
146
+ def test_hash_with_array_of_hashes
147
+ assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do |json|
148
+ json.element 'key' do
149
+ json.element 'inner_key1', 'value1'
150
+ json.element 'inner_key2', 'value2'
151
+ end
152
+ end
153
+ end
154
+ def test_hash_with_array_of_hashes_crosscheck_inner_key
155
+ assert_raises(MiniTest::Assertion) do
156
+ assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do |json|
157
+ json.element 'key' do
158
+ json.element 'wrong_inner_key1', 'value1'
159
+ end
160
+ end
161
+ end
162
+ end
163
+ def test_hash_with_array_of_hashes_crosscheck_inner_value
164
+ assert_raises(MiniTest::Assertion) do
165
+ assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do |json|
166
+ json.element 'key' do
167
+ json.element 'inner_key1', 'wrong_value1'
168
+ end
169
+ end
170
+ end
171
+ end
172
+
173
+ def test_array_with_two_hashes
174
+ assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do |json|
175
+ json.element 'key1', 'value1'
176
+ json.element 'key2', 'value2'
177
+ end
178
+ end
179
+ def test_array_with_nested_hashes
180
+ assert_json '[{"key1":{"key2":"value2"}}]' do |json|
181
+ json.element 'key1' do
182
+ json.element 'key2', 'value2'
183
+ end
184
+ end
185
+ end
186
+ def test_array_with_two_hashes_crosscheck
187
+ assert_raises(MiniTest::Assertion) do
188
+ assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do |json|
189
+ json.element 'wrong_key1', 'value1'
190
+ json.element 'key2', 'value2'
191
+ end
192
+ end
193
+ assert_raises(MiniTest::Assertion) do
194
+ assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do |json|
195
+ json.element 'key1', 'value1'
196
+ json.element 'key2', 'wrong_value2'
197
+ end
198
+ end
199
+ end
200
+
201
+ def test_nested_hashes
202
+ assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do |json|
203
+ json.element 'outer_key' do
204
+ json.element 'key' do
205
+ json.element 'inner_key', 'value'
206
+ end
207
+ end
208
+ end
209
+ end
210
+ def test_nested_hashes_crosscheck
211
+ assert_raises(MiniTest::Assertion) do
212
+ assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do |json|
213
+ json.element 'wrong_outer_key'
214
+ end
215
+ end
216
+ assert_raises(MiniTest::Assertion) do
217
+ assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do |json|
218
+ json.element 'outer_key' do
219
+ json.element 'key' do
220
+ json.element 'inner_key', 'wrong_value'
221
+ end
222
+ end
223
+ end
224
+ end
225
+ end
226
+
227
+ def test_real_xws
228
+ assert_json '[{"contact_request":{"sender_id":"3","receiver_id":"2","id":1}}]' do |json|
229
+ json.element 'contact_request' do
230
+ json.element 'sender_id', '3'
231
+ json.element 'receiver_id', '2'
232
+ json.element 'id', 1
233
+ end
234
+ end
235
+
236
+ assert_json '[{"private_message":{"sender":{"display_name":"first last"},"receiver_id":"2","body":"body"}}]' do |json|
237
+ json.element 'private_message' do
238
+ json.element 'sender' do
239
+ json.element 'display_name', 'first last'
240
+ end
241
+ json.element 'receiver_id', '2'
242
+ json.element 'body', 'body'
243
+ end
244
+ end
245
+ end
246
+
247
+ def test_not_enough_elements_in_array
248
+ assert_raises(MiniTest::Assertion) do
249
+ assert_json '["one","two"]' do |json|
250
+ json.element "one"
251
+ json.element "two"
252
+ json.element "three"
253
+ end
254
+ end
255
+ end
256
+
257
+ def test_not_enough_elements_in_hash_array
258
+ assert_raises(MiniTest::Assertion) do
259
+ assert_json '{"key":[{"key1":"value1"}, {"key2":"value2"}]}' do |json|
260
+ json.element 'key' do
261
+ json.element 'key1', 'value1'
262
+ json.element 'key2', 'value2'
263
+ json.element 'key3'
264
+ end
265
+ end
266
+ end
267
+ end
268
+
269
+ end
@@ -0,0 +1,273 @@
1
+ require 'test_helper'
2
+
3
+ class Seatbelt::AssertJsonNewDslTest < Test::Unit::TestCase
4
+ include Seatbelt::AssertJson
5
+
6
+ # def test_string
7
+ # assert_json '"key"' do
8
+ # has 'key'
9
+ # end
10
+ # end
11
+ # def test_string_crosscheck
12
+ # assert_raises(MiniTest::Assertion) do
13
+ # assert_json '"key"' do
14
+ # has 'wrong_key'
15
+ # end
16
+ # end
17
+ # end
18
+ # def test_regular_expression_for_strings
19
+ # assert_json "{\"my_key\":\"value\"}" do
20
+ # has /key/
21
+ # end
22
+ # end
23
+ def test_regular_expression_for_hash_values
24
+ assert_json '{"key":"value"}' do
25
+ has 'key', /alu/
26
+ end
27
+ end
28
+
29
+ def test_single_hash
30
+ assert_json '{"key":"value"}' do
31
+ has 'key', 'value'
32
+ end
33
+ end
34
+ def test_single_hash_with_outer_variable
35
+ @values = {'value' => 'value'}
36
+ assert_json '{"key":"value"}' do
37
+ has 'key', @values['value']
38
+ end
39
+ end
40
+ def test_single_hash_crosscheck_for_key
41
+ assert_raises(MiniTest::Assertion) do
42
+ assert_json '{"key":"value"}' do
43
+ has 'wrong_key', 'value'
44
+ end
45
+ end
46
+ end
47
+ def test_single_hash_crosscheck_for_value
48
+ assert_raises(MiniTest::Assertion) do
49
+ assert_json '{"key":"value"}' do
50
+ has 'key', 'wrong_value'
51
+ end
52
+ end
53
+ end
54
+
55
+ def test_has_not
56
+ assert_json '{"key":"value"}' do
57
+ has 'key', 'value'
58
+ has_not 'key_not_included'
59
+ end
60
+ end
61
+ def test_has_not_crosscheck
62
+ assert_raises(MiniTest::Assertion) do
63
+ assert_json '{"key":"value"}' do
64
+ has_not 'key'
65
+ end
66
+ end
67
+ end
68
+
69
+ def test_array
70
+ assert_json '["value1","value2","value3"]' do
71
+ has 'value1'
72
+ has 'value2'
73
+ has 'value3'
74
+ end
75
+ end
76
+ def test_has_not_array
77
+ assert_json '["value1","value2"]' do
78
+ has 'value1'
79
+ has 'value2'
80
+ has_not 'value3'
81
+ end
82
+ end
83
+ def test_array_crosscheck_order
84
+ assert_raises(MiniTest::Assertion) do
85
+ assert_json '["value1","value2","value3"]' do
86
+ has 'value2'
87
+ end
88
+ end
89
+ end
90
+ def test_array_crosscheck_for_first_item
91
+ assert_raises(MiniTest::Assertion) do
92
+ assert_json '["value1","value2","value3"]' do
93
+ has 'wrong_value1'
94
+ end
95
+ end
96
+ end
97
+ def test_array_crosscheck_for_second_item
98
+ assert_raises(MiniTest::Assertion) do
99
+ assert_json '["value1","value2","value3"]' do
100
+ has 'value1'
101
+ has 'wrong_value2'
102
+ end
103
+ end
104
+ end
105
+
106
+ def test_nested_arrays
107
+ assert_json '[[["deep","another_depp"],["second_deep"]]]' do
108
+ has [["deep","another_depp"],["second_deep"]]
109
+ end
110
+ end
111
+ def test_nested_arrays_crosscheck
112
+ assert_raises(MiniTest::Assertion) do
113
+ assert_json '[[["deep","another_depp"],["second_deep"]]]' do
114
+ has [["deep","wrong_another_depp"],["second_deep"]]
115
+ end
116
+ end
117
+ assert_raises(MiniTest::Assertion) do
118
+ assert_json '[[["deep","another_depp"],["second_deep"]]]' do
119
+ has [["deep","another_depp"],["wrong_second_deep"]]
120
+ end
121
+ end
122
+ end
123
+
124
+ def test_hash_with_value_array
125
+ assert_json '{"key":["value1","value2"]}' do
126
+ has 'key', ['value1', 'value2']
127
+ end
128
+ end
129
+ def test_hash_with_value_array_crosscheck_wrong_key
130
+ assert_raises(MiniTest::Assertion) do
131
+ assert_json '{"key":["value1","value2"]}' do
132
+ has 'wrong_key', ['value1', 'value2']
133
+ end
134
+ end
135
+ end
136
+ def test_hash_with_value_array_crosscheck_wrong_value1
137
+ assert_raises(MiniTest::Assertion) do
138
+ assert_json '{"key":["value1","value2"]}' do
139
+ has 'key', ['wrong_value1', 'value2']
140
+ end
141
+ end
142
+ end
143
+ def test_hash_with_value_array_crosscheck_wrong_value2
144
+ assert_raises(MiniTest::Assertion) do
145
+ assert_json '{"key":["value1","value2"]}' do
146
+ has 'key', ['value1', 'wrong_value2']
147
+ end
148
+ end
149
+ end
150
+
151
+ def test_hash_with_array_of_hashes
152
+ assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do
153
+ has 'key' do
154
+ has 'inner_key1', 'value1'
155
+ has 'inner_key2', 'value2'
156
+ end
157
+ end
158
+ end
159
+ def test_hash_with_array_of_hashes_crosscheck_inner_key
160
+ assert_raises(MiniTest::Assertion) do
161
+ assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do
162
+ has 'key' do
163
+ has 'wrong_inner_key1', 'value1'
164
+ end
165
+ end
166
+ end
167
+ end
168
+ def test_hash_with_array_of_hashes_crosscheck_inner_value
169
+ assert_raises(MiniTest::Assertion) do
170
+ assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do
171
+ has 'key' do
172
+ has 'inner_key1', 'wrong_value1'
173
+ end
174
+ end
175
+ end
176
+ end
177
+
178
+ def test_array_with_two_hashes
179
+ assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do
180
+ has 'key1', 'value1'
181
+ has 'key2', 'value2'
182
+ end
183
+ end
184
+ def test_array_with_nested_hashes
185
+ assert_json '[{"key1":{"key2":"value2"}}]' do
186
+ has 'key1' do
187
+ has 'key2', 'value2'
188
+ end
189
+ end
190
+ end
191
+ def test_array_with_two_hashes_crosscheck
192
+ assert_raises(MiniTest::Assertion) do
193
+ assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do
194
+ has 'wrong_key1', 'value1'
195
+ has 'key2', 'value2'
196
+ end
197
+ end
198
+ assert_raises(MiniTest::Assertion) do
199
+ assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do
200
+ has 'key1', 'value1'
201
+ has 'key2', 'wrong_value2'
202
+ end
203
+ end
204
+ end
205
+
206
+ def test_nested_hashes
207
+ assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do
208
+ has 'outer_key' do
209
+ has 'key' do
210
+ has 'inner_key', 'value'
211
+ end
212
+ end
213
+ end
214
+ end
215
+ def test_nested_hashes_crosscheck
216
+ assert_raises(MiniTest::Assertion) do
217
+ assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do
218
+ has 'wrong_outer_key'
219
+ end
220
+ end
221
+ assert_raises(MiniTest::Assertion) do
222
+ assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do
223
+ has 'outer_key' do
224
+ has 'key' do
225
+ has 'inner_key', 'wrong_value'
226
+ end
227
+ end
228
+ end
229
+ end
230
+ end
231
+
232
+ def test_real_xws
233
+ assert_json '[{"contact_request":{"sender_id":"3","receiver_id":"2","id":1}}]' do
234
+ has 'contact_request' do
235
+ has 'sender_id', '3'
236
+ has 'receiver_id', '2'
237
+ has 'id', 1
238
+ end
239
+ end
240
+
241
+ assert_json '[{"private_message":{"sender":{"display_name":"first last"},"receiver_id":"2","body":"body"}}]' do
242
+ has 'private_message' do
243
+ has 'sender' do
244
+ has 'display_name', 'first last'
245
+ end
246
+ has 'receiver_id', '2'
247
+ has 'body', 'body'
248
+ end
249
+ end
250
+ end
251
+
252
+ def test_not_enough_hass_in_array
253
+ assert_raises(MiniTest::Assertion) do
254
+ assert_json '["one","two"]' do
255
+ has "one"
256
+ has "two"
257
+ has "three"
258
+ end
259
+ end
260
+ end
261
+
262
+ def test_not_enough_hass_in_hash_array
263
+ assert_raises(MiniTest::Assertion) do
264
+ assert_json '{"key":[{"key1":"value1"}, {"key2":"value2"}]}' do
265
+ has 'key' do
266
+ has 'key1', 'value1'
267
+ has 'key2', 'value2'
268
+ has 'key3'
269
+ end
270
+ end
271
+ end
272
+ end
273
+ end
@@ -0,0 +1,78 @@
1
+ require 'test_helper'
2
+
3
+ class Seatbelt::AssertMailTest < Test::Unit::TestCase
4
+
5
+ def test_assert_mail
6
+ Mailer.test.deliver
7
+
8
+ assert_mail :to => 'test@seatbelt.co.nz'
9
+ assert_mail :cc => 'cc@seatbelt.co.nz'
10
+ assert_mail :bcc => 'bcc@seatbelt.co.nz'
11
+ assert_mail :from => 'from@seatbelt.co.nz'
12
+ assert_mail :subject => 'mail subject'
13
+ assert_mail :subject => 'subject' # uses regular expressions
14
+ assert_mail :body => 'mail body text'
15
+ assert_mail :body => 'body text' # uses regular expressions
16
+ assert_mail :body => ['mail body', 'body text']
17
+
18
+ assert_mail :to => 'test@seatbelt.co.nz', :subject => 'subject', :body => 'body'
19
+ end
20
+ def test_assert_mail_crosscheck
21
+ Mailer.test.deliver
22
+
23
+ assert_raises(MiniTest::Assertion) { assert_mail :to => 'unknown@seatbelt.co.nz' }
24
+ assert_raises(MiniTest::Assertion) { assert_mail :cc => 'unknown@seatbelt.co.nz' }
25
+ assert_raises(MiniTest::Assertion) { assert_mail :bcc => 'unknown@seatbelt.co.nz' }
26
+ assert_raises(MiniTest::Assertion) { assert_mail :from => 'unknown@seatbelt.co.nz' }
27
+ assert_raises(MiniTest::Assertion) { assert_mail :subject => 'unknown' }
28
+ assert_raises(MiniTest::Assertion) { assert_mail :body => 'unknown' }
29
+ assert_raises(MiniTest::Assertion) { assert_mail :to => 'unknown@seatbelt.co.nz', :subject => 'unknown', :body => 'unknown' }
30
+ end
31
+
32
+ def test_assert_mail_with_multiple_mails
33
+ Mailer.test.deliver
34
+ Mailer.test(:to => 'another@seatbelt.co.nz').deliver
35
+
36
+ assert_mail :to => 'test@seatbelt.co.nz'
37
+ assert_mail :to => 'another@seatbelt.co.nz'
38
+ end
39
+
40
+ def test_assert_mail_with_block
41
+ assert_mail :to => 'test@seatbelt.co.nz', :subject => 'subject', :body => 'body' do
42
+ Mailer.test.deliver
43
+ end
44
+ end
45
+ def test_assert_mail_with_block_crosscheck
46
+ assert_raises(MiniTest::Assertion) do
47
+ assert_mail :to => 'unknown@seatbelt.co.nz' do
48
+ Mailer.test.deliver
49
+ end
50
+ end
51
+ end
52
+
53
+ def test_assert_no_mail
54
+ assert_no_mail do
55
+ # do something
56
+ abc = 1 + 1
57
+ end
58
+
59
+ assert_no_mail :to => 'another@seatbelt.co.nz' do
60
+ Mailer.test.deliver
61
+ end
62
+ assert_no_mail :subject => 'my other subject' do
63
+ Mailer.test.deliver
64
+ end
65
+ assert_no_mail :body => 'my other body text' do
66
+ Mailer.test.deliver
67
+ end
68
+ end
69
+
70
+ def test_assert_no_mail_crosscheck
71
+ assert_raises(MiniTest::Assertion) do
72
+ assert_no_mail :to => 'test@seatbelt.co.nz' do
73
+ Mailer.test.deliver
74
+ end
75
+ end
76
+ end
77
+
78
+ end
@@ -0,0 +1,14 @@
1
+ ActionMailer::Base.delivery_method = :test
2
+ class Mailer < ActionMailer::Base
3
+
4
+ def test(options={})
5
+ mail({:from => 'from@seatbelt.co.nz',
6
+ :to => 'test@seatbelt.co.nz',
7
+ :cc => 'cc@seatbelt.co.nz',
8
+ :bcc => 'bcc@seatbelt.co.nz',
9
+ :subject => 'mail subject'}.merge(options)) do |format|
10
+ format.text { render :text => options[:body] || 'mail body text' }
11
+ end
12
+ end
13
+
14
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
4
+ require 'action_mailer'
5
+
6
+ $:.push File.expand_path("../../lib", __FILE__)
7
+ require File.dirname(__FILE__) + '/../lib/seatbelt.rb'
8
+
9
+ # Requiring custom spec helpers
10
+ Dir[File.dirname(__FILE__) + "/helpers/**/*.rb"].sort.each { |f| require File.expand_path(f) }
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seatbelt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thorsten Böttger
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: multi_json
16
+ requirement: &70161394805840 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70161394805840
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &70161394805420 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70161394805420
36
+ - !ruby/object:Gem::Dependency
37
+ name: actionmailer
38
+ requirement: &70161394805000 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70161394805000
47
+ description: Provide custom assertions for Ruby and Ruby on Rails
48
+ email:
49
+ - boettger@mt7.de
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - CHANGELOG.md
56
+ - Gemfile
57
+ - LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - lib/seatbelt.rb
61
+ - lib/seatbelt/assert_json.rb
62
+ - lib/seatbelt/assert_mail.rb
63
+ - lib/seatbelt/version.rb
64
+ - seatbelt.gemspec
65
+ - test/assert_json_old_dsl_test.rb
66
+ - test/assert_json_test.rb
67
+ - test/assert_mail_test.rb
68
+ - test/helpers/mailer.rb
69
+ - test/test_helper.rb
70
+ homepage: http://github.com/also/seatbelt
71
+ licenses:
72
+ - MIT
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.11
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Provide custom assertions for Ruby and Ruby on Rails
95
+ test_files:
96
+ - test/assert_json_old_dsl_test.rb
97
+ - test/assert_json_test.rb
98
+ - test/assert_mail_test.rb
99
+ - test/helpers/mailer.rb
100
+ - test/test_helper.rb