olag 0.1.10 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,5 +1,10 @@
1
+ 2011-08-25 Oren Ben-Kiki <github-oren@ben-kiki.org>
2
+
3
+ * Additional utilities.
4
+
1
5
  2011-07-24 Oren Ben-Kiki <github-oren@ben-kiki.org>
2
6
 
7
+ * Append rather than replace gem spec file members.
3
8
  * Allow in_path error blocks to return a value.
4
9
 
5
10
  2011-04-22 Oren Ben-Kiki <github-oren@ben-kiki.org>
@@ -1994,7 +1994,7 @@ number of Git commits by running <tt>rake version</tt>.
1994
1994
  </tr>
1995
1995
  </table>
1996
1996
  <pre class='ruby code syntax'>
1997
- <span class="Type">VERSION</span> = <span class="Special">&quot;</span><span class="Constant">0.1.10</span><span class="Special">&quot;</span>
1997
+ <span class="Type">VERSION</span> = <span class="Special">&quot;</span><span class="Constant">0.1.12</span><span class="Special">&quot;</span>
1998
1998
 
1999
1999
  end
2000
2000
  </pre>
@@ -2740,6 +2740,7 @@ Test collecting errors.
2740
2740
  <span class="PreProc">class</span> <span class="Type">TestCollectErrors</span> &lt; <span class="Type">Test</span>::<span class="Type">Unit</span>::<span class="Type">TestCase</span>
2741
2741
 
2742
2742
  <span class="PreProc">include</span> <span class="Type">Test</span>::<span class="Type">WithErrors</span>
2743
+ <span class="PreProc">include</span> <span class="Type">Test</span>::<span class="Type">WithFakeFS</span>
2743
2744
 
2744
2745
  <span class="PreProc">def</span> <span class="Identifier">test_one_error</span>
2745
2746
  <span class="Identifier">@errors</span> &lt;&lt; <span class="Special">&quot;</span><span class="Constant">Oops</span><span class="Special">&quot;</span>
@@ -2748,22 +2749,38 @@ Test collecting errors.
2748
2749
 
2749
2750
  <span class="PreProc">def</span> <span class="Identifier">test_path_error</span>
2750
2751
  <span class="Identifier">@errors</span>.in_path(<span class="Special">&quot;</span><span class="Constant">foo</span><span class="Special">&quot;</span>) <span class="Statement">do</span>
2751
- <span class="Identifier">@errors</span> &lt;&lt; <span class="Special">&quot;</span><span class="Constant">Eeek</span><span class="Special">&quot;</span>
2752
+ <span class="Identifier">@errors</span> &lt;&lt; <span class="Special">&quot;</span><span class="Constant">Oops</span><span class="Special">&quot;</span>
2752
2753
  <span class="Special">&quot;</span><span class="Constant">result</span><span class="Special">&quot;</span>
2753
2754
  <span class="Statement">end</span>.should == <span class="Special">&quot;</span><span class="Constant">result</span><span class="Special">&quot;</span>
