File: rspec_matcher.rb

package info (click to toggle)
ruby-attribute-normalizer 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 108 kB
  • sloc: ruby: 213; makefile: 7
file content (50 lines) | stat: -rw-r--r-- 1,133 bytes parent folder | download
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
module AttributeNormalizer

  module RSpecMatcher

    def normalize_attribute(attribute)
      NormalizeAttribute.new(attribute)
    end

    class NormalizeAttribute

      def initialize(attribute)
        @attribute = attribute
        @from = ''
      end

      def description
        "normalize #{@attribute} from #{@from.nil? ? 'nil' : "\"#{@from}\""} to #{@to.nil? ? 'nil' : "\"#{@to}\""}"
      end

      def failure_message
        "#{@attribute} did not normalize as expected! \"#{@subject.send(@attribute)}\" != #{@to.nil? ? 'nil' : "\"#{@to}\""}"
      end

      def failure_message_when_negated
        "expected #{@attribute} to not be normalized from #{@from.nil? ? 'nil' : "\"#{@from}\""} to #{@to.nil? ? 'nil' : "\"#{@to}\""}"
      end
      alias negative_failure_message failure_message_when_negated

      def from(value)
        @from = value
        self
      end

      def to(value)
        @to = value
        self
      end

      def matches?(subject)
        @subject = subject
        @subject.send("#{@attribute}=", @from)

        @subject.send(@attribute) == @to
      end

    end

  end

end