spec/streamingly/serde_spec.rb in streamingly-0.2.0 vs spec/streamingly/serde_spec.rb in streamingly-0.2.2

- old
+ new

@@ -26,6 +26,45 @@ it { expect(described_class.from_csv(text)).to eq(type.new(value)) } end end -end \ No newline at end of file + describe '.to_csv' do + it 'is identity function for a string' do + record = 'test_string' + expect(described_class.to_csv(record)).to eq record + end + + it 'is equal to string version of Streamingly kv' do + record = Streamingly::KV.new('key', 'value') + expect(described_class.to_csv(record)).to eq record.to_s + end + + it 'serializes struct to CSV, interpreting decimal fields as floats' do + Record = Struct.new(:number, :string) + record = Record.new(1, 'string_value') + expect(described_class.to_csv(record)).to eq 'Record,1,string_value' + end + end + + describe '.from_string_or_csv' do + it 'returns CSV serialization for CSV string' do + expect(described_class.from_string_or_csv('1,2')).to eq ['1', '2'] + end + + it 'returns string if not containing a comma' do + expect(described_class.from_string_or_csv('foo')).to eq 'foo' + end + + it 'returns string if containing a comma but not valid CSV' do + expect(described_class.from_string_or_csv('"foo,bar')).to eq '"foo,bar' + end + end + + describe '.from_tabbed_csv' do + it 'returns nested KV pair structure with the first tab as the split' do + expect(described_class.from_tabbed_csv("a\tb\tc")).to eq( + Streamingly::KV.new('a', Streamingly::KV.new('b', 'c')) + ) + end + end +end