Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Make it possible to get less verbose output for compare failures.

  • Loading branch information...
commit b9983efceda81c44ff8dee5bc168e96220c81e23 1 parent 82a0d9b
Martin Tepper authored March 30, 2012
6  README.rdoc
Source Rendered
@@ -128,6 +128,12 @@ The implicit rules are:
128 128
 
129 129
 You can add extra rules with the rule method - see Masterplan::DefineRules#rule for details.
130 130
 
  131
+You can control the verbosity of the output with the :format option:
  132
+
  133
+    >> Masterplan.compare(:scheme => doc, :to => {:example => :data}, :format => :mini)
  134
+
  135
+Valid values are :full (the default), or :mini. :mini produces only a one-line output (leaving out the "Expected...but was" part).
  136
+
131 137
 There is also an added assertion for unit tests or specs:
132 138
 
133 139
     assert_masterplan(doc, [{:example => :data})
26  lib/masterplan.rb
@@ -17,12 +17,14 @@ class FailedError < Test::Unit::AssertionFailedError
17 17
 
18 18
   class << self
19 19
 
20  
-    def compare(options = {:scheme => {}, :to => {}})
  20
+    def compare(options = {:scheme => {}, :to => {}, :format => :full})
21 21
       scheme = options[:scheme]
22 22
       testee = options[:to]
  23
+      format = options[:format] || :full
23 24
       raise ArgumentError, ":to needs to be a hash !" unless testee.is_a?(Hash)
24 25
       raise ArgumentError, ":scheme needs to be a Masterplan::Document !" unless scheme.is_a?(Document)
25  
-      compare_hash(scheme, testee)
  26
+      raise ArgumentError, ":format needs to be one of [:full, :mini] !" unless [:full, :mini].include?(format)
  27
+      compare_hash(scheme, testee, format)
26 28
       true
27 29
     end
28 30
 
@@ -36,7 +38,7 @@ def compare_value(template, value, path)
36 38
       end
37 39
     end
38 40
 
39  
-    def compare_hash(template, testee, trail = ["root"])
  41
+    def compare_hash(template, testee, format, trail = ["root"])
40 42
       template.stringify_keys!
41 43
       testee.stringify_keys!
42 44
       raise FailedError, "keys don't match in #{format_path(trail)}:\nexpected:\t#{template.keys.sort.join(',')}\nreceived:\t#{testee.keys.sort.join(',')}" if template.keys.sort != testee.keys.sort
@@ -51,7 +53,7 @@ def compare_hash(template, testee, trail = ["root"])
51 53
             array_path = current_path + [index]
52 54
             compare_value(elements_template, elements_value, format_path(array_path))
53 55
             if elements_value.is_a?(Hash)
54  
-              compare_hash(elements_template, elements_value, array_path)
  56
+              compare_hash(elements_template, elements_value, format, array_path)
55 57
             end
56 58
           end
57 59
         end
@@ -61,16 +63,16 @@ def compare_hash(template, testee, trail = ["root"])
61 63
             array_path = current_path + [index]
62 64
             compare_value(elements_template, elements_value, format_path(array_path))
63 65
             if elements_value.is_a?(Hash)
64  
-              compare_hash(elements_template, elements_value, array_path)
  66
+              compare_hash(elements_template, elements_value, format, array_path)
65 67
             end
66 68
           end
67 69
         end
68 70
         if value.is_a?(Hash)
69 71
           if t_value.is_a?(Masterplan::Rule)
70 72
             compare_value(t_value, value, current_path)
71  
-            compare_hash(t_value.example_value, value, current_path)
  73
+            compare_hash(t_value.example_value, value, format, current_path)
72 74
           else
73  
-            compare_hash(t_value, value, current_path)
  75
+            compare_hash(t_value, value, format, current_path)
74 76
           end
75 77
         end
76 78
       end
@@ -81,10 +83,14 @@ def compare_hash(template, testee, trail = ["root"])
81 83
       error = Masterplan::FailedError.new
82 84
       error.printed = true
83 85
 
84  
-      expected = PP.pp(template, '')
85  
-      outcome = PP.pp(testee, '')
  86
+      if format == :mini
  87
+        raise error, e.message, caller
  88
+      else
  89
+        expected = PP.pp(template, '')
  90
+        outcome = PP.pp(testee, '')
86 91
 
87  
-      raise error, "#{e.message}\n\nExpected:\n#{expected}\n\nbut was:\n#{outcome}", caller
  92
+        raise error, "#{e.message}\n\nExpected:\n#{expected}\n\nbut was:\n#{outcome}", caller
  93
+      end
88 94
     end
89 95
 
90 96
     def format_path(trail)
2  lib/masterplan/define_rules.rb
@@ -5,7 +5,7 @@ module Masterplan
5 5
   module DefineRules
6 6
 
7 7
     # This turns the supplied +example_value+ (any object) into an object that carries rules about itself with it.
8  
-    # The rules will be applied when a template is compared with assert_masterplan. Rules are:
  8
+    # The rules will be applied when a template is compared with assert_masterplan or Masterplan.compare. Rules are:
9 9
     # (default): This always applies - the value must be of the same class as the +example_value+
10 10
     # 'allow_nil': This allows the value to be nil (breaking the first rule)
11 11
     # 'included_in': Pass an array of values - the value must be one of these
38  spec/masterplan_spec.rb
@@ -67,6 +67,16 @@ def test_value_and_expect(testee, *error_and_descripton)
67 67
       end.should raise_error(ArgumentError, /scheme needs to be a Masterplan::Document/)
68 68
     end
69 69
 
  70
+    it "complains if not given a proper format key" do
  71
+      lambda do
  72
+        Masterplan.compare(
  73
+          :scheme => Masterplan::Document.new({}),
  74
+          :to => {},
  75
+          :format => :medium
  76
+        )
  77
+      end.should raise_error(ArgumentError, ":format needs to be one of [:full, :mini] !")
  78
+    end
  79
+
70 80
     it "complains if there are extra keys" do
71 81
       test_value_and_expect(
72 82
         { :ship => {}, :boat => {} },
@@ -109,11 +119,37 @@ def test_value_and_expect(testee, *error_and_descripton)
109 119
       )
110 120
     end
111 121
 
  122
+    [nil, :full].each do |format|
  123
+      it "produces full output for format = #{format}" do
  124
+        lambda do
  125
+          Masterplan.compare(
  126
+            :scheme => @scheme,
  127
+            :to => { :ship => [] },
  128
+            :format => format
  129
+          )
  130
+        end.should raise_error(
  131
+          /value at 'root'=>'ship' \(Array\) is not a Hash !\n\s*?Expected:.*?but was/m
  132
+        )
  133
+      end
  134
+    end
  135
+
  136
+    it "produces one-line output when using :mini format" do
  137
+      lambda do
  138
+        Masterplan.compare(
  139
+          :scheme => @scheme,
  140
+          :to => { :ship => [] },
  141
+          :format => :mini
  142
+        )
  143
+      end.should raise_error(
  144
+        "value at 'root'=>'ship' (Array) is not a Hash !"
  145
+      )
  146
+    end
  147
+
112 148
     it "checks all values of value arrays, but only against the first array value of the scheme"
113 149
     it "checks all array values one-to-one if the compare_each rule is used"
114 150
   end
115 151
 
116  
-  it "convertsinto plain example hashes"
  152
+  it "converts into plain example hashes"
117 153
   it "doesn't create a Document out of anything other than a Hash"
118 154
   it "checks that the examples of rules obey the rules"
119 155
   it "has a unit test extension method"

0 notes on commit b9983ef

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