Sha256: 1fee898ca128420107fe58f8ecd670aedd34cde9b53edecb6cb474349533b829

Contents?: true

Size: 1.62 KB

Versions: 4

Compression:

Stored size: 1.62 KB

Contents

require File.join(File.dirname(__FILE__), 'spec_helper')

describe FixedWidth::Generator do
  before(:each) do
    @definition = FixedWidth.define :test do |d|
      d.header do |h|
        h.trap { |line| line[0,4] == 'HEAD' }
        h.column :type, 4
        h.column :file_id, 10
      end
      d.body do |b|
        b.trap { |line| line[0,4] =~ /[^(HEAD|FOOT)]/ }
        b.column :first, 10
        b.column :last, 10
      end
      d.footer do |f|
        f.trap { |line| line[0,4] == 'FOOT' }
        f.column :type, 4
        f.column :file_id, 10
      end
    end
    @data = {
      :header => [ {:type => "HEAD", :file_id => "1" } ],
      :body => [ 
        {:first => "Paul", :last => "Hewson" },
        {:first => "Dave", :last => "Evans" }
      ],
      :footer => [ {:type => "FOOT", :file_id => "1" }]
    }
    @generator = FixedWidth::Generator.new(@definition)
  end

  it "should raise an error if there is no data for a required section" do
    @data.delete :header
    lambda {  @generator.generate(@data) }.should raise_error(FixedWidth::RequiredSectionEmptyError, "Required section 'header' was empty.")
  end

  it "should generate a string" do
    expected = "HEAD         1\n      Paul    Hewson\n      Dave     Evans\nFOOT         1"
    @generator.generate(@data).should == expected
  end
  
  it "should handle lazy data declaration (no array around single record for a section)" do
    expected = "HEAD         1\n      Paul    Hewson\n      Dave     Evans\nFOOT         1"
    @data[:header] = @data[:header].first
    @generator.generate(@data).should == expected
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
fixed_width-multibyte-0.2.3 spec/generator_spec.rb
fixed_width-multibyte-0.2.2 spec/generator_spec.rb
fixed_width-0.2.1 spec/generator_spec.rb
fixed_width-0.2.0 spec/generator_spec.rb