opal-factory_girl 4.5.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/factory_girl/lib/factory_girl.rb +132 -0
  3. data/factory_girl/lib/factory_girl/aliases.rb +18 -0
  4. data/factory_girl/lib/factory_girl/attribute.rb +62 -0
  5. data/factory_girl/lib/factory_girl/attribute/association.rb +27 -0
  6. data/factory_girl/lib/factory_girl/attribute/dynamic.rb +24 -0
  7. data/factory_girl/lib/factory_girl/attribute/sequence.rb +16 -0
  8. data/factory_girl/lib/factory_girl/attribute/static.rb +16 -0
  9. data/factory_girl/lib/factory_girl/attribute_assigner.rb +97 -0
  10. data/factory_girl/lib/factory_girl/attribute_list.rb +67 -0
  11. data/factory_girl/lib/factory_girl/callback.rb +40 -0
  12. data/factory_girl/lib/factory_girl/callbacks_observer.rb +21 -0
  13. data/factory_girl/lib/factory_girl/configuration.rb +33 -0
  14. data/factory_girl/lib/factory_girl/declaration.rb +23 -0
  15. data/factory_girl/lib/factory_girl/declaration/association.rb +28 -0
  16. data/factory_girl/lib/factory_girl/declaration/dynamic.rb +26 -0
  17. data/factory_girl/lib/factory_girl/declaration/implicit.rb +33 -0
  18. data/factory_girl/lib/factory_girl/declaration/static.rb +26 -0
  19. data/factory_girl/lib/factory_girl/declaration_list.rb +49 -0
  20. data/factory_girl/lib/factory_girl/decorator.rb +21 -0
  21. data/factory_girl/lib/factory_girl/decorator/attribute_hash.rb +17 -0
  22. data/factory_girl/lib/factory_girl/decorator/class_key_hash.rb +27 -0
  23. data/factory_girl/lib/factory_girl/decorator/disallows_duplicates_registry.rb +13 -0
  24. data/factory_girl/lib/factory_girl/decorator/invocation_tracker.rb +19 -0
  25. data/factory_girl/lib/factory_girl/decorator/new_constructor.rb +12 -0
  26. data/factory_girl/lib/factory_girl/definition.rb +141 -0
  27. data/factory_girl/lib/factory_girl/definition_hierarchy.rb +48 -0
  28. data/factory_girl/lib/factory_girl/definition_proxy.rb +174 -0
  29. data/factory_girl/lib/factory_girl/errors.rb +25 -0
  30. data/factory_girl/lib/factory_girl/evaluation.rb +23 -0
  31. data/factory_girl/lib/factory_girl/evaluator.rb +76 -0
  32. data/factory_girl/lib/factory_girl/evaluator_class_definer.rb +20 -0
  33. data/factory_girl/lib/factory_girl/factory.rb +162 -0
  34. data/factory_girl/lib/factory_girl/factory_runner.rb +33 -0
  35. data/factory_girl/lib/factory_girl/find_definitions.rb +25 -0
  36. data/factory_girl/lib/factory_girl/linter.rb +47 -0
  37. data/factory_girl/lib/factory_girl/null_factory.rb +18 -0
  38. data/factory_girl/lib/factory_girl/null_object.rb +24 -0
  39. data/factory_girl/lib/factory_girl/registry.rb +38 -0
  40. data/factory_girl/lib/factory_girl/reload.rb +8 -0
  41. data/factory_girl/lib/factory_girl/sequence.rb +62 -0
  42. data/factory_girl/lib/factory_girl/strategy/attributes_for.rb +13 -0
  43. data/factory_girl/lib/factory_girl/strategy/build.rb +15 -0
  44. data/factory_girl/lib/factory_girl/strategy/create.rb +18 -0
  45. data/factory_girl/lib/factory_girl/strategy/null.rb +11 -0
  46. data/factory_girl/lib/factory_girl/strategy/stub.rb +73 -0
  47. data/factory_girl/lib/factory_girl/strategy_calculator.rb +26 -0
  48. data/factory_girl/lib/factory_girl/strategy_syntax_method_registrar.rb +46 -0
  49. data/factory_girl/lib/factory_girl/syntax.rb +7 -0
  50. data/factory_girl/lib/factory_girl/syntax/default.rb +76 -0
  51. data/factory_girl/lib/factory_girl/syntax/methods.rb +95 -0
  52. data/factory_girl/lib/factory_girl/syntax_runner.rb +6 -0
  53. data/factory_girl/lib/factory_girl/trait.rb +30 -0
  54. data/factory_girl/lib/factory_girl/version.rb +3 -0
  55. data/lib/opal-factory_girl.rb +16 -0
  56. data/lib/opal/factory_girl/version.rb +8 -0
  57. data/opal/opal-factory_girl.rb +184 -0
  58. data/opal/opal/active_support/inflector/methods.rb +395 -0
  59. metadata +142 -0
