hirb 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,11 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
- require 'context' #gem install jeremymcanally-context -s http://gems.github.com
4
- require 'matchy' #gem install jeremymcanally-matchy -s http://gems.github.com
1
+ require 'bacon'
2
+ require File.dirname(__FILE__)+'/bacon_extensions'
5
3
  require 'mocha'
6
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+ require 'mocha-on-bacon'
7
5
  require 'hirb'
6
+ include Hirb
8
7
 
9
- class Test::Unit::TestCase
8
+ module TestHelpers
10
9
  # set these to avoid invoking stty multiple times which doubles test suite running time
11
10
  ENV["LINES"] = ENV["COLUMNS"] = "20"
12
11
  def reset_terminal_size
@@ -36,10 +35,15 @@ class Test::Unit::TestCase
36
35
  end
37
36
 
38
37
  def reset_config
39
- Hirb::View.instance_eval "@config = nil"
38
+ View.instance_eval "@config = nil"
40
39
  end
41
40
  end
42
41
 
42
+ class Bacon::Context
43
+ include TestHelpers
44
+ include BaconExtensions
45
+ end
46
+
43
47
  class String
44
48
  def unindent(num=nil)
45
49
  regex = num ? /^\s{#{num}}/ : /^\s*/
@@ -1,12 +1,12 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
- class Hirb::Helpers::TreeTest < Test::Unit::TestCase
3
+ describe "Tree helpers:" do
4
4
  def tree(*args)
5
- Hirb::Helpers::Tree.render(*args)
5
+ Helpers::Tree.render(*args)
6
6
  end
7
7
 
8
- context "basic tree" do
9
- test "with hash nodes renders" do
8
+ describe "basic tree" do
9
+ it "with hash nodes renders" do
10
10
  expected_tree = <<-TREE.unindent(6)
11
11
  0.0
12
12
  1.1
@@ -18,7 +18,7 @@ class Hirb::Helpers::TreeTest < Test::Unit::TestCase
18
18
  {:level=>1, :value=>'4.1'}]).should == expected_tree
19
19
  end
20
20
 
21
- test "with array nodes renders" do
21
+ it "with array nodes renders" do
22
22
  expected_tree = <<-TREE.unindent(6)
23
23
  0.0
24
24
  1.1
@@ -29,7 +29,7 @@ class Hirb::Helpers::TreeTest < Test::Unit::TestCase
29
29
  tree([[0, "0.0"], [1, "1.1"], [2, "2.2"], [2, "3.2"], [1, "4.1"]]).should == expected_tree
30
30
  end
31
31
 
32
- test "with non-string values renders" do
32
+ it "with non-string values renders" do
33
33
  expected_tree = <<-TREE.unindent(6)
34
34
  0.0
35
35
  1.1
@@ -40,7 +40,7 @@ class Hirb::Helpers::TreeTest < Test::Unit::TestCase
40
40
  tree([[0,0.0],[1,1.1],[2,2.2],[2,3.2],[1,4.1]]).should == expected_tree
41
41
  end
42
42
 
43
- test "with indent option renders" do
43
+ it "with indent option renders" do
44
44
  expected_tree = <<-TREE.unindent(6)
45
45
  0.0
46
46
  1.1
@@ -51,7 +51,7 @@ class Hirb::Helpers::TreeTest < Test::Unit::TestCase
51
51
  tree([[0, "0.0"], [1, "1.1"], [2, "2.2"], [2, "3.2"], [1, "4.1"]], :indent=>2).should == expected_tree
52
52
  end
53
53
 
54
- test "with limit option renders" do
54
+ it "with limit option renders" do
55
55
  expected_tree = <<-TREE.unindent(6)
56
56
  0.0
57
57
  1.1
@@ -60,7 +60,7 @@ class Hirb::Helpers::TreeTest < Test::Unit::TestCase
60
60
  tree([[0, "0.0"], [1, "1.1"], [2, "2.2"], [2, "3.2"], [1, "4.1"]], :limit=>1).should == expected_tree
61
61
  end
62
62
 
63
- test "with description option renders" do
63
+ it "with description option renders" do
64
64
  expected_tree = <<-TREE.unindent(6)
