Skip to content

Commit f580f3d

Browse files
author
Hugh McGowan
committed
refactor bookmark to simplify and improve checks for invalid bookmarks
1 parent ac2a5e2 commit f580f3d

File tree

4 files changed

+75
-47
lines changed

4 files changed

+75
-47
lines changed

Diff for: lib/rasta.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ def add_choices(builder)
1515
builder.add_choice(:results_path, :type=>:string, :default=>'rasta_test_results') do | command_line |
1616
command_line.uses_option("-r", "--results-path PATH", "Location of test results.")
1717
end
18-
builder.add_choice(:continue, :type=>:string, :default=>nil) do | command_line |
19-
command_line.uses_option("-c", "--continue BOOKMARK", "Continue spreadsheet from a give bookmark.")
18+
builder.add_choice(:bookmark, :type=>:string, :default=>nil) do | command_line |
19+
command_line.uses_option("-c", "--continue BOOKMARK", "Continue spreadsheet from a given bookmark.")
2020
end
2121
builder.add_choice(:pages, :type=>:integer, :default=>0) do | command_line |
2222
command_line.uses_option("--pages [COUNT]", "Number of pages to process.")

Diff for: lib/rasta/fixture_runner.rb

+16-8
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,17 @@ def underscore(camel_cased_word)
108108