@@ -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,33 @@
1
+ module FactoryGirl
2
+ # @api private
3
+ class Configuration
4
+ attr_reader :factories, :sequences, :traits, :strategies, :callback_names
5
+
6
+ def initialize
7
+ @factories = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Factory'))
8
+ @sequences = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Sequence'))
9
+ @traits = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Trait'))
10
+ @strategies = Registry.new('Strategy')
11
+ @callback_names = Set.new
12
+ @definition = Definition.new
13
+
14
+ to_create { |instance| instance.save! }
15
+ initialize_with { new }
16
+ end
17
+
18
+ delegate :to_create, :skip_create, :constructor, :before, :after,
19
+ :callback, :callbacks, to: :@definition
20
+
21
+ def initialize_with(&block)
22
+ @definition.define_constructor(&block)
23
+ end
24
+
25
+ def duplicate_attribute_assignment_from_initialize_with
26
+ false
27
+ end
28
+
29
+ def duplicate_attribute_assignment_from_initialize_with=(value)
30
+ 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
31
+ end
32
+ end
33
+ 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,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,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,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)
14
+ __send__(symbol, *args)
15
+ end
16
+
17
+ def self.const_missing(name)
18
+ ::Object.const_get(name)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,17 @@
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.inject({}) do |result, attribute_name|
11
+ result[attribute_name] = send(attribute_name)
12
+ result
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,27 @@
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
+ else
22
+ key.to_s.underscore.to_sym
23
+ end
24
+ end
25
+ end
26
+ end
27
+ 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,141 @@
1
+ module FactoryGirl
2
+ # @api private
3
+ class Definition
4
+ attr_reader :defined_traits, :declarations
5
+
6
+ def initialize(name = nil, base_traits = [])
7
+ @declarations = DeclarationList.new(name)
8
+ @callbacks = []
9
+ @defined_traits = Set.new
10
+ @to_create = nil
11
+ @base_traits = base_traits
12
+ @additional_traits = []
13
+ @constructor = nil
14
+ @attributes = nil
15
+ @compiled = false
16
+ end
17
+
18
+ delegate :declare_attribute, to: :declarations
19
+
20
+ def attributes
21
+ @attributes ||= AttributeList.new.tap do |attribute_list|
22
+ attribute_lists = aggregate_from_traits_and_self(:attributes) { declarations.attributes }
23
+ attribute_lists.each do |attributes|
24
+ attribute_list.apply_attributes attributes
25
+ end
26
+ end
27
+ end
28
+
29
+ def to_create(&block)
30
+ if block_given?
31
+ @to_create = block
32
+ else
33
+ aggregate_from_traits_and_self(:to_create) { @to_create }.last
34
+ end
35
+ end
36
+
37
+ def constructor
38
+ aggregate_from_traits_and_self(:constructor) { @constructor }.last
39
+ end
40
+
41
+ def callbacks
42
+ aggregate_from_traits_and_self(:callbacks) { @callbacks }
43
+ end
44
+
45
+ def compile
46
+ unless @compiled
47
+ declarations.attributes
48
+
49
+ defined_traits.each do |defined_trait|
50
+ base_traits.each { |bt| bt.define_trait defined_trait }
51
+ additional_traits.each { |bt| bt.define_trait defined_trait }
52
+ end
53
+
54
+ @compiled = true
55
+ end
56
+ end
57
+
58
+ def overridable
59
+ declarations.overridable
60
+ self
61
+ end
62
+
63
+ def inherit_traits(new_traits)
64
+ @base_traits += new_traits
65
+ end
66
+
67
+ def append_traits(new_traits)
68
+ @additional_traits += new_traits
69
+ end
70
+
71
+ def add_callback(callback)
72
+ @callbacks << callback
73
+ end
74
+
75
+ def skip_create
76
+ @to_create = ->(instance) { }
77
+ end
78
+
79
+ def define_trait(trait)
80
+ @defined_traits.add(trait)
81
+ end
82
+
83
+ def define_constructor(&block)
84
+ @constructor = block
85
+ end
86
+
87
+ def before(*names, &block)
88
+ callback(*names.map { |name| "before_#{name}" }, &block)
89
+ end
90
+
91
+ def after(*names, &block)
92
+ callback(*names.map { |name| "after_#{name}" }, &block)
93
+ end
94
+
95
+ def callback(*names, &block)
96
+ names.each do |name|
97
+ FactoryGirl.register_callback(name)
98
+ add_callback(Callback.new(name, block))
99
+ end
100
+ end
101
+
102
+ private
103
+
104
+ def base_traits
105
+ @base_traits.map { |name| trait_by_name(name) }
106
+ end
107
+
108
+ def additional_traits
109
+ @additional_traits.map { |name| trait_by_name(name) }
110
+ end
111
+
112
+ def trait_by_name(name)
113
+ trait_for(name) || FactoryGirl.trait_by_name(name)
114
+ end
115
+
116
+ def trait_for(name)
117
+ defined_traits.detect { |trait| trait.name == name }
118
+ end
119
+
120
+ def initialize_copy(source)
121
+ super
122
+ @attributes = nil
123
+ @compiled = false
124
+ end
125
+
126
+ def aggregate_from_traits_and_self(method_name, &block)
127
+ compile
128
+ [].tap do |list|
129
+ base_traits.each do |trait|
130
+ list << trait.send(method_name)
131
+ end
132
+
133
+ list << instance_exec(&block)
134
+
135
+ additional_traits.each do |trait|
136
+ list << trait.send(method_name)
137
+ end
138
+ end.flatten.compact
139
+ end
140
+ end
141
+ end