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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bf5af4a13928500eeb05766325ec71498e894293
4
- data.tar.gz: 1c8a50b5f315674be91a1e197d0ef5beec599df9
3
+ metadata.gz: fe7810f3f6091b3f84741650bc0b0ed5fc47c6e4
4
+ data.tar.gz: e9bcc09993433dc175bb0246e6a1b6e073c99645
5
5
  SHA512:
6
- metadata.gz: 475c52b5823b42c36831f750cb1d22230049027416f525c268e55f97347d13ed6c63eb91bd0357f092dd0a237a4fdb14281860961cf888c7bc1afde8930b6f36
7
- data.tar.gz: dcfc1d27771f5937d6f063d5d8852ade45b6863a2312f3b71f7c386458c00db618dce2f80235590a9461a2c62b7b37dc178ec7b845e6c724e4eac8eac7ef95ee
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
- ## TODO
43
+ #### `be_success`
44
+ Passes if `response` has a not 4xx and 5xx error code.
44
45
 
45
- - [ ] `expect(action).to be_success`
46
- - [ ] `expect(response).to render_template("index")`
47
- - [ ] `expect(response).to redirect_to(widgets_path)`
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
- * [https://github.com/rspec/rspec](https://github.com/rspec/rspec)
52
- * [https://github.com/rspec/rspec-core](https://github.com/rspec/rspec-core)
53
- * [https://github.com/rspec/rspec-expectations](https://github.com/rspec/rspec-expectations)
54
- * [https://github.com/rspec/rspec-mocks](https://github.com/rspec/rspec-mocks)
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
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module Hanami
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
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.1.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-25 00:00:00.000000000 Z
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