opal-factory_girl 4.5.0.3

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.
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,33 @@
1
+ module FactoryGirl
2
+ class FactoryRunner
3
+ def initialize(name, strategy, traits_and_overrides)
4
+ @name = name
5
+ @strategy = strategy
6
+
7
+ @overrides = traits_and_overrides.extract_options!
8
+ @traits = traits_and_overrides
9
+ end
10
+
11
+ def run(runner_strategy = @strategy, &block)
12
+ factory = FactoryGirl.factory_by_name(@name)
13
+
14
+ factory.compile
15
+
16
+ if @traits.any?
17
+ factory = factory.with_traits(@traits)
18
+ end
19
+
20
+ instrumentation_payload = {
21
+ name: @name,
22
+ strategy: runner_strategy,
23
+ traits: @traits,
24
+ overrides: @overrides,
25
+ factory: factory
26
+ }
27
+
28
+ ActiveSupport::Notifications.instrument('factory_girl.run_factory', instrumentation_payload) do
29
+ factory.run(runner_strategy, @overrides, &block)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ module FactoryGirl
2
+ class << self
3
+ # An Array of strings specifying locations that should be searched for
4
+ # factory definitions. By default, factory_girl will attempt to require
5
+ # "factories", "test/factories" and "spec/factories". Only the first
6
+ # existing file will be loaded.
7
+ attr_accessor :definition_file_paths
8
+ end
9
+
10
+ self.definition_file_paths = %w(factories test/factories spec/factories)
11
+
12
+ def self.find_definitions
13
+ absolute_definition_file_paths = definition_file_paths.map { |path| File.expand_path(path) }
14
+
15
+ absolute_definition_file_paths.uniq.each do |path|
16
+ load("#{path}.rb") if File.exist?("#{path}.rb")
17
+
18
+ if File.directory? path
19
+ Dir[File.join(path, '**', '*.rb')].sort.each do |file|
20
+ load file
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,47 @@
1
+ module FactoryGirl
2
+ class Linter
3
+ def self.lint!(factories_to_lint)
4
+ new(factories_to_lint).lint!
5
+ end
6
+
7
+ def initialize(factories_to_lint)
8
+ @factories_to_lint = factories_to_lint
9
+ @invalid_factories = calculate_invalid_factories
10
+ end
11
+
12
+ def lint!
13
+ if invalid_factories.any?
14
+ raise InvalidFactoryError, error_message
15
+ end
16
+ end
17
+
18
+ attr_reader :factories_to_lint, :invalid_factories
19
+ private :factories_to_lint, :invalid_factories
20
+
21
+ private
22
+
23
+ def calculate_invalid_factories
24
+ factories_to_lint.inject({}) do |result, factory|
25
+ begin
26
+ FactoryGirl.create(factory.name)
27
+ rescue => error
28
+ result[factory] = error
29
+ end
30
+
31
+ result
32
+ end
33
+ end
34
+
35
+ def error_message
36
+ lines = invalid_factories.map do |factory, exception|
37
+ "* #{factory.name} - #{exception.message} (#{exception.class.name})"
38
+ end
39
+
40
+ <<-ERROR_MESSAGE.strip
41
+ The following factories are invalid:
42
+
43
+ #{lines.join("\n")}
44
+ ERROR_MESSAGE
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,18 @@
1
+ module FactoryGirl
2
+ # @api private
3
+ class NullFactory
4
+ attr_reader :definition
5
+
6
+ def initialize
7
+ @definition = Definition.new(:null_factory)
8
+ end
9
+
10
+ delegate :defined_traits, :callbacks, :attributes, :constructor,
11
+ :to_create, to: :definition
12
+
13
+ def compile; end
14
+ def class_name; end
15
+ def evaluator_class; FactoryGirl::Evaluator; end
16
+ def hierarchy_class; FactoryGirl::DefinitionHierarchy; end
17
+ end
18
+ end
@@ -0,0 +1,24 @@
1
+ module FactoryGirl
2
+ # @api private
3
+ class NullObject < ::BasicObject
4
+ def initialize(methods_to_respond_to)
5
+ @methods_to_respond_to = methods_to_respond_to.map(&:to_s)
6
+ end
7
+
8
+ def method_missing(name, *args, &block)
9
+ if respond_to?(name)
10
+ nil
11
+ else
12
+ super
13
+ end
14
+ end
15
+
16
+ def respond_to?(method, include_private=false)
17
+ @methods_to_respond_to.include? method.to_s
18
+ end
19
+
20
+ def respond_to_missing?(*args)
21
+ false
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,38 @@
1
+ module FactoryGirl
2
+ class Registry
3
+ include Enumerable
4
+
5
+ attr_reader :name
6
+
7
+ def initialize(name)
8
+ @name = name
9
+ @items = Decorator::ClassKeyHash.new({})
10
+ end
11
+
12
+ def clear
13
+ @items.clear
14
+ end
15
+
16
+ def each(&block)
17
+ @items.values.uniq.each(&block)
18
+ end
19
+
20
+ def find(name)
21
+ if registered?(name)
22
+ @items[name]
23
+ else
24
+ raise ArgumentError, "#{@name} not registered: #{name}"
25
+ end
26
+ end
27
+
28
+ alias :[] :find
29
+
30
+ def register(name, item)
31
+ @items[name] = item
32
+ end
33
+
34
+ def registered?(name)
35
+ @items.key?(name)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,8 @@
1
+ module FactoryGirl
2
+ def self.reload
3
+ reset_configuration
4
+ register_default_strategies
5
+ register_default_callbacks
6
+ find_definitions
7
+ end
8
+ end
@@ -0,0 +1,62 @@
1
+ module FactoryGirl
2
+
3
+ # Sequences are defined using sequence within a FactoryGirl.define block.
4
+ # Sequence values are generated using next.
5
+ # @api private
6
+ class Sequence
7
+ attr_reader :name
8
+
9
+ def initialize(name, *args, &proc)
10
+ @name = name
11
+ @proc = proc
12
+
13
+ options = args.extract_options!
14
+ @value = args.first || 1
15
+ @aliases = options.fetch(:aliases) { [] }
16
+
17
+ if !@value.respond_to?(:peek)
18
+ @value = EnumeratorAdapter.new(@value)
19
+ end
20
+ end
21
+
22
+ def next(scope = nil)
23
+ if @proc && scope
24
+ scope.instance_exec(value, &@proc)
25
+ elsif @proc
26
+ @proc.call(value)
27
+ else
28
+ value
29
+ end
30
+ ensure
31
+ increment_value
32
+ end
33
+
34
+ def names
35
+ [@name] + @aliases
36
+ end
37
+
38
+ private
39
+
40
+ def value
41
+ @value.peek
42
+ end
43
+
44
+ def increment_value
45
+ @value.next
46
+ end
47
+
48
+ class EnumeratorAdapter
49
+ def initialize(value)
50
+ @value = value
51
+ end
52
+
53
+ def peek
54
+ @value
55
+ end
56
+
57
+ def next
58
+ @value = @value.next
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,13 @@
1
+ module FactoryGirl
2
+ module Strategy
3
+ class AttributesFor
4
+ def association(runner)
5
+ runner.run(:null)
6
+ end
7
+
8
+ def result(evaluation)
9
+ evaluation.hash
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module FactoryGirl
2
+ module Strategy
3
+ class Build
4
+ def association(runner)
5
+ runner.run
6
+ end
7
+
8
+ def result(evaluation)
9
+ evaluation.object.tap do |instance|
10
+ evaluation.notify(:after_build, instance)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ module FactoryGirl
2
+ module Strategy
3
+ class Create
4
+ def association(runner)
5
+ runner.run
6
+ end
7
+
8
+ def result(evaluation)
9
+ evaluation.object.tap do |instance|
10
+ evaluation.notify(:after_build, instance)
11
+ evaluation.notify(:before_create, instance)
12
+ evaluation.create(instance)
13
+ evaluation.notify(:after_create, instance)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ module FactoryGirl
2
+ module Strategy
3
+ class Null
4
+ def association(runner)
5
+ end
6
+
7
+ def result(evaluation)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,73 @@
1
+ module FactoryGirl
2
+ module Strategy
3
+ class Stub
4
+ @@next_id = 1000
5
+
6
+ def association(runner)
7
+ runner.run(:build_stubbed)
8
+ end
9
+
10
+ def result(evaluation)
11
+ evaluation.object.tap do |instance|
12
+ stub_database_interaction_on_result(instance)
13
+ evaluation.notify(:after_stub, instance)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def next_id
20
+ @@next_id += 1
21
+ end
22
+
23
+ def stub_database_interaction_on_result(result_instance)
24
+ result_instance.id ||= next_id
25
+
26
+ result_instance.instance_eval do
27
+ def persisted?
28
+ !new_record?
29
+ end
30
+
31
+ def new_record?
32
+ id.nil?
33
+ end
34
+
35
+ def save(*args)
36
+ raise "stubbed models are not allowed to access the database - #{self.class.to_s}#save(#{args.join(",")})"
37
+ end
38
+
39
+ def destroy(*args)
40
+ raise "stubbed models are not allowed to access the database - #{self.class.to_s}#destroy(#{args.join(",")})"
41
+ end
42
+
43
+ def connection
44
+ raise "stubbed models are not allowed to access the database - #{self.class.to_s}#connection()"
45
+ end
46
+
47
+ def reload(*args)
48
+ raise "stubbed models are not allowed to access the database - #{self.class.to_s}#reload()"
49
+ end
50
+
51
+ def update_attribute(*args)
52
+ raise "stubbed models are not allowed to access the database - #{self.class.to_s}#update_attribute(#{args.join(",")})"
53
+ end
54
+
55
+ def update_column(*args)
56
+ raise "stubbed models are not allowed to access the database - #{self.class.to_s}#update_column(#{args.join(",")})"
57
+ end
58
+ end
59
+
60
+ created_at_missing_default = result_instance.respond_to?(:created_at) && !result_instance.created_at
61
+ result_instance_missing_created_at = !result_instance.respond_to?(:created_at)
62
+
63
+ if created_at_missing_default || result_instance_missing_created_at
64
+ result_instance.instance_eval do
65
+ def created_at
66
+ @created_at ||= Time.now.in_time_zone
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,26 @@
1
+ module FactoryGirl
2
+ # @api private
3
+ class StrategyCalculator
4
+ def initialize(name_or_object)
5
+ @name_or_object = name_or_object
6
+ end
7
+
8
+ def strategy
9
+ if strategy_is_object?
10
+ @name_or_object
11
+ else
12
+ strategy_name_to_object
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def strategy_is_object?
19
+ @name_or_object.is_a?(Class)
20
+ end
21
+
22
+ def strategy_name_to_object
23
+ FactoryGirl.strategy_by_name(@name_or_object)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,46 @@
1
+ module FactoryGirl
2
+ # @api private
3
+ class StrategySyntaxMethodRegistrar
4
+ def initialize(strategy_name)
5
+ @strategy_name = strategy_name
6
+ end
7
+
8
+ def define_strategy_methods
9
+ define_singular_strategy_method
10
+ define_list_strategy_method
11
+ define_pair_strategy_method
12
+ end
13
+
14
+ private
15
+
16
+ def define_singular_strategy_method
17
+ strategy_name = @strategy_name
18
+
19
+ define_syntax_method(strategy_name) do |name, *traits_and_overrides, &block|
20
+ FactoryRunner.new(name, strategy_name, traits_and_overrides).run(&block)
21
+ end
22
+ end
23
+
24
+ def define_list_strategy_method
25
+ strategy_name = @strategy_name
26
+
27
+ define_syntax_method("#{strategy_name}_list") do |name, amount, *traits_and_overrides, &block|
28
+ amount.times.map { send(strategy_name, name, *traits_and_overrides, &block) }
29
+ end
30
+ end
31
+
32
+ def define_pair_strategy_method
33
+ strategy_name = @strategy_name
34
+
35
+ define_syntax_method("#{strategy_name}_pair") do |name, *traits_and_overrides, &block|
36
+ 2.times.map { send(strategy_name, name, *traits_and_overrides, &block) }
37
+ end
38
+ end
39
+
40
+ def define_syntax_method(name, &block)
41
+ FactoryGirl::Syntax::Methods.module_exec do
42
+ define_method(name, &block)
43
+ end
44
+ end
45
+ end
46
+ end