factory_bot-blueprint-rspec 0.1.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: 383c2e021a8f95d413975221ad71a812573085d87c344d9092489cd4701c7bcc
4
+ data.tar.gz: 855d528ff18cfa4e02645fd6eb7a24b65e170918ba759454ccd06bc5c7e14574
5
+ SHA512:
6
+ metadata.gz: 791b93263904413b1bbdc11df06f18c5181fe44882a10c1d9b6c114b1eec33ee3222edf6a52fff90af03aead6e9e04dfedcf22e8410687bd8d11edea92203a26
7
+ data.tar.gz: 057af3f3d8bba6f971a4f6e18c40ab69a13829973b38b3bf1c73ddb6477104d704d5d24d524cbf6b567c8f2ebf2c4e1a7587161c3e601256efd9bfb843040f85
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 yubrot
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.
data/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # `factory_bot-blueprint-rspec` gem
2
+
3
+ This is a part of [FactoryBot::Blueprint](https://github.com/yubrot/factory_bot-blueprint) library.
4
+
5
+ `factory_bot-blueprint` integration for RSpec.
6
+
7
+ ## License
8
+
9
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,171 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FactoryBot
4
+ module Blueprint
5
+ module RSpec
6
+ # Helper methods to integrate <code>factory_bot-blueprint</code> with RSpec.
7
+ # This module is automatically extended to {RSpec::Core::ExampleGroup}.
8
+ #
9
+ # To use FactoryBot::Blueprint from RSpec with minimal effort, usually {#letbp} is the best choice.
10
+ module Driver
11
+ # Shorthand method for <code>let(name) { ::FactoryBot::Blueprint.plan(...) }</code>.
12
+ # You can access the let binding context by <code>#ext</code> in DSL code.
13
+ # @param name [Symbol] name of the object to be declared using RSpec's <code>let</code>
14
+ # @param inherit [Boolean] whether to extend the blueprint by <code>super()</code>
15
+ # @yield Write Blueprint DSL code here
16
+ # @example
17
+ # RSpec.describe "something" do
18
+ # let(:blog_id) { SecureRandom.uuid }
19
+ #
20
+ # let_blueprint(:blog_bp) do
21
+ # let.blog(id: ext.blog_id, title: "Daily log") do
22
+ # let.article(title: "Article 1")
23
+ # article(title: "Article 2")
24
+ # article(title: "Article 3")
25
+ # end
26
+ # end
27
+ # end
28
+ def let_blueprint(name, inherit: false, &)
29
+ let(name) { ::FactoryBot::Blueprint.plan(inherit ? super() : nil, ext: self, &) }
30
+ end
31
+
32
+ # Build objects by <code>build</code> strategy in FactoryBot from a blueprint and declare them using RSpec's
33
+ # <code>let</code>.
34
+ # @param map [Hash{Symbol => Object}]
35
+ # map data structure from source blueprints to instance definitions.
36
+ # Each instance will be built with <code>FactoryBot::Blueprint.build(__send__(source))</code>
37
+ # @example
38
+ # RSpec.describe "something" do
39
+ # let_blueprint(:blog_bp) do
40
+ # # ... Write some DSL ...
41
+ # end
42
+ #
43
+ # # Simplest example:
44
+ # # This is equivalent to `let_blueprint_build blog_bp: { representative: :blog }`
45
+ # let_blueprint_build blog_bp: :blog
46
+ #
47
+ # # Another shorthand example:
48
+ # # This is equivalent to `let_blueprint_build blog_bp: { items: %i[blog article] }`
49
+ # let_blueprint_build blog_bp: %i[blog article]
50
+ #
51
+ # # Most flexible example:
52
+ # # :representative specifies the name of the representative object to be declared. Defaults to nil
53
+ # # :items specifies the names of the objects to be declared. Defaults to []
54
+ # # :instance specifies the name of the instance object to be declared. Defaults to :"#{source}_instance"
55
+ # let_blueprint_build blog_bp: { representative: :blog, items: %i[article], instance: :blog_instance }
56
+ #
57
+ # # Above example will be expanded to:
58
+ # let(:blog_instance) { ::FactoryBot::Blueprint.build(blog_bp) } # the instance object
59
+ # let(:blog) { blog_instance[blog_bp.representative_node.name] } # the representative object
60
+ # let(:article) { blog_instance[:article] } # the item objects
61
+ # end
62
+ def let_blueprint_build(**map) = let_blueprint_instantiate(:build, **map)
63
+
64
+ # Build objects by <code>create</code> strategy in FactoryBot from a blueprint and declare them using RSpec's
65
+ # <code>let</code>.
66
+ # See {#let_blueprint_build} for more details.
67
+ # @param map [Hash{Symbol => Object}]
68
+ # map data structure from source blueprints to instance definitions.
69
+ # Each instance will be built with <code>FactoryBot::Blueprint.build(__send__(source))</code>
70
+ def let_blueprint_create(**map) = let_blueprint_instantiate(:create, **map)
71
+
72
+ # @!visibility private
73
+ def let_blueprint_instantiate(strategy, **map)
74
+ raise ArgumentError, "Unsupported strategy: #{strategy}" if strategy && !%i[create build].include?(strategy)
75
+
76
+ map.each do |source, definition|
77
+ raise TypeError, "source must be a Symbol" unless source.is_a?(Symbol)
78
+
79
+ definition =
80
+ case definition
81
+ when Symbol
82
+ { representative: definition }
83
+ when Array
84
+ { items: definition }
85
+ when Hash
86
+ definition
87
+ else
88
+ raise TypeError, "definition must be one of Symbol, Array, Hash"
89
+ end
90
+
91
+ representative_name = definition[:representative]
92
+ item_names = definition[:items] || []
93
+ instance = definition[:instance] || :"#{source}_instance"
94
+
95
+ if representative_name && !representative_name.is_a?(Symbol)
96
+ raise TypeError, "representative must be a Symbol"
97
+ end
98
+ if !item_names.is_a?(Array) || !item_names.all? { _1.is_a?(Symbol) }
99
+ raise TypeError, "items must be an Array of Symbols"
100
+ end
101
+ raise TypeError, "instance must be a Symbol" unless instance.is_a?(Symbol)
102
+
103
+ if strategy # If no strategy is specified, the instance is assumed to exist
104
+ let(instance) { ::FactoryBot::Blueprint.instantiate(strategy, __send__(source)) }
105
+ end
106
+
107
+ if representative_name
108
+ let(representative_name) { __send__(instance)[__send__(source).representative_node.name] }
109
+ end
110
+
111
+ item_names.each do |name|
112
+ let(name) { __send__(instance)[name] }
113
+ end
114
+ end
115
+ end
116
+
117
+ # Write the blueprint in DSL, create an instance of it, and declare each object of the instance using RSpec's
118
+ # <code>let</code>.
119
+ #
120
+ # This is a shorthand for {#let_blueprint} with {#let_blueprint_build} or {#let_blueprint_create}.
121
+ # @param name [Symbol]
122
+ # name of the representative object to be declared using RSpec's <code>let</code>.
123
+ # It is also used as a name prefix of the blueprint
124
+ # @param items [Array<Symbol>] names of the objects to be declared using RSpec's <code>let</code>
125
+ # @param inherit [Boolean] whether to extend the blueprint by <code>super()</code>
126
+ # @param strategy [:create, :build]
127
+ # FactoryBot strategy to use when building objects.
128
+ # This option is ignored if <code>inherit: true</code>
129
+ # @yield Write Blueprint DSL code here
130
+ # @example
131
+ # RSpec.describe "something" do
132
+ # letbp(:blog, %i[article]) do
133
+ # blog(title: "Daily log") do
134
+ # let.article(title: "Article 1")
135
+ # article(title: "Article 2")
136
+ # article(title: "Article 3")
137
+ # end
138
+ # end
139
+ #
140
+ # # Above example will be expanded to:
141
+ # let_blueprint(:blog_blueprint) do
142
+ # blog(title: "Daily log") do
143
+ # let.article(title: "Article 1")
144
+ # article(title: "Article 2")
145
+ # article(title: "Article 3")
146
+ # end
147
+ # end
148
+ # let_blueprint_create blog_blueprint: { representative: :blog, items: %i[article] }
149
+ # end
150
+ def letbp(name, items = [], inherit: false, strategy: :create, &)
151
+ raise TypeError, "name must be a Symbol" unless name.is_a?(Symbol)
152
+
153
+ source = :"#{name}_blueprint"
154
+ strategy = nil if inherit
155
+
156
+ let_blueprint(source, inherit:, &)
157
+ let_blueprint_instantiate strategy, source => { representative: name, items: }
158
+ end
159
+ end
160
+ end
161
+ end
162
+ end
163
+
164
+ # @!visibility private
165
+ module RSpec
166
+ module Core
167
+ class ExampleGroup
168
+ extend ::FactoryBot::Blueprint::RSpec::Driver
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FactoryBot
4
+ module Blueprint
5
+ module RSpec
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "factory_bot/blueprint"
4
+ require_relative "rspec/version"
5
+ require_relative "rspec/driver"
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factory_bot-blueprint-rspec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - yubrot
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: factory_bot-blueprint
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-core
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ description: FactoryBot::Blueprint integration for RSpec
42
+ email:
43
+ - yubrot@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - LICENSE.txt
49
+ - README.md
50
+ - lib/factory_bot/blueprint/rspec.rb
51
+ - lib/factory_bot/blueprint/rspec/driver.rb
52
+ - lib/factory_bot/blueprint/rspec/version.rb
53
+ homepage: https://github.com/yubrot/factory_bot-blueprint
54
+ licenses:
55
+ - MIT
56
+ metadata:
57
+ homepage_uri: https://github.com/yubrot/factory_bot-blueprint
58
+ source_code_uri: https://github.com/yubrot/factory_bot-blueprint
59
+ changelog_uri: https://github.com/yubrot/factory_bot-blueprint/blob/main/CHANGELOG.md
60
+ rubygems_mfa_required: 'true'
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 3.1.0
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.5.11
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: FactoryBot::Blueprint integration for RSpec
80
+ test_files: []