hirb 0.1.2 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_helper.rb CHANGED
@@ -7,6 +7,37 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
7
  require 'hirb'
8
8
 
9
9
  class Test::Unit::TestCase
10
+ # set these to avoid invoking stty multiple times which doubles test suite running time
11
+ ENV["LINES"] = ENV["COLUMNS"] = "20"
12
+ def reset_terminal_size
13
+ ENV["LINES"] = ENV["COLUMNS"] = "20"
14
+ end
15
+
16
+ def capture_stdout(&block)
17
+ original_stdout = $stdout
18
+ $stdout = fake = StringIO.new
19
+ begin
20
+ yield
21
+ ensure
22
+ $stdout = original_stdout
23
+ end
24
+ fake.string
25
+ end
26
+
27
+ def capture_stderr(&block)
28
+ original_stderr = $stderr
29
+ $stderr = fake = StringIO.new
30
+ begin
31
+ yield
32
+ ensure
33
+ $stderr = original_stderr
34
+ end
35
+ fake.string
36
+ end
37
+
38
+ def reset_config
39
+ Hirb::View.instance_eval "@config = nil"
40
+ end
10
41
  end
11
42
 
12
43
  class String
@@ -14,4 +45,14 @@ class String
14
45
  regex = num ? /^\s{#{num}}/ : /^\s*/
15
46
  gsub(regex, '').chomp
16
47
  end
48
+ end
49
+
50
+ # mocks IRB for View + Pager
51
+ module ::IRB
52
+ class Irb
53
+ def initialize(context)
54
+ @context = context
55
+ end
56
+ def output_value; end
57
+ end
17
58
  end
data/test/util_test.rb CHANGED
@@ -1,21 +1,56 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
- class Hirb::UtilTest < Test::Unit::TestCase
4
- test "any_const_get returns nested class" do
5
- Hirb::Util.any_const_get("Test::Unit").should == ::Test::Unit
6
- end
7
-
8
- test "any_const_get returns nil for invalid class" do
9
- Hirb::Util.any_const_get("Basdfr").should == nil
10
- end
11
-
12
- test "any_const_get returns class when given class" do
13
- Hirb::Util.any_const_get(String).should == String
14
- end
15
-
16
- test "recursive_hash_merge merges" do
17
- expected_hash = {:output=>{:fields=>["f1", "f2"], :method=>"blah"}, :key1=>"hash1", :key2=>"hash2"}
18
- Hirb::Util.recursive_hash_merge({:output=>{:fields=>%w{f1 f2}}, :key1=>'hash1'},
19
- {:output=>{:method=>'blah'}, :key2=>'hash2'}).should == expected_hash
3
+ module Hirb
4
+ class UtilTest < Test::Unit::TestCase
5
+ test "any_const_get returns nested class" do
6
+ Util.any_const_get("Test::Unit").should == ::Test::Unit
7
+ end
8
+
9
+ test "any_const_get returns nil for invalid class" do
10
+ Util.any_const_get("Basdfr").should == nil
11
+ end
12
+
13
+ test "any_const_get returns class when given class" do
14
+ Util.any_const_get(String).should == String
15
+ end
16
+
17
+ test "recursive_hash_merge merges" do
18
+ expected_hash = {:output=>{:fields=>["f1", "f2"], :method=>"blah"}, :key1=>"hash1", :key2=>"hash2"}
19
+ Util.recursive_hash_merge({:output=>{:fields=>%w{f1 f2}}, :key1=>'hash1'},
20
+ {:output=>{:method=>'blah'}, :key2=>'hash2'}).should == expected_hash
21
+ end
22
+
23
+ test "choose_from_array specifies range with -" do
24
+ Util.choose_from_array([1,2,3,4], '1-2,4').should == [1,2,4]
25
+ end
26
+
27
+ test "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
+ test "choose_from_array chooses all with *" do
32
+ Util.choose_from_array([1,2,3,4], '*').should == [1,2,3,4]
33
+ end
34
+
35
+ test "choose_from_array ignores non-numerical input" do
36
+ Util.choose_from_array([1,2,3,4], 'a,2').should == [2]
37
+ end
38
+
39
+ test "choose_from_array ignores 0" do
40
+ Util.choose_from_array([1,2,3,4], '0,2').should == [2]
41
+ end
42
+
43
+ test "choose_from_array returns empty when empty input" do
44
+ Util.choose_from_array([1,2,3,4], "\n").should == []
45
+ end
46
+
47
+ test "choose_from_array returns empty with an invalid range" do
48
+ Util.choose_from_array([1,2,3,4], "5").should == []
49
+ end
50
+
51
+ test "capture_stdout" do
52
+ string = "sweetness man"
53
+ Util.capture_stdout { puts string }.should == string + "\n"
54
+ end
20
55
  end
21
- end
56
+ end
data/test/view_test.rb CHANGED
@@ -1,169 +1,116 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
- # mocks IRB for testing
4
- module ::IRB
5
- class Irb
6
- def initialize(context)
7
- @context = context
8
- end
9
- def output_value; end
10
- end
11
- end
12
-
13
- class Hirb::ViewTest < Test::Unit::TestCase
14
- def set_config(value)
15
- Hirb::View.output_config = value
16
- Hirb::View.reset_cached_output_config
17
- end
18
-
19
- def output_config
20
- Hirb::View.config[:output]
21
- end
22
-
23
- test "output_class_options merges ancestor options" do
24
- set_config "String"=>{:args=>[1,2]}, "Object"=>{:method=>:object_output, :ancestor=>true}, "Kernel"=>{:method=>:default_output}
25
- expected_result = {:method=>:object_output, :args=>[1, 2], :ancestor=>true}
26
- Hirb::View.output_class_options(String).should == expected_result
27
- end
28
-
29
- test "output_class_options doesn't ancestor options" do
30
- set_config "String"=>{:args=>[1,2]}, "Object"=>{:method=>:object_output}, "Kernel"=>{:method=>:default_output}
31
- expected_result = {:args=>[1, 2]}
32
- Hirb::View.output_class_options(String).should == expected_result
33
- end
34
-
35
- test "output_class_options returns hash when nothing found" do
36
- Hirb::View.load_config
37
- Hirb::View.output_class_options(String).should == {}
38
- end
39
-
40
- context "enable" do
41
- before(:each) {Hirb::View.config = {}}
42
- after(:each) { Hirb::View.disable }
43
- test "redefines irb output_value" do
44
- Hirb::View.expects(:render_output).once
45
- Hirb::View.enable
46
- context_stub = stub(:last_value=>'')
47
- ::IRB::Irb.new(context_stub).output_value
48
- end
49
-
50
- test "sets default config" do
51
- eval "module ::Hirb::Views::Something_Base; def self.render; end; end"
52
- Hirb::View.enable
53
- output_config["Something::Base"].should == {:class=>"Hirb::Views::Something_Base"}
54
- end
55
-
56
- test "sets default config with default_options" do
57
- eval "module ::Hirb::Views::Blah; def self.render; end; def self.default_options; {:ancestor=>true}; end; end"
58
- Hirb::View.enable
59
- output_config["Blah"].should == {:class=>"Hirb::Views::Blah", :ancestor=>true}
60
- end
61
-
62
- test "with block sets config" do
63
- class_hash = {"Something::Base"=>{:class=>"BlahBlah"}}
64
- Hirb::View.enable {|c| c.output = class_hash }
65
- output_config['Something::Base'].should == class_hash['Something::Base']
66
- end
67
- end
68
-
69
- test "reload_config resets config to detect new Hirb::Views" do
70
- Hirb::View.load_config
71
- output_config.keys.include?('Zzz').should be(false)
72
- eval "module ::Hirb::Views::Zzz; def self.render; end; end"
73
- Hirb::View.reload_config
74
- output_config.keys.include?('Zzz').should be(true)
75
- end
76
-
77
- test "reload_config picks up local changes" do
78
- Hirb::View.load_config
79
- output_config.keys.include?('Dooda').should be(false)
80
- Hirb::View.output_config.merge!('Dooda'=>{:class=>"DoodaView"})
81
- Hirb::View.reload_config
82
- output_config['Dooda'].should == {:class=>"DoodaView"}
83
- end
84
-
85
- test "disable points output_value back to original output_value" do
86
- Hirb::View.expects(:render_output).never
87
- Hirb::View.enable
88
- Hirb::View.disable
89
- context_stub = stub(:last_value=>'')
90
- ::IRB::Irb.new(context_stub).output_value
91
- end
92
-
93
- context "render_output" do
94
- before(:all) {
95
- eval %[module ::Commify
96
- def self.render(strings)
97
- strings = [strings] unless strings.is_a?(Array)
98
- strings.map {|e| e.split('').join(',')}.join("\n")
99
- end
100
- end]
101
- Hirb::View.enable
102
- }
103
- after(:all) { Hirb::View.disable }
104
-
105
- test "formats with config method option" do
106
- eval "module ::Kernel; def commify(string); string.split('').join(','); end; end"
107
- set_config "String"=>{:method=>:commify}
108
- Hirb::View.render_method.expects(:call).with('d,u,d,e')
109
- Hirb::View.render_output('dude')
110
- end
111
-
112
- test "formats with config class option" do
113
- set_config "String"=>{:class=>"Commify"}
114
- Hirb::View.render_method.expects(:call).with('d,u,d,e')
115
- Hirb::View.render_output('dude')
3
+ module Hirb
4
+ class ViewTest < Test::Unit::TestCase
5
+ def formatter_config
6
+ View.formatter.config
116
7
  end
117
8
 
118
- test "formats with output array" do
119
- set_config "String"=>{:class=>"Commify"}
120
- Hirb::View.render_method.expects(:call).with('d,u,d,e')
121
- Hirb::View.render_output(['dude'])
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
122
15
  end
123
16
 
124
- test "formats with config options option" do
125
- eval "module ::Blahify; def self.render(*args); end; end"
126
- set_config "String"=>{:class=>"Blahify", :options=>{:fields=>%w{a b}}}
127
- Blahify.expects(:render).with('dude', :fields=>%w{a b})
128
- Hirb::View.render_output('dude')
17
+ test "page_output doesn't page when view is disabled" do
18
+ 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)
129
23
  end
