Changeset 01a3332bfe0a
- Timestamp:
- 01/21/11 19:41:16 (16 months ago)
- Branch:
- ruby-modules
- Files:
-
- 2 added
- 3 edited
-
.hgignore (added)
-
chunker/.rspec (added)
-
chunker/Rakefile (modified) (6 diffs)
-
chunker/lib/chunker.rb (modified) (7 diffs)
-
chunker/spec/chunker_spec.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
chunker/Rakefile
r3 r4 1 1 #!/usr/bin/env rake 2 #3 # Chunker Rakefile4 2 # 5 3 … … 8 6 9 7 require 'rake' 10 require ' spec'11 require 'r ake/testtask'8 require 'rspec' 9 require 'rspec/core/rake_task' 12 10 require 'rake/packagetask' 13 11 require 'rake/gempackagetask' 14 require 'spec/rake/spectask'15 12 require 'rubygems/installer' 16 13 require 'rubygems/uninstaller' … … 26 23 27 24 SPECDIR = BASEDIR + 'spec' 28 SPEC_FILES = Pathname.glob( SPECDIR + '**/*_spec.rb' ) .reject {|f| f =~ /^\.svn/ }25 SPEC_FILES = Pathname.glob( SPECDIR + '**/*_spec.rb' ) 29 26 30 27 LIBDIR = BASEDIR + 'lib' 31 LIB_FILES = Pathname.glob( LIBDIR + '**/*.rb') .reject {|i| i =~ /\.svn/ }28 LIB_FILES = Pathname.glob( LIBDIR + '**/*.rb') 32 29 33 30 RELEASE_FILES = TEXT_FILES + LIB_FILES + SPEC_FILES … … 57 54 58 55 PKG_NAME = 'chunker' 59 PKG_VERSION = find_pattern( LIBDIR + 'chunker.rb', /VERSION = ['"](\d\.\d(?:\/\d)?)['"]/ ) 60 PKG_REVISION = find_pattern( LIBDIR + 'chunker.rb', /SVNRev = .+Rev: (\d+)/ ) 61 PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}.#{PKG_REVISION}" 56 PKG_VERSION = find_pattern( LIBDIR + 'chunker.rb', /VERSION = ['"](.+)['"]/ ) 57 PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}" 62 58 63 59 … … 66 62 ###################################################################### 67 63 68 task :default => [ :test, :package ] 64 task :test => 'test:spec' 65 task :default => :test 66 # task :default => [ :test, :package ] 69 67 68 namespace :test do 69 desc 'Generate verbose and pretty output' 70 RSpec::Core::RakeTask.new( :spec ) do |task| 71 task.pattern = SPEC_FILES 72 task.rspec_opts = ['-b', '-fd', '-c'] 73 end 70 74 71 ### Task: run rspec tests 72 ### 73 desc "Run tests" 74 Spec::Rake::SpecTask.new('test') do |task| 75 task.spec_files = SPEC_FILES 76 task.spec_opts = %w{ -c -fs } 75 desc 'Generate quiet non-colored plain-text output' 76 RSpec::Core::RakeTask.new( :quiet ) do |task| 77 task.pattern = SPEC_FILES 78 task.rspec_opts = ['-f', 'p'] 79 end 77 80 end 78 79 81 80 82 ### Task: generate ctags … … 90 92 ### 91 93 gem = Gem::Specification.new do |gem| 92 pkg_build = PKG_REVISION || 093 94 94 gem.summary = "A convenience library for parsing __END__ tokens consistently." 95 95 gem.name = PKG_NAME 96 gem.version = "%s.%s" % [ PKG_VERSION, pkg_build ]96 gem.version = PKG_VERSION 97 97 gem.author = 'Mahlon E. Smith' 98 98 gem.email = 'mahlon@martini.nu' 99 99 gem.homepage = 'http://projects.martini.nu/ruby-modules/wiki/Chunker' 100 gem.rubyforge_project = 'mahlon'101 100 gem.has_rdoc = true 102 101 -
chunker/lib/chunker.rb
r2 r4 1 #!/usr/bin/ruby 1 # vim: set nosta noet ts=4 sw=4: 2 3 require 'strscan' 4 require 'stringio' 5 2 6 # 3 7 # Chunker: A convenience library for parsing __END__ tokens consistently. … … 18 22 module Chunker 19 23 20 require 'strscan'21 require 'stringio'24 # VCS Revision 25 VCSRev = %q$Rev$ 22 26 23 # SVN Revision 24 # 25 SVNRev = %q$Rev$ 26 27 # SVN Id 28 # 29 SVNId = %q$Id$ 27 # VCS Id 28 VCSId = %q$Id$ 30 29 31 30 # Package version 32 # 33 VERSION = '0.1' 31 VERSION = '1.0.0' 34 32 35 33 … … 41 39 42 40 # The mark for a DATA block. 43 #44 41 END_TOKEN = /^__END__\r?\n/ 45 42 46 43 # The mark for a 'sub' block. 47 #48 44 CHUNK_TOKEN = /^__([A-Z\_0-9]+)__\r?\n/ 49 45 … … 60 56 io.close 61 57 58 # put each chunk into its own constant 59 # 62 60 if @scanner.check_until( CHUNK_TOKEN ) 63 # put each chunk into its own constant64 61 self.extract_blocks 62 63 # no sub blocks, put the whole mess into DATA_END 64 # 65 65 else 66 # no sub blocks, put the whole mess into DATA_END67 66 @klass.const_set( :DATA_END, StringIO.new( end_string ) ) 68 67 end … … 94 93 label = @scanner[1] 95 94 95 # Pull the next token text out of the data, set up the next pass 96 # 96 97 if data = @scanner.scan_until( CHUNK_TOKEN ) 97 # Pull the next token text out of the data, set up the next pass 98 # 99 data = data[ 0, data.length - @scanner[0].length ] 98 data = data[ 0, data.length - @scanner[0].length ] 100 99 @scanner.pos = self.next_position 100 101 # No additional blocks 102 # 101 103 else 102 # No additional blocks103 #104 104 data = @scanner.rest 105 105 end … … 107 107 108 108 # Add the IO constant to the class that included me. 109 #110 109 @klass.const_set( "DATA_#{label}".to_sym, StringIO.new( data ) ) 111 110 end … … 127 126 # klass.instance_eval{ __FILE__ } awww, nope. 128 127 # __FILE__ won't work here, so we find the filename via caller(). 129 #130 128 io = File.open( caller(1).last.sub(/:.*?$/, ''), 'r' ) 131 129 -
chunker/spec/chunker_spec.rb
r2 r4 1 # !/usr/bin/env ruby1 # vim: set nosta noet ts=4 sw=4 ft=rspec: 2 2 3 3 BEGIN { … … 9 9 } 10 10 11 require 'rspec' 11 12 require 'chunker' 12 require 'rubygems'13 require 'spec'14 13 15 14 ENDSTUFF = <<ENDSTUFF … … 66 65 67 66 68 describe Chunker ::DataParserdo67 describe Chunker do 69 68 70 it "doesn't include content above the __END__ token" 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/ 69 describe Chunker::DataParser do 70 71 it "doesn't include content above the __END__ token" do 72 klass = Class.new 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 83 end 76 84 77 it "doesn't contain the __END__ token 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__/ 85 86 context 'when included from another class' do 87 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 117 end 82 118 end 83 84 85 describe 'A class that includes Chunker' do86 87 it "has all content in DATA_END if there are no sub blocks" do88 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 end96 97 it "separates data sub blocks into individual constants" do98 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 end106 107 it "has IO constants that contain the data block contents" do108 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 == ENDSTUFF112 klass.const_get( :DATA_POOP ).read.chomp.should == POOP113 klass.const_get( :DATA_HURRRRG ).read.chomp.should == HURRRRG114 klass.const_get( :DATA_HURGADURGA ).read.chomp.should == HURGADURGA115 end116 end117
Note: See TracChangeset
for help on using the changeset viewer.
