Skip to content

Commit

Permalink
Merge branch 'feature-grader' of github.com:richardxia/rag into featu…
Browse files Browse the repository at this point in the history
…re-grader
  • Loading branch information
Richard Xia committed Apr 10, 2012
2 parents 83c5b15 + a328c7f commit 0c2b305
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
3 changes: 2 additions & 1 deletion hw3.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
scenarios:
- &restrict_pg_r
match: "restrict to movies with 'PG' or 'R' ratings"
match: "restrict to movies with [\"\']PG[\"\'] or [\"\']R[\"\'] ratings"
desc: "restrict to movies with 'PG' or 'R' ratings"
- &all_x_selected
match: "all (ratings|checkboxes) selected"
desc: "all ratings or checkboxes selected"
Expand Down
5 changes: 5 additions & 0 deletions lib/graders/feature_grader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def match?(str)
!!(str =~ @regex)
end

# Checks whether the given str represents the presence of this feature
def present_on?(str)
!!(str =~ /^\s*Scenario: #{@regex}/)
end

def to_s
@desc
end
Expand Down
39 changes: 36 additions & 3 deletions lib/graders/feature_grader/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ module Regex
attr_reader :failures

# +Hash+ with
# [+:failed+] [_String_] +cucumber+ scenarios that failed
# [+:failed+] _Array_<_String_> +cucumber+ scenarios that failed
# [+:missing+] _Array_<_ScenarioMatcher_> +cucumber+ scenarios that weren't seen running
# [+:steps+] [_Hash_] +:total+, +:passed+
attr_reader :scenarios

Expand Down Expand Up @@ -83,7 +84,10 @@ def initialize(grader, feature_={}, config={})
@target_pass = feature.has_key?("pass") ? feature.delete("pass") : true

@failures = feature.delete("failures") || []
@scenarios = {:failed => []}
@scenarios = {
:failed => [],
:missing => []
}

@env = feature.envify # whatever's left over
end
Expand Down Expand Up @@ -132,6 +136,16 @@ def run!
self.send :process_output, lines
end

##
# XXX Can only raise StandardError in these rescue clauses.
# Any other subclass will give incorrect results ("undefined method `-@'").
##

rescue TestFailedError => e
log "test failed: #{e.inspect}"
log e.backtrace
raise StandardError, e.message

rescue => e
log "test failed: #{e.inspect}"#.red.bold
log e.backtrace
Expand Down Expand Up @@ -180,13 +194,21 @@ def correct?
# This step is correct if:
# any +failures+ +?+ all +failures+ have failed +:+ it passed in cucumber
def correct!

if @scenarios[:missing].any?
raise IncorrectAnswer, "Could not find required scenario(s): #{@scenarios[:missing].collect(&:desc).join(", ")}"
end

if @failures.any?
unless @failures.all? {|matcher| @scenarios[:failed].any? {|s| matcher.match? s}}
missing_failures = @failures.reject {|matcher| @scenarios[:failed].any? {|s| matcher.match? s}}
missing_failures = missing_failures.collect{|f| " - #{f.to_s}"}.join("\n")
mods = self.desc || "(None)"
mods = " - #{mods}"
raise IncorrectAnswer, "The following scenarios passed incorrectly (should have failed):\n#{missing_failures}\nwith the following modifications:\n#{mods}"
raise IncorrectAnswer, [
"The following required failures were not detected:", missing_failures,
"with the following modifications:", mods
].join("\n")
end
else
unless @scenarios[:failed].empty? or @scenarios[:steps][:total] != @scenarios[:steps][:passed]
Expand All @@ -204,6 +226,17 @@ def process_output(output)

begin # parse failing scenarios (between FailingScenarios and BlankLine)
if i = output.find_index {|line| line =~ Regex::FailingScenarios}

begin # check for required scenarios
temp = output[0..i]
@scenarios[:missing] = @failures
temp.each do |line|
if failure = @failures.detect {|f| f.present_on?(line)}
@scenarios[:missing] -= [failure]
end
end
end

temp = output[i+1..-1]
i = temp.find_index {|line| line =~ Regex::BlankLine}
@scenarios[:failed] = temp.first(i)
Expand Down

0 comments on commit 0c2b305

Please sign in to comment.