ffactory_girl 4.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +7 -0
  2. data/.autotest +9 -0
  3. data/.gitignore +9 -0
  4. data/.rspec +2 -0
  5. data/.simplecov +4 -0
  6. data/.travis.yml +38 -0
  7. data/.yardopts +5 -0
  8. data/Appraisals +19 -0
  9. data/CONTRIBUTING.md +60 -0
  10. data/GETTING_STARTED.md +1391 -0
  11. data/Gemfile +8 -0
  12. data/Gemfile.lock +116 -0
  13. data/LICENSE +19 -0
  14. data/NAME.md +11 -0
  15. data/NEWS +290 -0
  16. data/README.md +98 -0
  17. data/Rakefile +36 -0
  18. data/cucumber.yml +1 -0
  19. data/factory_girl.gemspec +36 -0
  20. data/gemfiles/3.2.gemfile +10 -0
  21. data/gemfiles/3.2.gemfile.lock +105 -0
  22. data/gemfiles/4.0.gemfile +10 -0
  23. data/gemfiles/4.0.gemfile.lock +105 -0
  24. data/gemfiles/4.1.gemfile +10 -0
  25. data/gemfiles/4.1.gemfile.lock +104 -0
  26. data/gemfiles/4.2.gemfile +10 -0
  27. data/gemfiles/4.2.gemfile.lock +104 -0
  28. data/gemfiles/5.0.gemfile +10 -0
  29. data/gemfiles/5.0.gemfile.lock +103 -0
  30. data/lib/factory_girl/aliases.rb +18 -0
  31. data/lib/factory_girl/attribute/association.rb +27 -0
  32. data/lib/factory_girl/attribute/dynamic.rb +24 -0
  33. data/lib/factory_girl/attribute/sequence.rb +16 -0
  34. data/lib/factory_girl/attribute/static.rb +16 -0
  35. data/lib/factory_girl/attribute.rb +62 -0
  36. data/lib/factory_girl/attribute_assigner.rb +97 -0
  37. data/lib/factory_girl/attribute_list.rb +67 -0
  38. data/lib/factory_girl/callback.rb +40 -0
  39. data/lib/factory_girl/callbacks_observer.rb +21 -0
  40. data/lib/factory_girl/configuration.rb +37 -0
  41. data/lib/factory_girl/declaration/association.rb +28 -0
  42. data/lib/factory_girl/declaration/dynamic.rb +26 -0
  43. data/lib/factory_girl/declaration/implicit.rb +33 -0
  44. data/lib/factory_girl/declaration/static.rb +26 -0
  45. data/lib/factory_girl/declaration.rb +23 -0
  46. data/lib/factory_girl/declaration_list.rb +49 -0
  47. data/lib/factory_girl/decorator/attribute_hash.rb +16 -0
  48. data/lib/factory_girl/decorator/class_key_hash.rb +28 -0
  49. data/lib/factory_girl/decorator/disallows_duplicates_registry.rb +13 -0
  50. data/lib/factory_girl/decorator/invocation_tracker.rb +19 -0
  51. data/lib/factory_girl/decorator/new_constructor.rb +12 -0
  52. data/lib/factory_girl/decorator.rb +21 -0
  53. data/lib/factory_girl/definition.rb +136 -0
  54. data/lib/factory_girl/definition_hierarchy.rb +48 -0
  55. data/lib/factory_girl/definition_proxy.rb +174 -0
  56. data/lib/factory_girl/errors.rb +25 -0
  57. data/lib/factory_girl/evaluation.rb +27 -0
  58. data/lib/factory_girl/evaluator.rb +82 -0
  59. data/lib/factory_girl/evaluator_class_definer.rb +20 -0
  60. data/lib/factory_girl/factory.rb +163 -0
  61. data/lib/factory_girl/factory_runner.rb +33 -0
  62. data/lib/factory_girl/find_definitions.rb +25 -0
  63. data/lib/factory_girl/linter.rb +97 -0
  64. data/lib/factory_girl/null_factory.rb +18 -0
  65. data/lib/factory_girl/null_object.rb +24 -0
  66. data/lib/factory_girl/registry.rb +38 -0
  67. data/lib/factory_girl/reload.rb +8 -0
  68. data/lib/factory_girl/sequence.rb +62 -0
  69. data/lib/factory_girl/strategy/attributes_for.rb +13 -0
  70. data/lib/factory_girl/strategy/build.rb +15 -0
  71. data/lib/factory_girl/strategy/create.rb +18 -0
  72. data/lib/factory_girl/strategy/null.rb +11 -0
  73. data/lib/factory_girl/strategy/stub.rb +111 -0
  74. data/lib/factory_girl/strategy_calculator.rb +26 -0
  75. data/lib/factory_girl/strategy_syntax_method_registrar.rb +54 -0
  76. data/lib/factory_girl/syntax/default.rb +76 -0
  77. data/lib/factory_girl/syntax/methods.rb +111 -0
  78. data/lib/factory_girl/syntax.rb +7 -0
  79. data/lib/factory_girl/syntax_runner.rb +6 -0
  80. data/lib/factory_girl/trait.rb +30 -0
  81. data/lib/factory_girl/version.rb +3 -0
  82. data/lib/factory_girl.rb +156 -0
  83. metadata +271 -0
