Skip to content

Commit

Permalink
cqfn#96: allow markers on contiguous lines
Browse files Browse the repository at this point in the history
  • Loading branch information
mbao01 committed Jul 12, 2021
1 parent 9b7e2b3 commit 99eb17d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
15 changes: 10 additions & 5 deletions lib/pdd/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require_relative '../pdd/puzzle'

module PDD
MARKERS = ["\x40todo", 'TODO:?'].freeze
# Source.
class Source
# Ctor.
Expand All @@ -36,6 +37,12 @@ def initialize(file, path)
@path = path
end

def match_markers(line)
MARKERS.map do |mkr|
%r{(.*(?:^|\s))#{mkr}\s+#([\w\-\.:/]+)\s+(.+)}.match(line)
end.compact
end

# Fetch all puzzles.
def puzzles
PDD.log.info "Reading #{@path}..."
Expand All @@ -44,10 +51,8 @@ def puzzles
lines.each_with_index do |line, idx|
begin
check_rules(line)
["\x40todo", 'TODO:?'].each do |pfx|
%r{(.*(?:^|\s))#{pfx}\s+#([\w\-\.:/]+)\s+(.+)}.match(line) do |m|
puzzles << puzzle(lines.drop(idx + 1), m, idx)
end
match_markers(line).each do |m|
puzzles << puzzle(lines.drop(idx + 1), m, idx)
end
rescue Error, ArgumentError => ex
message = "puzzle at line ##{idx + 1}; #{ex.message}"
Expand Down Expand Up @@ -139,7 +144,7 @@ def minutes(num, units)
# Fetch puzzle tail (all lines after the first one)
def tail(lines, prefix, start)
lines
.take_while { |t| t.start_with?(prefix) }
.take_while { |t| match_markers(t).none? && t.start_with?(prefix) }
.map { |t| t[prefix.length, t.length] }
.take_while { |t| t =~ /^[ a-zA-Z0-9]/ }
.each_with_index do |t, i|
Expand Down
17 changes: 15 additions & 2 deletions test/test_source_todo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
require_relative '../lib/pdd/sources'

class TestSourceTodo < Minitest::Test
def check_valid_puzzle(text, lines, body, ticket)
def check_valid_puzzle(text, lines, body, ticket, count = 1)
Dir.mktmpdir 'test' do |dir|
file = File.join(dir, 'a.txt')
File.write(file, text)
list = PDD::VerboseSource.new(file, PDD::Source.new(file, 'hey')).puzzles
assert_equal 1, list.size
assert_equal count, list.size
puzzle = list.first
assert_equal lines, puzzle.props[:lines]
assert_equal body, puzzle.props[:body]
Expand Down Expand Up @@ -82,6 +82,19 @@ def test_todo_colon_parsing
)
end

def test_multiple_todo_colon
check_valid_puzzle(
"
// TODO: #45 task description
// TODO: #46 another task description
",
'2-2',
'task description',
'45',
2
)
end

def test_todo_colon_parsing_multi_line
check_valid_puzzle(
"
Expand Down

0 comments on commit 99eb17d

Please sign in to comment.