Skip to content

Commit

Permalink
Happy path is happy
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Peabody committed Jan 10, 2011
1 parent 61f7294 commit ed52868
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
10 changes: 4 additions & 6 deletions koans/edgecase.rb
@@ -1,8 +1,6 @@
#!/usr/bin/env ruby
# -*- ruby -*-

require 'test/unit/assertions'

# --------------------------------------------------------------------
# Support code for the Ruby Koans.
# --------------------------------------------------------------------
Expand Down Expand Up @@ -90,10 +88,10 @@ module Color

module_function

COLORS.each do |color, value|
module_eval "def #{color}(string); colorize(string, #{value}); end"
module_function color
end
# COLORS.each do |color, value|
# module_eval "def #{color}(string); colorize(string, #{value}); end"
# module_function color
# end

def colorize(string, color_value)
if use_colors?
Expand Down
17 changes: 14 additions & 3 deletions overrides.rb
@@ -1,12 +1,23 @@
module EdgeCase
class << self
def simple_output; false; end
end
module Color
module_function
# def use_colors?
# false
# end
COLORS.each do |color, value|
module_eval %{def #{color}(string); "<div style='color:#{color};'>\#\{string\}</div>"; end}
module_function color
# COLORS.each do |color, value|
# module_eval %{def #{color}(string); "<div style='color:#{color};'>\#\{string\}</div>"; end}
# module_function color
# end
COLORS = {
:clear => 0, :black => 30, :red => 31,
:green => 32, :yellow => 33, :blue => 34,
:magenta => 35, :cyan => 36,
}
def method_missing(method_name,string)
"<div style='color:#{method_name};'>\#\{string\}</div>"
end

end
Expand Down
33 changes: 16 additions & 17 deletions test.rb
Expand Up @@ -2,7 +2,7 @@
require 'sinatra'
require 'timeout'
require File.expand_path(File.dirname(__FILE__) + '/string')
EDGECASE_CODE = IO.read("koans/edgecase.rb").split(/END\s?\{/).first
EDGECASE_CODE = IO.read("koans/edgecase.rb").remove_require_lines.split(/END\s?\{/).first
EDGECASE_OVERRIDES = IO.read("overrides.rb")
ARRAY_ORIGINAL = IO.read("koans/about_arrays.rb").remove_require_lines

Expand All @@ -18,42 +18,41 @@ def input
count = count + 1
x
end
module RunResults
SENSEI = nil
ERRORS = ""
end
unique_id = rand(10000)
runnable_code = "
require 'timeout'
require 'test/unit/assertions'
RESULTS = {:error => '', :failures => [], :pass_count => 0}
$SAFE = 3
begin
Timeout::timeout(2) {
module KoanArena
module UniqueRun#{unique_id}
#{::EDGECASE_CODE}
#{::EDGECASE_OVERRIDES}
#{runnable_code}
path = EdgeCase::ThePath.new
path.online_walk
::RunResults::SENSEI = path.sensei
RESULTS[:pass_count] = path.sensei.pass_count - 1
RESULTS[:failures] = path.sensei.failures
end
end
}
rescue SecurityError
::RunResults::ERRORS = \"What do you think you're doing, Dave?\"
rescue SecurityError => se
RESULTS[:error] = \"What do you think you're doing, Dave? \"
rescue TimeoutError => te
::RunResults::ERRORS = 'Do you have an infinite loop?'
RESULTS[:error] = 'Do you have an infinite loop?'
rescue StandardError => e
::RunResults::ERRORS = [e.message, e.backtrace].flatten.join('<br/>')
RESULTS[:error] = [e.message, e.backtrace].flatten.join('<br/>')
end
KoanArena.send(:remove_const, :UniqueRun#{unique_id})
RESULTS
"
Thread.new { eval runnable_code, TOPLEVEL_BINDING }.value
results = Thread.new { eval runnable_code, TOPLEVEL_BINDING }.value

pass_count = (RunResults::SENSEI && RunResults::SENSEI.pass_count) || 0
failures = (RunResults::SENSEI && RunResults::SENSEI.failures.map(&:message)) || []
pass_count = results[:pass_count]
failures = results[:failures]
inputs = ARRAY_ORIGINAL.
gsub("\s", "&nbsp;").
swap_input_fields(input, pass_count, failures)
swap_input_fields(input, pass_count, failures.map(&:message))

page = "<form>
<div style='position: absolute; height: 100%; width: 60%; overflow: auto;'>
Expand All @@ -65,7 +64,7 @@ module UniqueRun#{unique_id}
#{pass_count}<br/>
#{failures.join('<br/>')}
#{failures.count}
#{RunResults::ERRORS}
#{results[:error]}
</div>
</form>
<pre style='position:absolute;top:500px'>
Expand Down

0 comments on commit ed52868

Please sign in to comment.