1 # vim: set nosta noet ts=4 sw=4 ft=rspec: |
|
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 'rspec' |
|
12 require 'chunker' |
|
13 |
|
14 ENDSTUFF = <<ENDSTUFF |
|
15 Stuff within the end block. |
|
16 |
|
17 Content of the END block |
|
18 Content of the END block |
|
19 Content of the END block |
|
20 Content of the END block |
|
21 ENDSTUFF |
|
22 |
|
23 HURGADURGA = <<HURGADURGA |
|
24 |
|
25 Content of the HURGADURGA block |
|
26 Content of the HURGADURGA block |
|
27 Content of the HURGADURGA block |
|
28 Content of the HURGADURGA block |
|
29 |
|
30 HURGADURGA |
|
31 |
|
32 HURRRRG = <<HURRRRG |
|
33 123123123 123123123 123123123 |
|
34 123123123 123123123 123123123 |
|
35 123123123 123123123 123123123 |
|
36 HURRRRG |
|
37 |
|
38 POOP = <<POOP |
|
39 Content of the POOP block |
|
40 POOP |
|
41 |
|
42 FILE_TEXT = <<EO_FILE_TEXT |
|
43 |
|
44 This is stuff we shouldn't see or care about. |
|
45 You know, stuff like code, presumably. |
|
46 |
|
47 __END__ |
|
48 #{ENDSTUFF} |
|
49 EO_FILE_TEXT |
|
50 |
|
51 FILE_TEXT_MULTIPLE = <<EO_FILE_TEXT |
|
52 |
|
53 This is stuff we shouldn't see or care about. |
|
54 You know, stuff like code, presumably. |
|
55 |
|
56 __END__ |
|
57 #{ENDSTUFF} |
|
58 __POOP__ |
|
59 #{POOP} |
|
60 __HURRRRG__ |
|
61 #{HURRRRG} |
|
62 __HURGADURGA__ |
|
63 #{HURGADURGA} |
|
64 EO_FILE_TEXT |
|
65 |
|
66 |
|
67 describe Chunker do |
|
68 |
|
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 |
|
83 end |
|
84 |
|
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 |
|
117 end |
|
118 end |
|