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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dfe191bdf8185297a622da7437455efe0f8cc184
4
+ data.tar.gz: 9392baa09edf4c369ef401d7a438e6ba987da140
5
+ SHA512:
6
+ metadata.gz: 1efca3f1f569898b22874b732bb0e895bef38d9fb0c00204be6a062942034caba6e6aca5fdf604d11373327a8d9e942f86e849c442b9d757cd6d35a995995728
7
+ data.tar.gz: 74c78b1a22cd6f777497ca3bb8301d11238084c9920fe72b77638c74672f69f8ecf57e74949f48adec67c164577fbdadceb2a5b5e112c5ce49d8349b8368eb90
@@ -0,0 +1,132 @@
1
+ require 'set'
2
+ require 'active_support/core_ext/module/delegation'
3
+ require 'active_support/deprecation'
4
+ require 'active_support/notifications'
5
+
6
+ require 'factory_girl/definition_hierarchy'
7
+ require 'factory_girl/configuration'
8
+ require 'factory_girl/errors'
9
+ require 'factory_girl/factory_runner'
10
+ require 'factory_girl/strategy_syntax_method_registrar'
11
+ require 'factory_girl/strategy_calculator'
12
+ require 'factory_girl/strategy/build'
13
+ require 'factory_girl/strategy/create'
14
+ require 'factory_girl/strategy/attributes_for'
15
+ require 'factory_girl/strategy/stub'
16
+ require 'factory_girl/strategy/null'
17
+ require 'factory_girl/registry'
18
+ require 'factory_girl/null_factory'
19
+ require 'factory_girl/null_object'
20
+ require 'factory_girl/evaluation'
21
+ require 'factory_girl/factory'
22
+ require 'factory_girl/attribute_assigner'
23
+ require 'factory_girl/evaluator'
24
+ require 'factory_girl/evaluator_class_definer'
25
+ require 'factory_girl/attribute'
26
+ require 'factory_girl/callback'
27
+ require 'factory_girl/callbacks_observer'
28
+ require 'factory_girl/declaration_list'
29
+ require 'factory_girl/declaration'
30
+ require 'factory_girl/sequence'
31
+ require 'factory_girl/attribute_list'
32
+ require 'factory_girl/trait'
33
+ require 'factory_girl/aliases'
34
+ require 'factory_girl/definition'
35
+ require 'factory_girl/definition_proxy'
36
+ require 'factory_girl/syntax'
37
+ require 'factory_girl/syntax_runner'
38
+ require 'factory_girl/find_definitions'
39
+ require 'factory_girl/reload'
40
+ require 'factory_girl/decorator'
41
+ require 'factory_girl/decorator/attribute_hash'
42
+ require 'factory_girl/decorator/class_key_hash'
43
+ require 'factory_girl/decorator/disallows_duplicates_registry'
44
+ require 'factory_girl/decorator/invocation_tracker'
45
+ require 'factory_girl/decorator/new_constructor'
46
+ require 'factory_girl/linter'
47
+ require 'factory_girl/version'
48
+
49
+ module FactoryGirl
50
+ def self.configuration
51
+ @configuration ||= Configuration.new
52
+ end
53
+
54
+ def self.reset_configuration
55
+ @configuration = nil
56
+ end
57
+
58
+ def self.lint(factories_to_lint = FactoryGirl.factories)
59
+ Linter.lint!(factories_to_lint)
60
+ end
61
+
62
+ class << self
63
+ delegate :factories, :sequences, :traits, :callbacks, :strategies, :callback_names,
64
+ :to_create, :skip_create, :initialize_with, :constructor, :duplicate_attribute_assignment_from_initialize_with,
65
+ :duplicate_attribute_assignment_from_initialize_with=, to: :configuration
66
+ end
67
+
68
+ def self.register_factory(factory)
69
+ factory.names.each do |name|
70
+ factories.register(name, factory)
71
+ end
72
+ factory
73
+ end
74
+
75
+ def self.factory_by_name(name)
76
+ factories.find(name)
77
+ end
78
+
79
+ def self.register_sequence(sequence)
80
+ sequence.names.each do |name|
81
+ sequences.register(name, sequence)
82
+ end
83
+ sequence
84
+ end
85
+
86
+ def self.sequence_by_name(name)
87
+ sequences.find(name)
88
+ end
89
+
90
+ def self.register_trait(trait)
91
+ trait.names.each do |name|
92
+ traits.register(name, trait)
93
+ end
94
+ trait
95
+ end
96
+
97
+ def self.trait_by_name(name)
98
+ traits.find(name)
99
+ end
100
+
101
+ def self.register_strategy(strategy_name, strategy_class)
102
+ strategies.register(strategy_name, strategy_class)
103
+ StrategySyntaxMethodRegistrar.new(strategy_name).define_strategy_methods
104
+ end
105
+
106
+ def self.strategy_by_name(name)
107
+ strategies.find(name)
108
+ end
109
+
110
+ def self.register_default_strategies
111
+ register_strategy(:build, FactoryGirl::Strategy::Build)
112
+ register_strategy(:create, FactoryGirl::Strategy::Create)
113
+ register_strategy(:attributes_for, FactoryGirl::Strategy::AttributesFor)
114
+ register_strategy(:build_stubbed, FactoryGirl::Strategy::Stub)
115
+ register_strategy(:null, FactoryGirl::Strategy::Null)
116
+ end
117
+
118
+ def self.register_default_callbacks
119
+ register_callback(:after_build)
120
+ register_callback(:after_create)
121
+ register_callback(:after_stub)
122
+ register_callback(:before_create)
123
+ end
124
+
125
+ def self.register_callback(name)
126
+ name = name.to_sym
127
+ callback_names << name
128
+ end
129
+ end
130
+
131
+ FactoryGirl.register_default_strategies
132
+ FactoryGirl.register_default_callbacks
@@ -0,0 +1,18 @@
1
+ module FactoryGirl
2
+ class << self
3
+ attr_accessor :aliases
4
+ end
5
+
6
+ self.aliases = [
7
+ [/(.+)_id/, '\1'],
8
+ [/(.*)/, '\1_id']
9
+ ]
10
+
11
+ def self.aliases_for(attribute)
12
+ aliases.map do |(pattern, replace)|
13
+ if pattern.match(attribute.to_s)
14
+ attribute.to_s.sub(pattern, replace).to_sym
15
+ end
16
+ end.compact << attribute
17
+ end
18
+ end
@@ -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,27 @@
1
+ module FactoryGirl
2
+ class Attribute
3
+ # @api private
4
+ class Association < Attribute
5
+ attr_reader :factory
6
+
7
+ def initialize(name, factory, overrides)
8
+ super(name, false)
9
+ @factory = factory
10
+ @overrides = overrides
11
+ end
12
+
13
+ def to_proc
14
+ factory = @factory
15
+ overrides = @overrides
16
+ traits_and_overrides = [factory, overrides].flatten
17
+ factory_name = traits_and_overrides.shift
18
+
19
+ -> { association(factory_name, *traits_and_overrides) }
20
+ end
21
+
22
+ def association?
23
+ true
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ module FactoryGirl
2
+ class Attribute
3
+ # @api private
4
+ class Dynamic < Attribute
5
+ def initialize(name, ignored, block)
6
+ super(name, ignored)
7
+ @block = block
8
+ end
9
+
10
+ def to_proc
11
+ block = @block
12
+
13
+ -> {
14
+ value = case block.arity
15
+ when 1, -1 then instance_exec(self, &block)
16
+ else instance_exec(&block)
17
+ end
18
+ raise SequenceAbuseError if FactoryGirl::Sequence === value
19
+ value
20
+ }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,16 @@
1
+ module FactoryGirl
2
+ class Attribute
3
+ # @api private
4
+ class Sequence < Attribute
5
+ def initialize(name, sequence, ignored)
6
+ super(name, ignored)
7
+ @sequence = sequence
8
+ end
9
+
10
+ def to_proc
11
+ sequence = @sequence
12
+ -> { FactoryGirl.generate(sequence) }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module FactoryGirl
2
+ class Attribute
3
+ # @api private
4
+ class Static < Attribute
5
+ def initialize(name, value, ignored)
6
+ super(name, ignored)
7
+ @value = value
8
+ end
9
+
10
+ def to_proc
11
+ value = @value
12
+ -> { value }
13
+ end
14
+ end
15
+ end
16
+ 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