Skip to content

Commit

Permalink
#24 check for duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Dec 5, 2014
1 parent dae96e1 commit 6813f01
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 21 deletions.
13 changes: 11 additions & 2 deletions features/applies_rules.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Feature: Applies Post-Parsing Rules
As a source code writer I want to be sure that
certain post-parsing rules are applied

Scenario: Throwing exception on invalid estimate
Scenario: Throwing exception on big estimates
Given I have a "Sample.java" file with content:
"""
@todo #13:180m This puzzle has too big estimate
Expand All @@ -11,7 +11,7 @@ Feature: Applies Post-Parsing Rules
Then Exit code is not zero
Then Stdout contains "bigger than 90 minutes"

Scenario: Throwing exception on invalid estimate
Scenario: Throwing exception on small estimates
Given I have a "Sample.java" file with content:
"""
@todo #13:15min This puzzle has too small estimate
Expand All @@ -20,3 +20,12 @@ Feature: Applies Post-Parsing Rules
Then Exit code is not zero
Then Stdout contains "lower than 30 minutes"

Scenario: Throwing exception on duplicates
Given I have a "Sample.java" file with content:
"""
@todo #13:15min The text
@todo #13:15min The text
"""
When I run bin/pdd with ""
Then Exit code is not zero
Then Stdout contains "there are 2 duplicate"
2 changes: 1 addition & 1 deletion features/avoiding_duplicates.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ Feature: Avoiding Duplicate Puzzles
}
}
"""
When I run pdd it fails with "Duplicate key-sequence"
When I run pdd it fails with "errors, see log above"
1 change: 1 addition & 0 deletions features/cli.feature
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Feature: Command Line Processing
"""
When I run bin/pdd with "> out.xml"
Then Exit code is zero
And Stdout is empty
And XML file "out.xml" matches "/puzzles[count(puzzle)=1]"

Scenario: Excluding unnecessary files
Expand Down
6 changes: 5 additions & 1 deletion features/step_definitions/steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

When(/^I run bin\/pdd with "([^"]*)"$/) do |arg|
home = File.join(File.dirname(__FILE__), '../..')
@stdout = `ruby -I#{home}/lib #{home}/bin/pdd #{arg} 2>&1`
@stdout = `ruby -I#{home}/lib #{home}/bin/pdd #{arg}`
@exitstatus = $CHILD_STATUS.exitstatus
end

Expand All @@ -82,6 +82,10 @@
end
end

Then(/^Stdout is empty$/) do
fail "STDOUT is not empty:\n#{@stdout}" unless @stdout == ''
end

Then(/^XML file "([^"]+)" matches "([^"]+)"$/) do |file, xpath|
fail "File #{file} doesn't exit" unless File.exist?(file)
xml = Nokogiri::XML.parse(File.read(file))
Expand Down
2 changes: 0 additions & 2 deletions features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,3 @@

require 'simplecov'
require 'pdd'

PDD.log = Logger.new(STDOUT)
21 changes: 11 additions & 10 deletions lib/pdd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,18 @@ class SchemaError < Error

# Get logger.
def self.log
unless @log
@log = Logger.new(STDOUT)
@log.formatter = proc { |severity, _, _, msg|
puts "#{severity}: #{msg.dump}"
unless @logger
@logger = Logger.new(STDOUT)
@logger.formatter = proc { |severity, _, _, msg|
"#{severity}: #{msg.dump}\n"
}
@logger.level = Logger::ERROR
end
@log
@logger
end

class << self
attr_writer :log
attr_writer :logger
end

# Code base abstraction
Expand All @@ -71,7 +72,7 @@ class Base
# +opts+:: Options
def initialize(opts)
@opts = opts
PDD.log = Logger.new(File::NULL) unless @opts.verbose?
PDD.log.level = Logger::INFO if @opts.verbose?
PDD.log.info "my version is #{PDD::VERSION}"
end

Expand Down Expand Up @@ -134,16 +135,16 @@ def render(puzzle, xml)
def rules(xml)
doc = Nokogiri::XML(xml)
total = 0
@opts[:rule].push('max-duplicates:1').map do |r|
(@opts[:rule] || []).push('max-duplicates:1').map do |r|
name, value = r.split(':')
rule = RULES[name]
fail "rule '#{name}' doesn't exist" if rule.nil?
rule.new(doc, value.to_i).errors.each do |e|
PDD.log.error e
total += 1
end
end unless @opts[:rule].nil?
fail "#{total} errors, see log above" unless total == 0
end
fail PDD::Error, "#{total} errors, see log above" unless total == 0
xml
end

Expand Down
4 changes: 2 additions & 2 deletions lib/pdd/rule/duplicates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def initialize(xml, max)

def errors
@xml.xpath('//puzzle')
.group_by{ |p| p.xpath('body/text()').to_s }
.map do |body, puzzles|
.group_by { |p| p.xpath('body/text()').to_s }
.map do |_, puzzles|
next nil if puzzles.count <= @max
"there are #{puzzles.count} duplicate(s) of the same puzzle: " +
puzzles.map do |p|
Expand Down
1 change: 0 additions & 1 deletion test/test_duplicates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
# License:: MIT
class TestMaxDuplicates < Minitest::Test
def test_max_duplicates
return
rule = PDD::Rule::MaxDuplicates.new(
Nokogiri::XML::Document.parse(
'<puzzles><puzzle><body>test</body></puzzle>
Expand Down
4 changes: 2 additions & 2 deletions test/test_pdd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_basic
opts = opts(['-v', '-s', dir, '-e', '**/*.png', '-r', 'max-estimate:15'])
File.write(File.join(dir, 'a.txt'), '@todo #55 hello!')
matches(
Nokogiri::XML::Document.parse(PDD::Base.new(opts).xml),
Nokogiri::XML(PDD::Base.new(opts).xml),
[
'/processing-instruction("xml-stylesheet")[contains(.,".xsl")]',
'/puzzles/@version',
Expand All @@ -53,7 +53,7 @@ def test_rules_failure
Dir.mktmpdir 'test' do |dir|
opts = opts(['-v', '-s', dir, '-e', '**/*.png', '-r', 'min-estimate:30'])
File.write(File.join(dir, 'a.txt'), '@todo #90 hello!')
assert_raises RuntimeError do
assert_raises PDD::Error do
PDD::Base.new(opts).xml
end
end
Expand Down

0 comments on commit 6813f01

Please sign in to comment.