-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtest.rb
91 lines (85 loc) · 2.67 KB
/
test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require 'rubygems'
require 'sinatra'
require 'haml'
require 'timeout'
require 'pp'
require File.expand_path(File.dirname(__FILE__) + '/string')
require File.expand_path(File.dirname(__FILE__) + '/path_grabber')
KOAN_FILENAMES = PathGrabber.new.koan_filenames
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
def input
(params[:input] ||= []).map{|i| i.gsub('raise','')}
end
def current_koan_name
claimed = params[:koan].to_s
if KOAN_FILENAMES.include? claimed
claimed
else
KOAN_FILENAMES.first
end
end
def next_koan_name
KOAN_FILENAMES[current_koan_count]
end
def current_koan_count
KOAN_FILENAMES.index(current_koan_name)+1
end
def current_koan
IO.read("koans/#{current_koan_name}.rb").remove_require_lines.gsub("assert false", "assert __")
end
def runnable_code
code = current_koan.swap_user_values(input)
unique_id = rand(1000)
<<-RUNNABLE_CODE
require 'timeout'
require 'test/unit/assertions'
Test::Unit::Assertions::AssertionMessage.use_pp= false
RESULTS = {:error => nil, :failures => {}, :pass_count => 0}
$SAFE = 3
begin
Timeout.timeout(2) {
module KoanArena
module UniqueRun#{unique_id}
#{::EDGECASE_CODE}
#{::EDGECASE_OVERRIDES}
#{code}
path = EdgeCase::ThePath.new
path.online_walk
RESULTS[:pass_count] = path.sensei.pass_count
RESULTS[:failures] = path.sensei.failures
end
end
KoanArena.send(:remove_const, :UniqueRun#{unique_id})
}
rescue SecurityError => se
RESULTS[:error] = \"What do you think you're doing, Dave? \"
rescue TimeoutError => te
RESULTS[:error] = 'Do you have an infinite loop?'
rescue StandardError => e
RESULTS[:error] = ['standarderror', e.message, e.backtrace, e.inspect].flatten.join('<br/>')
end
RESULTS
RUNNABLE_CODE
end
get '/' do
return haml '%pre= runnable_code' if params[:dump]
count = 0
results = Thread.new { eval runnable_code, TOPLEVEL_BINDING }.value
@pass_count = results[:pass_count]
@failures = results[:failures]
@error = results[:error]
if @error
return "#{@error} <br/><br/> Click your browser back button to return."
elsif @failures.count > 0
@inputs = current_koan.gsub("\s", " ").swap_input_fields(input, @pass_count, @failures)
return haml :koans
else
if KOAN_FILENAMES.last == current_koan_name
return haml :end
else
return haml :next_koan
end
end
end