Skip to content
This repository
Browse code

Refactor and document

  • Loading branch information...
commit 415e2db70d3b19b46a4d3d0fe62f50400f9d2b61 1 parent e580913
Jonas Nicklas authored March 17, 2013
175  lib/capybara/helpers.rb
@@ -4,90 +4,117 @@ module Capybara
4 4
 
5 5
   # @api private
6 6
   module Helpers
7  
-    class << self
8  
-      ##
9  
-      #
10  
-      # Normalizes whitespace space by stripping leading and trailing
11  
-      # whitespace and replacing sequences of whitespace characters
12  
-      # with a single space.
13  
-      #
14  
-      # @param [String] text     Text to normalize
15  
-      # @return [String]         Normalized text
16  
-      #
17  
-      def normalize_whitespace(text)
18  
-        text.to_s.gsub(/[[:space:]]+/, ' ').strip
19  
-      end
  7
+    extend self
20 8
 
21  
-      ##
22  
-      #
23  
-      # Escapes any characters that would have special meaning in a regexp
24  
-      # if text is not a regexp
25  
-      #
26  
-      # @param [String] text Text to escape
27  
-      # @return [String]     Escaped text
28  
-      #
29  
-      def to_regexp(text)
30  
-        text.is_a?(Regexp) ? text : Regexp.new(Regexp.escape(normalize_whitespace(text)))
31  
-      end
  9
+    ##
  10
+    #
  11
+    # Normalizes whitespace space by stripping leading and trailing
  12
+    # whitespace and replacing sequences of whitespace characters
  13
+    # with a single space.
  14
+    #
  15
+    # @param [String] text     Text to normalize
  16
+    # @return [String]         Normalized text
  17
+    #
  18
+    def normalize_whitespace(text)
  19
+      text.to_s.gsub(/[[:space:]]+/, ' ').strip
  20
+    end
32 21
 
33  
-      ##
34  
-      #
35  
-      # Injects a `<base>` tag into the given HTML code, pointing to
36  
-      # `Capybara.asset_host`.
37  
-      #
38  
-      # @param [String] html     HTML code to inject into
39  
-      # @param [String]          The modified HTML code
40  
-      #
41  
-      def inject_asset_host(html)
42  
-        if Capybara.asset_host
43  
-          if Nokogiri::HTML(html).css("base").empty? and match = html.match(/<head[^<]*?>/)
44  
-            html.clone.insert match.end(0), "<base href='#{Capybara.asset_host}' />"
45  
-          end
46  
-        else
47  
-          html
  22
+    ##
  23
+    #
  24
+    # Escapes any characters that would have special meaning in a regexp
  25
+    # if text is not a regexp
  26
+    #
  27
+    # @param [String] text Text to escape
  28
+    # @return [String]     Escaped text
  29
+    #
  30
+    def to_regexp(text)
  31
+      text.is_a?(Regexp) ? text : Regexp.new(Regexp.escape(normalize_whitespace(text)))
  32
+    end
  33
+
  34
+    ##
  35
+    #
  36
+    # Injects a `<base>` tag into the given HTML code, pointing to
  37
+    # `Capybara.asset_host`.
  38
+    #
  39
+    # @param [String] html     HTML code to inject into
  40
+    # @param [String]          The modified HTML code
  41
+    #
  42
+    def inject_asset_host(html)
  43
+      if Capybara.asset_host
  44
+        if Nokogiri::HTML(html).css("base").empty? and match = html.match(/<head[^<]*?>/)
  45
+          html.clone.insert match.end(0), "<base href='#{Capybara.asset_host}' />"
48 46
         end
  47
+      else
  48
+        html
49 49
       end
50 50
     end
51  
-  end
52 51
 
53  
-  # @api private
54  
-  module CountHelpers
55  
-    class << self
56  
-      def matches_count?(count, options={})
57  
-        case
58  
-        when options[:between]
59  
-          options[:between] === count
60  
-        when options[:count]
61  
-          Integer(options[:count]) == count
62  
-        when options[:maximum]
63  
-          Integer(options[:maximum]) >= count
64  
-        when options[:minimum]
65  
-          Integer(options[:minimum]) <= count
66  
-        else
67  
-          count > 0
68  
-        end
  52
+    ##
  53
+    #
  54
+    # Checks if the given count matches the given count options. By default,
  55
+    # when no options are given, count should be larger than zero.
  56
+    #
  57
+    # @param [Integer] count     The actual number. Should be coercible via Integer()
  58
+    # @option [Range] between    Count must be within the given range
  59
+    # @option [Integer] count    Count must be exactly this
  60
+    # @option [Integer] maximum  Count must be smaller than or equal to this value
  61
+    # @option [Integer] minimum  Count must be larger than or equal to this value
  62
+    #
  63
+    def matches_count?(count, options={})
  64
+      case
  65
+      when options[:between]
  66
+        options[:between] === count
  67
+      when options[:count]
  68
+        Integer(options[:count]) == count
  69
+      when options[:maximum]
  70
+        Integer(options[:maximum]) >= count
  71
+      when options[:minimum]
  72
+        Integer(options[:minimum]) <= count
  73