130
-
131
- test "doesn't format and returns false when no format method found" do
132
- Hirb::View.load_config
133
- Hirb::View.render_method.expects(:call).never
134
- Hirb::View.render_output(Date.today).should == false
24
+
25
+ context "enable" do
26
+ before(:each) { reset_config }
27
+ after(:each) { Hirb.disable }
28
+ test "redefines irb output_value" do
29
+ View.expects(:render_output).once
30
+ Hirb.enable
31
+ context_stub = stub(:last_value=>'')
32
+ ::IRB::Irb.new(context_stub).output_value
33
+ end
34
+ test "is enabled?" do
35
+ Hirb.enable
36
+ assert View.enabled?
37
+ end
38
+
39
+ test "works without irb" do
40
+ Object.stubs(:const_defined?).with(:IRB).returns(false)
41
+ Hirb.enable
42
+ assert formatter_config.size > 0
43
+ end
44
+
45
+ test "with config_file option sets config_file" do
46
+ Hirb.config_file.should_not == 'test_file'
47
+ Hirb.enable :config_file=> 'test_file'
48
+ Hirb.config_file.should == 'test_file'
49
+ end
135
50
  end
136
-
137
- test "formats with explicit class option" do
138
- set_config 'String'=>{:class=>"Blahify"}
139
- Hirb::View.render_method.expects(:call).with('d,u,d,e')
140
- Hirb::View.render_output('dude', :class=>"Commify")
51
+
52
+ context "resize" do
53
+ def pager; View.pager; end
54
+ before(:each) { View.pager = nil; reset_config; Hirb.enable }
55
+ after(:each) { Hirb.disable}
56
+ test "changes width and height with stty" do
57
+ Util.expects(:command_exists?).with('stty').returns(true)
58
+ ENV['COLUMNS'] = ENV['LINES'] = nil # bypasses env usage
59
+ View.resize
60
+ pager.width.should_not == 10
61
+ pager.height.should_not == 10
62
+ reset_terminal_size
63
+ end
64
+
65
+ test "changes width and height with ENV" do
66
+ ENV['COLUMNS'] = ENV['LINES'] = '10' # simulates resizing
67
+ View.resize
68
+ pager.width.should == 10
69
+ pager.height.should == 10
70
+ end
71
+
72
+ test "with no environment or stty still has valid width and height" do
73
+ View.config[:width] = View.config[:height] = nil
74
+ Util.expects(:command_exists?).with('stty').returns(false)
75
+ ENV['COLUMNS'] = ENV['LINES'] = nil
76
+ View.resize
77
+ pager.width.is_a?(Integer).should be(true)
78
+ pager.height.is_a?(Integer).should be(true)
79
+ reset_terminal_size
80
+ end
141
81
  end
