hirb 0.1.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.
@@ -0,0 +1,21 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
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
20
+ end
21
+ end
@@ -0,0 +1,169 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
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')
116
+ end
117
+
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'])
122
+ end
123
+
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')
129
+ 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
135
+ 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")
141
+ 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)
147
+ end
148
+
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)
153
+ end
154
+
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
+ }
161
+ end
162
+
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})
167
+ end
168
+ end
169
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hirb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Horner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-23 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A mini view framework for console/irb that's easy to use, even while under its influence.
17
+ email: gabriel.horner@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - LICENSE.txt
25
+ files:
26
+ - CHANGELOG.rdoc
27
+ - LICENSE.txt
28
+ - Rakefile
29
+ - README.rdoc
30
+ - VERSION.yml
31
+ - lib/hirb
32
+ - lib/hirb/console.rb
33
+ - lib/hirb/hash_struct.rb
34
+ - lib/hirb/helpers
35
+ - lib/hirb/helpers/active_record_table.rb
36
+ - lib/hirb/helpers/auto_table.rb
37
+ - lib/hirb/helpers/object_table.rb
38
+ - lib/hirb/helpers/parent_child_tree.rb
39
+ - lib/hirb/helpers/table.rb
40
+ - lib/hirb/helpers/tree.rb
41
+ - lib/hirb/helpers.rb
42
+ - lib/hirb/import_object.rb
43
+ - lib/hirb/util.rb
44
+ - lib/hirb/view.rb
45
+ - lib/hirb/views
46
+ - lib/hirb/views/activerecord_base.rb
47
+ - lib/hirb.rb
48
+ - test/hirb_test.rb
49
+ - test/import_test.rb
50
+ - test/table_test.rb
51
+ - test/test_helper.rb
52
+ - test/tree_test.rb
53
+ - test/util_test.rb
54
+ - test/view_test.rb
55
+ has_rdoc: true
56
+ homepage: http://github.com/cldwalker/hirb
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --inline-source
60
+ - --charset=UTF-8
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ requirements: []
76
+
77
+ rubyforge_project: tagaholic
78
+ rubygems_version: 1.3.1
79
+ signing_key:
80
+ specification_version: 2
81
+ summary: A mini view framework for console/irb that's easy to use, even while under its influence.
82
+ test_files: []
83
+