2754
- <span class="Identifier">@errors</span> &lt;&lt; <span class="Special">&quot;</span><span class="Constant">Oops</span><span class="Special">&quot;</span>
2755
- <span class="Identifier">@errors</span>.should == [ <span class="Special">&quot;</span><span class="Special">#{</span><span class="Identifier">$0</span><span class="Special">}</span><span class="Constant">: Eeek in file: foo</span><span class="Special">&quot;</span>, <span class="Special">&quot;</span><span class="Special">#{</span><span class="Identifier">$0</span><span class="Special">}</span><span class="Constant">: Oops</span><span class="Special">&quot;</span> ]
2755
+ <span class="Identifier">@errors</span>.should == [ <span class="Special">&quot;</span><span class="Special">#{</span><span class="Identifier">$0</span><span class="Special">}</span><span class="Constant">: Oops in file: foo</span><span class="Special">&quot;</span> ]
2756
2756
  <span class="PreProc">end</span>
2757
2757
 
2758
2758
  <span class="PreProc">def</span> <span class="Identifier">test_line_error</span>
2759
2759
  <span class="Identifier">@errors</span>.in_path(<span class="Special">&quot;</span><span class="Constant">foo</span><span class="Special">&quot;</span>) <span class="Statement">do</span>
2760
2760
  <span class="Identifier">@errors</span>.at_line(<span class="Constant">1</span>)
2761
- <span class="Identifier">@errors</span> &lt;&lt; <span class="Special">&quot;</span><span class="Constant">Eeek</span><span class="Special">&quot;</span>
2761
+ <span class="Identifier">@errors</span> &lt;&lt; <span class="Special">&quot;</span><span class="Constant">Oops</span><span class="Special">&quot;</span>
2762
2762
  <span class="Statement">end</span>
2763
- <span class="Identifier">@errors</span> &lt;&lt; <span class="Special">&quot;</span><span class="Constant">Oops</span><span class="Special">&quot;</span>
2764
- <span class="Identifier">@errors</span>.should == [ <span class="Special">&quot;</span><span class="Special">#{</span><span class="Identifier">$0</span><span class="Special">}</span><span class="Constant">: Eeek in file: foo at line: 1</span><span class="Special">&quot;</span>, <span class="Special">&quot;</span><span class="Special">#{</span><span class="Identifier">$0</span><span class="Special">}</span><span class="Constant">: Oops</span><span class="Special">&quot;</span> ]
2763
+ <span class="Identifier">@errors</span>.should == [ <span class="Special">&quot;</span><span class="Special">#{</span><span class="Identifier">$0</span><span class="Special">}</span><span class="Constant">: Oops in file: foo at line: 1</span><span class="Special">&quot;</span> ]
2764
+ <span class="PreProc">end</span>
2765
+
2766
+ <span class="PreProc">def</span> <span class="Identifier">test_file_error</span>
2767
+ write_fake_file(<span class="Special">&quot;</span><span class="Constant">foo</span><span class="Special">&quot;</span>, <span class="Special">&quot;</span><span class="Constant">bar</span><span class="Special">\n</span><span class="Special">&quot;</span>)
2768
+ <span class="Identifier">@errors</span>.in_file(<span class="Special">&quot;</span><span class="Constant">foo</span><span class="Special">&quot;</span>) <span class="Statement">do</span>
2769
+ <span class="Identifier">@errors</span> &lt;&lt; <span class="Special">&quot;</span><span class="Constant">Oops</span><span class="Special">&quot;</span>
2770
+ <span class="Special">&quot;</span><span class="Constant">result</span><span class="Special">&quot;</span>
2771
+ <span class="Statement">end</span>.should == <span class="Special">&quot;</span><span class="Constant">result</span><span class="Special">&quot;</span>
2772
+ <span class="Identifier">@errors</span>.should == [ <span class="Special">&quot;</span><span class="Special">#{</span><span class="Identifier">$0</span><span class="Special">}</span><span class="Constant">: Oops in file: foo</span><span class="Special">&quot;</span> ]
2773
+ <span class="PreProc">end</span>
2774
+
2775
+ <span class="PreProc">def</span> <span class="Identifier">test_file_lines_error</span>
2776
+ write_fake_file(<span class="Special">&quot;</span><span class="Constant">foo</span><span class="Special">&quot;</span>, <span class="Special">&quot;</span><span class="Constant">bar</span><span class="Special">\n</span><span class="Constant">baz</span><span class="Special">\n</span><span class="Special">&quot;</span>)
2777
+ <span class="Identifier">@errors</span>.in_file_lines(<span class="Special">&quot;</span><span class="Constant">foo</span><span class="Special">&quot;</span>) <span class="Statement">do</span> |<span class="Identifier">line</span>|
2778
+ <span class="Identifier">@errors</span> &lt;&lt; <span class="Special">&quot;</span><span class="Constant">Oops</span><span class="Special">&quot;</span> <span class="Statement">if</span> line == <span class="Special">&quot;</span><span class="Constant">baz</span><span class="Special">\n</span><span class="Special">&quot;</span>
2779
+ <span class="Statement">end</span>
2780
+ <span class="Identifier">@errors</span>.should == [ <span class="Special">&quot;</span><span class="Special">#{</span><span class="Identifier">$0</span><span class="Special">}</span><span class="Constant">: Oops in file: foo at line: 2</span><span class="Special">&quot;</span> ]
2765
2781
  <span class="PreProc">end</span>
2766
2782
 
2783
+
2767
2784
  <span class="PreProc">end</span>
2768
2785
  </pre>
2769
2786
  </div>
@@ -2888,6 +2905,42 @@ Collect a list of errors.
2888
2905
  <pre class='ruby code syntax'>
2889
2906
  <span class="PreProc">class</span> <span class="Type">Errors</span> &lt; <span class="Type">Array</span>
2890
2907
 
2908
+ </pre>
2909
+ <table class='layout'>
2910
+ <tr>
2911
+ <td class='indentation'>
2912
+ <pre> </pre>
2913
+ </td>
2914
+ <td class='html'>
2915
+ <div class='rdoc comment markup'>
2916
+ <p>
2917
+ The current path we are reporting errors for, if any.
2918
+ </p>
2919
+ </div>
2920
+ </td>
2921
+ </tr>
2922
+ </table>
2923
+ <pre class='ruby code syntax'>
2924
+ <span class="Statement">attr_reader</span> <span class="Constant">:path</span>
2925
+
2926
+ </pre>
2927
+ <table class='layout'>
2928
+ <tr>
2929
+ <td class='indentation'>
2930
+ <pre> </pre>
2931
+ </td>
2932
+ <td class='html'>
2933
+ <div class='rdoc comment markup'>
2934
+ <p>
2935
+ The current line number we are reporting errors for, if any.
2936
+ </p>
2937
+ </div>
2938
+ </td>
2939
+ </tr>
2940
+ </table>
2941
+ <pre class='ruby code syntax'>
2942
+ <span class="Statement">attr_reader</span> <span class="Constant">:line_number</span>
2943
+
2891
2944
  </pre>
2892
2945
  <table class='layout'>
2893
2946
  <tr>
@@ -2906,7 +2959,7 @@ Create an empty errors collection.
2906
2959
  <pre class='ruby code syntax'>
2907
2960
  <span class="PreProc">def</span> <span class="Identifier">initialize</span>
2908
2961
  <span class="Identifier">@path</span> = <span class="Constant">nil</span>
2909
- <span class="Identifier">@line</span> = <span class="Constant">nil</span>
2962
+ <span class="Identifier">@line_number</span> = <span class="Constant">nil</span>
2910
2963
  <span class="PreProc">end</span>
2911
2964
 
2912
2965
  </pre>
@@ -2926,13 +2979,61 @@ Associate all errors collected by a block with a specific disk file.
2926
2979
  </table>
2927
2980
  <pre class='ruby code syntax'>
2928
2981
  <span class="PreProc">def</span> <span class="Identifier">in_path</span>(path, &amp;block)
2929
- prev_path, prev_line = <span class="Identifier">@path</span>, <span class="Identifier">@line</span>
2930
- <span class="Identifier">@path</span>, <span class="Identifier">@line</span> = path, <span class="Constant">nil</span>
2931
- result = block.call
2932
- <span class="Identifier">@path</span>, <span class="Identifier">@line</span> = prev_path, prev_line
2982
+ prev_path, prev_line_number = <span class="Identifier">@path</span>, <span class="Identifier">@line_number</span>
2983
+ <span class="Identifier">@path</span>, <span class="Identifier">@line_number</span> = path, <span class="Constant">nil</span>
2984
+ result = block.call(path)
2985
+ <span class="Identifier">@path</span>, <span class="Identifier">@line_number</span> = prev_path, prev_line_number
2933
2986
  <span class="Statement">return</span> result
2934
2987
  <span class="PreProc">end</span>
2935
2988
 
2989
+ </pre>
2990
+ <table class='layout'>
2991
+ <tr>
2992
+ <td class='indentation'>
2993
+ <pre> </pre>
2994
+ </td>
2995
+ <td class='html'>
2996
+ <div class='rdoc comment markup'>
2997
+ <p>
2998
+ Associate all errors collected by a block with a disk file that is opened
2999
+ and passed to the block.
3000
+ </p>
3001
+ </div>
3002
+ </td>
3003
+ </tr>
3004
+ </table>
3005
+ <pre class='ruby code syntax'>
3006
+ <span class="PreProc">def</span> <span class="Identifier">in_file</span>(path, mode = <span class="Special">&quot;</span><span class="Constant">r</span><span class="Special">&quot;</span>, &amp;block)
3007
+ <span class="Statement">return</span> in_path(path) { <span class="Type">File</span>.open(path, mode, &amp;block) }
3008
+ <span class="PreProc">end</span>
3009
+
3010
+ </pre>
3011
+ <table class='layout'>
3012
+ <tr>
3013
+ <td class='indentation'>
3014
+ <pre> </pre>
3015
+ </td>
3016
+ <td class='html'>
3017
+ <div class='rdoc comment markup'>
3018
+ <p>
3019
+ Associate all errors collected by a block with a line read from a disk file
3020
+ that is opened and passed to the block.
3021
+ </p>
3022
+ </div>
3023
+ </td>
3024
+ </tr>
3025
+ </table>
3026
+ <pre class='ruby code syntax'>
3027
+ <span class="PreProc">def</span> <span class="Identifier">in_file_lines</span>(path, &amp;block)
3028
+ in_file(path) <span class="Statement">do</span> |<span class="Identifier">file</span>|
3029
+ <span class="Identifier">@line_number</span> = <span class="Constant">0</span>
3030
+ file.each_line <span class="Statement">do</span> |<span class="Identifier">line</span>|
3031
+ <span class="Identifier">@line_number</span> += <span class="Constant">1</span>
3032
+ block.call(line)
3033
+ <span class="Statement">end</span>
3034
+ <span class="Statement">end</span>
3035
+ <span class="PreProc">end</span>
3036
+
2936
3037
  </pre>
2937
3038
  <table class='layout'>
2938
3039
  <tr>
@@ -2949,8 +3050,8 @@ Set the line number for any errors collected from here on.
2949
3050
  </tr>
2950
3051
  </table>
2951
3052
  <pre class='ruby code syntax'>
2952
- <span class="PreProc">def</span> <span class="Identifier">at_line</span>(line)
2953
- <span class="Identifier">@line</span> = line
3053
+ <span class="PreProc">def</span> <span class="Identifier">at_line</span>(line_number)
3054
+ <span class="Identifier">@line_number</span> = line_number
2954
3055
  <span class="PreProc">end</span>
2955
3056
 
2956
3057
  </pre>
@@ -2995,8 +3096,8 @@ Annotate an error message with the context (current file and line).
2995
3096
  <pre class='ruby code syntax'>
2996
3097
  <span class="PreProc">def</span> <span class="Identifier">annotate_error_message</span>(message)
2997
3098
  <span class="Statement">return</span> <span class="Special">&quot;</span><span class="Special">#{</span><span class="Identifier">$0</span><span class="Special">}</span><span class="Constant">: </span><span class="Special">#{</span>message<span class="Special">}</span><span class="Special">&quot;</span> <span class="Statement">unless</span> <span class="Identifier">@path</span>
2998
- <span class="Statement">return</span> <span class="Special">&quot;</span><span class="Special">#{</span><span class="Identifier">$0</span><span class="Special">}</span><span class="Constant">: </span><span class="Special">#{</span>message<span class="Special">}</span><span class="Constant"> in file: </span><span class="Special">#{</span><span class="Identifier">@path</span><span class="Special">}</span><span class="Special">&quot;</span> <span class="Statement">unless</span> <span class="Identifier">@line</span>
2999
- <span class="Statement">return</span> <span class="Special">&quot;</span><span class="Special">#{</span><span class="Identifier">$0</span><span class="Special">}</span><span class="Constant">: </span><span class="Special">#{</span>message<span class="Special">}</span><span class="Constant"> in file: </span><span class="Special">#{</span><span class="Identifier">@path</span><span class="Special">}</span><span class="Constant"> at line: </span><span class="Special">#{</span><span class="Identifier">@line</span><span class="Special">}</span><span class="Special">&quot;</span>
3099
+ <span class="Statement">return</span> <span class="Special">&quot;</span><span class="Special">#{</span><span class="Identifier">$0</span><span class="Special">}</span><span class="Constant">: </span><span class="Special">#{</span>message<span class="Special">}</span><span class="Constant"> in file: </span><span class="Special">#{</span><span class="Identifier">@path</span><span class="Special">}</span><span class="Special">&quot;</span> <span class="Statement">unless</span> <span class="Identifier">@line_number</span>
3100
+ <span class="Statement">return</span> <span class="Special">&quot;</span><span class="Special">#{</span><span class="Identifier">$0</span><span class="Special">}</span><span class="Constant">: </span><span class="Special">#{</span>message<span class="Special">}</span><span class="Constant"> in file: </span><span class="Special">#{</span><span class="Identifier">@path</span><span class="Special">}</span><span class="Constant"> at line: </span><span class="Special">#{</span><span class="Identifier">@line_number</span><span class="Special">}</span><span class="Special">&quot;</span>
3000
3101
  <span class="PreProc">end</span>
3001
3102
 
3002
3103
  end
@@ -3024,6 +3125,7 @@ when running applications inside unit tests):
3024
3125
  </div>
3025
3126
  <div class="chunk html">
3026
3127
  <pre class='ruby code syntax'>
3128
+ <span class="PreProc">require</span> <span class="Special">&quot;</span><span class="Constant">fileutils</span><span class="Special">&quot;</span>
3027
3129
  <span class="PreProc">require</span> <span class="Special">&quot;</span><span class="Constant">fakefs/safe</span><span class="Special">&quot;</span>
3028
3130
 
3029
3131
  <span class="PreProc">module</span> <span class="Type">Test</span>
@@ -3046,6 +3148,31 @@ Mix-in for tests that use the FakeFS fake file system.
3046
3148
  <pre class='ruby code syntax'>
3047
3149
  <span class="PreProc">module</span> <span class="Type">WithFakeFS</span>
3048
3150
 
3151
+ </pre>
3152
+ <table class='layout'>
3153
+ <tr>
3154
+ <td class='indentation'>
3155
+ <pre> </pre>
3156
+ </td>
3157
+ <td class='html'>
3158
+ <div class='rdoc comment markup'>
3159
+ <p>
3160
+ Create and write into a file on the fake file system.
3161
+ </p>
3162
+ </div>
3163
+ </td>
3164
+ </tr>
3165
+ </table>
3166
+ <pre class='ruby code syntax'>
3167
+ <span class="PreProc">def</span> <span class="Identifier">write_fake_file</span>(path, content = <span class="Constant">nil</span>, &amp;block)
3168
+ directory = <span class="Type">File</span>.dirname(path)
3169
+ <span class="Type">FileUtils</span>.mkdir_p(directory) <span class="Statement">unless</span> <span class="Type">File</span>.exists?(directory)
3170
+ <span class="Type">File</span>.open(path, <span class="Special">&quot;</span><span class="Constant">w</span><span class="Special">&quot;</span>) <span class="Statement">do</span> |<span class="Identifier">file</span>|
3171
+ file.write(content) <span class="Statement">unless</span> content.nil?
3172
+ block.call(file) <span class="Statement">unless</span> block.nil?
3173
+ <span class="Statement">end</span>
3174
+ <span class="PreProc">end</span>
3175
+
3049
3176
  </pre>
3050
3177
  <table class='layout'>
3051
3178
  <tr>
@@ -3485,6 +3612,27 @@ Run the error application.
3485
3612
  <span class="Statement">super</span> { <span class="Identifier">@errors</span> &lt;&lt; <span class="Special">&quot;</span><span class="Constant">Oops!</span><span class="Special">&quot;</span> }
3486
3613
  <span class="PreProc">end</span>
3487
3614
 
3615
+ </pre>
3616
+ <table class='layout'>
3617
+ <tr>
3618
+ <td class='indentation'>
3619
+ <pre> </pre>
3620
+ </td>
3621
+ <td class='html'>
3622
+ <div class='rdoc comment markup'>
3623
+ <p>
3624
+ Test minimal number of arguments.
3625
+ </p>
3626
+ </div>
3627
+ </td>
3628
+ </tr>
3629
+ </table>
3630
+ <pre class='ruby code syntax'>
3631
+ <span class="PreProc">def</span> <span class="Identifier">parse_arguments</span>
3632
+ expect_at_least(<span class="Constant">2</span>, <span class="Special">&quot;</span><span class="Constant">fake arguments</span><span class="Special">&quot;</span>)
3633
+ expect_at_most(<span class="Constant">3</span>, <span class="Special">&quot;</span><span class="Constant">fake arguments</span><span class="Special">&quot;</span>)
3634
+ <span class="PreProc">end</span>
3635
+
3488
3636
  end
3489
3637
 
3490
3638
  </pre>
@@ -3511,9 +3659,19 @@ Test running a Olag Application.
3511
3659
  <span class="Type">Olag</span>::<span class="Type">Application</span>.with_argv([]) { <span class="Type">Olag</span>::<span class="Type">Application</span>.new(<span class="Constant">true</span>).run }.should == <span class="Constant">0</span>
3512
3660
  <span class="PreProc">end</span>
3513
3661
 
3662
+ <span class="PreProc">def</span> <span class="Identifier">test_inexact_arguments</span>
3663
+ <span class="Type">Olag</span>::<span class="Type">Application</span>.with_argv(<span class="Special">%w(</span><span class="Constant">-e stderr foo</span><span class="Special">)</span>) { <span class="Type">Olag</span>::<span class="Type">Application</span>.new(<span class="Constant">true</span>).run }.should == <span class="Constant">1</span>
3664
+ <span class="Type">File</span>.read(<span class="Special">&quot;</span><span class="Constant">stderr</span><span class="Special">&quot;</span>).should.include?(<span class="Special">&quot;</span><span class="Constant">Expects no arguments</span><span class="Special">&quot;</span>)
3665
+ <span class="PreProc">end</span>
3666
+
3667
+ <span class="PreProc">def</span> <span class="Identifier">test_missing_arguments</span>
3668
+ <span class="Type">Olag</span>::<span class="Type">Application</span>.with_argv(<span class="Special">%w(</span><span class="Constant">-e stderr foo</span><span class="Special">)</span>) { <span class="Type">ErrorApplication</span>.new(<span class="Constant">true</span>).run }.should == <span class="Constant">1</span>
3669
+ <span class="Type">File</span>.read(<span class="Special">&quot;</span><span class="Constant">stderr</span><span class="Special">&quot;</span>).should.include?(<span class="Special">&quot;</span><span class="Constant">Expects at least 2 fake arguments</span><span class="Special">&quot;</span>)
3670
+ <span class="PreProc">end</span>
3671
+
3514
3672
  <span class="PreProc">def</span> <span class="Identifier">test_extra_arguments</span>
3515
- <span class="Type">Olag</span>::<span class="Type">Application</span>.with_argv(<span class="Special">%w(</span><span class="Constant">-e stderr dummy</span><span class="Special">)</span>) { <span class="Type">Olag</span>::<span class="Type">Application</span>.new(<span class="Constant">true</span>).run }.should == <span class="Constant">1</span>
3516
- <span class="Type">File</span>.read(<span class="Special">&quot;</span><span class="Constant">stderr</span><span class="Special">&quot;</span>).should.include?(<span class="Special">&quot;</span><span class="Constant">Expects no command line file arguments</span><span class="Special">&quot;</span>)
3673
+ <span class="Type">Olag</span>::<span class="Type">Application</span>.with_argv(<span class="Special">%w(</span><span class="Constant">-e stderr foo bar baz bad</span><span class="Special">)</span>) { <span class="Type">ErrorApplication</span>.new(<span class="Constant">true</span>).run }.should == <span class="Constant">1</span>
3674
+ <span class="Type">File</span>.read(<span class="Special">&quot;</span><span class="Constant">stderr</span><span class="Special">&quot;</span>).should.include?(<span class="Special">&quot;</span><span class="Constant">Expects at most 3 fake arguments</span><span class="Special">&quot;</span>)
3517
3675
  <span class="PreProc">end</span>
3518
3676
 
3519
3677
  <span class="PreProc">def</span> <span class="Identifier">test_print_version</span>
@@ -3527,7 +3685,7 @@ Test running a Olag Application.
3527
3685
  <span class="PreProc">end</span>
3528
3686
 
3529
3687
  <span class="PreProc">def</span> <span class="Identifier">test_print_errors</span>
3530
- <span class="Type">Olag</span>::<span class="Type">Application</span>.with_argv(<span class="Special">%w(</span><span class="Constant">-e stderr</span><span class="Special">)</span>) { <span class="Type">ErrorApplication</span>.new(<span class="Constant">true</span>).run }.should == <span class="Constant">1</span>
3688
+ <span class="Type">Olag</span>::<span class="Type">Application</span>.with_argv(<span class="Special">%w(</span><span class="Constant">-e stderr foo bar</span><span class="Special">)</span>) { <span class="Type">ErrorApplication</span>.new(<span class="Constant">true</span>).run }.should == <span class="Constant">1</span>
3531
3689
  <span class="Type">File</span>.read(<span class="Special">&quot;</span><span class="Constant">stderr</span><span class="Special">&quot;</span>).should.include?(<span class="Special">&quot;</span><span class="Constant">Oops!</span><span class="Special">&quot;</span>)
3532
3690
  <span class="PreProc">end</span>
3533
3691
 
@@ -3671,6 +3829,90 @@ Parse the command line options of the program.
3671
3829
  parse_arguments
3672
3830
  <span class="PreProc">end</span>
3673
3831
 
3832
+ </pre>
3833
+ <table class='layout'>
3834
+ <tr>
3835
+ <td class='indentation'>
3836
+ <pre> </pre>
3837
+ </td>
3838
+ <td class='html'>
3839
+ <div class='rdoc comment markup'>
3840
+ <p>
3841
+ Expect a limited number of remaining arguments (verified by the block).
3842
+ </p>
3843
+ </div>
3844
+ </td>
3845
+ </tr>
3846
+ </table>
3847
+ <pre class='ruby code syntax'>
3848
+ <span class="PreProc">def</span> <span class="Identifier">expect_limited_arguments</span>(message_prefix, arguments_limit, argument_type, &amp;block)
3849
+ <span class="Statement">if</span> !<span class="Statement">yield</span>(<span class="Identifier">ARGV</span>.size)
3850
+ <span class="Identifier">$stderr</span>.puts(<span class="Special">&quot;</span><span class="Special">#{</span><span class="Identifier">$0</span><span class="Special">}</span><span class="Constant">: </span><span class="Special">#{</span>message_prefix<span class="Special">}</span><span class="Constant"> </span><span class="Special">#{</span>arguments_limit<span class="Special">}</span><span class="Constant"> </span><span class="Special">#{</span>argument_type<span class="Special">}</span><span class="Constant">.</span><span class="Special">&quot;</span>)
3851
+ <span class="Statement">exit</span>(<span class="Constant">1</span>)
3852
+ <span class="Statement">end</span>
3853
+ <span class="PreProc">end</span>
3854
+
3855
+ </pre>
3856
+ <table class='layout'>
3857
+ <tr>
3858
+ <td class='indentation'>
3859
+ <pre> </pre>
3860
+ </td>
3861
+ <td class='html'>
3862
+ <div class='rdoc comment markup'>
3863
+ <p>
3864
+ Ensure we have got at least a certain number of command line arguments.
3865
+ </p>
3866
+ </div>
3867
+ </td>
3868
+ </tr>
3869
+ </table>
3870
+ <pre class='ruby code syntax'>
3871
+ <span class="PreProc">def</span> <span class="Identifier">expect_at_least</span>(minimal_arguments, argument_type)
3872
+ expect_limited_arguments(<span class="Special">&quot;</span><span class="Constant">Expects at least</span><span class="Special">&quot;</span>, minimal_arguments, argument_type) { |<span class="Identifier">remaining_arguments</span>| remaining_arguments &gt;= minimal_arguments }
3873
+ <span class="PreProc">end</span>
3874
+
3875
+ </pre>
3876
+ <table class='layout'>
3877
+ <tr>
3878
+ <td class='indentation'>
3879
+ <pre> </pre>
3880
+ </td>
3881
+ <td class='html'>
3882
+ <div class='rdoc comment markup'>
3883
+ <p>
3884
+ Ensure we have got at least a certain number of command line arguments.
3885
+ </p>
3886
+ </div>
3887
+ </td>
3888
+ </tr>
3889
+ </table>
3890
+ <pre class='ruby code syntax'>
3891
+ <span class="PreProc">def</span> <span class="Identifier">expect_at_most</span>(maximal_arguments, argument_type)
3892
+ expect_limited_arguments(<span class="Special">&quot;</span><span class="Constant">Expects at most</span><span class="Special">&quot;</span>, maximal_arguments, argument_type) { |<span class="Identifier">remaining_arguments</span>| remaining_arguments &lt;= maximal_arguments }
3893
+ <span class="PreProc">end</span>
3894
+
3895
+ </pre>
3896
+ <table class='layout'>
3897
+ <tr>
3898
+ <td class='indentation'>
3899
+ <pre> </pre>
3900
+ </td>
3901
+ <td class='html'>
3902
+ <div class='rdoc comment markup'>
3903
+ <p>
3904
+ Ensure we have got an exact number of command line arguments.
3905
+ </p>
3906
+ </div>
3907
+ </td>
3908
+ </tr>
3909
+ </table>
3910
+ <pre class='ruby code syntax'>
3911
+ <span class="PreProc">def</span> <span class="Identifier">expect_exactly</span>(exact_arguments, argument_type)
3912
+ arguments_limit = exact_arguments == <span class="Constant">0</span> ? <span class="Special">'</span><span class="Constant">no</span><span class="Special">'</span> : exact_arguments
3913
+ expect_limited_arguments(<span class="Special">&quot;</span><span class="Constant">Expects</span><span class="Special">&quot;</span>, arguments_limit, argument_type) { |<span class="Identifier">remaining_arguments</span>| remaining_arguments == exact_arguments }
3914
+ <span class="PreProc">end</span>
3915
+
3674
3916
  </pre>
3675
3917
  <table class='layout'>
3676
3918
  <tr>
@@ -3690,9 +3932,7 @@ are no such arguments.
3690
3932
  </table>
3691
3933
  <pre class='ruby code syntax'>
3692
3934
  <span class="PreProc">def</span> <span class="Identifier">parse_arguments</span>
3693
- <span class="Statement">return</span> <span class="Statement">if</span> <span class="Identifier">ARGV</span>.size == <span class="Constant">0</span>
3694
- <span class="Identifier">$stderr</span>.puts(<span class="Special">&quot;</span><span class="Special">#{</span><span class="Identifier">$0</span><span class="Special">}</span><span class="Constant">: Expects no command line file arguments.</span><span class="Special">&quot;</span>)
3695
- <span class="Statement">exit</span>(<span class="Constant">1</span>)
3935
+ expect_exactly(<span class="Constant">0</span>, <span class="Special">&quot;</span><span class="Constant">arguments</span><span class="Special">&quot;</span>)
3696
3936
  <span class="PreProc">end</span>
3697
3937
 
3698
3938
  </pre>
@@ -46,13 +46,35 @@ module Olag
46
46
  parse_arguments
47
47
  end
48
48
 
49
+ # Expect a limited number of remaining arguments (verified by the block).
50
+ def expect_limited_arguments(message_prefix, arguments_limit, argument_type, &block)
51
+ if !yield(ARGV.size)
52
+ $stderr.puts("#{$0}: #{message_prefix} #{arguments_limit} #{argument_type}.")
53
+ exit(1)
54
+ end
55
+ end
56
+
57
+ # Ensure we have got at least a certain number of command line arguments.
58
+ def expect_at_least(minimal_arguments, argument_type)
59
+ expect_limited_arguments("Expects at least", minimal_arguments, argument_type) { |remaining_arguments| remaining_arguments >= minimal_arguments }
60
+ end
61
+
62
+ # Ensure we have got at least a certain number of command line arguments.
63
+ def expect_at_most(maximal_arguments, argument_type)
64
+ expect_limited_arguments("Expects at most", maximal_arguments, argument_type) { |remaining_arguments| remaining_arguments <= maximal_arguments }
65
+ end
66
+
67
+ # Ensure we have got an exact number of command line arguments.
68
+ def expect_exactly(exact_arguments, argument_type)
69
+ arguments_limit = exact_arguments == 0 ? 'no' : exact_arguments
70
+ expect_limited_arguments("Expects", arguments_limit, argument_type) { |remaining_arguments| remaining_arguments == exact_arguments }
71
+ end
72
+
49
73
  # Parse remaining command-line file arguments. This is expected to be
50
74
  # overriden by the concrete application sub-class. By default assumes there
51
75
  # are no such arguments.
52
76
  def parse_arguments
53
- return if ARGV.size == 0
54
- $stderr.puts("#{$0}: Expects no command line file arguments.")
55
- exit(1)
77
+ expect_exactly(0, "arguments")
56
78
  end
57
79
 
58
80
  # Define application flags. This is expected to be overriden by the
@@ -3,24 +3,48 @@ module Olag
3
3
  # Collect a list of errors.
4
4
  class Errors < Array
5
5
 
6
+ # The current path we are reporting errors for, if any.
7
+ attr_reader :path
8
+
9
+ # The current line number we are reporting errors for, if any.
10
+ attr_reader :line_number
11
+
6
12
  # Create an empty errors collection.
7
13
  def initialize
8
14
  @path = nil
9
- @line = nil
15
+ @line_number = nil
10
16
  end
11
17
 
12
18
  # Associate all errors collected by a block with a specific disk file.
13
19
  def in_path(path, &block)
14
- prev_path, prev_line = @path, @line
15
- @path, @line = path, nil
16
- result = block.call
17
- @path, @line = prev_path, prev_line
20
+ prev_path, prev_line_number = @path, @line_number
21
+ @path, @line_number = path, nil
22
+ result = block.call(path)
23
+ @path, @line_number = prev_path, prev_line_number
18
24
  return result
19
25
  end
20
26
 
27
+ # Associate all errors collected by a block with a disk file that is opened
28
+ # and passed to the block.
29
+ def in_file(path, mode = "r", &block)
30
+ return in_path(path) { File.open(path, mode, &block) }
31
+ end
32
+
33
+ # Associate all errors collected by a block with a line read from a disk
34
+ # file that is opened and passed to the block.
35
+ def in_file_lines(path, &block)
36
+ in_file(path) do |file|
37
+ @line_number = 0
38
+ file.each_line do |line|
39
+ @line_number += 1
40
+ block.call(line)
41
+ end
42
+ end
43
+ end
44
+
21
45
  # Set the line number for any errors collected from here on.
22
- def at_line(line)
23
- @line = line
46
+ def at_line(line_number)
47
+ @line_number = line_number
24
48
  end
25
49
 
26
50
  # Add a single error to the collection, with automatic context annotation
@@ -35,8 +59,8 @@ module Olag
35
59
  # Annotate an error message with the context (current file and line).
36
60
  def annotate_error_message(message)
37
61
  return "#{$0}: #{message}" unless @path
38
- return "#{$0}: #{message} in file: #{@path}" unless @line
39
- return "#{$0}: #{message} in file: #{@path} at line: #{@line}"
62
+ return "#{$0}: #{message} in file: #{@path}" unless @line_number
63
+ return "#{$0}: #{message} in file: #{@path} at line: #{@line_number}"
40
64
  end
41
65
 
42
66
  end
@@ -1,3 +1,4 @@
1
+ require "fileutils"
1
2
  require "fakefs/safe"
2
3
 
3
4
  module Test
@@ -5,6 +6,16 @@ module Test
5
6
  # Mix-in for tests that use the FakeFS fake file system.
6
7
  module WithFakeFS
7
8
 
9
+ # Create and write into a file on the fake file system.
10
+ def write_fake_file(path, content = nil, &block)
11
+ directory = File.dirname(path)
12
+ FileUtils.mkdir_p(directory) unless File.exists?(directory)
13
+ File.open(path, "w") do |file|
14
+ file.write(content) unless content.nil?
15
+ block.call(file) unless block.nil?
16
+ end
17
+ end
18
+
8
19
  # Aliasing methods needs to be deferred to when the module is included and
9
20
  # be executed in the context of the class.
10
21
  def self.included(base)
@@ -3,6 +3,6 @@ module Olag
3
3
 
4
4
  # This version number. The third number is automatically updated to track the
5
5
  # number of Git commits by running <tt>rake version</tt>.
6
- VERSION = "0.1.10"
6
+ VERSION = "0.1.12"
7
7
 
8
8
  end
@@ -6,6 +6,7 @@ require "test/spec"
6
6
  class TestCollectErrors < Test::Unit::TestCase
7
7
 
8
8
  include Test::WithErrors
9
+ include Test::WithFakeFS
9
10
 
10
11
  def test_one_error
11
12
  @errors << "Oops"
@@ -14,20 +15,36 @@ class TestCollectErrors < Test::Unit::TestCase
14
15
 
15
16
  def test_path_error
16
17
  @errors.in_path("foo") do
17
- @errors << "Eeek"
18
+ @errors << "Oops"
18
19
  "result"
19
20
  end.should == "result"
20
- @errors << "Oops"
21
- @errors.should == [ "#{$0}: Eeek in file: foo", "#{$0}: Oops" ]
21
+ @errors.should == [ "#{$0}: Oops in file: foo" ]
22
22
  end
23
23
 
24
24
  def test_line_error
25
25
  @errors.in_path("foo") do
26
26
  @errors.at_line(1)
27
- @errors << "Eeek"
27
+ @errors << "Oops"
28
28
  end
29
- @errors << "Oops"
30
- @errors.should == [ "#{$0}: Eeek in file: foo at line: 1", "#{$0}: Oops" ]
29
+ @errors.should == [ "#{$0}: Oops in file: foo at line: 1" ]
30
+ end
31
+
32
+ def test_file_error
33
+ write_fake_file("foo", "bar\n")
34
+ @errors.in_file("foo") do
35
+ @errors << "Oops"
36
+ "result"
37
+ end.should == "result"
38
+ @errors.should == [ "#{$0}: Oops in file: foo" ]
31
39
  end
32
40
 
41
+ def test_file_lines_error
42
+ write_fake_file("foo", "bar\nbaz\n")
43
+ @errors.in_file_lines("foo") do |line|
44
+ @errors << "Oops" if line == "baz\n"
45
+ end
46
+ @errors.should == [ "#{$0}: Oops in file: foo at line: 2" ]
47
+ end
48
+
49
+
33
50
  end
@@ -10,6 +10,12 @@ class ErrorApplication < Olag::Application
10
10
  super { @errors << "Oops!" }
11
11
  end
12
12
 
13
+ # Test minimal number of arguments.
14
+ def parse_arguments
15
+ expect_at_least(2, "fake arguments")
16
+ expect_at_most(3, "fake arguments")
17
+ end
18
+
13
19
  end
14
20
 
15
21
  # Test running a Olag Application.
@@ -21,9 +27,19 @@ class TestRunApplication < Test::Unit::TestCase
21
27
  Olag::Application.with_argv([]) { Olag::Application.new(true).run }.should == 0
22
28
  end
23
29
 
30
+ def test_inexact_arguments
31
+ Olag::Application.with_argv(%w(-e stderr foo)) { Olag::Application.new(true).run }.should == 1
32
+ File.read("stderr").should.include?("Expects no arguments")
33
+ end
34
+
35
+ def test_missing_arguments
36
+ Olag::Application.with_argv(%w(-e stderr foo)) { ErrorApplication.new(true).run }.should == 1
37
+ File.read("stderr").should.include?("Expects at least 2 fake arguments")
38
+ end
39
+
24
40
  def test_extra_arguments
25
- Olag::Application.with_argv(%w(-e stderr dummy)) { Olag::Application.new(true).run }.should == 1
26
- File.read("stderr").should.include?("Expects no command line file arguments")
41
+ Olag::Application.with_argv(%w(-e stderr foo bar baz bad)) { ErrorApplication.new(true).run }.should == 1
42
+ File.read("stderr").should.include?("Expects at most 3 fake arguments")
27
43
  end
28
44
 
29
45
  def test_print_version
@@ -37,7 +53,7 @@ class TestRunApplication < Test::Unit::TestCase
37
53
  end
38
54
 
39
55
  def test_print_errors
40
- Olag::Application.with_argv(%w(-e stderr)) { ErrorApplication.new(true).run }.should == 1
56
+ Olag::Application.with_argv(%w(-e stderr foo bar)) { ErrorApplication.new(true).run }.should == 1
41
57
  File.read("stderr").should.include?("Oops!")
42
58
  end
43
59
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: olag
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 10
10
- version: 0.1.10
9
+ - 12
10
+ version: 0.1.12
11
11
  platform: ruby
12
12
  authors:
13
13
  - Oren Ben-Kiki
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-24 00:00:00 Z
18
+ date: 2011-08-25 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: Saikuro
@@ -202,7 +202,7 @@ licenses: []
202
202
  post_install_message:
203
203
  rdoc_options:
204
204
  - --title
205
- - Olag 0.1.10
205
+ - Olag 0.1.12
206
206
  - --main
207
207
  - README.rdoc
208
208
  - --line-numbers