chunker/spec/chunker_spec.rb
branchruby-modules
changeset 1 9e127bf6e84f
child 2 e5c705047540
equal deleted inserted replaced
0:83c0eed6db19 1:9e127bf6e84f
       
     1 #!/usr/bin/env ruby
       
     2 
       
     3 BEGIN {
       
     4 	require 'pathname'
       
     5 	basedir = Pathname.new( __FILE__ ).dirname.parent
       
     6 	libdir = basedir + "lib"
       
     7 
       
     8 	$LOAD_PATH.unshift( libdir ) unless $LOAD_PATH.include?( libdir )
       
     9 }
       
    10 
       
    11 require 'chunker'
       
    12 require 'rubygems'
       
    13 require 'spec'
       
    14 
       
    15 ENDSTUFF = <<ENDSTUFF
       
    16 Stuff within the end block.
       
    17 
       
    18 Content of the END block
       
    19 Content of the END block
       
    20 Content of the END block
       
    21 Content of the END block
       
    22 ENDSTUFF
       
    23 
       
    24 HURGADURGA = <<HURGADURGA
       
    25 
       
    26 Content of the HURGADURGA block
       
    27 Content of the HURGADURGA block
       
    28 Content of the HURGADURGA block
       
    29 Content of the HURGADURGA block
       
    30 
       
    31 HURGADURGA
       
    32 
       
    33 HURRRRG = <<HURRRRG
       
    34 	123123123 123123123 123123123
       
    35 	123123123 123123123 123123123
       
    36 	123123123 123123123 123123123
       
    37 HURRRRG
       
    38 
       
    39 POOP = <<POOP
       
    40 Content of the POOP block
       
    41 POOP
       
    42 
       
    43 FILE_TEXT = <<EO_FILE_TEXT
       
    44 
       
    45 This is stuff we shouldn't see or care about.
       
    46 You know, stuff like code, presumably.
       
    47 
       
    48 __END__
       
    49 #{ENDSTUFF}
       
    50 EO_FILE_TEXT
       
    51 
       
    52 FILE_TEXT_MULTIPLE = <<EO_FILE_TEXT
       
    53 
       
    54 This is stuff we shouldn't see or care about.
       
    55 You know, stuff like code, presumably.
       
    56 
       
    57 __END__
       
    58 #{ENDSTUFF}
       
    59 __POOP__
       
    60 #{POOP}
       
    61 __HURRRRG__
       
    62 #{HURRRRG}
       
    63 __HURGADURGA__
       
    64 #{HURGADURGA}
       
    65 EO_FILE_TEXT
       
    66 
       
    67 
       
    68 describe Chunker::DataParser do
       
    69 
       
    70 	it "doesn't include content above the __END__ marker" do
       
    71 		klass = Class.new
       
    72 		dp = Chunker::DataParser.new( klass, StringIO.new( FILE_TEXT_MULTIPLE ))
       
    73 		dp.instance_variable_get( :@scanner ).string.
       
    74 			should_not =~ /This is stuff we shouldn't see/
       
    75 	end
       
    76 
       
    77 	it "doesn't contain the __END__ marker itself" do
       
    78 		klass = Class.new
       
    79 		dp = Chunker::DataParser.new( klass, StringIO.new( FILE_TEXT ))
       
    80 		dp.instance_variable_get( :@scanner ).string.should_not =~ /^__END__/
       
    81 	end
       
    82 end
       
    83 
       
    84 
       
    85 describe 'A class that includes Chunker' do
       
    86 
       
    87 	it "has all content in DATA_END if there are no sub blocks" do
       
    88 		File.stub!( :open ).and_return( StringIO.new( FILE_TEXT ))
       
    89 		klass = Class.new { include Chunker }
       
    90 
       
    91 		klass.constants.should_not include( 'DATA_POOP' )
       
    92 		klass.constants.should_not include( 'DATA_HURRRRG' )
       
    93 		klass.constants.should_not include( 'DATA_HURGADURGA' )
       
    94 		klass.constants.should include( 'DATA_END' )
       
    95 	end
       
    96 
       
    97 	it "separates data sub blocks into individual constants" do
       
    98 		File.stub!( :open ).and_return( StringIO.new( FILE_TEXT_MULTIPLE ))
       
    99 		klass = Class.new { include Chunker }
       
   100 
       
   101 		klass.constants.should include( 'DATA_END' )
       
   102 		klass.constants.should include( 'DATA_POOP' )
       
   103 		klass.constants.should include( 'DATA_HURRRRG' )
       
   104 		klass.constants.should include( 'DATA_HURGADURGA' )
       
   105 	end
       
   106 
       
   107 	it "has IO constants that contain the data block contents" do
       
   108 		File.stub!( :open ).and_return( StringIO.new( FILE_TEXT_MULTIPLE ))
       
   109 		klass = Class.new { include Chunker }
       
   110 
       
   111 		klass.const_get( :DATA_END ).read.chomp.should        == ENDSTUFF
       
   112 		klass.const_get( :DATA_POOP ).read.chomp.should       == POOP
       
   113 		klass.const_get( :DATA_HURRRRG ).read.chomp.should    == HURRRRG
       
   114 		klass.const_get( :DATA_HURGADURGA ).read.chomp.should == HURGADURGA
       
   115 	end
       
   116 end
       
   117