Class: DiffMatcher::Difference
- Inherits:
-
Object
- Object
- DiffMatcher::Difference
- Defined in:
- lib/diff_matcher/difference.rb
Constant Summary collapse
- RESET =
"\e[0m"- BOLD =
"\e[1m"- RED =
"\e[31m"- GREEN =
"\e[32m"- YELLOW =
"\e[33m"- BLUE =
"\e[34m"- MAGENTA =
"\e[35m"- CYAN =
"\e[36m"- DEFAULT_COLOR_SCHEME =
{ :missing => [RED , "-"], :additional => [YELLOW, "+"], :match_value => [nil , nil], :match_regexp => [GREEN , "~"], :match_class => [BLUE , ":"], :match_matcher => [BLUE , "|"], :match_range => [CYAN , "."], :match_proc => [CYAN , "{"], :match_rspec => [CYAN , "R"] }
Class Attribute Summary collapse
- .color_enabled ⇒ Object
-
.color_scheme ⇒ Object
Returns the value of attribute color_scheme.
- .color_schemes ⇒ Object
Class Method Summary collapse
Instance Method Summary collapse
- #dif ⇒ Object
- #dif_count ⇒ Object
-
#initialize(expected, actual, opts = {}) ⇒ Difference
constructor
A new instance of Difference.
- #matching? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(expected, actual, opts = {}) ⇒ Difference
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/diff_matcher/difference.rb', line 125 def initialize(expected, actual, opts={}) @opts = opts @ignore_additional = opts[:ignore_additional] @quiet = opts[:quiet] @color_scheme = self.class.color_schemes[opts[:color_scheme]] || self.class.color_scheme || self.class.color_schemes[:default] @color_enabled = (opts[:color_enabled].nil? && opts[:color_scheme].nil?) ? self.class.color_enabled : !!opts[:color_scheme] || opts[:color_enabled] @optional_keys = opts.delete(:optional_keys) || [] @dif_count = 0 @difference = difference(expected, actual) @html_output = opts[:html_output] @color_enabled = true if @html_output end |
Class Attribute Details
.color_enabled ⇒ Object
116 117 118 |
# File 'lib/diff_matcher/difference.rb', line 116 def color_enabled @color_enabled.nil? ? !!@color_scheme : @color_enabled end |
.color_scheme ⇒ Object
Returns the value of attribute color_scheme.
96 97 98 |
# File 'lib/diff_matcher/difference.rb', line 96 def color_scheme @color_scheme end |
.color_schemes ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/diff_matcher/difference.rb', line 99 def color_schemes @color_schemes ||= { :default => DEFAULT_COLOR_SCHEME, :white_background => DEFAULT_COLOR_SCHEME.merge( :additional => [MAGENTA, "+"] ) } end |
Class Method Details
.configure(&block) ⇒ Object
120 121 122 |
# File 'lib/diff_matcher/difference.rb', line 120 def configure(&block) block.call(self) end |
Instance Method Details
#dif ⇒ Object
168 169 170 |
# File 'lib/diff_matcher/difference.rb', line 168 def dif @difference end |
#dif_count ⇒ Object
164 165 166 |
# File 'lib/diff_matcher/difference.rb', line 164 def dif_count @dif_count end |
#matching? ⇒ Boolean
138 139 140 141 142 143 144 |
# File 'lib/diff_matcher/difference.rb', line 138 def matching? @match ||= @difference ? item_types.map { |item_type| @color_scheme[item_type] }.inject(0) { |count, (color, prefix)| count + @difference.scan("#{color}#{prefix}").size } == 0 : true end |
#to_s ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/diff_matcher/difference.rb', line 146 def to_s if @difference msg = "\e[0m" + @difference.split("\n").join("\n\e[0m") where = @color_scheme.keys.collect { |item_type| unless item_type == :match_value color, prefix = @color_scheme[item_type] count = msg.scan("#{color}#{prefix}").size @dif_count += count if [:missing, :additional].include? item_type "#{color}#{prefix} #{BOLD}#{count} #{item_type}#{RESET}" if count > 0 end }.compact.join(", ") msg << "\nWhere, #{where}" if where.size > 0 msg.gsub!(/\e\[\d+m/, "") unless @color_enabled @html_output ? "<pre>\n#{escape_to_html(msg)}\n</pre>" : msg end end |