-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathstring.rb
126 lines (122 loc) · 4.06 KB
/
string.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class String
def remove_require_lines
self.split("\n").reject{|line| line.start_with? 'require' }.join("\n")
end
def preify
self.gsub('<','<').gsub('>','>').gsub("\n","<br/>").gsub("\s"," ")
end
def swap_user_values(input_values)
count = 0
method_area = false
method_name = nil
method_indentation = 0
in_ruby_v = nil
in_ruby_indentation = 0
in_correct_ruby_v = true
self.split("\n").map do |line|
if match = line.match(/^(\s{2}*)in_ruby_version.*[\"\'](.*)[\"\']/)
in_ruby_indentation = match[1].size
in_ruby_v = match[2]
in_correct_ruby_v = in_ruby_v.include? "1.8"
next
elsif in_ruby_v
if line.match(/^\s{#{in_ruby_indentation}}end/)
in_ruby_v = nil
in_ruby_indentation = 0
in_correct_ruby_v = true
next
else
line = line[in_ruby_indentation..-1].to_s
end
end
next unless in_correct_ruby_v
if match = line.match(/^(\s{2}*)def test_/)
method_area = true
method_indentation = match[1].size
method_name = (methodx = line.match(/test_\S*/)) && methodx[0]
elsif line.start_with?(/\s{#{method_indentation}}end/) && method_area
method_area = false
method_name = nil
end
if line.strip.start_with? "#"
line
else
line.gsub(/__/) do |match|
if %w{test_assert_truth test_assert_with_message}.include?(method_name) &&
(input_values[count].nil? || input_values[count].empty?)
input_values[count] = 'false'
end
x = input_values[count].to_s == "" ? "__" : "#{input_values[count]}"
count = count + 1
x
end
end
end.compact.join("\n")
end
def swap_input_fields(input_values, passes, failures)
count = 0
method_count = 0
method_area = false
method_name = nil
method_indentation = 0
in_ruby_v = nil
in_ruby_indentation = 0
in_correct_ruby_v = true
self.split("\n").map do |line|
true_line = line.gsub(' ',' ')
if match = true_line.match(/^(\s{2}*)in_ruby_version.*[\"\'](.*)[\"\']/)
in_ruby_indentation = match[1].size
in_ruby_v = match[2]
in_correct_ruby_v = in_ruby_v.include? "1.8"
next
elsif in_ruby_v
if true_line.match(/^\s{#{in_ruby_indentation}}end/)
in_ruby_v = nil
in_ruby_indentation = 0
in_correct_ruby_v = true
next
else
line = line[(in_ruby_indentation*(' '.size))..-1].to_s
true_line = line.gsub(' ',' ')
end
end
next unless in_correct_ruby_v
if match = true_line.match(/^(\s{2}*)def test_/)
method_area = true
method_indentation = match[1].size
method_count = method_count + 1
method_name = (methodx = true_line.match(/test_\S*/)) && methodx[0]
failure = failures[method_name.to_sym]
"#{fail_message(failure)}
<div nowrap='nowrap' class='#{failure ? 'failed' : 'passed'}'>
#{line}"
elsif method_area && true_line.match(/^\s{#{method_indentation}}end/)
method_area = false
"#{line}</div>"
elsif line.gsub(" ","").start_with?("#")
line
else
line.gsub(/__/) do |match|
if %w{test_assert_truth test_assert_with_message}.include?(method_name) &&
(input_values[count].nil? || input_values[count].empty?)
x = 'false'
else
x = input_values[count].to_s.gsub("'", "'")
end
count = count + 1
"<input type='text' name='input[]' value='#{x}\' />"
end
end
end.compact.join('<br/>')
end
def fail_message(failure)
return nil if failure.nil?
if failure.message.include? "FILL ME IN"
" Please meditate on the following.".preify
elsif failure.message.include? "undefined local"
failure.message.split("for #<").first.preify
else
" The answers which you seek:\n #{failure.message.gsub("\n"," ")}".preify
end
end
end