+      else
  74
+        count > 0
69 75
       end
  76
+    end
70 77
 
71  
-      def failure_message(description, options={})
72  
-        message = "expected to find #{description}"
73  
-        if options[:count]
74  
-          message << " #{options[:count]} #{declension('time', 'times', options[:count])}"
75  
-        elsif options[:between]
76  
-          message << " between #{options[:between].first} and #{options[:between].last} times"
77  
-        elsif options[:maximum]
78  
-          message << " at most #{options[:maximum]} #{declension('time', 'times', options[:maximum])}"
79  
-        elsif options[:minimum]
80  
-          message << " at least #{options[:minimum]} #{declension('time', 'times', options[:minimum])}"
81  
-        end
82  
-        message
  78
+    ##
  79
+    #
  80
+    # Generates a failure message given a description of the query and count
  81
+    # options.
  82
+    #
  83
+    # @param [String] description   Description of a query
  84
+    # @option [Range] between       Count should have been within the given range
  85
+    # @option [Integer] count       Count should have been exactly this
  86
+    # @option [Integer] maximum     Count should have been smaller than or equal to this value
  87
+    # @option [Integer] minimum     Count should have been larger than or equal to this value
  88
+    #
  89
+    def failure_message(description, options={})
  90
+      message = "expected to find #{description}"
  91
+      if options[:count]
  92
+        message << " #{options[:count]} #{declension('time', 'times', options[:count])}"
  93
+      elsif options[:between]
  94
+        message << " between #{options[:between].first} and #{options[:between].last} times"
  95
+      elsif options[:maximum]
  96
+        message << " at most #{options[:maximum]} #{declension('time', 'times', options[:maximum])}"
  97
+      elsif options[:minimum]
  98
+        message << " at least #{options[:minimum]} #{declension('time', 'times', options[:minimum])}"
83 99
       end
  100
+      message
  101
+    end
84 102
 
85  
-      def declension(singular, plural, count)
86  
-        if count == 1
87  
-          singular
88  
-        else
89  
-          plural
90  
-        end
  103
+    ##
  104
+    #
  105
+    # A poor man's `pluralize`. Given two declensions, one singular and one
  106
+    # plural, as well as a count, this will pick the correct declension. This
  107
+    # way we can generate gramatically correct error message.
  108
+    #
  109
+    # @param [String] singular     The singular form of the word
  110
+    # @param [String] plural       The plural form of the word
  111
+    # @param [Integer] count       The number of items
  112
+    #
  113
+    def declension(singular, plural, count)
  114
+      if count == 1
  115
+        singular
  116
+      else
  117
+        plural
91 118
       end
92 119
     end
93 120
   end
9  lib/capybara/node/matchers.rb
@@ -458,15 +458,14 @@ def ==(other)
458 458
         self.eql?(other) or (other.respond_to?(:base) and base == other.base)
459 459
       end
460 460
 
461  
-      private
  461
+    private
462 462
 
463 463
       def text_found?(*args)
464 464
         type = args.shift if args.first.is_a?(Symbol) or args.first.nil?
465  
-        content = args.shift
466  
-        options = (args.first.is_a?(Hash))? args.first : {}
  465
+        content, options = args
467 466
         count = Capybara::Helpers.normalize_whitespace(text(type)).scan(Capybara::Helpers.to_regexp(content)).count
468  
-        
469  
-        Capybara::CountHelpers.matches_count?(count, options)
  467
+
  468
+        Capybara::Helpers.matches_count?(count, options || {})
470 469
       end
471 470
     end
472 471
   end
6  lib/capybara/result.rb
@@ -32,13 +32,13 @@ def initialize(elements, query)
32 32
     def_delegators :@result, :each, :[], :at, :size, :count, :length, :first, :last, :empty?
33 33
 
34 34
     def matches_count?
35  
-      Capybara::CountHelpers.matches_count?(@result.size, @query.options)
  35
+      Capybara::Helpers.matches_count?(@result.size, @query.options)
36 36
     end
37 37
 
38 38
     def failure_message
39  
-      message = Capybara::CountHelpers.failure_message(@query.description, @query.options)
  39
+      message = Capybara::Helpers.failure_message(@query.description, @query.options)
40 40
       if count > 0
41  
-        message << ", found #{count} #{Capybara::CountHelpers.declension("match", "matches", count)}: " << @result.map(&:text).map(&:inspect).join(", ")
  41
+        message << ", found #{count} #{Capybara::Helpers.declension("match", "matches", count)}: " << @result.map(&:text).map(&:inspect).join(", ")
42 42
       else
43 43
         message << " but there were no matches"
44 44
       end
2  lib/capybara/rspec/matchers.rb
@@ -52,7 +52,7 @@ def does_not_match?(actual)
52 52
       end
53 53
 
54 54
       def failure_message_for_should
55  
-        message = Capybara::CountHelpers.failure_message(description, options)
  55
+        message = Capybara::Helpers.failure_message(description, options)
56 56
         message << " in #{format(@actual.text(type))}"
57 57
         message
58 58
       end

0 notes on commit 415e2db

Please sign in to comment.
Something went wrong with that request. Please try again.