142
-
143
- test "formats with output_method option" do
144
- set_config 'String'=>{:class=>"Blahify"}
145
- Hirb::View.render_method.expects(:call).with('d,u,d')
146
- Hirb::View.render_output('dude', :class=>"Commify", :output_method=>:chop)
82
+
83
+ test "disable points output_value back to original output_value" do
84
+ View.expects(:render_output).never
85
+ Hirb.enable
86
+ Hirb.disable
87
+ context_stub = stub(:last_value=>'')
88
+ ::IRB::Irb.new(context_stub).output_value
147
89
  end
148
90
 
149
- test "formats output array with output_method option" do
150
- set_config 'String'=>{:class=>"Blahify"}
151
- Hirb::View.render_method.expects(:call).with("d,u,d\nm,a")
152
- Hirb::View.render_output(['dude', 'man'], :class=>"Commify", :output_method=>:chop)
91
+ test "disable works without irb defined" do
92
+ Object.stubs(:const_defined?).with(:IRB).returns(false)
93
+ Hirb.enable
94
+ Hirb.disable
95
+ View.enabled?.should be(false)
153
96
  end
154
97
 
155
- test "formats with block" do
156
- Hirb::View.load_config
157
- Hirb::View.render_method.expects(:call).with('=dude=')
158
- Hirb::View.render_output('dude') {|output|
159
- "=#{output}="
160
- }
98
+ test "capture_and_render" do
99
+ string = 'no waaaay'
100
+ View.render_method.expects(:call).with(string)
101
+ View.capture_and_render { print string }
102
+ end
103
+
104
+ test "state is toggled by toggle_pager" do
105
+ previous_state = View.config[:pager]
106
+ View.toggle_pager
107
+ View.config[:pager].should == !previous_state
161
108
  end
