hirb 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +8 -2
- data/Rakefile +23 -41
- data/gemspec +20 -0
- data/lib/bond/completions/hirb.rb +14 -0
- data/lib/hirb.rb +0 -3
- data/lib/hirb/helpers/table.rb +1 -1
- data/lib/hirb/version.rb +1 -1
- data/test/auto_table_test.rb +25 -27
- data/test/bacon_extensions.rb +26 -0
- data/test/console_test.rb +7 -9
- data/test/dynamic_view_test.rb +71 -77
- data/test/formatter_test.rb +39 -37
- data/test/hirb_test.rb +8 -8
- data/test/import_test.rb +4 -4
- data/test/menu_test.rb +42 -42
- data/test/object_table_test.rb +65 -67
- data/test/pager_test.rb +133 -135
- data/test/resizer_test.rb +48 -47
- data/test/table_test.rb +69 -70
- data/test/test_helper.rb +11 -7
- data/test/tree_test.rb +34 -46
- data/test/util_test.rb +57 -59
- data/test/view_test.rb +138 -140
- data/test/views_test.rb +8 -10
- metadata +66 -36
data/CHANGELOG.rdoc
CHANGED
@@ -1,6 +1,12 @@
|
|
1
|
+
== 0.3.2
|
2
|
+
* Added irb autocompletions for bond.
|
3
|
+
* Fixed tests for ruby 1.9.
|
4
|
+
* Changed tests to use bacon.
|
5
|
+
* Removed jeweler in Rakefile and pointless $LOAD_PATH manipulation.
|
6
|
+
|
1
7
|
== 0.3.1
|
2
|
-
* Bug fix on DynamicView.class_to_method to allow overrides of default views
|
3
|
-
* Modified mongo_mapper view to have _id first
|
8
|
+
* Bug fix on DynamicView.class_to_method to allow overrides of default views.
|
9
|
+
* Modified mongo_mapper view to have _id first.
|
4
10
|
|
5
11
|
== 0.3.0
|
6
12
|
* Added dynamic views.
|
data/Rakefile
CHANGED
@@ -1,53 +1,35 @@
|
|
1
1
|
require 'rake'
|
2
|
-
require '
|
3
|
-
require 'rake/rdoctask'
|
4
|
-
begin
|
5
|
-
require 'rcov/rcovtask'
|
2
|
+
require 'fileutils'
|
6
3
|
|
7
|
-
|
8
|
-
|
9
|
-
t.test_files = FileList['test/**/*_test.rb']
|
10
|
-
t.rcov_opts = ["-T -x '/Library/Ruby/*'"]
|
11
|
-
t.verbose = true
|
12
|
-
end
|
13
|
-
rescue LoadError
|
14
|
-
puts "Rcov not available. Install it for rcov-related tasks with: sudo gem install rcov"
|
4
|
+
def gemspec
|
5
|
+
@gemspec ||= eval(File.read('gemspec'), binding, 'gemspec')
|
15
6
|
end
|
16
7
|
|
17
|
-
|
18
|
-
|
19
|
-
|
8
|
+
desc "Build the gem"
|
9
|
+
task :gem=>:gemspec do
|
10
|
+
sh "gem build gemspec"
|
11
|
+
FileUtils.mkdir_p 'pkg'
|
12
|
+
FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
|
13
|
+
end
|
20
14
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
s.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."
|
26
|
-
s.email = "gabriel.horner@gmail.com"
|
27
|
-
s.homepage = "http://tagaholic.me/hirb/"
|
28
|
-
s.authors = ["Gabriel Horner"]
|
29
|
-
s.rubyforge_project = 'tagaholic'
|
30
|
-
s.has_rdoc = true
|
31
|
-
s.extra_rdoc_files = ["README.rdoc", "LICENSE.txt"]
|
32
|
-
s.files = FileList["[A-Z]*", "{bin,lib,test}/**/*"]
|
33
|
-
end
|
15
|
+
desc "Install the gem locally"
|
16
|
+
task :install => :gem do
|
17
|
+
sh %{gem install pkg/#{gemspec.name}-#{gemspec.version}}
|
18
|
+
end
|
34
19
|
|
35
|
-
|
36
|
-
|
20
|
+
desc "Generate the gemspec"
|
21
|
+
task :generate do
|
22
|
+
puts gemspec.to_ruby
|
37
23
|
end
|
38
24
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
t.verbose = false
|
25
|
+
desc "Validate the gemspec"
|
26
|
+
task :gemspec do
|
27
|
+
gemspec.validate
|
43
28
|
end
|
44
29
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
rdoc.options << '--line-numbers' << '--inline-source'
|
49
|
-
rdoc.rdoc_files.include('README*')
|
50
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
30
|
+
desc 'Run tests'
|
31
|
+
task :test do |t|
|
32
|
+
sh 'bacon -q -Ilib -I. test/*_test.rb'
|
51
33
|
end
|
52
34
|
|
53
|
-
task :default => :test
|
35
|
+
task :default => :test
|
data/gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require 'rubygems' unless Object.const_defined?(:Gem)
|
3
|
+
require File.dirname(__FILE__) + "/lib/hirb/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "hirb"
|
7
|
+
s.version = Hirb::VERSION
|
8
|
+
s.authors = ["Gabriel Horner"]
|
9
|
+
s.email = "gabriel.horner@gmail.com"
|
10
|
+
s.homepage = "http://tagaholic.me/hirb/"
|
11
|
+
s.summary = "A mini view framework for console/irb that's easy to use, even while under its influence."
|
12
|
+
s.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."
|
13
|
+
s.required_rubygems_version = ">= 1.3.6"
|
14
|
+
s.rubyforge_project = 'tagaholic'
|
15
|
+
s.add_development_dependency 'bacon'
|
16
|
+
s.add_development_dependency 'mocha'
|
17
|
+
s.add_development_dependency 'mocha-on-bacon'
|
18
|
+
s.files = Dir.glob(%w[{lib,test}/**/*.rb bin/* [A-Z]*.{txt,rdoc} ext/**/*.{rb,c}]) + %w{Rakefile gemspec}
|
19
|
+
s.extra_rdoc_files = ["README.rdoc", "LICENSE.txt"]
|
20
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
complete(:methods=>%w{Hirb::View.enable Hirb.enable}) {
|
2
|
+
%w{config_file output_method output width height formatter pager pager_command}
|
3
|
+
}
|
4
|
+
complete(:methods=>%w{Hirb::Helpers::Table.render table}) {
|
5
|
+
%w{fields headers max_fields max_width resize number change_fields}+
|
6
|
+
%w{filters header_filter filter_any filter_classes vertical all_fields}+
|
7
|
+
%w{description escape_special_chars table_class hide_empty}
|
8
|
+
}
|
9
|
+
complete(:method=>"Hirb::Helpers::Tree.render") {
|
10
|
+
%w{type validate indent limit description multi_line_nodes value_method children_method}
|
11
|
+
}
|
12
|
+
complete(:methods=>%w{Hirb::Menu.render menu}) {
|
13
|
+
%w{helper_class prompt ask directions readline two_d default_field action multi_action action_object command}
|
14
|
+
}
|
data/lib/hirb.rb
CHANGED
data/lib/hirb/helpers/table.rb
CHANGED
@@ -164,7 +164,7 @@ module Hirb
|
|
164
164
|
rows = filter_values(rows)
|
165
165
|
rows.each_with_index {|e,i| e[:hirb_number] = (i + 1).to_s} if @options[:number]
|
166
166
|
deleted_callbacks = Array(@options[:delete_callbacks]).map {|e| "#{e}_callback" }
|
167
|
-
(methods.grep(/_callback$/) - deleted_callbacks).sort.each do |meth|
|
167
|
+
(methods.grep(/_callback$/).map {|e| e.to_s} - deleted_callbacks).sort.each do |meth|
|
168
168
|
rows = send(meth, rows, @options.dup)
|
169
169
|
end
|
170
170
|
validate_values(rows)
|
data/lib/hirb/version.rb
CHANGED
data/test/auto_table_test.rb
CHANGED
@@ -1,32 +1,30 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
end
|
3
|
+
describe "auto table" do
|
4
|
+
it "converts nonarrays to arrays and renders" do
|
5
|
+
require 'set'
|
6
|
+
expected_table = <<-TABLE.unindent
|
7
|
+
+-------+
|
8
|
+
| value |
|
9
|
+
+-------+
|
10
|
+
| 1 |
|
11
|
+
| 2 |
|
12
|
+
| 3 |
|
13
|
+
+-------+
|
14
|
+
3 rows in set
|
15
|
+
TABLE
|
16
|
+
Helpers::AutoTable.render(::Set.new([1,2,3])).should == expected_table
|
17
|
+
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
end
|
19
|
+
it "renders hash" do
|
20
|
+
expected_table = <<-TABLE.unindent
|
21
|
+
+---+-------+
|
22
|
+
| 0 | 1 |
|
23
|
+
+---+-------+
|
24
|
+
| a | 12345 |
|
25
|
+
+---+-------+
|
26
|
+
1 row in set
|
27
|
+
TABLE
|
28
|
+
Helpers::AutoTable.render({:a=>12345}).should == expected_table
|
31
29
|
end
|
32
30
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module BaconExtensions
|
2
|
+
def self.included(mod)
|
3
|
+
mod.module_eval do
|
4
|
+
# nested context methods automatically inherit methods from parent contexts
|
5
|
+
def describe(*args, &block)
|
6
|
+
context = Bacon::Context.new(args.join(' '), &block)
|
7
|
+
(parent_context = self).methods(false).each {|e|
|
8
|
+
class<<context; self end.send(:define_method, e) {|*args| parent_context.send(e, *args)}
|
9
|
+
}
|
10
|
+
@before.each { |b| context.before(&b) }
|
11
|
+
@after.each { |b| context.after(&b) }
|
12
|
+
context.run
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def xit(*args); end
|
18
|
+
def xdescribe(*args); end
|
19
|
+
def before_all; yield; end
|
20
|
+
def after_all; yield; end
|
21
|
+
def assert(description, &block)
|
22
|
+
it(description) do
|
23
|
+
block.call.should == true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/test/console_test.rb
CHANGED
@@ -1,12 +1,10 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
Hirb::View.config.is_a?(Hash).should == true
|
10
|
-
end
|
3
|
+
describe "parse_input" do
|
4
|
+
it "config is set if it wasn't before" do
|
5
|
+
reset_config
|
6
|
+
View.expects(:render_output)
|
7
|
+
Console.render_output('blah')
|
8
|
+
View.config.is_a?(Hash).should == true
|
11
9
|
end
|
12
|
-
end
|
10
|
+
end
|
data/test/dynamic_view_test.rb
CHANGED
@@ -1,100 +1,94 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
end
|
9
|
-
after(:all) { Formatter.dynamic_config = {} }
|
10
|
-
|
11
|
-
context "add" do
|
12
|
-
before(:all) { View.load_config }
|
3
|
+
describe "DynamicView" do
|
4
|
+
def output_expects(output, expects)
|
5
|
+
Helpers::ObjectTable.expects(:render).with(output, expects)
|
6
|
+
Helpers::AutoTable.render(output)
|
7
|
+
end
|
13
8
|
|
14
|
-
|
15
|
-
|
16
|
-
Hirb.add_dynamic_view 'Blah', {}
|
17
|
-
}.message.should =~ /:helper.*required/
|
18
|
-
end
|
9
|
+
describe "add" do
|
10
|
+
before_all { View.load_config }
|
19
11
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
12
|
+
it "raises error if no :helper option" do
|
13
|
+
lambda { Hirb.add_dynamic_view 'Blah', {} }.should.raise(ArgumentError).
|
14
|
+
message.should =~ /:helper.*required/
|
15
|
+
end
|
25
16
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
end
|
17
|
+
it "raises error if :helper option not a dynamic_view module" do
|
18
|
+
lambda { Hirb.add_dynamic_view('Blah', :helper=>:table) {|obj| } }.
|
19
|
+
should.raise(ArgumentError).message.should =~ /:helper.*must/
|
20
|
+
end
|
31
21
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
output_expects [Date.new], :fields=>Date::DAYNAMES
|
37
|
-
end
|
22
|
+
it "raises error if views module not a module" do
|
23
|
+
lambda { Hirb.add_dynamic_view 'Blah', :helper=>:auto_table }.should.raise(ArgumentError).
|
24
|
+
message.should =~ /must be a module/
|
25
|
+
end
|
38
26
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end
|
43
|
-
Hirb.add_dynamic_view('Date', :helper=>:auto_table) do |obj|
|
44
|
-
{:fields=>[:blah]}
|
45
|
-
end
|
46
|
-
output_expects [Date.new], :fields=>[:blah]
|
27
|
+
it "adds a view with block" do
|
28
|
+
Hirb.add_dynamic_view('Date', :helper=>:auto_table) do |obj|
|
29
|
+
{:fields=>obj.class::DAYNAMES}
|
47
30
|
end
|
31
|
+
output_expects [Date.new], :fields=>Date::DAYNAMES
|
48
32
|
end
|
49
33
|
|
50
|
-
|
51
|
-
|
52
|
-
|
34
|
+
it "when adding views with a block, second view for same class overrides first one" do
|
35
|
+
Hirb.add_dynamic_view('Date', :helper=>:auto_table) do |obj|
|
36
|
+
{:fields=>obj.class::DAYNAMES}
|
37
|
+
end
|
38
|
+
Hirb.add_dynamic_view('Date', :helper=>:auto_table) do |obj|
|
39
|
+
{:fields=>[:blah]}
|
53
40
|
end
|
41
|
+
output_expects [Date.new], :fields=>[:blah]
|
54
42
|
end
|
43
|
+
end
|
55
44
|
|
56
|
-
|
57
|
-
|
45
|
+
it "class_to_method and method_to_class convert to each other" do
|
46
|
+
["DBI::Row", "Hirb::View"].each do |e|
|
47
|
+
Helpers::AutoTable.method_to_class(DynamicView.class_to_method(e).downcase).should == e
|
58
48
|
end
|
49
|
+
end
|
59
50
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
mod_block = block_given? ? block : lambda {|obj| {:fields=>obj.class::DAYNAMES}}
|
64
|
-
mod.send(:define_method, :date_view, mod_block)
|
65
|
-
Hirb.add_dynamic_view mod, :helper=>:auto_table
|
66
|
-
end
|
51
|
+
it "class_to_method converts correctly" do
|
52
|
+
DynamicView.class_to_method("DBI::Row").should == 'd_b_i__row_view'
|
53
|
+
end
|
67
54
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
55
|
+
describe "dynamic_view" do
|
56
|
+
def define_view(mod_name= :Blah, &block)
|
57
|
+
mod = Views.const_set(mod_name, Module.new)
|
58
|
+
mod_block = block_given? ? block : lambda {|obj| {:fields=>obj.class::DAYNAMES}}
|
59
|
+
mod.send(:define_method, :date_view, mod_block)
|
60
|
+
Hirb.add_dynamic_view mod, :helper=>:auto_table
|
61
|
+
end
|
72
62
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
end
|
63
|
+
before_all { View.load_config }
|
64
|
+
before { Formatter.dynamic_config = {} }
|
65
|
+
after { Views.send(:remove_const, :Blah) }
|
77
66
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
end
|
67
|
+
it "sets a view's options" do
|
68
|
+
define_view
|
69
|
+
output_expects [Date.new], :fields=>Date::DAYNAMES
|
70
|
+
end
|
83
71
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
72
|
+
it "does override existing formatter dynamic_config" do
|
73
|
+
Formatter.dynamic_config["Date"] = {:class=>Helpers::Table}
|
74
|
+
define_view
|
75
|
+
Formatter.dynamic_config["Date"].should == {:class=>Hirb::Helpers::AutoTable, :ancestor=>true}
|
76
|
+
end
|
77
|
+
|
78
|
+
it "raises a readable error when error occurs in a view" do
|
79
|
+
define_view {|obj| raise 'blah' }
|
80
|
+
lambda { Helpers::AutoTable.render([Date.new]) }.should.raise(RuntimeError).
|
81
|
+
message.should =~ /'Date'.*date_view.*\nblah/
|
82
|
+
end
|
90
83
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
end
|
96
|
-
output_expects [Date.new], :fields=>(Date::DAYNAMES + ['blah'])
|
84
|
+
it "another view can reuse an old view's options" do
|
85
|
+
define_view
|
86
|
+
define_view(:Blah2) do |obj|
|
87
|
+
{:fields=>obj.class::DAYNAMES + ['blah']}
|
97
88
|
end
|
89
|
+
output_expects [Date.new], :fields=>(Date::DAYNAMES + ['blah'])
|
98
90
|
end
|
91
|
+
after_all { reset_config }
|
99
92
|
end
|
100
|
-
|
93
|
+
after_all { Formatter.dynamic_config = {} }
|
94
|
+
end
|
data/test/formatter_test.rb
CHANGED
@@ -1,45 +1,48 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
2
|
|
3
|
-
|
4
|
-
class FormatterTest < Test::Unit::TestCase
|
3
|
+
describe "Formatter" do
|
5
4
|
def set_formatter(hash={})
|
6
5
|
@formatter = Formatter.new(hash)
|
7
6
|
end
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
set_formatter "String"=>{:args=>[1,2], :options=>{:fields=>[:to_s]}},
|
8
|
+
describe "klass_config" do
|
9
|
+
it "recursively merges ancestor options" do
|
10
|
+
@formatter = set_formatter "String"=>{:args=>[1,2], :options=>{:fields=>[:to_s]}},
|
12
11
|
"Object"=>{:method=>:object_output, :ancestor=>true, :options=>{:vertical=>true}},
|
13
12
|
"Kernel"=>{:method=>:default_output}
|
14
13
|
expected_result = {:method=>:object_output, :args=>[1, 2], :ancestor=>true, :options=>{:fields=>[:to_s], :vertical=>true}}
|
15
14
|
@formatter.klass_config(::String).should == expected_result
|
16
15
|
end
|
17
16
|
|
18
|
-
|
19
|
-
set_formatter "String"=>{:args=>[1,2]}, "Object"=>{:method=>:object_output},
|
17
|
+
it "doesn't merge ancestor options" do
|
18
|
+
@formatter = set_formatter "String"=>{:args=>[1,2]}, "Object"=>{:method=>:object_output},
|
19
|
+
"Kernel"=>{:method=>:default_output}
|
20
20
|
@formatter.klass_config(::String).should == {:args=>[1, 2]}
|
21
21
|
end
|
22
22
|
|
23
|
-
|
23
|
+
it "returns hash when nothing found" do
|
24
24
|
set_formatter.klass_config(::String).should == {}
|
25
25
|
end
|
26
26
|
|
27
|
-
|
28
|
-
|
27
|
+
describe "with dynamic_config" do
|
28
|
+
def set_formatter(hash={})
|
29
|
+
@formatter = Formatter.new(hash)
|
30
|
+
end
|
31
|
+
after { Formatter.dynamic_config = {}}
|
29
32
|
|
30
|
-
|
33
|
+
it "merges ancestor options and sets local config" do
|
31
34
|
Formatter.dynamic_config = {"Object"=>{:method=>:blah}, "Kernel"=>{:args=>[1,2], :ancestor=>true}}
|
32
35
|
set_formatter.klass_config(::String).should == {:args=>[1,2], :ancestor=>true}
|
33
36
|
@formatter.config['Kernel'].should == {:args=>[1,2], :ancestor=>true}
|
34
37
|
end
|
35
38
|
|
36
|
-
|
39
|
+
it "uses local config over dynamic_config" do
|
37
40
|
Formatter.dynamic_config = {"String"=>{:method=>:blah}}
|
38
41
|
set_formatter "String"=>{:args=>[1,2]}
|
39
42
|
@formatter.klass_config(::String).should == {:args=>[1,2]}
|
40
43
|
end
|
41
44
|
|
42
|
-
|
45
|
+
it "uses dynamic_config and sets local config" do
|
43
46
|
Formatter.dynamic_config = {"String"=>{:method=>:blah}}
|
44
47
|
set_formatter.klass_config(::String).should == {:method=>:blah}
|
45
48
|
@formatter.config['String'].should == {:method=>:blah}
|
@@ -47,30 +50,30 @@ class FormatterTest < Test::Unit::TestCase
|
|
47
50
|
end
|
48
51
|
end
|
49
52
|
|
50
|
-
|
51
|
-
|
53
|
+
describe "formatter methods:" do
|
54
|
+
before_all { eval "module ::Dooda; end" }
|
52
55
|
|
53
|
-
|
54
|
-
set_formatter
|
56
|
+
it "add_view sets formatter config" do
|
57
|
+
@formatter = set_formatter
|
55
58
|
@formatter.add_view ::Dooda, :class=>"DoodaView"
|
56
59
|
@formatter.klass_config(::Dooda).should == {:class=>"DoodaView"}
|
57
60
|
end
|
58
61
|
|
59
|
-
|
60
|
-
set_formatter "Dooda"=>{:class=>"DoodaView"}
|
62
|
+
it "add_view overwrites existing formatter config" do
|
63
|
+
@formatter = set_formatter "Dooda"=>{:class=>"DoodaView"}
|
61
64
|
@formatter.add_view ::Dooda, :class=>"DoodaView2"
|
62
65
|
@formatter.klass_config(::Dooda).should == {:class=>"DoodaView2"}
|
63
66
|
end
|
64
67
|
|
65
|
-
|
66
|
-
set_formatter
|
68
|
+
it "parse_console_options passes all options except for formatter options into :options" do
|
69
|
+
@formatter = set_formatter
|
67
70
|
options = {:class=>'blah', :method=>'blah', :output_method=>'blah', :blah=>'blah'}
|
68
71
|
expected_options = {:class=>'blah', :method=>'blah', :output_method=>'blah', :options=>{:blah=>'blah'}}
|
69
72
|
@formatter.parse_console_options(options).should == expected_options
|
70
73
|
end
|
71
74
|
end
|
72
75
|
|
73
|
-
|
76
|
+
describe "format_output" do
|
74
77
|
def view_output(*args, &block); View.view_output(*args, &block); end
|
75
78
|
def render_method(*args); View.render_method(*args); end
|
76
79
|
|
@@ -78,7 +81,7 @@ class FormatterTest < Test::Unit::TestCase
|
|
78
81
|
Hirb.enable :output=>value
|
79
82
|
end
|
80
83
|
|
81
|
-
|
84
|
+
before_all {
|
82
85
|
eval %[module ::Commify
|
83
86
|
def self.render(strings)
|
84
87
|
strings = [strings] unless strings.is_a?(Array)
|
@@ -87,76 +90,75 @@ class FormatterTest < Test::Unit::TestCase
|
|
87
90
|
end]
|
88
91
|
reset_config
|
89
92
|
}
|
90
|
-
before
|
91
|
-
after
|
93
|
+
before { View.formatter = nil; reset_config }
|
94
|
+
after { Hirb.disable }
|
92
95
|
|
93
|
-
|
96
|
+
it "formats with method option" do
|
94
97
|
eval "module ::Kernel; def commify(string); string.split('').join(','); end; end"
|
95
98
|
enable_with_output "String"=>{:method=>:commify}
|
96
99
|
render_method.expects(:call).with('d,u,d,e')
|
97
100
|
view_output('dude')
|
98
101
|
end
|
99
102
|
|
100
|
-
|
103
|
+
it "formats with class option" do
|
101
104
|
enable_with_output "String"=>{:class=>"Commify"}
|
102
105
|
render_method.expects(:call).with('d,u,d,e')
|
103
106
|
view_output('dude')
|
104
107
|
end
|
105
108
|
|
106
|
-
|
109
|
+
it "formats with class option as symbol" do
|
107
110
|
enable_with_output "String"=>{:class=>:auto_table}
|
108
111
|
Helpers::AutoTable.expects(:render)
|
109
112
|
view_output('dude')
|
110
113
|
end
|
111
114
|
|
112
|
-
|
115
|
+
it "formats output array" do
|
113
116
|
enable_with_output "String"=>{:class=>"Commify"}
|
114
117
|
render_method.expects(:call).with('d,u,d,e')
|
115
118
|
view_output(['dude'])
|
116
119
|
end
|
117
120
|
|
118
|
-
|
121
|
+
it "formats with options option" do
|
119
122
|
eval "module ::Blahify; def self.render(*args); end; end"
|
120
123
|
enable_with_output "String"=>{:class=>"Blahify", :options=>{:fields=>%w{a b}}}
|
121
124
|
Blahify.expects(:render).with('dude', :fields=>%w{a b})
|
122
125
|
view_output('dude')
|
123
126
|
end
|
124
127
|
|
125
|
-
|
128
|
+
it "doesn't format and returns false when no format method found" do
|
126
129
|
Hirb.enable
|
127
130
|
render_method.expects(:call).never
|
128
131
|
view_output(Date.today).should == false
|
129
132
|
end
|
130
133
|
|
131
|
-
|
134
|
+
it "formats with output_method option as method" do
|
132
135
|
enable_with_output 'String'=>{:class=>"Commify", :output_method=>:chop}
|
133
136
|
render_method.expects(:call).with('d,u,d')
|
134
137
|
view_output('dude')
|
135
138
|
end
|
136
139
|
|
137
|
-
|
140
|
+
it "formats with output_method option as proc" do
|
138
141
|
enable_with_output 'String'=>{:class=>"Commify", :output_method=>lambda {|e| e.chop}}
|
139
142
|
render_method.expects(:call).with('d,u,d')
|
140
143
|
view_output('dude')
|
141
144
|
end
|
142
145
|
|
143
|
-
|
146
|
+
it "formats output array with output_method option" do
|
144
147
|
enable_with_output 'String'=>{:class=>"Commify", :output_method=>:chop}
|
145
148
|
render_method.expects(:call).with("d,u,d\nm,a")
|
146
149
|
view_output(['dude', 'man'])
|
147
150
|
end
|
148
151
|
|
149
|
-
|
152
|
+
it "formats with explicit class option" do
|
150
153
|
enable_with_output 'String'=>{:class=>"Blahify"}
|
151
154
|
render_method.expects(:call).with('d,u,d,e')
|
152
155
|
view_output('dude', :class=>"Commify")
|
153
156
|
end
|
154
157
|
|
155
|
-
|
158
|
+
it "formats with explicit options option merges with existing options" do
|
156
159
|
enable_with_output "String"=>{:class=>"Commify", :options=>{:fields=>%w{f1 f2}}}
|
157
160
|
Commify.expects(:render).with('dude', :max_width=>10, :fields=>%w{f1 f2})
|
158
161
|
view_output('dude', :options=>{:max_width=>10})
|
159
162
|
end
|
160
163
|
end
|
161
164
|
end
|
162
|
-
end
|