rspec-hanami 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +43 -8
- data/lib/rspec/hanami/form_parser.rb +33 -0
- data/lib/rspec/hanami/match_status.rb +35 -0
- data/lib/rspec/hanami/matchers.rb +5 -0
- data/lib/rspec/hanami/matchers/be_status.rb +28 -0
- data/lib/rspec/hanami/matchers/form_matchers.rb +75 -0
- data/lib/rspec/hanami/matchers/have_http_status.rb +2 -35
- data/lib/rspec/hanami/matchers/redirect_to.rb +30 -0
- data/lib/rspec/hanami/version.rb +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe7810f3f6091b3f84741650bc0b0ed5fc47c6e4
|
4
|
+
data.tar.gz: e9bcc09993433dc175bb0246e6a1b6e073c99645
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b62efc3b5892a5a711c19f4afb5dd2ad7d9de765fd5254fc232f775b63ebfc73f8e32c8dc2037a5f3c55766fcd3a744861416cf3fe673c00054ae048449c4216
|
7
|
+
data.tar.gz: f55cb79cfe6ed792df926d46369927a8b4a91e37a843dddf9e75c9eeecbb9d2d441d63dff1d26555ff20ee3cda2a50d8d82982cbb0351fa50c931d932d474285
|
data/README.md
CHANGED
@@ -40,18 +40,53 @@ expect(response).to have_http_status(:missing)
|
|
40
40
|
expect(response).to have_http_status(:redirect)
|
41
41
|
```
|
42
42
|
|
43
|
-
|
43
|
+
#### `be_success`
|
44
|
+
Passes if `response` has a not 4xx and 5xx error code.
|
44
45
|
|
45
|
-
|
46
|
-
|
47
|
-
|
46
|
+
``` ruby
|
47
|
+
response = action.call(params)
|
48
|
+
expect(response).to be_success
|
49
|
+
````
|
50
|
+
|
51
|
+
#### `redirect_to`
|
52
|
+
Passes if `response` has a redirect to special url
|
53
|
+
|
54
|
+
``` ruby
|
55
|
+
response = action.call(params)
|
56
|
+
expect(response).to redirect_to('site.com')
|
57
|
+
```
|
58
|
+
|
59
|
+
### Views Specs
|
60
|
+
#### `have_action`
|
61
|
+
Passes if form object has a action
|
62
|
+
|
63
|
+
``` ruby
|
64
|
+
expect(view.form).to have_action('/users')
|
65
|
+
expect(view.form).to_not have_action('/books')
|
66
|
+
```
|
67
|
+
|
68
|
+
#### `have_method`
|
69
|
+
Passes if form object has a method
|
70
|
+
|
71
|
+
``` ruby
|
72
|
+
expect(view.form).to have_method('POST')
|
73
|
+
expect(view.form).to have_method(:post)
|
74
|
+
expect(view.form).to_not have_method(:put)
|
75
|
+
```
|
76
|
+
|
77
|
+
#### `have_field`
|
78
|
+
Passes if form object has a field with wanted params
|
79
|
+
|
80
|
+
``` ruby
|
81
|
+
expect(view.form).to have_field(node: input, type: 'text', id: 'user-first-name')
|
82
|
+
```
|
48
83
|
|
49
84
|
## Also see
|
50
85
|
|
51
|
-
*
|
52
|
-
*
|
53
|
-
*
|
54
|
-
*
|
86
|
+
* <https://github.com/rspec/rspec>
|
87
|
+
* <https://github.com/rspec/rspec-core>
|
88
|
+
* <https://github.com/rspec/rspec-expectations>
|
89
|
+
* <https://github.com/rspec/rspec-mocks>
|
55
90
|
|
56
91
|
## Feature Requests & Bugs
|
57
92
|
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'hanami/utils/hash'
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
module Hanami
|
5
|
+
class FormParser
|
6
|
+
def call(html)
|
7
|
+
meta_list = select_inputs(html).map! { |input| input_data_hash(input) }
|
8
|
+
meta_list.map! { |hash| ::Hanami::Utils::Hash.new(hash).symbolize! }
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def select_inputs(html)
|
14
|
+
html.scan(/<input.+>|<textarea[^<]+>/)
|
15
|
+
end
|
16
|
+
|
17
|
+
def input_data_hash(input)
|
18
|
+
# trim all '<' and '>' chars. After that I add data
|
19
|
+
# attr 'node' for first element (html tag name). And
|
20
|
+
# in the end I split string on key-value pairs, trim '"'
|
21
|
+
# and split key-value to key and value array.
|
22
|
+
Hash[
|
23
|
+
input
|
24
|
+
.tr('<', '')
|
25
|
+
.tr('>', '')
|
26
|
+
.prepend('node=')
|
27
|
+
.split(' ')
|
28
|
+
.map! { |key_value| key_value.tr('"', '').split('=') }
|
29
|
+
]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Hanami
|
3
|
+
class MatchStatus
|
4
|
+
def call(actual, expected)
|
5
|
+
case expected
|
6
|
+
when Numeric
|
7
|
+
actual == expected
|
8
|
+
when Symbol
|
9
|
+
chec_symbol_status(actual, expected)
|
10
|
+
else
|
11
|
+
false
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
TYPE_CODES = {
|
18
|
+
error: /(4|5)../,
|
19
|
+
success: /2../,
|
20
|
+
missing: /404/,
|
21
|
+
redirect: /3../,
|
22
|
+
}.freeze
|
23
|
+
|
24
|
+
def chec_symbol_status(actual, expected)
|
25
|
+
status = TYPE_CODES[expected]
|
26
|
+
|
27
|
+
if status
|
28
|
+
!!actual.to_s.match(status)
|
29
|
+
else
|
30
|
+
actual == Rack::Utils::SYMBOL_TO_STATUS_CODE[expected]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -1,6 +1,11 @@
|
|
1
1
|
require 'rspec/core/warnings'
|
2
2
|
require 'rspec/expectations'
|
3
|
+
require 'rspec/hanami/form_parser'
|
4
|
+
require 'rspec/hanami/match_status'
|
3
5
|
require 'rspec/hanami/matchers/have_http_status'
|
6
|
+
require 'rspec/hanami/matchers/be_status'
|
7
|
+
require 'rspec/hanami/matchers/form_matchers'
|
8
|
+
require 'rspec/hanami/matchers/redirect_to'
|
4
9
|
|
5
10
|
module RSpec
|
6
11
|
module Hanami
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Hanami
|
3
|
+
module Matchers
|
4
|
+
extend ::RSpec::Matchers::DSL
|
5
|
+
|
6
|
+
# @api public
|
7
|
+
# Passes if `response` has a not 4xx and 5xx error code.
|
8
|
+
#
|
9
|
+
# @example Accepts numeric and symbol statuses
|
10
|
+
# response = action.call(params)
|
11
|
+
# expect(response).to be_success
|
12
|
+
#
|
13
|
+
matcher :be_success do
|
14
|
+
attr_reader :actual, :object
|
15
|
+
|
16
|
+
description { "have response success" }
|
17
|
+
match do |object|
|
18
|
+
@object = object
|
19
|
+
@actual = object.first
|
20
|
+
!RSpec::Hanami::MatchStatus.new.call(actual, :error)
|
21
|
+
end
|
22
|
+
|
23
|
+
failure_message { |actual| "expect #{object} to have status success" }
|
24
|
+
diffable
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Hanami
|
3
|
+
module Matchers
|
4
|
+
extend ::RSpec::Matchers::DSL
|
5
|
+
|
6
|
+
# @api public
|
7
|
+
# Passes if form object has a action
|
8
|
+
#
|
9
|
+
# @example Accepts numeric and symbol statuses
|
10
|
+
#
|
11
|
+
# expect(view.form).to have_action('/users')
|
12
|
+
#
|
13
|
+
matcher :have_action do |action|
|
14
|
+
attr_reader :form, :attributes
|
15
|
+
|
16
|
+
description { "have correct action param" }
|
17
|
+
match do |form|
|
18
|
+
@form = form
|
19
|
+
@attributes = form.instance_variable_get('@attributes')
|
20
|
+
@attributes[:action] == action
|
21
|
+
end
|
22
|
+
|
23
|
+
failure_message { |actual| "expect #{form.inspect} (#{attributes}) to have #{action} action param" }
|
24
|
+
diffable
|
25
|
+
end
|
26
|
+
|
27
|
+
# @api public
|
28
|
+
# Passes if form object has a method
|
29
|
+
#
|
30
|
+
# @example Accepts numeric and symbol statuses
|
31
|
+
#
|
32
|
+
# expect(view.form).to have_action('/users')
|
33
|
+
#
|
34
|
+
matcher :have_method do |method|
|
35
|
+
attr_reader :form, :attributes
|
36
|
+
|
37
|
+
description { "have correct method param" }
|
38
|
+
match do |form|
|
39
|
+
@form = form
|
40
|
+
@attributes = form.instance_variable_get('@attributes')
|
41
|
+
@attributes[:method].downcase.to_sym == method.downcase.to_sym
|
42
|
+
end
|
43
|
+
|
44
|
+
failure_message { |actual| "expect #{form.inspect} (#{attributes}) to have #{method} method param" }
|
45
|
+
diffable
|
46
|
+
end
|
47
|
+
|
48
|
+
# @api public
|
49
|
+
# Passes if form object has a field with wanted params
|
50
|
+
#
|
51
|
+
# @example Accepts numeric and symbol statuses
|
52
|
+
#
|
53
|
+
# expect(view.form).to have_field(node: input, type: 'text', id: 'user-first-name')
|
54
|
+
#
|
55
|
+
matcher :have_field do |params|
|
56
|
+
require 'hanami/utils/hash'
|
57
|
+
attr_reader :form, :form_data, :params
|
58
|
+
|
59
|
+
description { "have field with params" }
|
60
|
+
match do |form|
|
61
|
+
@form = form
|
62
|
+
@params = ::Hanami::Utils::Hash.new(@params).symbolize!
|
63
|
+
@form_data = RSpec::Hanami::FormParser.new.call(form.to_s)
|
64
|
+
|
65
|
+
form_data.any? do |input|
|
66
|
+
input.merge(params) == params.merge(input)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
failure_message { |actual| "expect #{form.inspect} (#{attributes}) to have #{method} method param" }
|
71
|
+
diffable
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -22,51 +22,18 @@ module RSpec
|
|
22
22
|
# expect(response).to have_http_status(:redirect)
|
23
23
|
#
|
24
24
|
matcher :have_http_status do |status|
|
25
|
-
attr_reader :actual
|
26
|
-
attr_reader :object
|
25
|
+
attr_reader :actual, :object
|
27
26
|
|
28
27
|
description { "have response #{status}" }
|
29
28
|
match do |object|
|
30
29
|
@object = object
|
31
30
|
@actual = object.first
|
32
|
-
MatchStatus.new.call(actual, status)
|
31
|
+
RSpec::Hanami::MatchStatus.new.call(actual, status)
|
33
32
|
end
|
34
33
|
|
35
34
|
failure_message { |actual| "expect #{object} to have status #{status}" }
|
36
35
|
diffable
|
37
36
|
end
|
38
|
-
|
39
|
-
class MatchStatus
|
40
|
-
def call(actual, expected)
|
41
|
-
case expected
|
42
|
-
when Numeric
|
43
|
-
actual == expected
|
44
|
-
when Symbol
|
45
|
-
chec_symbol_status(actual, expected)
|
46
|
-
else
|
47
|
-
false
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
|
-
TYPE_CODES = {
|
54
|
-
error: /(4|5)../,
|
55
|
-
success: /2../,
|
56
|
-
missing: /404/,
|
57
|
-
redirect: /3../,
|
58
|
-
}.freeze
|
59
|
-
|
60
|
-
def chec_symbol_status(actual, expected)
|
61
|
-
status = TYPE_CODES[expected]
|
62
|
-
|
63
|
-
if status
|
64
|
-
!!actual.to_s.match(status)
|
65
|
-
else
|
66
|
-
actual == Rack::Utils::SYMBOL_TO_STATUS_CODE[expected]
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
37
|
end
|
71
38
|
end
|
72
39
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Hanami
|
3
|
+
module Matchers
|
4
|
+
extend ::RSpec::Matchers::DSL
|
5
|
+
|
6
|
+
# @api public
|
7
|
+
# Passes if `response` has a redirect to special url
|
8
|
+
#
|
9
|
+
# @example Accepts numeric and symbol statuses
|
10
|
+
# response = action.call(params)
|
11
|
+
# expect(response).to redirect_to('site.com')
|
12
|
+
#
|
13
|
+
matcher :redirect_to do |url|
|
14
|
+
attr_reader :actual, :object, :location
|
15
|
+
|
16
|
+
description { "have redirect to url" }
|
17
|
+
match do |object|
|
18
|
+
@object = object
|
19
|
+
@location = object[1]['Location']
|
20
|
+
@actual = object.first
|
21
|
+
RSpec::Hanami::MatchStatus.new.call(actual, :redirect) &&
|
22
|
+
location == url
|
23
|
+
end
|
24
|
+
|
25
|
+
failure_message { |actual| "expect #{object} to redirect #{url}" }
|
26
|
+
diffable
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/rspec/hanami/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-hanami
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anton Davydov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -95,8 +95,13 @@ files:
|
|
95
95
|
- README.md
|
96
96
|
- Rakefile
|
97
97
|
- lib/rspec/hanami.rb
|
98
|
+
- lib/rspec/hanami/form_parser.rb
|
99
|
+
- lib/rspec/hanami/match_status.rb
|
98
100
|
- lib/rspec/hanami/matchers.rb
|
101
|
+
- lib/rspec/hanami/matchers/be_status.rb
|
102
|
+
- lib/rspec/hanami/matchers/form_matchers.rb
|
99
103
|
- lib/rspec/hanami/matchers/have_http_status.rb
|
104
|
+
- lib/rspec/hanami/matchers/redirect_to.rb
|
100
105
|
- lib/rspec/hanami/version.rb
|
101
106
|
- rspec-hanami.gemspec
|
102
107
|
homepage: https://github.com/davydovanton/rspec-hanami
|