65
65
  0.0
66
66
  1.1
@@ -73,7 +73,7 @@ class Hirb::Helpers::TreeTest < Test::Unit::TestCase
73
73
  tree([[0, "0.0"], [1, "1.1"], [2, "2.2"], [2, "3.2"], [1, "4.1"]], :description=>true).should == expected_tree
74
74
  end
75
75
 
76
- test "with type directory renders" do
76
+ it "with type directory renders" do
77
77
  expected_tree = <<-TREE.unindent
78
78
  0.0
79
79
  |-- 1.1
@@ -84,7 +84,7 @@ class Hirb::Helpers::TreeTest < Test::Unit::TestCase
84
84
  tree([[0, "0.0"], [1, "1.1"], [2, "2.2"], [2, "3.2"], [1, "4.1"]], :type=>:directory).should == expected_tree
85
85
  end
86
86
 
87
- test "with type directory and multiple children per level renders" do
87
+ it "with type directory and multiple children per level renders" do
88
88
  expected_tree = <<-TREE.unindent
89
89
  0.0
90
90
  |-- 1.1
@@ -97,19 +97,19 @@ class Hirb::Helpers::TreeTest < Test::Unit::TestCase
97
97
  tree([[0,'0.0'], [1,'1.1'], [2,'2.2'],[3,'3.3'],[2,'4.2'],[3,'5.3'],[1,'6.1']], :type=>:directory).should == expected_tree
98
98
  end
99
99
 
100
- test "with type number renders" do
100
+ it "with type number renders" do
101
101
  expected_tree = <<-TREE.unindent(6)
102
102
  1. 0
103
103
  1. 1
104
104
  1. 2
105
105
  2. 3
106
- 2. 4
106
+ 2. 4
107
107
  TREE
108
- tree([[0,'0'],[1,'1'],[2,'2'],[2,'3'],[1,'4']], :type=>:number)
108
+ tree([[0,'0'],[1,'1'],[2,'2'],[2,'3'],[1,'4']], :type=>:number).should == expected_tree
109
109
  end
110
110
 
111
- test "with multi-line nodes option renders" do
112
- expected_tree = <<-TREE.unindent
111
+ it "with multi-line nodes option renders" do
112
+ expected_tree = <<-TREE.unindent(6)
113
113
  parent
114
114
  +-------+
115
115
  | value |
@@ -122,7 +122,7 @@ class Hirb::Helpers::TreeTest < Test::Unit::TestCase
122
122
  stuff
123
123
  TREE
124
124
  node1 = "+-------+\n| value |\n+-------+\n| 1 |\n| 2 |\n| 3 |\n+-------+"
125
- tree([ [0, 'parent'],[1, node1],[2, "indented\nstuff"]], :multi_line_nodes=>true)
125
+ tree([ [0, 'parent'],[1, node1],[2, "indented\nstuff"]], :multi_line_nodes=>true).should == expected_tree
126
126
  end
127
127
  end
128
128
 
@@ -132,8 +132,8 @@ class Hirb::Helpers::TreeTest < Test::Unit::TestCase
132
132
  mock(value_method=>value, :children=>children.map {|e| mock_node(e, value_method)})
133
133
  end
134
134
 
135
- context "parent_child_tree" do
136
- test "with name value renders" do
135
+ describe "parent_child_tree" do
136
+ it "with name value renders" do
137
137
  expected_tree = <<-TREE.unindent
138
138
  0.0
139
139
  |-- 1.1
@@ -141,23 +141,11 @@ class Hirb::Helpers::TreeTest < Test::Unit::TestCase
141
141
  | `-- 3.2
142
142
  `-- 4.1
143
143
  TREE
144
- root = mock_node(['0.0', ['1.1', ['2.1', '3.2'], '4.1']], :name)
145
- Hirb::Helpers::ParentChildTree.render(root, :type=>:directory).should == expected_tree
144
+ root = mock_node(['0.0', ['1.1', ['2.1', ['3.2']], '4.1']], :name)
145
+ Helpers::ParentChildTree.render(root, :type=>:directory).should == expected_tree
146
146
  end
147
147
 
