File: declaration.rb

package info (click to toggle)
ruby-factory-bot 6.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,372 kB
  • sloc: ruby: 7,827; makefile: 6
file content (78 lines) | stat: -rw-r--r-- 1,621 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
module DeclarationMatchers
  def have_dynamic_declaration(name)
    DeclarationMatcher.new(:dynamic).named(name)
  end

  def have_association_declaration(name)
    DeclarationMatcher.new(:association).named(name)
  end

  def have_implicit_declaration(name)
    DeclarationMatcher.new(:implicit).named(name)
  end

  class DeclarationMatcher
    def initialize(declaration_type)
      @declaration_type = declaration_type
    end

    def matches?(subject)
      subject.declarations.include?(expected_declaration)
    end

    def named(name)
      @name = name
      self
    end

    def ignored
      @ignored = true
      self
    end

    def with_value(value)
      @value = value
      self
    end

    def with_factory(factory)
      @factory = factory
      self
    end

    def with_options(options)
      @options = options
      self
    end

    def failure_message
      [
        "expected declarations to include declaration of type #{@declaration_type}",
        @options ? "with options #{options}" : nil
      ].compact.join " "
    end

    private

    def expected_declaration
      case @declaration_type
      when :dynamic then FactoryBot::Declaration::Dynamic.new(@name, ignored?, @value)
      when :implicit then FactoryBot::Declaration::Implicit.new(@name, @factory, ignored?)
      when :association
        if @options
          FactoryBot::Declaration::Association.new(@name, options)
        else
          FactoryBot::Declaration::Association.new(@name)
        end
      end
    end

    def ignored?
      !!@ignored
    end

    def options
      @options || {}
    end
  end
end