cldwalker-hirb 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
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: cldwalker-hirb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Horner
@@ -9,44 +9,48 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-18 00:00:00 -07:00
12
+ date: 2009-06-19 00:00:00 -07: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
42
  - lib/hirb/import_object.rb
43
+ - lib/hirb/menu.rb
44
+ - lib/hirb/pager.rb
43
45
  - lib/hirb/util.rb
44
46
  - lib/hirb/view.rb
45
- - lib/hirb/views
46
47
  - lib/hirb/views/activerecord_base.rb
47
- - lib/hirb.rb
48
+ - test/console_test.rb
49
+ - test/formatter_test.rb
48
50
  - test/hirb_test.rb
49
51
  - test/import_test.rb
52
+ - test/menu_test.rb
53
+ - test/pager_test.rb
50
54
  - test/table_test.rb
51
55
  - test/test_helper.rb
52
56
  - test/tree_test.rb
@@ -56,7 +60,6 @@ has_rdoc: true
56
60
  homepage: http://github.com/cldwalker/hirb
57
61
  post_install_message:
58
62
  rdoc_options:
59
- - --inline-source
60
63
  - --charset=UTF-8
61
64
  require_paths:
62
65
  - lib
@@ -74,10 +77,20 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
77
  version:
75
78
  requirements: []
76
79
 
77
- rubyforge_project:
80
+ rubyforge_project: tagaholic
78
81
  rubygems_version: 1.2.0
79
82
  signing_key:
80
- specification_version: 2
83
+ specification_version: 3
81
84
  summary: A mini view framework for console/irb that's easy to use, even while under its influence.
82
- test_files: []
83
-
85
+ test_files:
86
+ - test/console_test.rb
87
+ - test/formatter_test.rb
88
+ - test/hirb_test.rb
89
+ - test/import_test.rb
90
+ - test/menu_test.rb
91
+ - test/pager_test.rb
92
+ - test/table_test.rb
93
+ - test/test_helper.rb
94
+ - test/tree_test.rb
95
+ - test/util_test.rb
96
+ - test/view_test.rb