chunker/spec/chunker_spec.rb
branchruby-modules
changeset 4 01a3332bfe0a
parent 2 e5c705047540
equal deleted inserted replaced
3:233041485364 4:01a3332bfe0a
     1 #!/usr/bin/env ruby
     1 # vim: set nosta noet ts=4 sw=4 ft=rspec:
     2 
     2 
     3 BEGIN {
     3 BEGIN {
     4 	require 'pathname'
     4 	require 'pathname'
     5 	basedir = Pathname.new( __FILE__ ).dirname.parent
     5 	basedir = Pathname.new( __FILE__ ).dirname.parent
     6 	libdir = basedir + "lib"
     6 	libdir = basedir + "lib"
     7 
     7 
     8 	$LOAD_PATH.unshift( libdir ) unless $LOAD_PATH.include?( libdir )
     8 	$LOAD_PATH.unshift( libdir ) unless $LOAD_PATH.include?( libdir )
     9 }
     9 }
    10 
    10 
       
    11 require 'rspec'
    11 require 'chunker'
    12 require 'chunker'
    12 require 'rubygems'
       
    13 require 'spec'
       
    14 
    13 
    15 ENDSTUFF = <<ENDSTUFF
    14 ENDSTUFF = <<ENDSTUFF
    16 Stuff within the end block.
    15 Stuff within the end block.
    17 
    16 
    18 Content of the END block
    17 Content of the END block
    63 __HURGADURGA__
    62 __HURGADURGA__
    64 #{HURGADURGA}
    63 #{HURGADURGA}
    65 EO_FILE_TEXT
    64 EO_FILE_TEXT
    66 
    65 
    67 
    66 
    68 describe Chunker::DataParser do
    67 describe Chunker do
    69 
    68 
    70 	it "doesn't include content above the __END__ token" do
    69 	describe Chunker::DataParser do
    71 		klass = Class.new
    70 
    72 		dp = Chunker::DataParser.new( klass, StringIO.new( FILE_TEXT_MULTIPLE ))
    71 		it "doesn't include content above the __END__ token" do
    73 		dp.instance_variable_get( :@scanner ).string.
    72 			klass = Class.new
    74 			should_not =~ /This is stuff we shouldn't see/
    73 			dp = Chunker::DataParser.new( klass, StringIO.new( FILE_TEXT_MULTIPLE ))
       
    74 			dp.instance_variable_get( :@scanner ).string.
       
    75 				should_not =~ /This is stuff we shouldn't see/
       
    76 		end
       
    77 
       
    78 		it "doesn't contain the __END__ token itself" do
       
    79 			klass = Class.new
       
    80 			dp = Chunker::DataParser.new( klass, StringIO.new( FILE_TEXT ))
       
    81 			dp.instance_variable_get( :@scanner ).string.should_not =~ /^__END__/
       
    82 		end
    75 	end
    83 	end
    76 
    84 
    77 	it "doesn't contain the __END__ token itself" do
    85 
    78 		klass = Class.new
    86 	context 'when included from another class' do
    79 		dp = Chunker::DataParser.new( klass, StringIO.new( FILE_TEXT ))
    87 
    80 		dp.instance_variable_get( :@scanner ).string.should_not =~ /^__END__/
    88 		it "has all content in DATA_END if there are no sub blocks" do
       
    89 			File.stub!( :open ).and_return( StringIO.new( FILE_TEXT ))
       
    90 			klass = Class.new { include Chunker }
       
    91 
       
    92 			klass.constants.should_not include( 'DATA_POOP' )
       
    93 			klass.constants.should_not include( 'DATA_HURRRRG' )
       
    94 			klass.constants.should_not include( 'DATA_HURGADURGA' )
       
    95 			klass.constants.should include( 'DATA_END' )
       
    96 		end
       
    97 
       
    98 		it "separates data sub blocks into individual constants" do
       
    99 			File.stub!( :open ).and_return( StringIO.new( FILE_TEXT_MULTIPLE ))
       
   100 			klass = Class.new { include Chunker }
       
   101 
       
   102 			klass.constants.should include( 'DATA_END' )
       
   103 			klass.constants.should include( 'DATA_POOP' )
       
   104 			klass.constants.should include( 'DATA_HURRRRG' )
       
   105 			klass.constants.should include( 'DATA_HURGADURGA' )
       
   106 		end
       
   107 
       
   108 		it "has IO constants that contain the data block contents" do
       
   109 			File.stub!( :open ).and_return( StringIO.new( FILE_TEXT_MULTIPLE ))
       
   110 			klass = Class.new { include Chunker }
       
   111 
       
   112 			klass.const_get( :DATA_END ).read.chomp.should        == ENDSTUFF
       
   113 			klass.const_get( :DATA_POOP ).read.chomp.should       == POOP
       
   114 			klass.const_get( :DATA_HURRRRG ).read.chomp.should    == HURRRRG
       
   115 			klass.const_get( :DATA_HURGADURGA ).read.chomp.should == HURGADURGA
       
   116 		end
    81 	end
   117 	end
    82 end
   118 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