Skip to content

Commit 3e9c0ad

Browse files
author
Hugh McGowan
committed
refactor bookmark and fix bug in fixture runner
1 parent f580f3d commit 3e9c0ad

File tree

4 files changed

+77
-56
lines changed

4 files changed

+77
-56
lines changed

examples/fixtures/MathFunctions.rb

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ class MathFunctions
66
def before_all
77
raise RuntimeError
88
end
9-
def before_each
10-
true
11-
end
129
def add
1310
x+y
1411
end

examples/fixtures/StringFunctions.rb

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
class StringFunctions
44
include Rasta::Fixture::ClassicFixture
55
attr_accessor :phrase, :searchterm
6+
def before_each
7+
raise
8+
end
69
def chop
710
phrase.chop
811
end

lib/rasta/bookmark.rb

+73-52
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,78 @@ module Rasta
33
class BookmarkError < RuntimeError; end
44

55
class Bookmark
6-
attr_accessor :page_count, :max_page_count
7-
attr_accessor :record_count, :max_record_count, :continue
6+
attr_accessor :page_count, :max_page_count, :page, :record
7+
attr_accessor :record_count, :max_record_count, :bookmark
88

99
def initialize(options = {})
1010
@page_count = 0
1111
@record_count = 0
1212
@max_page_count = options[:pages] || 0
1313
@max_record_count = options[:records] || 0
14-
@continue = options[:continue] || false
15-
@found_bookmark_page = false
16-
@found_bookmark_record = false
17-
if @continue
18-
@bookmark_page, @bookmark_record = parse_bookmark(@continue)
19-
@found_bookmark_record = true unless @bookmark_record
14+
@bookmark = options[:bookmark] unless (@bookmark && @bookmark.empty?)
15+
initialize_finders
16+
read_bookmark if @bookmark
17+
end
18+
19+
def initialize_finders
20+
if @bookmark
21+
@found_page = false
22+
@found_record = false
2023
else
21-
@found_bookmark_page = true
22-
@found_bookmark_record = true
24+
# No bookmark exists so act as if the first page and
25+
# record is the bookmark so all records are seen
26+
@found_page = true
27+
@found_record = true
2328
end
29+
end
30+
31+
def read_bookmark
32+
if @bookmark =~ /^([^\[]+)(\[(\S+)\])?/
33+
self.page = $1
34+
self.record = $3
35+
else
36+
raise BookmarkError, "Invalid bookmark name '#{@bookmark}'"
37+
end
2438
end
25-
39+
40+
def page=(x)
41+
@page = x
42+
end
43+
44+
def record=(x)
45+
@record = x
46+
case @record
47+
when bookmark_column
48+
@record = GenericSpreadsheet.letter_to_number(@record)
49+
when bookmark_row
50+
@record = @record.to_i
51+
when nil
52+
@found_record = true # no record specified, but valid bookmark
53+
else
54+
raise BookmarkError, "Invalid record #{x} for bookmark '#{@bookmark}'"
55+
end
56+
end
57+
58+
def bookmark_column
59+
/\A[a-z]+\Z/i
60+
end
61+
62+
def bookmark_row
63+
/\A\d+\Z/
64+
end
65+
66+
def page_name
67+
@page =~ /^([^#]+)/
68+
$1
69+
end
70+
71+
def exists?(roo)
72+
roo.sheets.include?(page_name)
73+
end
74+
2675
def found_page?(page)
27-
if @found_bookmark_page || page == @bookmark_page
28-
@found_bookmark_page = true
76+
if @found_page || page == @page
77+
@found_page = true
2978
@page_count += 1
3079
true
3180
else
@@ -34,54 +83,26 @@ def found_page?(page)
3483
end
3584

3685
def found_record?(record)
37-
if @found_bookmark_record || record == @bookmark_record
38-
@found_bookmark_record = true
86+
if @found_record || record == @record
87+
@found_record = true
3988
@record_count += 1
4089
true
4190
else
4291
false
4392
end
44-
4593
end
4694

4795
def exceeded_max_records?
48-
return false if @max_record_count == 0 and @max_page_count == 0
49-
return true if (@record_count > @max_record_count) and @max_record_count > 0
50-
return true if (@page_count > @max_page_count) and @max_page_count > 0
51-
return false
52-
end
53-
54-
def valid_bookmark
55-
/^([^\[]+)(\[(\S+)\])?/
56-
end
57-
58-
def column_record
59-
/\A[a-z]+\Z/i
60-
end
61-
62-
def row_record
63-
/\A\d+\Z/
64-
end
65-
66-
def parse_bookmark(name)
67-
return [nil,nil] if name.nil?
68-
if name =~ valid_bookmark
69-
pagename = $1
70-
record = $3
71-
case record
72-
when column_record
73-
record = GenericSpreadsheet.letter_to_number(record)
74-
when row_record
75-
record = record.to_i
76-
when nil
77-
# no record set, which is fine
78-
else
79-
raise BookmarkError, "Invalid record name for bookmark '#{name}'"
80-
end
81-
return pagename, record
82-
else
83-
raise BookmarkError, "Invalid bookmark name '#{name}'"
96+
if @max_record_count == 0 and @max_page_count == 0
97+
false
98+
elsif (@record_count > @max_record_count) and @max_record_count > 0
99+
true
100+
elsif (@page_count > @max_page_count) and @max_page_count > 0
101+
true
102+
else
103+
false
84104
end
85105
end
106+
86107
end
87108
end

lib/rasta/fixture_runner.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def run_test_fixtures
111111
@roo = Roo::Spreadsheet.open(spreadsheet)
112112
Spec::Runner.options.reporter.roo = @roo
113113
@bookmark = Rasta::Bookmark.new(@options)
114-
check_for_valid_page @bookmark
114+
check_for_valid_page
115115
@loader = ClassLoader.new(@options[:fixture_path])
116116
@loader.load_test_fixtures
117117
@roo.sheets.each do |sheet|

0 commit comments

Comments
 (0)