162
109
 
163
- test "console_render_output merge options option" do
164
- set_config "String"=>{:class=>"Commify", :options=>{:fields=>%w{f1 f2}}}
165
- Commify.expects(:render).with('dude', :max_width=>10, :fields=>%w{f1 f2})
166
- Hirb::View.render_output('dude', :options=>{:max_width=>10})
110
+ test "state is toggled by toggle_formatter" do
111
+ previous_state = View.config[:formatter]
112
+ View.toggle_formatter
113
+ View.config[:formatter].should == !previous_state
167
114
  end
168
115
  end
169
116
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hirb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Horner
@@ -9,44 +9,52 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-23 00:00:00 -04:00
12
+ date: 2009-06-30 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description: A mini view framework for console/irb that's easy to use, even while under its influence.
16
+ description: Hirb currently provides a mini view framework for console applications, designed to improve irb's default output. Hirb improves console output by providing a smart pager and auto-formatting output. The smart pager detects when an output exceeds a screenful and thus only pages output as needed. Auto-formatting adds a view to an output's class. This is helpful in separating views from content (MVC anyone?). The framework encourages reusing views by letting you package them in classes and associate them with any number of output classes.
17
17
  email: gabriel.horner@gmail.com
18
18
  executables: []
19
19
 
20
20
  extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