109109
def run_test_fixtures
110110
@options[:spreadsheets].each do |spreadsheet|
111-
roo = Roo::Spreadsheet.open(spreadsheet)
112-
Spec::Runner.options.reporter.roo = roo
113-
111+
@roo = Roo::Spreadsheet.open(spreadsheet)
112+
Spec::Runner.options.reporter.roo = @roo
113+
@bookmark = Rasta::Bookmark.new(@options)
114+
check_for_valid_page @bookmark
114115
@loader = ClassLoader.new(@options[:fixture_path])
115116
@loader.load_test_fixtures
116-
bookmark = Rasta::Bookmark.new(@options)
117-
roo.sheets.each do |sheet|
117+
@roo.sheets.each do |sheet|
118118
next if sheet =~ /^#/ #skip sheets that are only comments
119119
begin
120-
roo.default_sheet = sheet
121-
base_sheet_name = roo.default_sheet.gsub(/#.*/, '')
120+
@roo.default_sheet = sheet
121+
base_sheet_name = @roo.default_sheet.gsub(/#.*/, '')
122122
classname = @loader.find_class_by_name(base_sheet_name)
123123
fixture = classname.new
124124
rescue ArgumentError => e
@@ -127,13 +127,21 @@ def run_test_fixtures
127127
if @options[:extend_fixture]
128128
fixture.extend( @options[:extend_fixture])
129129
end
130-
fixture.initialize_fixture(roo, bookmark)
130+
fixture.initialize_fixture(@roo, @bookmark)
131131
fixture.execute_worksheet
132132
end
133133
end
134134
end
135135
private :run_test_fixtures
136136

137+
def check_for_valid_page
138+
if @bookmark.page
139+
raise BookmarkError, "Unable to find worksheet: #{@bookmark.page}" unless @bookmark.exists?(@roo)
140+
end
141+
@bookmark
142+
end
143+
private :check_for_valid_page
144+
137145
def stop_rspec
138146
Spec::Runner.options.reporter.original_dump if Spec::Runner.options
139147
Spec::Runner.options.clear_format_options

Diff for: spec/bookmark_spec.rb

+37-21
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
@bookmark.max_page_count.should == 0
1616
@bookmark.record_count.should == 0
1717
@bookmark.max_record_count.should == 0
18-
@bookmark.continue.should == false
18+
@bookmark.bookmark.should be_nil
1919
end
2020
it 'should always find the page' do
2121
@bookmark.found_page?('foo').should == true
@@ -30,29 +30,29 @@
3030

3131
describe 'Bookmark with commandline options' do
3232
it 'should be able to find a bookmarked page' do
33-
@bookmark = Rasta::Bookmark.new({:continue => 'MyPage'})
33+
@bookmark = Rasta::Bookmark.new({:bookmark => 'MyPage'})
3434
@bookmark.found_page?('foo').should == false
3535
@bookmark.found_page?('MyPage').should == true
3636
@bookmark.found_record?(3).should == true
3737
@bookmark.exceeded_max_records?.should == false
3838
end
3939
it 'should be able to find a bookmarked page with record' do
40-
@bookmark = Rasta::Bookmark.new({:continue => 'MyPage[D]'})
40+
@bookmark = Rasta::Bookmark.new({:bookmark => 'MyPage[D]'})
4141
@bookmark.found_page?('foo').should == false
4242
@bookmark.found_page?('MyPage').should == true
4343
@bookmark.found_record?(3).should == false
4444
@bookmark.found_record?(4).should == true
4545
@bookmark.exceeded_max_records?.should == false
4646
end
47-
it 'should be able to continue given number of pages' do
47+
it 'should be able to bookmark given number of pages' do
4848
@bookmark = Rasta::Bookmark.new({:pages => 1})
4949
@bookmark.found_page?('MyPage').should == true
5050
@bookmark.found_record?(4).should == true
5151
@bookmark.exceeded_max_records?.should == false # pages = 1
5252
@bookmark.page_count += 1
5353
@bookmark.exceeded_max_records?.should == true # pages = 2
5454
end
55-
it 'should be able to continue a given number of records' do
55+
it 'should be able to bookmark a given number of records' do
5656
@bookmark = Rasta::Bookmark.new({:records => 1})
5757
@bookmark.found_page?('MyPage').should == true
5858
@bookmark.found_record?(4).should == true
@@ -66,36 +66,52 @@
6666
before :all do
6767
@bookmark = Rasta::Bookmark.new
6868
end
69-
it 'should parse an empty bookmark' do
70-
@bookmark.parse_bookmark(nil).should == [nil,nil]
71-
end
7269
it 'should parse a page without a record' do
73-
@bookmark.parse_bookmark('MyPage').should == ['MyPage',nil]
70+
@bookmark.send 'bookmark=', 'MyPage'
71+
@bookmark.read_bookmark
72+
@bookmark.page.should == 'MyPage'
73+
@bookmark.record.should == nil
7474
end
7575
it 'should parse a page with a numeric record' do
76-
@bookmark.parse_bookmark('MyPage[1]').should == ['MyPage',1]
76+
@bookmark.send 'bookmark=', 'MyPage[1]'
77+
@bookmark.read_bookmark
78+
@bookmark.page.should == 'MyPage'
79+
@bookmark.record.should == 1
7780
end
7881
it 'should parse a page with an alpha record' do
79-
@bookmark.parse_bookmark('MyPage[A]').should == ['MyPage',1]
82+
@bookmark.send 'bookmark=', 'MyPage[A]'
83+
@bookmark.read_bookmark
84+
@bookmark.page.should == 'MyPage'
85+
@bookmark.record.should == 1
8086
end
8187
it 'should parse a page with an extended alpha record' do
82-
@bookmark.parse_bookmark('MyPage[AA]').should == ['MyPage',27]
88+
@bookmark.send 'bookmark=', 'MyPage[AA]'
89+
@bookmark.read_bookmark
90+
@bookmark.page.should == 'MyPage'
91+
@bookmark.record.should == 27
8392
end
8493
it 'should handle lowercase records' do
85-
@bookmark.parse_bookmark('MyPage[aa]').should == ['MyPage',27]
94+
@bookmark.send 'bookmark=', 'MyPage[aa]'
95+
@bookmark.read_bookmark
96+
@bookmark.page.should == 'MyPage'
97+
@bookmark.record.should == 27
8698
end
8799
it 'should parse a page with an embedded comment' do
88-
@bookmark.parse_bookmark('MyPage#comment').should == ['MyPage#comment',nil]
100+
@bookmark.send 'bookmark=', 'MyPage#comment'
101+
@bookmark.read_bookmark
102+
@bookmark.page.should == 'MyPage#comment'
103+
@bookmark.record.should == nil
89104
end
90105
it 'should raise an exception for an invalid bookmark' do
91-
lambda{ @bookmark.parse_bookmark("[1]") }.should raise_error(Rasta::BookmarkError)
106+
lambda{
107+
@bookmark.send 'bookmark=', '[]'
108+
@bookmark.read_bookmark
109+
}.should raise_error(Rasta::BookmarkError)
92110
end
93111
it 'should raise an exception for an invalid bookmark record' do
94-
lambda{ @bookmark.parse_bookmark("MyPage[foo1]") }.should raise_error(Rasta::BookmarkError)
112+
lambda{
113+
@bookmark.send 'bookmark=', 'MyPage[foo1]'
114+
@bookmark.read_bookmark
115+
}.should raise_error(Rasta::BookmarkError)
95116
end
96117
end
97-
98-
describe 'Bookmark parsing on worksheet' do
99-
it 'should handle bookmarks to invalid worksheets'
100-
it 'should handle bookmarks to invalid records'
101-
end

Diff for: spec/options_spec.rb

+20-16
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def stop_rspec
5959
end
6060
end
6161

62-
describe 'continue from bookmark' do
62+
describe 'bookmark from bookmark' do
6363
it_should_behave_like 'rasta_options'
6464

6565
it 'Should count the correct number of records when parsing' do
@@ -69,7 +69,7 @@ def stop_rspec
6969
@test_fixture.rasta_metrics['MathFunctions#pending'].record_count.should == 7
7070
end
7171
it 'Should be able to start from a tab' do
72-
@options.merge!(:continue=>'StringFunctions')
72+
@options.merge!(:bookmark=>'StringFunctions')
7373
Rasta::FixtureRunner.new(@options).execute
7474
@test_fixture.rasta_metrics['MathFunctions'].record_count.should == 0
7575
@test_fixture.rasta_metrics['StringFunctions'].record_count.should == 3
@@ -83,7 +83,7 @@ def stop_rspec
8383
@test_fixture.rasta_metrics['MathFunctions#pending'].record_count.should == 0
8484
end
8585
it 'Should be able to do n pages from a tab' do
86-
@options.merge!(:pages=>2, :continue=>'StringFunctions')
86+
@options.merge!(:pages=>2, :bookmark=>'StringFunctions')
8787
Rasta::FixtureRunner.new(@options).execute
8888
@test_fixture.rasta_metrics['MathFunctions'].record_count.should == 0
8989
@test_fixture.rasta_metrics['StringFunctions'].record_count.should == 3
@@ -97,52 +97,56 @@ def stop_rspec
9797
@test_fixture.rasta_metrics['MathFunctions#pending'].record_count.should == 0
9898
end
9999
it 'Should be able to do n records from a page' do
100-
@options.merge!(:records=>3, :continue=>'MathFunctions#pending')
100+
@options.merge!(:records=>3, :bookmark=>'MathFunctions#pending')
101101
Rasta::FixtureRunner.new(@options).execute
102102
@test_fixture.rasta_metrics['MathFunctions'].record_count.should == 0
103103
@test_fixture.rasta_metrics['StringFunctions'].record_count.should == 0
104104
@test_fixture.rasta_metrics['MathFunctions#pending'].record_count.should == 3
105105
end
106-
it 'Should be able to continue from a page row' do
107-
@options.merge!(:continue=>'MathFunctions#pending[5]')
106+
it 'Should be able to bookmark from a page row' do
107+
@options.merge!(:bookmark=>'MathFunctions#pending[5]')
108108
Rasta::FixtureRunner.new(@options).execute
109109
@test_fixture.rasta_metrics['MathFunctions'].record_count.should == 0
110110
@test_fixture.rasta_metrics['StringFunctions'].record_count.should == 0
111111
@test_fixture.rasta_metrics['MathFunctions#pending'].record_count.should == 4
112112
end
113-
it 'Should be able to continue from a page row with n records' do
114-
@options.merge!(:continue=>'MathFunctions#pending[5]', :records=>2)
113+
it 'Should be able to bookmark from a page row with n records' do
114+
@options.merge!(:bookmark=>'MathFunctions#pending[5]', :records=>2)
115115
Rasta::FixtureRunner.new(@options).execute
116116
@test_fixture.rasta_metrics['MathFunctions'].record_count.should == 0
117117
@test_fixture.rasta_metrics['StringFunctions'].record_count.should == 0
118118
@test_fixture.rasta_metrics['MathFunctions#pending'].record_count.should == 2
119119
end
120-
it 'Should be able to continue from a page row with n pages' do
121-
@options.merge!(:continue=>'MathFunctions[5]', :pages=>2)
120+
it 'Should be able to bookmark from a page row with n pages' do
121+
@options.merge!(:bookmark=>'MathFunctions[5]', :pages=>2)
122122
Rasta::FixtureRunner.new(@options).execute
123123
@test_fixture.rasta_metrics['MathFunctions'].record_count.should == 4
124124
@test_fixture.rasta_metrics['StringFunctions'].record_count.should == 3
125125
@test_fixture.rasta_metrics['MathFunctions#pending'].record_count.should == 0
126126
end
127-
it 'Should be able to continue from a page column' do
128-
@options.merge!(:continue=>'StringFunctions[D]')
127+
it 'Should be able to bookmark from a page column' do
128+
@options.merge!(:bookmark=>'StringFunctions[D]')
129129
Rasta::FixtureRunner.new(@options).execute
130130
@test_fixture.rasta_metrics['MathFunctions'].record_count.should == 0
131131
@test_fixture.rasta_metrics['StringFunctions'].record_count.should == 1
132132
@test_fixture.rasta_metrics['MathFunctions#pending'].record_count.should == 7
133133
end
134-
it 'Should be able to continue from a page column with n records' do
135-
@options.merge!(:continue=>'StringFunctions[D]', :records=>3)
134+
it 'Should be able to bookmark from a page column with n records' do
135+
@options.merge!(:bookmark=>'StringFunctions[D]', :records=>3)
136136
Rasta::FixtureRunner.new(@options).execute
137137
@test_fixture.rasta_metrics['MathFunctions'].record_count.should == 0
138138
@test_fixture.rasta_metrics['StringFunctions'].record_count.should == 1
139139
@test_fixture.rasta_metrics['MathFunctions#pending'].record_count.should == 2
140140
end
141-
it 'Should be able to continue from a page column with n pages' do
142-
@options.merge!(:continue=>'StringFunctions[D]', :pages=>1)
141+
it 'Should be able to bookmark from a page column with n pages' do
142+
@options.merge!(:bookmark=>'StringFunctions[D]', :pages=>1)
143143
Rasta::FixtureRunner.new(@options).execute
144144
@test_fixture.rasta_metrics['MathFunctions'].record_count.should == 0
145145
@test_fixture.rasta_metrics['StringFunctions'].record_count.should == 1
146146
@test_fixture.rasta_metrics['MathFunctions#pending'].record_count.should == 0
147147
end
148+
it 'Should fail gracefully when the bookmark page is invalid' do
149+
@options.merge!(:bookmark=>'MissingPage')
150+
lambda{ Rasta::FixtureRunner.new(@options).execute }.should raise_error
151+
end
148152
end

0 commit comments

Comments
 (0)