@@ -0,0 +1,62 @@
1
+ require 'factory_girl/attribute/static'
2
+ require 'factory_girl/attribute/dynamic'
3
+ require 'factory_girl/attribute/association'
4
+ require 'factory_girl/attribute/sequence'
5
+
6
+ module FactoryGirl
7
+ # @api private
8
+ class Attribute
9
+ attr_reader :name, :ignored
10
+
11
+ def initialize(name, ignored)
12
+ @name = name.to_sym
13
+ @ignored = ignored
14
+ ensure_non_attribute_writer!
15
+ end
16
+
17
+ def to_proc
18
+ -> { }
19
+ end
20
+
21
+ def association?
22
+ false
23
+ end
24
+
25
+ def alias_for?(attr)
26
+ FactoryGirl.aliases_for(attr).include?(name)
27
+ end
28
+
29
+ private
30
+
31
+ def ensure_non_attribute_writer!
32
+ NonAttributeWriterValidator.new(@name).validate!
33
+ end
34
+
35
+ class NonAttributeWriterValidator
36
+ def initialize(method_name)
37
+ @method_name = method_name.to_s
38
+ @method_name_setter_match = @method_name.match(/(.*)=$/)
39
+ end
40
+
41
+ def validate!
42
+ if method_is_writer?
43
+ raise AttributeDefinitionError, error_message
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def method_is_writer?
50
+ !!@method_name_setter_match
51
+ end
52
+
53
+ def attribute_name
54
+ @method_name_setter_match[1]
55
+ end
56
+
57
+ def error_message
58
+ "factory_girl uses '#{attribute_name} value' syntax rather than '#{attribute_name} = value'"
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,97 @@
1
+ module FactoryGirl
2
+ # @api private
3
+ class AttributeAssigner
4
+ def initialize(evaluator, build_class, &instance_builder)
5
+ @build_class = build_class
6
+ @instance_builder = instance_builder
7
+ @evaluator = evaluator
8
+ @attribute_list = evaluator.class.attribute_list
9
+ @attribute_names_assigned = []
10
+ end
11
+
12
+ def object
13
+ @evaluator.instance = build_class_instance
14
+ build_class_instance.tap do |instance|
15
+ attributes_to_set_on_instance.each do |attribute|
16
+ instance.public_send("#{attribute}=", get(attribute))
17
+ @attribute_names_assigned << attribute
18
+ end
19
+ end
20
+ end
21
+
22
+ def hash
23
+ @evaluator.instance = build_hash
24
+
25
+ attributes_to_set_on_hash.inject({}) do |result, attribute|
26
+ result[attribute] = get(attribute)
27
+ result
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def method_tracking_evaluator
34
+ @method_tracking_evaluator ||= Decorator::AttributeHash.new(decorated_evaluator, attribute_names_to_assign)
35
+ end
36
+
37
+ def decorated_evaluator
38
+ Decorator::InvocationTracker.new(
39
+ Decorator::NewConstructor.new(@evaluator, @build_class)
40
+ )
41
+ end
42
+
43
+ def methods_invoked_on_evaluator
44
+ method_tracking_evaluator.__invoked_methods__
45
+ end
46
+
47
+ def build_class_instance
48
+ @build_class_instance ||= method_tracking_evaluator.instance_exec(&@instance_builder)
49
+ end
50
+
51
+ def build_hash
52
+ @build_hash ||= NullObject.new(hash_instance_methods_to_respond_to)
53
+ end
54
+
55
+ def get(attribute_name)
56
+ @evaluator.send(attribute_name)
57
+ end
58
+
59
+ def attributes_to_set_on_instance
60
+ (attribute_names_to_assign - @attribute_names_assigned - methods_invoked_on_evaluator).uniq
61
+ end
62
+
63
+ def attributes_to_set_on_hash
64
+ attribute_names_to_assign - association_names
65
+ end
66
+
67
+ def attribute_names_to_assign
68
+ @attribute_names_to_assign ||= non_ignored_attribute_names + override_names - ignored_attribute_names - alias_names_to_ignore
69
+ end
70
+
71
+ def non_ignored_attribute_names
72
+ @attribute_list.non_ignored.names
73
+ end
74
+
75
+ def ignored_attribute_names
76
+ @attribute_list.ignored.names
77
+ end
78
+
79
+ def association_names
80
+ @attribute_list.associations.names
81
+ end
82
+
83
+ def override_names
84
+ @evaluator.__override_names__
85
+ end
86
+
87
+ def hash_instance_methods_to_respond_to
88
+ @attribute_list.names + override_names + @build_class.instance_methods
89
+ end
90
+
91
+ def alias_names_to_ignore
92
+ @attribute_list.non_ignored.flat_map do |attribute|
93
+ override_names.map { |override| attribute.name if attribute.alias_for?(override) && attribute.name != override && !ignored_attribute_names.include?(override) }
94
+ end.compact
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,67 @@
1
+ module FactoryGirl
2
+ # @api private
3
+ class AttributeList
4
+ include Enumerable
5
+
6
+ def initialize(name = nil, attributes = [])
7
+ @name = name
8
+ @attributes = attributes
9
+ end
10
+
11
+ def define_attribute(attribute)
12
+ ensure_attribute_not_self_referencing! attribute
13
+ ensure_attribute_not_defined! attribute
14
+
15
+ add_attribute attribute
16
+ end
17
+
18
+ def each(&block)
19
+ @attributes.each(&block)
20
+ end
21
+
22
+ def names
23
+ map(&:name)
24
+ end
25
+
26
+ def associations
27
+ AttributeList.new(@name, select(&:association?))
28
+ end
29
+
30
+ def ignored
31
+ AttributeList.new(@name, select(&:ignored))
32
+ end
33
+
34
+ def non_ignored
35
+ AttributeList.new(@name, reject(&:ignored))
36
+ end
37
+
38
+ def apply_attributes(attributes_to_apply)
39
+ attributes_to_apply.each { |attribute| add_attribute(attribute) }
40
+ end
41
+
42
+ private
43
+
44
+ def add_attribute(attribute)
45
+ @attributes << attribute
46
+ attribute
47
+ end
48
+
49
+ def ensure_attribute_not_defined!(attribute)
50
+ if attribute_defined?(attribute.name)
51
+ raise AttributeDefinitionError, "Attribute already defined: #{attribute.name}"
52
+ end
53
+ end
54
+
55
+ def ensure_attribute_not_self_referencing!(attribute)
56
+ if attribute.respond_to?(:factory) && attribute.factory == @name
57
+ raise AssociationDefinitionError, "Self-referencing association '#{attribute.name}' in '#{attribute.factory}'"
58
+ end
59
+ end
60
+
61
+ def attribute_defined?(attribute_name)
62
+ @attributes.any? do |attribute|
63
+ attribute.name == attribute_name
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,40 @@
1
+ module FactoryGirl
2
+ class Callback
3
+ attr_reader :name
4
+
5
+ def initialize(name, block)
6
+ @name = name.to_sym
7
+ @block = block
8
+ ensure_valid_callback_name!
9
+ end
10
+
11
+ def run(instance, evaluator)
12
+ case block.arity
13
+ when 1, -1 then syntax_runner.instance_exec(instance, &block)
14
+ when 2 then syntax_runner.instance_exec(instance, evaluator, &block)
15
+ else syntax_runner.instance_exec(&block)
16
+ end
17
+ end
18
+
19
+ def ==(other)
20
+ name == other.name &&
21
+ block == other.block
22
+ end
23
+
24
+ protected
25
+ attr_reader :block
26
+
27
+ private
28
+
29
+ def ensure_valid_callback_name!
30
+ unless FactoryGirl.callback_names.include?(name)
31
+ raise InvalidCallbackNameError, "#{name} is not a valid callback name. " +
32
+ "Valid callback names are #{FactoryGirl.callback_names.inspect}"
33
+ end
34
+ end
35
+
36
+ def syntax_runner
37
+ @syntax_runner ||= SyntaxRunner.new
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,21 @@
1
+ module FactoryGirl
2
+ # @api private
3
+ class CallbacksObserver
4
+ def initialize(callbacks, evaluator)
5
+ @callbacks = callbacks
6
+ @evaluator = evaluator
7
+ end
8
+
9
+ def update(name, result_instance)
10
+ callbacks_by_name(name).each do |callback|
11
+ callback.run(result_instance, @evaluator)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def callbacks_by_name(name)
18
+ @callbacks.select { |callback| callback.name == name }
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,37 @@
1
+ module FactoryGirl
2
+ # @api private
3
+ class Configuration
4
+ attr_reader :factories, :sequences, :traits, :strategies, :callback_names
5
+
6
+ attr_accessor :allow_class_lookup, :use_parent_strategy
7
+
8
+ def initialize
9
+ @factories = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Factory'))
10
+ @sequences = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Sequence'))
11
+ @traits = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Trait'))
12
+ @strategies = Registry.new('Strategy')
13
+ @callback_names = Set.new
14
+ @definition = Definition.new
15
+
16
+ @allow_class_lookup = true
17
+
18
+ to_create { |instance| instance.save! }
19
+ initialize_with { new }
20
+ end
21
+
22
+ delegate :to_create, :skip_create, :constructor, :before, :after,
23
+ :callback, :callbacks, to: :@definition
24
+
25
+ def initialize_with(&block)
26
+ @definition.define_constructor(&block)
27
+ end
28
+
29
+ def duplicate_attribute_assignment_from_initialize_with
30
+ false
31
+ end
32
+
33
+ def duplicate_attribute_assignment_from_initialize_with=(value)
34
+ ActiveSupport::Deprecation.warn 'Assignment of duplicate_attribute_assignment_from_initialize_with is unnecessary as this is now default behavior in FactoryGirl 4.0; this line can be removed', caller
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,28 @@
1
+ module FactoryGirl
2
+ class Declaration
3
+ # @api private
4
+ class Association < Declaration
5
+ def initialize(name, *options)
6
+ super(name, false)
7
+ @options = options.dup
8
+ @overrides = options.extract_options!
9
+ @traits = options
10
+ end
11
+
12
+ def ==(other)
13
+ name == other.name &&
14
+ options == other.options
15
+ end
16
+
17
+ protected
18
+ attr_reader :options
19
+
20
+ private
21
+
22
+ def build
23
+ factory_name = @overrides[:factory] || name
24
+ [Attribute::Association.new(name, factory_name, [@traits, @overrides.except(:factory)].flatten)]
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ module FactoryGirl
2
+ class Declaration
3
+ # @api private
4
+ class Dynamic < Declaration
5
+ def initialize(name, ignored = false, block = nil)
6
+ super(name, ignored)
7
+ @block = block
8
+ end
9
+
10
+ def ==(other)
11
+ name == other.name &&
12
+ ignored == other.ignored &&
13
+ block == other.block
14
+ end
15
+
16
+ protected
17
+ attr_reader :block
18
+
19
+ private
20
+
21
+ def build
22
+ [Attribute::Dynamic.new(name, @ignored, @block)]
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,33 @@
1
+ module FactoryGirl
2
+ class Declaration
3
+ # @api private
4
+ class Implicit < Declaration
5
+ def initialize(name, factory = nil, ignored = false)
6
+ super(name, ignored)
7
+ @factory = factory
8
+ end
9
+
10
+ def ==(other)
11
+ name == other.name &&
12
+ factory == other.factory &&
13
+ ignored == other.ignored
14
+ end
15
+
16
+ protected
17
+ attr_reader :factory
18
+
19
+ private
20
+
21
+ def build
22
+ if FactoryGirl.factories.registered?(name)
23
+ [Attribute::Association.new(name, name, {})]
24
+ elsif FactoryGirl.sequences.registered?(name)
25
+ [Attribute::Sequence.new(name, name, @ignored)]
26
+ else
27
+ @factory.inherit_traits([name])
28
+ []
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,26 @@
1
+ module FactoryGirl
2
+ class Declaration
3
+ # @api private
4
+ class Static < Declaration
5
+ def initialize(name, value, ignored = false)
6
+ super(name, ignored)
7
+ @value = value
8
+ end
9
+
10
+ def ==(other)
11
+ name == other.name &&
12
+ value == other.value &&
13
+ ignored == other.ignored
14
+ end
15
+
16
+ protected
17
+ attr_reader :value
18
+
19
+ private
20
+
21
+ def build
22
+ [Attribute::Static.new(name, @value, @ignored)]
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ require 'factory_girl/declaration/static'
2
+ require 'factory_girl/declaration/dynamic'
3
+ require 'factory_girl/declaration/association'
4
+ require 'factory_girl/declaration/implicit'
5
+
6
+ module FactoryGirl
7
+ # @api private
8
+ class Declaration
9
+ attr_reader :name
10
+
11
+ def initialize(name, ignored = false)
12
+ @name = name
13
+ @ignored = ignored
14
+ end
15
+
16
+ def to_attributes
17
+ build
18
+ end
19
+
20
+ protected
21
+ attr_reader :ignored
22
+ end
23
+ end
@@ -0,0 +1,49 @@
1
+ module FactoryGirl
2
+ # @api private
3
+ class DeclarationList
4
+ include Enumerable
5
+
6
+ def initialize(name = nil)
7
+ @declarations = []
8
+ @name = name
9
+ @overridable = false
10
+ end
11
+
12
+ def declare_attribute(declaration)
13
+ delete_declaration(declaration) if overridable?
14
+
15
+ @declarations << declaration
16
+ declaration
17
+ end
18
+
19
+ def overridable
20
+ @overridable = true
21
+ end
22
+
23
+ def attributes
24
+ @attributes ||= AttributeList.new(@name).tap do |list|
25
+ to_attributes.each do |attribute|
26
+ list.define_attribute(attribute)
27
+ end
28
+ end
29
+ end
30
+
31
+ def each(&block)
32
+ @declarations.each(&block)
33
+ end
34
+
35
+ private
36
+
37
+ def delete_declaration(declaration)
38
+ @declarations.delete_if { |decl| decl.name == declaration.name }
39
+ end
40
+
41
+ def to_attributes
42
+ @declarations.inject([]) { |result, declaration| result += declaration.to_attributes }
43
+ end
44
+
45
+ def overridable?
46
+ @overridable
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,16 @@
1
+ module FactoryGirl
2
+ class Decorator
3
+ class AttributeHash < Decorator
4
+ def initialize(component, attributes = [])
5
+ super(component)
6
+ @attributes = attributes
7
+ end
8
+
9
+ def attributes
10
+ @attributes.each_with_object({}) do |attribute_name, result|
11
+ result[attribute_name] = send(attribute_name)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,28 @@
1
+ module FactoryGirl
2
+ class Decorator
3
+ class ClassKeyHash < Decorator
4
+ def [](key)
5
+ @component[symbolized_key key]
6
+ end
7
+
8
+ def []=(key, value)
9
+ @component[symbolized_key key] = value
10
+ end
11
+
12
+ def key?(key)
13
+ @component.key? symbolized_key(key)
14
+ end
15
+
16
+ private
17
+
18
+ def symbolized_key(key)
19
+ if key.respond_to?(:to_sym)
20
+ key.to_sym
21
+ elsif FactoryGirl.allow_class_lookup
22
+ ActiveSupport::Deprecation.warn "Looking up factories by class is deprecated and will be removed in 5.0. Use symbols instead and set FactoryGirl.allow_class_lookup = false", caller
23
+ key.to_s.underscore.to_sym
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ module FactoryGirl
2
+ class Decorator
3
+ class DisallowsDuplicatesRegistry < Decorator
4
+ def register(name, item)
5
+ if registered?(name)
6
+ raise DuplicateDefinitionError, "#{@component.name} already registered: #{name}"
7
+ else
8
+ @component.register(name, item)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ module FactoryGirl
2
+ class Decorator
3
+ class InvocationTracker < Decorator
4
+ def initialize(component)
5
+ super
6
+ @invoked_methods = []
7
+ end
8
+
9
+ def method_missing(name, *args, &block)
10
+ @invoked_methods << name
11
+ super
12
+ end
13
+
14
+ def __invoked_methods__
15
+ @invoked_methods.uniq
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ module FactoryGirl
2
+ class Decorator
3
+ class NewConstructor < Decorator
4
+ def initialize(component, build_class)
5
+ super(component)
6
+ @build_class = build_class
7
+ end
8
+
9
+ delegate :new, to: :@build_class
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ module FactoryGirl
2
+ class Decorator < BasicObject
3
+ undef_method :==
4
+
5
+ def initialize(component)
6
+ @component = component
7
+ end
8
+
9
+ def method_missing(name, *args, &block)
10
+ @component.send(name, *args, &block)
11
+ end
12
+
13
+ def send(symbol, *args, &block)
14
+ __send__(symbol, *args, &block)
15
+ end
16
+
17
+ def self.const_missing(name)
18
+ ::Object.const_get(name)
19
+ end
20
+ end
21
+ end