/
abstract_test.rb
126 lines (98 loc) · 4.19 KB
/
abstract_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require File.join(File.dirname(__FILE__), '../../test_helper')
describe HammerCLI::Output::Adapter::Abstract do
let(:adapter_class) { HammerCLI::Output::Adapter::Abstract }
let(:adapter) { HammerCLI::Output::Adapter::Abstract.new }
it "should have features" do
adapter.features.must_be_kind_of Array
end
class UnknownTestFormatter < HammerCLI::Output::Formatters::FieldFormatter
def format(data, field_params={})
data+'.'
end
def required_features
%i[unknown]
end
end
it "allows default pagination" do
adapter.paginate_by_default?.must_equal true
end
it "should filter formatters with incompatible features" do
HammerCLI::Output::Formatters::FormatterLibrary.expects(:new).with({ :type => [] })
adapter_class.new({}, {:type => [UnknownTestFormatter.new]})
end
it "should keep compatible formatters" do
formatter = UnknownTestFormatter.new
HammerCLI::Output::Formatters::FormatterLibrary.expects(:new).with({ :type => [formatter] })
# set :unknown tag to abstract
adapter_class.any_instance.stubs(:features).returns([:unknown])
adapter_class.new({}, {:type => [formatter]})
end
it "should put serializers first" do
formatter1 = UnknownTestFormatter.new
formatter1.stubs(:required_features).returns([])
formatter2 = UnknownTestFormatter.new
formatter2.stubs(:required_features).returns([:serialized])
HammerCLI::Output::Formatters::FormatterLibrary.expects(:new).with({ :type => [formatter2, formatter1] })
# set :unknown tag to abstract
adapter_class.any_instance.stubs(:features).returns([:serialized])
adapter_class.new({}, {:type => [formatter1, formatter2]})
end
context "messages" do
it "should print message to stdout" do
proc { adapter.print_message("MESSAGE") }.must_output(/.*MESSAGE.*/, "")
end
it "should print formatted message with parameters" do
proc { adapter.print_message("MESSAGE %{a}, %{b}", :a => 'A', :b => 'B') }.must_output(/.*MESSAGE A, B.*/, "")
end
it 'should print message with nil params' do
proc { adapter.print_message('MESSAGE', nil) }.must_output(/.*MESSAGE.*/, '')
end
end
it "should raise not implemented on print_collection" do
proc { adapter.print_collection([], HammerCLI::Output::RecordCollection.new([])) }.must_raise NotImplementedError
end
context "error messages" do
it "should print error message to stderr" do
proc { adapter.print_error("MESSAGE") }.must_output("", /.*MESSAGE.*/)
end
let(:expected_output) { "MESSAGE:\n"+
" error\n"+
" message\n"+
" details\n"
}
let(:expected_formatted_output) { "MESSAGE A, B:\n"+
" error A\n"+
" error B\n"
}
it "should print list details of error to stderr" do
proc { adapter.print_error("MESSAGE", ["error", "message", "details"]) }.must_output("", expected_output)
end
it "should print string details of error to stderr" do
proc { adapter.print_error("MESSAGE", "error\nmessage\ndetails") }.must_output("", expected_output)
end
it "should print formatted message with parameters" do
proc {
adapter.print_error("MESSAGE %{a}, %{b}", ["error %{a}", "error %{b}"], :a => 'A', :b => 'B')
}.must_output("", expected_formatted_output)
end
end
context "test data_for_field" do
let(:field) { Fields::Field.new(:path => [:field1]) }
it "returns nil if the record is nil" do
record = nil
assert_nil adapter.send(:data_for_field, field, record)
end
it "returns nil if the record is not a hash" do
record = []
assert_nil adapter.send(:data_for_field, field, record)
end
it "returns DataMissing instance, if the data does not exist for the field" do
record = { :field2 => :value2 }
assert_instance_of HammerCLI::Output::DataMissing, adapter.send(:data_for_field, field, record)
end
it "returns the value, if data exists for the field" do
record = { :field1 => :value1, :field2 => :value2 }
assert_equal adapter.send(:data_for_field, field, record), :value1
end
end
end