148
- test "with object_id value renders" do
149
- expected_tree = <<-TREE.unindent
150
- 0.0
151
- |-- 1.1
152
- |-- 2.1
153
- | `-- 3.2
154
- `-- 4.1
155
- TREE
156
- root = mock_node(['0.0', ['1.1', ['2.1', '3.2'], '4.1']], :object_id)
157
- Hirb::Helpers::ParentChildTree.render(root, :type=>:directory).should == expected_tree
158
- end
159
-
160
- test "with value_method option renders" do
148
+ it "with value_method option renders" do
161
149
  expected_tree = <<-TREE.unindent
162
150
  0.0
163
151
  |-- 1.1
@@ -165,11 +153,11 @@ class Hirb::Helpers::TreeTest < Test::Unit::TestCase
165
153
  | `-- 3.2
166
154
  `-- 4.1
167
155
  TREE
168
- root = mock_node(['0.0', ['1.1', ['2.1', '3.2'], '4.1']], :blah)
169
- Hirb::Helpers::ParentChildTree.render(root, :type=>:directory, :value_method=>:blah).should == expected_tree
156
+ root = mock_node(['0.0', ['1.1', ['2.1', ['3.2']], '4.1']], :blah)
157
+ Helpers::ParentChildTree.render(root, :type=>:directory, :value_method=>:blah).should == expected_tree
170
158
  end
171
159
 
172
- test "with children_method proc option renders" do
160
+ it "with children_method proc option renders" do
173
161
  expected_tree = <<-TREE.unindent
174
162
  1
175
163
  |-- 2
@@ -177,20 +165,20 @@ class Hirb::Helpers::TreeTest < Test::Unit::TestCase
177
165
  |-- 4
