test/tabular_test.rb in iostreams-0.15.0 vs test/tabular_test.rb in iostreams-0.16.0
- old
+ new
@@ -58,28 +58,28 @@
assert_equal ['first', nil, 'third'], header
end
it 'raises exception for columns not in the whitelist' do
tabular = IOStreams::Tabular.new(columns: [' first ', 'Unknown Column', 'thirD '], allowed_columns: @allowed_columns, skip_unknown: false)
- exc = assert_raises IOStreams::Tabular::Errors::InvalidHeader do
+ exc = assert_raises IOStreams::Errors::InvalidHeader do
tabular.cleanse_header!
end
assert_equal 'Unknown columns after cleansing: Unknown Column', exc.message
end
it 'raises exception missing required columns' do
required = ['first', 'second', 'fifth']
tabular = IOStreams::Tabular.new(columns: [' first ', 'Second', 'thirD '], allowed_columns: @allowed_columns, required_columns: required)
- exc = assert_raises IOStreams::Tabular::Errors::InvalidHeader do
+ exc = assert_raises IOStreams::Errors::InvalidHeader do
tabular.cleanse_header!
end
assert_equal 'Missing columns after cleansing: fifth', exc.message
end
it 'raises exception when no columns left' do
tabular = IOStreams::Tabular.new(columns: ['one', 'two', 'three'], allowed_columns: @allowed_columns)
- exc = assert_raises IOStreams::Tabular::Errors::InvalidHeader do
+ exc = assert_raises IOStreams::Errors::InvalidHeader do
tabular.cleanse_header!
end
assert_equal 'All columns are unknown after cleansing: one,two,three', exc.message
end
end
@@ -133,10 +133,51 @@
assert hash = tabular.record_parse('1|2|3')
assert_equal({'first_field' => '1', 'second' => '2', 'third' => '3'}, hash)
end
end
+ describe ':fixed format' do
+ let :tabular do
+ layout = [
+ {key: 'name', size: 23},
+ {key: 'address', size: 40},
+ {key: 'zip', size: 5}
+ ]
+ IOStreams::Tabular.new(format: :fixed, format_options: {layout: layout})
+ end
+
+ it 'parses to hash' do
+ assert hash = tabular.record_parse('Jack over there 34618')
+ assert_equal({'name' => 'Jack', 'address' => 'over there', 'zip' => '34618'}, hash)
+ end
+
+ it 'parses short string' do
+ # TODO: Raise exception on lines that are too short?
+ assert hash = tabular.record_parse('Jack over th')
+ assert_equal({'name' => 'Jack', 'address' => 'over th', 'zip' => ''}, hash)
+ end
+
+ it 'parses longer string' do
+ # TODO: Raise exception on lines that are too long?
+ assert hash = tabular.record_parse('Jack over there 34618........................................')
+ assert_equal({'name' => 'Jack', 'address' => 'over there', 'zip' => '34618'}, hash)
+ end
+
+ it 'parses empty strings' do
+ assert hash = tabular.record_parse(' 34618')
+ assert_equal({'name' => '', 'address' => '', 'zip' => '34618'}, hash)
+ end
+
+ it 'parses nil data as nil' do
+ refute tabular.record_parse(nil)
+ end
+
+ it 'parses empty string as nil' do
+ refute tabular.record_parse('')
+ end
+ end
+
it 'skips columns not in the whitelist' do
tabular.header.allowed_columns = ['first', 'second', 'third', 'fourth', 'fifth']
tabular.cleanse_header!
assert hash = tabular.record_parse('1,2,3')
assert_equal({'second' => '2', 'third' => '3'}, hash)
@@ -175,9 +216,44 @@
end
it 'renders psv numeric and pipe data' do
assert psv_string = tabular.render({'third' => 23, 'first_field' => 'a|b|c', 'second' => '|'})
assert_equal 'a:b:c|:|23', psv_string
+ end
+ end
+
+ describe ':fixed format' do
+ let :tabular do
+ layout = [
+ {key: 'name', size: 23},
+ {key: 'address', size: 40},
+ {key: 'zip', size: 5}
+ ]
+ IOStreams::Tabular.new(format: :fixed, format_options: {layout: layout})
+ end
+
+ it 'renders fixed data' do
+ assert string = tabular.render({'name' => 'Jack', 'address' => 'over there', 'zip' => 34618, 'phone' => '5551231234'})
+ assert_equal 'Jack over there 34618', string
+ end
+
+ it 'truncates long data' do
+ assert string = tabular.render({'name' => 'Jack', 'address' => 'over there', 'zip' => 3461832653653265, 'phone' => '5551231234'})
+ assert_equal 'Jack over there 34618', string
+ end
+
+ it 'renders nil as empty string' do
+ assert string = tabular.render('zip' => 3461832653653265)
+ assert_equal ' 34618', string
+ end
+
+ it 'renders boolean' do
+ assert string = tabular.render({'name' => true, 'address' => false, 'zip' => nil, 'phone' => '5551231234'})
+ assert_equal 'true false ', string
+ end
+
+ it 'renders no data as nil' do
+ refute tabular.render({})
end
end
end
end
end