Initial commit of chunker, a ruby module to aid with data blocks.
#!/usr/bin/env ruby
BEGIN {
require 'pathname'
basedir = Pathname.new( __FILE__ ).dirname.parent
libdir = basedir + "lib"
$LOAD_PATH.unshift( libdir ) unless $LOAD_PATH.include?( libdir )
}
require 'chunker'
require 'rubygems'
require 'spec'
ENDSTUFF = <<ENDSTUFF
Stuff within the end block.
Content of the END block
Content of the END block
Content of the END block
Content of the END block
ENDSTUFF
HURGADURGA = <<HURGADURGA
Content of the HURGADURGA block
Content of the HURGADURGA block
Content of the HURGADURGA block
Content of the HURGADURGA block
HURGADURGA
HURRRRG = <<HURRRRG
123123123 123123123 123123123
123123123 123123123 123123123
123123123 123123123 123123123
HURRRRG
POOP = <<POOP
Content of the POOP block
POOP
FILE_TEXT = <<EO_FILE_TEXT
This is stuff we shouldn't see or care about.
You know, stuff like code, presumably.
__END__
#{ENDSTUFF}
EO_FILE_TEXT
FILE_TEXT_MULTIPLE = <<EO_FILE_TEXT
This is stuff we shouldn't see or care about.
You know, stuff like code, presumably.
__END__
#{ENDSTUFF}
__POOP__
#{POOP}
__HURRRRG__
#{HURRRRG}
__HURGADURGA__
#{HURGADURGA}
EO_FILE_TEXT
describe Chunker::DataParser do
it "doesn't include content above the __END__ marker" do
klass = Class.new
dp = Chunker::DataParser.new( klass, StringIO.new( FILE_TEXT_MULTIPLE ))
dp.instance_variable_get( :@scanner ).string.
should_not =~ /This is stuff we shouldn't see/
end
it "doesn't contain the __END__ marker itself" do
klass = Class.new
dp = Chunker::DataParser.new( klass, StringIO.new( FILE_TEXT ))
dp.instance_variable_get( :@scanner ).string.should_not =~ /^__END__/
end
end
describe 'A class that includes Chunker' do
it "has all content in DATA_END if there are no sub blocks" do
File.stub!( :open ).and_return( StringIO.new( FILE_TEXT ))
klass = Class.new { include Chunker }
klass.constants.should_not include( 'DATA_POOP' )
klass.constants.should_not include( 'DATA_HURRRRG' )
klass.constants.should_not include( 'DATA_HURGADURGA' )
klass.constants.should include( 'DATA_END' )
end
it "separates data sub blocks into individual constants" do
File.stub!( :open ).and_return( StringIO.new( FILE_TEXT_MULTIPLE ))
klass = Class.new { include Chunker }
klass.constants.should include( 'DATA_END' )
klass.constants.should include( 'DATA_POOP' )
klass.constants.should include( 'DATA_HURRRRG' )
klass.constants.should include( 'DATA_HURGADURGA' )
end
it "has IO constants that contain the data block contents" do
File.stub!( :open ).and_return( StringIO.new( FILE_TEXT_MULTIPLE ))
klass = Class.new { include Chunker }
klass.const_get( :DATA_END ).read.chomp.should == ENDSTUFF
klass.const_get( :DATA_POOP ).read.chomp.should == POOP
klass.const_get( :DATA_HURRRRG ).read.chomp.should == HURRRRG
klass.const_get( :DATA_HURGADURGA ).read.chomp.should == HURGADURGA
end
end