178
166
  `-- 5
179
167
  TREE
180
- Hirb::Helpers::ParentChildTree.render(1, :type=>:directory,
181
- :children_method=>lambda {|e| e == 1 ? (2..5).to_a : []}, :value_method=>:to_s)
168
+ Helpers::ParentChildTree.render(1, :type=>:directory,
169
+ :children_method=>lambda {|e| e == 1 ? (2..5).to_a : []}, :value_method=>:to_s).should == expected_tree
182
170
  end
183
171
  end
184
172
 
185
- test "tree with parentless nodes renders ParentlessNodeError" do
186
- assert_raises(Hirb::Helpers::Tree::ParentlessNodeError) { tree([[0, "0.0"], [2, '1.2']], :validate=>true) }
173
+ it "tree with parentless nodes renders ParentlessNodeError" do
174
+ lambda { tree([[0, "0.0"], [2, '1.2']], :validate=>true) }.should.raise(Helpers::Tree::ParentlessNodeError)
187
175
  end
188
176
 
189
- test "tree with hash nodes missing level raises MissingLevelError" do
190
- assert_raises(Hirb::Helpers::Tree::Node::MissingLevelError) { tree([{:value=>'ok'}]) }
177
+ it "tree with hash nodes missing level raises MissingLevelError" do
178
+ lambda { tree([{:value=>'ok'}]) }.should.raise(Helpers::Tree::Node::MissingLevelError)
191
179
  end
192
180
 
193
- test "tree with hash nodes missing level raises MissingValueError" do
194
- assert_raises(Hirb::Helpers::Tree::Node::MissingValueError) { tree([{:level=>0}]) }
181
+ it "tree with hash nodes missing level raises MissingValueError" do
182
+ lambda { tree([{:level=>0}]) }.should.raise(Helpers::Tree::Node::MissingValueError)
195
183
  end
196
- end
184
+ end
@@ -1,61 +1,59 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
- module Hirb
4
- class UtilTest < Test::Unit::TestCase
5
- test "camelize converts underscore lowercase to camelcase" do
6
- Util.camelize('hirb/util').should == "Hirb::Util"
7
- Util.camelize('hirb_hash').should == "HirbHash"
8
- end
9
-
10
- test "any_const_get returns nested class" do
11
- Util.any_const_get("Test::Unit").should == ::Test::Unit
12
- end
13
-
14
- test "any_const_get returns nil for invalid class" do
15
- Util.any_const_get("Basdfr").should == nil
16
- end
17
-
18
- test "any_const_get returns class when given class" do
19
- Util.any_const_get(String).should == String
20
- end
21
-
22
- test "recursive_hash_merge merges" do
23
- expected_hash = {:output=>{:fields=>["f1", "f2"], :method=>"blah"}, :key1=>"hash1", :key2=>"hash2"}
24
- Util.recursive_hash_merge({:output=>{:fields=>%w{f1 f2}}, :key1=>'hash1'},
25
- {:output=>{:method=>'blah'}, :key2=>'hash2'}).should == expected_hash
26
- end
27
-
28
- test "choose_from_array specifies range with -" do
29
- Util.choose_from_array([1,2,3,4], '1-2,4').should == [1,2,4]
30
- end
31
-
32
- test "choose_from_array specifies range with .." do
33
- Util.choose_from_array([1,2,3,4], '1 .. 2,4').should == [1,2,4]
34
- end
35
-
36
- test "choose_from_array chooses all with *" do
37
- Util.choose_from_array([1,2,3,4], '*').should == [1,2,3,4]
38
- end
39
-
40
- test "choose_from_array ignores non-numerical input" do
41
- Util.choose_from_array([1,2,3,4], 'a,2').should == [2]
42
- end
43
-
44
- test "choose_from_array ignores 0" do
45
- Util.choose_from_array([1,2,3,4], '0,2').should == [2]
46
- end
47
-
48
- test "choose_from_array returns empty when empty input" do
49
- Util.choose_from_array([1,2,3,4], "\n").should == []
50
- end
51
-
52
- test "choose_from_array returns empty with an invalid range" do
53
- Util.choose_from_array([1,2,3,4], "5").should == []
54
- end
55
-
56
- test "capture_stdout" do
57
- string = "sweetness man"
58
- Util.capture_stdout { puts string }.should == string + "\n"
59
- end
60
- end
61
- end
3
+ describe "Util" do
4
+ it "camelize converts underscore lowercase to camelcase" do
5
+ Util.camelize('hirb/util').should == "Hirb::Util"
6
+ Util.camelize('hirb_hash').should == "HirbHash"
7
+ end
8
+
9
+ it "any_const_get returns nested class" do
10
+ Util.any_const_get("YAML::BaseNode").should == ::YAML::BaseNode
11
+ end
12
+
13
+ it "any_const_get returns nil for invalid class" do
14
+ Util.any_const_get("Basdfr").should == nil
15
+ end
16
+
17
+ it "any_const_get returns class when given class" do
18
+ Util.any_const_get(String).should == String
19
+ end
20
+
21
+ it "recursive_hash_merge merges" do
22
+ expected_hash = {:output=>{:fields=>["f1", "f2"], :method=>"blah"}, :key1=>"hash1", :key2=>"hash2"}
23
+ Util.recursive_hash_merge({:output=>{:fields=>%w{f1 f2}}, :key1=>'hash1'},
24
+ {:output=>{:method=>'blah'}, :key2=>'hash2'}).should == expected_hash
25
+ end
26
+
27
+ it "choose_from_array specifies range with -" do
28
+ Util.choose_from_array([1,2,3,4], '1-2,4').should == [1,2,4]
29
+ end
30
+
31
+ it "choose_from_array specifies range with .." do
32
+ Util.choose_from_array([1,2,3,4], '1 .. 2,4').should == [1,2,4]
33
+ end
34
+
35
+ it "choose_from_array chooses all with *" do
36
+ Util.choose_from_array([1,2,3,4], '*').should == [1,2,3,4]
37
+ end
38
+
39
+ it "choose_from_array ignores non-numerical input" do
40
+ Util.choose_from_array([1,2,3,4], 'a,2').should == [2]
41
+ end
42
+
43
+ it "choose_from_array ignores 0" do
44
+ Util.choose_from_array([1,2,3,4], '0,2').should == [2]
45
+ end
46
+
47
+ it "choose_from_array returns empty when empty input" do
48
+ Util.choose_from_array([1,2,3,4], "\n").should == []
49
+ end
50
+
51
+ it "choose_from_array returns empty with an invalid range" do
52
+ Util.choose_from_array([1,2,3,4], "5").should == []
53
+ end
54
+
55
+ it "capture_stdout" do
56
+ string = "sweetness man"
57
+ Util.capture_stdout { puts string }.should == string + "\n"
58
+ end
59
+ end
@@ -1,168 +1,166 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
- module Hirb
4
- class ViewTest < Test::Unit::TestCase
5
- def formatter_config
6
- View.formatter.config
7
- end
8
-
9
- test "page_output pages when view is enabled" do
10
- Hirb.enable
11
- View.pager.stubs(:activated_by?).returns(true)
12
- View.pager.expects(:page)
13
- View.page_output('blah').should be(true)
14
- Hirb.disable
15
- end
3
+ describe "View" do
4
+ def formatter_config
5
+ View.formatter.config
6
+ end
7
+
8
+ it "page_output pages when view is enabled" do
9
+ Hirb.enable
10
+ View.pager.stubs(:activated_by?).returns(true)
11
+ View.pager.expects(:page)
12
+ View.page_output('blah').should == true
13
+ Hirb.disable
14
+ end
15
+
16
+ it "page_output doesn't page when view is disabled" do
17
+ Hirb.enable
18
+ Hirb.disable
19
+ View.pager.stubs(:activated_by?).returns(true)
20
+ View.pager.expects(:page).never
21
+ View.page_output('blah').should == false
22
+ end
16
23
 
17
- test "page_output doesn't page when view is disabled" do
24
+ it "view_output catches unexpected errors and prints them" do
25
+ reset_config
26
+ Hirb.enable
27
+ View.expects(:render_output).raises('blah')
28
+ capture_stderr { View.view_output([1,2,3]) }.should =~ /Hirb Error: blah/
29
+ Hirb.disable
30
+ end
31
+
32
+ describe "enable" do
33
+ before { reset_config }
34
+ after { Hirb.disable }
35
+ it "redefines irb output_value" do
36
+ View.expects(:render_output).once
18
37
  Hirb.enable
19
- Hirb.disable
20
- View.pager.stubs(:activated_by?).returns(true)
21
- View.pager.expects(:page).never
22
- View.page_output('blah').should be(false)
38
+ context_stub = stub(:last_value=>'')
39
+ ::IRB::Irb.new(context_stub).output_value
23
40
  end
24
41
 
25
- test "view_output catches unexpected errors and prints them" do
26
- reset_config
42
+ it "is enabled?" do
27
43
  Hirb.enable
28
- View.expects(:render_output).raises('blah')
29
- capture_stderr { View.view_output([1,2,3]) }.should =~ /Hirb Error: blah/
30
- Hirb.disable
44
+ View.enabled?.should == true
31
45
  end
32
46
 
33
- context "enable" do
34
- before(:each) { reset_config }
35
- after(:each) { Hirb.disable }
36
- test "redefines irb output_value" do
37
- View.expects(:render_output).once
38
- Hirb.enable
39
- context_stub = stub(:last_value=>'')
40
- ::IRB::Irb.new(context_stub).output_value
41
- end
42
-
43
- test "is enabled?" do
44
- Hirb.enable
45
- assert View.enabled?
46
- end
47
-
48
- def output_class_config(klass)
49
- { :output=>{klass=>{:class=>:auto_table}} }
50
- end
51
-
52
- test "sets formatter config" do
53
- class_hash = {"Something::Base"=>{:class=>"BlahBlah"}}
54
- Hirb.enable :output=>class_hash
55
- View.formatter_config['Something::Base'].should == class_hash['Something::Base']
56
- end
57
-
58
- test "when called multiple times merges configs" do
59
- Hirb.config = nil
60
- # default config + config_file
61
- Hirb.expects(:read_config_file).returns(output_class_config('Regexp'))
62
- Hirb.enable output_class_config('String')
63
-
64
- # add config file and explicit config
65
- [{:config_file=>'ok'}, output_class_config('Struct')].each do |config|
66
- Hirb.expects(:read_config_file).times(2).returns(
67
- output_class_config('ActiveRecord::Base'), output_class_config('Array'))
68
- Hirb.enable config
69
- end
70
-
71
- Hirb.config_files.include?('ok').should == true
72
- output_keys = %w{ActiveRecord::Base Array Regexp String Struct}
73
- Hirb::View.config[:output].keys.sort.should == output_keys
74
- end
75
-
76
- test "when called multiple times without config doesn't affect config" do
77
- Hirb.enable
78
- old_config = Hirb::View.config
79
- Hirb.expects(:read_config_file).never
80
- Hirb::View.expects(:load_config).never
81
- Hirb.enable
82
- Hirb::View.config.should == old_config
83
- end
84
-
85
- test "works without irb" do
86
- Object.stubs(:const_defined?).with(:IRB).returns(false)
87
- Hirb.enable
88
- assert formatter_config.size > 0
89
- end
90
-
91
- test "with config_file option adds to config_file" do
92
- Hirb.enable :config_file=> 'test_file'
93
- Hirb.config_files.include?('test_file').should == true
94
- end
95
-
96
- test "with output_method option realiases output_method" do
97
- eval %[module ::Mini; extend self; def output(str); puts(str.inspect); end; end]
98
- Hirb.enable :output_method=>"Mini.output", :output=>{"Symbol"=>{:output_method=>lambda {|e| e.to_s }}}
99
- capture_stdout { ::Mini.output(:yoyo) }.should == "yoyo\n"
100
- capture_stdout { ::Mini.output('blah') }.should == "\"blah\"\n"
101
- end
47
+ def output_class_config(klass)
48
+ { :output=>{klass=>{:class=>:auto_table}} }
102
49
  end
103
50
 
104
- context "resize" do
105
- def pager; View.pager; end
106
- before(:each) { View.pager = nil; reset_config; Hirb.enable }
107
- after(:each) { Hirb.disable}
108
- test "changes width and height with stty" do
109
- Util.expects(:command_exists?).with('stty').returns(true)
110
- ENV['COLUMNS'] = ENV['LINES'] = nil # bypasses env usage
111
- View.resize
112
- pager.width.should_not == 10
113
- pager.height.should_not == 10
114
- reset_terminal_size
115
- end
51
+ it "sets formatter config" do
52
+ class_hash = {"Something::Base"=>{:class=>"BlahBlah"}}
53
+ Hirb.enable :output=>class_hash
54
+ View.formatter_config['Something::Base'].should == class_hash['Something::Base']
55
+ end
116
56
 
117
- test "changes width and height with ENV" do
118
- ENV['COLUMNS'] = ENV['LINES'] = '10' # simulates resizing
119
- View.resize
120
- pager.width.should == 10
121
- pager.height.should == 10
57
+ it "when called multiple times merges configs" do
58
+ Hirb.config = nil
59
+ # default config + config_file
60
+ Hirb.expects(:read_config_file).returns(output_class_config('Regexp'))
61
+ Hirb.enable output_class_config('String')
62
+
63
+ # add config file and explicit config
64
+ [{:config_file=>'ok'}, output_class_config('Struct')].each do |config|
65
+ Hirb.expects(:read_config_file).times(2).returns(
66
+ output_class_config('ActiveRecord::Base'), output_class_config('Array'))
67
+ Hirb.enable config
122
68
  end
123
69
 
124
- test "with no environment or stty still has valid width and height" do
125
- View.config[:width] = View.config[:height] = nil
126
- Util.expects(:command_exists?).with('stty').returns(false)
127
- ENV['COLUMNS'] = ENV['LINES'] = nil
128
- View.resize
129
- pager.width.is_a?(Integer).should be(true)
130
- pager.height.is_a?(Integer).should be(true)
131
- reset_terminal_size
132
- end
70
+ Hirb.config_files.include?('ok').should == true
71
+ output_keys = %w{ActiveRecord::Base Array Regexp String Struct}
72
+ View.config[:output].keys.sort.should == output_keys
133
73
  end
134
74
 
135
- test "disable points output_value back to original output_value" do
136
- View.expects(:render_output).never
75
+ it "when called multiple times without config doesn't affect config" do
137
76
  Hirb.enable
138
- Hirb.disable
139
- context_stub = stub(:last_value=>'')
140
- ::IRB::Irb.new(context_stub).output_value
77
+ old_config = View.config
78
+ Hirb.expects(:read_config_file).never
79
+ View.expects(:load_config).never
80
+ Hirb.enable
81
+ View.config.should == old_config
141
82
  end
142
83
 
143
- test "disable works without irb defined" do
84
+ it "works without irb" do
144
85
  Object.stubs(:const_defined?).with(:IRB).returns(false)
145
86
  Hirb.enable
146
- Hirb.disable
147
- View.enabled?.should be(false)
87
+ formatter_config.size.should.be > 0
88
+ end
89
+
90
+ it "with config_file option adds to config_file" do
91
+ Hirb.enable :config_file=> 'test_file'
92
+ Hirb.config_files.include?('test_file').should == true
93
+ end
94
+
95
+ it "with output_method option realiases output_method" do
96
+ eval %[module ::Mini; extend self; def output(str); puts(str.inspect); end; end]
97
+ Hirb.enable :output_method=>"Mini.output", :output=>{"Symbol"=>{:output_method=>lambda {|e| e.to_s }}}
98
+ capture_stdout { ::Mini.output(:yoyo) }.should == "yoyo\n"
99
+ capture_stdout { ::Mini.output('blah') }.should == "\"blah\"\n"
148
100
  end
101
+ end
149
102
 
150
- test "capture_and_render" do
151
- string = 'no waaaay'
152
- View.render_method.expects(:call).with(string)
153
- View.capture_and_render { print string }
103
+ describe "resize" do
104
+ def pager; View.pager; end
105
+ before { View.pager = nil; reset_config; Hirb.enable }
106
+ after { Hirb.disable}
107
+ it "changes width and height with stty" do
108
+ Util.expects(:command_exists?).with('stty').returns(true)
109
+ ENV['COLUMNS'] = ENV['LINES'] = nil # bypasses env usage
110
+ View.resize
111
+ pager.width.should.not == 10
112
+ pager.height.should.not == 10
113
+ reset_terminal_size
154
114
  end
155
-
156
- test "state is toggled by toggle_pager" do
157
- previous_state = View.config[:pager]
158
- View.toggle_pager
159
- View.config[:pager].should == !previous_state
115
+
116
+ it "changes width and height with ENV" do
117
+ ENV['COLUMNS'] = ENV['LINES'] = '10' # simulates resizing
118
+ View.resize
119
+ pager.width.should == 10
120
+ pager.height.should == 10
160
121
  end
161
-
162
- test "state is toggled by toggle_formatter" do
163
- previous_state = View.config[:formatter]
164
- View.toggle_formatter
165
- View.config[:formatter].should == !previous_state
122
+
123
+ it "with no environment or stty still has valid width and height" do
124
+ View.config[:width] = View.config[:height] = nil
125
+ Util.expects(:command_exists?).with('stty').returns(false)
126
+ ENV['COLUMNS'] = ENV['LINES'] = nil
127
+ View.resize
128
+ pager.width.is_a?(Integer).should == true
129
+ pager.height.is_a?(Integer).should == true
130
+ reset_terminal_size
166
131
  end
167
132
  end
133
+
134
+ it "disable points output_value back to original output_value" do
135
+ View.expects(:render_output).never
136
+ Hirb.enable
137
+ Hirb.disable
138
+ context_stub = stub(:last_value=>'')
139
+ ::IRB::Irb.new(context_stub).output_value
140
+ end
141
+
142
+ it "disable works without irb defined" do
143
+ Object.stubs(:const_defined?).with(:IRB).returns(false)
144
+ Hirb.enable
145
+ Hirb.disable
146
+ View.enabled?.should == false
147
+ end
148
+
149
+ it "capture_and_render" do
150
+ string = 'no waaaay'
151
+ View.render_method.expects(:call).with(string)
152
+ View.capture_and_render { print string }
153
+ end
154
+
155
+ it "state is toggled by toggle_pager" do
156
+ previous_state = View.config[:pager]
157
+ View.toggle_pager
158
+ View.config[:pager].should == !previous_state
159
+ end
160
+
161
+ it "state is toggled by toggle_formatter" do
162
+ previous_state = View.config[:formatter]
163
+ View.toggle_formatter
164
+ View.config[:formatter].should == !previous_state
165
+ end
168
166
  end