shoulda-matchers-viewcomponent 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bb373e08e2294cd568fd64d7fd0cd12b39b40cbd9a906f731d5657512fedfac9
4
+ data.tar.gz: befd64bdf96be45b81885bf9df77fa666329998ab23c891fb8d90828c3a03b5a
5
+ SHA512:
6
+ metadata.gz: 63a059031a017c707a3d4e8ae9085927536d000dff85df3dc1ad4890ef4fbb722dd7d38ede128a7fae46b5b384c3b41e53b061aefab122797bc78270d803ad5a
7
+ data.tar.gz: 3c18e5eb1cbf3ac47a16b236e94665b4683c21e799e9371686d594f7b7b24a42cf4a9b577855b9da12cccab9c60645ff19405458709ff0429871d60aac632407
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-09-12
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in shoulda-matchers-viewcomponent.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Pedro Carbajal and Beezwax Datatools, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+ Footer
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # Shoulda Matchers for ViewComponent
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/shoulda-matchers-viewcomponent.svg?style=flat)](https://rubygems.org/gems/shoulda-matchers-viewcomponent)
4
+ [![CI](https://github.com/beezwax/shoulda-matchers-viewcomponent/actions/workflows/ci.yml/badge.svg)](https://github.com/beezwax/shoulda-matchers-viewcomponent/actions/workflows/ci.yml)
5
+ [![Powered by Beezwax](https://img.shields.io/badge/Powered%20By-Beezwax-gold?logo=data:image/svg+xml;charset=utf-8;base64,PHN2ZyB3aWR0aD0iMTA5IiBoZWlnaHQ9IjEwOSIgdmlld0JveD0iMCAwIDEwOSAxMDkiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+DQogICA8Zz4NCiAgICAgIDxwYXRoIGQ9Ik01MC44IDEwNi42MjVDNTkuMSA5OS4zMjUgNjEuOSA5MS42MjUgNjEuOSA5MS42MjVDNjMuMiA4Ny43MjUgNjQuNyA4NC41MjUgNjQuNyA2OS4zMjVDNjQuNyA0Ni40MjUgNTYuMiAzOC45MjUgNDcgMzIuMDI1QzQwLjggMjcuMzI1IDI0LjkgMjEuMTI1IDE1LjUgMTcuNjI1TDIuMjk5OTggMjUuMTI1QzEuNDk5OTggMjU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WiIgZmlsbD0iI0ZGRDkzOSI+PC9wYXRoPg0KICAgPC9nPg0KPC9zdmc+)](https://beezwax.net/)
6
+
7
+ Adds convenient
8
+ [ViewComponent](https://github.com/ViewComponent/view_component) matchers to
9
+ [shoulda matchers](https://github.com/thoughtbot/shoulda-matchers).
10
+
11
+ ## Installation
12
+
13
+ Include `shoulda-matchers-viewcomponent` in your Gemfile:
14
+
15
+ ```ruby
16
+ group :test do
17
+ # ...
18
+ gem 'shoulda-matchers-viewcomponent'
19
+ end
20
+ ```
21
+
22
+ And run `bundle`.
23
+
24
+ Then configure Shoulda to use the ViewComponent matchers:
25
+
26
+ ```ruby
27
+ # If using RSpec, this would typically go in spec/rails_helper.rb
28
+ Shoulda::Matchers.configure do |config|
29
+ config.integrate do |with|
30
+ # ...
31
+ with.library :view_component
32
+ end
33
+ end
34
+ ```
35
+
36
+ ## Usage
37
+
38
+ At the moment only one matcher exists, `use_collection_parameter`, which
39
+ checks that a collection parameter was set for the component through
40
+ `with_collection_parameter`. E.g.:
41
+
42
+ ```ruby
43
+ # app/components/product_component.rb
44
+ class ProductComponent < ViewComponent::Base
45
+ with_collection_parameter :product
46
+
47
+ def initialize(product:, product_counter:, product_iteration:)
48
+ # ...
49
+ end
50
+ end
51
+
52
+ # spec/components/product_component_spec.rb
53
+ RSpec.describe ProductComponent, type: :component do
54
+ it do
55
+ expect(described_class).to use_collection_parameter(:product)
56
+ .with_counter # Optional, checks for product_counter: param
57
+ .with_iteration # Optional, checks for product_iteration: param
58
+ end
59
+ end
60
+ ```
61
+
62
+ ## Development
63
+
64
+ After checking out the repo, run `bin/setup` to install dependencies. You can
65
+ also run `bin/console` for an interactive prompt that will allow you to
66
+ experiment.
67
+
68
+ To install this gem onto your local machine, run `bundle exec rake install`. To
69
+ release a new version, update the version number in `version.rb`, and then run
70
+ `bundle exec rake release`, which will create a git tag for the version, push
71
+ git commits and the created tag, and push the `.gem` file to
72
+ [rubygems.org](https://rubygems.org).
73
+
74
+ ## Contributing
75
+
76
+ Bug reports and pull requests are welcome on GitHub at
77
+ https://github.com/beezwax/shoulda-matchers-viewcomponent.
78
+
79
+ ## License
80
+
81
+ The gem is available as open source under the terms of the
82
+ [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shoulda
4
+ module Matchers
5
+ module Integrations
6
+ module Libraries
7
+ class ViewComponent
8
+ Integrations.register_library(self, :view_component)
9
+
10
+ include Integrations::Inclusion
11
+ include Integrations::Rails
12
+
13
+ def integrate_with(test_framework)
14
+ test_framework.include(matchers_module, type: :component)
15
+ end
16
+
17
+ private
18
+
19
+ def matchers_module
20
+ Shoulda::Matchers::ViewComponent
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shoulda
4
+ module Matchers
5
+ module ViewComponent
6
+ # The `use_collection_parameter` matcher tests usage of the
7
+ # `with_collection_parameter` macro.
8
+ #
9
+ # class ProductComponent < ViewComponent::Base
10
+ # with_collection_parameter :product
11
+ #
12
+ # def initialize(product:)
13
+ # end
14
+ # end
15
+ #
16
+ # # RSpec
17
+ # RSpec.describe ProductComponent, type: :component do
18
+ # it { expect(described_class).to use_collection_parameter(:product) }
19
+ # end
20
+ #
21
+ # # Minitest (Shoulda)
22
+ # class ProductComponentTest < ActiveSupport::TestCase
23
+ # should use_collection_parameter(:product)
24
+ # end
25
+ #
26
+ # @return [HaveReadonlyAttributeMatcher]
27
+ #
28
+ def use_collection_parameter(value)
29
+ UseCollectionParameter.new(value)
30
+ end
31
+
32
+ # @private
33
+ class UseCollectionParameter
34
+ def initialize(attribute)
35
+ @attribute = attribute.to_s
36
+ end
37
+
38
+ def matches?(subject)
39
+ @subject = subject
40
+
41
+ collection_parameter_matches? &&
42
+ has_matching_initialize_parameter? &&
43
+ requires_counter? && requires_iteration?
44
+ end
45
+
46
+ def description
47
+ description = "use collection parameter " \
48
+ ":#{@attribute} with matching #initialize(#{@attribute}:"
49
+
50
+ if @with_counter
51
+ description += ", #{@attribute}_counter:"
52
+ end
53
+
54
+ if @with_iteration
55
+ description += ", #{@attribute}_iteration:"
56
+ end
57
+
58
+ description += '[, ...]) keyword argument'
59
+
60
+ description += 's' if @with_counter || @with_iteration
61
+
62
+ description
63
+ end
64
+
65
+ def failure_message
66
+ "expected #{@subject} to #{description}"
67
+ end
68
+
69
+ def failure_message_when_negated
70
+ "expected #{@subject} not to #{description}"
71
+ end
72
+
73
+ def with_counter
74
+ @with_counter = true
75
+ self
76
+ end
77
+
78
+ def with_iteration
79
+ @with_iteration = true
80
+ self
81
+ end
82
+
83
+ private
84
+
85
+ def collection_parameter_matches?
86
+ @subject.collection_parameter.to_s == @attribute
87
+ end
88
+
89
+ def has_matching_initialize_parameter?
90
+ has_keyword_parameter?(@attribute)
91
+ end
92
+
93
+ def requires_counter?
94
+ return true unless @with_counter
95
+ has_keyword_parameter?("#{@attribute}_counter")
96
+ end
97
+
98
+ def requires_iteration?
99
+ return true unless @with_iteration
100
+ has_keyword_parameter?("#{@attribute}_iteration")
101
+ end
102
+
103
+ def has_keyword_parameter?(parameter)
104
+ @subject.instance_method(:initialize).parameters.any? do |(type, name)|
105
+ type == :keyreq && name.to_s == parameter
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shoulda
4
+ module Matchers
5
+ module ViewComponent
6
+ VERSION = "1.0.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'shoulda/matchers/view_component/use_collection_parameter'
4
+ require 'shoulda/matchers/integrations/libraries/view_component'
5
+
6
+ module Shoulda
7
+ module Matchers
8
+ module ViewComponent
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "view_component"
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/shoulda/matchers/view_component/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "shoulda-matchers-viewcomponent"
7
+ spec.version = Shoulda::Matchers::ViewComponent::VERSION
8
+ spec.authors = ["Pedro Carbajal"]
9
+ spec.email = ["pedro_c@beezwax.net"]
10
+
11
+ spec.summary = "Shoulda matchers extension for ViewComponent"
12
+ spec.homepage = "https://github.com/beezwax/shoulda-matchers-viewcomponent"
13
+ spec.required_ruby_version = ">= 2.6.0"
14
+
15
+ spec.license = "MIT"
16
+
17
+ #spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+ spec.metadata["changelog_uri"] = "https://github.com/beezwax/shoulda-matchers-viewcomponent/blob/master/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_dependency "shoulda-matchers", "~> 4.0"
35
+
36
+ spec.add_development_dependency "view_component"
37
+ spec.add_development_dependency "rspec"
38
+ spec.add_development_dependency "rails"
39
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shoulda-matchers-viewcomponent
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Pedro Carbajal
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-09-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: shoulda-matchers
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: view_component
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - pedro_c@beezwax.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".rspec"
77
+ - CHANGELOG.md
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/shoulda/matchers/integrations/libraries/view_component.rb
83
+ - lib/shoulda/matchers/view_component.rb
84
+ - lib/shoulda/matchers/view_component/use_collection_parameter.rb
85
+ - lib/shoulda/matchers/view_component/version.rb
86
+ - lib/shoulda/matchers/viewcomponent.rb
87
+ - shoulda-matchers-viewcomponent.gemspec
88
+ homepage: https://github.com/beezwax/shoulda-matchers-viewcomponent
89
+ licenses:
90
+ - MIT
91
+ metadata:
92
+ homepage_uri: https://github.com/beezwax/shoulda-matchers-viewcomponent
93
+ source_code_uri: https://github.com/beezwax/shoulda-matchers-viewcomponent
94
+ changelog_uri: https://github.com/beezwax/shoulda-matchers-viewcomponent/blob/master/CHANGELOG.md
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 2.6.0
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubygems_version: 3.3.3
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Shoulda matchers extension for ViewComponent
114
+ test_files: []