- - README.rdoc
24
23
  - LICENSE.txt
24
+ - README.rdoc
25
25
  files:
26
26
  - CHANGELOG.rdoc
27
27
  - LICENSE.txt
28
- - Rakefile
29
28
  - README.rdoc
29
+ - Rakefile
30
30
  - VERSION.yml
31
- - lib/hirb
31
+ - lib/hirb.rb
32
32
  - lib/hirb/console.rb
33
+ - lib/hirb/formatter.rb
33
34
  - lib/hirb/hash_struct.rb
34
- - lib/hirb/helpers
35
+ - lib/hirb/helpers.rb
35
36
  - lib/hirb/helpers/active_record_table.rb
36
37
  - lib/hirb/helpers/auto_table.rb
37
38
  - lib/hirb/helpers/object_table.rb
38
39
  - lib/hirb/helpers/parent_child_tree.rb
39
40
  - lib/hirb/helpers/table.rb
40
41
  - lib/hirb/helpers/tree.rb
41
- - lib/hirb/helpers.rb
42
+ - lib/hirb/helpers/vertical_table.rb
42
43
  - lib/hirb/import_object.rb
44
+ - lib/hirb/menu.rb
45
+ - lib/hirb/pager.rb
43
46
  - lib/hirb/util.rb
44
47
  - lib/hirb/view.rb
45
- - lib/hirb/views
46
48
  - lib/hirb/views/activerecord_base.rb
47
- - lib/hirb.rb
49
+ - test/active_record_table_test.rb
50
+ - test/auto_table_test.rb
51
+ - test/console_test.rb
52
+ - test/formatter_test.rb
48
53
  - test/hirb_test.rb
49
54
  - test/import_test.rb
55
+ - test/menu_test.rb
56
+ - test/object_table_test.rb
57
+ - test/pager_test.rb
50
58
  - test/table_test.rb
51
59
  - test/test_helper.rb
52
60
  - test/tree_test.rb
@@ -54,9 +62,10 @@ files:
54
62
  - test/view_test.rb
55
63
  has_rdoc: true
56
64
  homepage: http://github.com/cldwalker/hirb
65
+ licenses: []
66
+
57
67
  post_install_message:
58
68
  rdoc_options:
59
- - --inline-source
60
69
  - --charset=UTF-8
61
70
  require_paths:
62
71
  - lib
@@ -75,9 +84,22 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
84
  requirements: []
76
85
 
77
86
  rubyforge_project: tagaholic
78
- rubygems_version: 1.3.1
87
+ rubygems_version: 1.3.2
79
88
  signing_key:
80
- specification_version: 2
89
+ specification_version: 3
81
90
  summary: A mini view framework for console/irb that's easy to use, even while under its influence.
82
- test_files: []
83
-
91
+ test_files:
92
+ - test/active_record_table_test.rb
93
+ - test/auto_table_test.rb
94
+ - test/console_test.rb
95
+ - test/formatter_test.rb
96
+ - test/hirb_test.rb
97
+ - test/import_test.rb
98
+ - test/menu_test.rb
99
+ - test/object_table_test.rb
100
+ - test/pager_test.rb
101
+ - test/table_test.rb
102
+ - test/test_helper.rb
103
+ - test/tree_test.rb
104
+ - test/util_test.rb
105
+ - test/view_test.rb