-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathcoding_prompt.rb
More file actions
481 lines (419 loc) · 15.5 KB
/
coding_prompt.rb
File metadata and controls
481 lines (419 loc) · 15.5 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# == Schema Information
#
# Table name: coding_prompts
#
# id :integer not null, primary key
# created_at :datetime
# updated_at :datetime
# class_name :string(255)
# wrapper_code :text(65535) not null
# test_script :text(65535) not null
# method_name :string(255)
# starter_code :text(65535)
# hide_examples :boolean default(FALSE), not null
#
require 'fileutils'
require 'csv'
# =============================================================================
# Represents one coding prompt in an exercise. In spirit,
# this is a subclass of Prompt, and inherits all of the fields of Prompt via
# acts_as (see the documentation on-line for the activerecord-acts_as
# gem).
#
class CodingPrompt < ActiveRecord::Base
include TestCaseHelper
#~ Relationships ............................................................
acts_as :prompt
has_many :test_cases, inverse_of: :coding_prompt, dependent: :destroy
#~ Validation ...............................................................
validates :wrapper_code, presence: true
validates :test_script, presence: true
accepts_nested_attributes_for :test_cases, allow_destroy: true
#~ Hooks ....................................................................
before_validation :set_defaults
after_commit :parse_tests, on: [ :create, :update ], if: :test_script_changed?
#~ Instance methods .........................................................
# -------------------------------------------------------------
def question_type
Exercise::Q_CODING
end
# -------------------------------------------------------------
def is_mcq?
false
end
# -------------------------------------------------------------
def is_coding?
true
end
# -------------------------------------------------------------
def language
exercise_version.exercise.language || 'Java'
end
# -------------------------------------------------------------
def examples
test_cases.only_examples
end
# -------------------------------------------------------------
def new_answer(args)
CodingPromptAnswer.new()
end
# -------------------------------------------------------------
def test_file_name
prompt_dir + '/' + self.class_name + 'Test.' +
Exercise.extension_of(self.language)
end
# -------------------------------------------------------------
def prepare_starter_code
result = self.starter_code
if result.nil?
result = ''
end
return result.gsub(/\b___\b/, '')
end
# -------------------------------------------------------------
# Duplicates some code in parse_tests, but too lazy to refactor ATM
def regenerate_tests
if self.test_cases.empty?
# Probably never generated anyway, so ...
parse_tests
return
end
if self.id && !self.test_script.blank?
# Prep the directory to contain the generated test class
dir = prompt_dir
if Dir.exist?(dir)
FileUtils.remove_dir(dir)
end
FileUtils.mkdir_p(dir)
case self.language
when 'Java'
if self.test_script =~ /\s*(import[^,;"']*;|public\s+class)/
raise NotImplementedError, "regenerate_JUnit_tests not implemented yet!"
end
when 'C++'
if self.test_script =~ /\s*(using\s+namespace|#(define|undef|ifdef|ifndef|include|if|line)\s)/
raise NotImplementedError, "regenerate_JUnit_tests not implemented yet!"
end
end
# Default, if none of above cases return
generate_CSV_tests(test_file_name, self)
end
end
#~ Private instance methods .................................................
private
# -------------------------------------------------------------
def set_defaults
# Should the default class name be the same across all languages?
case self.language
when 'Java'
self.class_name ||= 'Answer'
self.wrapper_code ||= "public class Answer\n{\n ___\n}\n"
# TODO: auto-guess method name from starter code
end
end
# -------------------------------------------------------------
def prompt_dir
'usr/resources/' + self.language + '/tests/' + self.id.to_s
end
# -------------------------------------------------------------
def parse_tests
if self.id && !self.test_script.blank?
# If there are old test cases, clear them out first
if !self.test_cases.empty?
self.test_cases.each do |tc|
tc.delete
end
self.test_cases = []
end
# Prep the directory to contain the generated test class
dir = prompt_dir
if Dir.exist?(dir)
FileUtils.remove_dir(dir)
end
FileUtils.mkdir_p(dir)
case self.language
when 'Java'
if self.test_script =~ /\s*(import[^,;"']*;|public\s+class)/
parse_JUnit_tests
return
end
when 'C++'
if self.test_script =~ /\s*(using\s+namespace|#(define|undef|ifdef|ifndef|include|if|line)\s)/
parse_CxxTest_tests
return
end
end
# Default, if none of above cases return
parse_CSV_tests(self.test_script)
generate_CSV_tests(test_file_name, self)
end
end
# -------------------------------------------------------------
def parse_CSV_tests(csv_text)
# Now parse the test description into test case objects
CSV.parse(csv_text) do |row|
tc = TestCase.new(
weight: 1.0,
coding_prompt: self,
input: row[0] || "", # empty string is truthy in ruby
expected_output: row[1],
example: false,
hidden: false,
static: false,
screening: false)
if !row[2].blank?
tc.parse_description_specifier(row[2])
end
if !row[3].blank?
tc.negative_feedback = row[3]
end
if !row[4].blank?
if row[4] =~ /screening/i
tc.weight = 1.0
tc.screening = true
else
wt = row[4]
if wt.sub!(/^screening:\s*/i, '')
tc.screening = true
end
tc.weight = wt.to_f
if tc.weight < 0
tc.weight = 1.0
end
end
end
if tc.save
self.test_cases << tc
else
puts "error saving test case: #{tc.errors.full_messages.to_s}"
end
end
end
# -------------------------------------------------------------
def parse_JUnit_tests
junit = self.test_script.gsub(/\r\n/, "\n")
# First, collect any embedded static tests
junit.scan(
/(?:\/\/\p{Blank}*static\p{Blank}*tests\p{Blank}*:\p{Blank}*(.*\n(?:\p{Blank}*\/\/.*\n)*))|(?:\/\*\s*static\s*tests\s*:\s*((?:[^*]|(?:\*+[^*\/]))*)\*+\/)/i
) do |tests1, tests2|
if tests2.blank?
tests = tests1.gsub(/^\p{Blank}*\/\/\p{Blank}*/, '').gsub(/\p{Blank}*$/, '')
else
tests = tests2.gsub(/^\p{Blank}*(\*\p{Blank}*)?/, '').gsub(/\p{Blank}*$/, '')
end
tests.sub!(/\n*$/m, "\n")
parse_CSV_tests(tests)
end
# Now, extract metadata about and rename each test method
junit.gsub!(
/((?:\p{Blank}*\/\/.*\n)|(?:\p{Blank}*\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/\p{Blank}*\n?))*((?:\s*@(?:Test|Example|Hidden|Screening|(?:Weight|ScoringWeight)\s*\(\s*[0-9]+(?:\.[0-9]*)?\s*\)|(?:Description|Hint|NegativeFeedback)\s*\(\s*"\s*(?:[^"]|\\")*\s*"\s*\)))*)(\s*public\s+void\s+)([a-zA-Z0-9_]+)(\s*\(\s*\))/
) do |match|
comment = Regexp.last_match(1)
attrs = Regexp.last_match(2)
publicvoid = Regexp.last_match(3)
name = Regexp.last_match(4)
args = Regexp.last_match(5)
if name =~ /^test/ || attrs =~ /@Test\b/
tc = TestCase.new(
weight: 1.0,
coding_prompt: self,
input: '',
expected_output: '',
example: false,
hidden: false,
static: false,
screening: false)
tc.description = '<No Test Description Provided!>'
desc = nil
# Attempt to pull description string from comments
if comment =~ /description\s*:\s*((?:[^*\r\n]|(?:\*+[^*\/\r\n]))*)(?:\*\/\s*)?$/i
desc = $1
end
# Attempt to pull description string from attribute, which overrides
if attrs =~ /@(?:Description|Hint)\s*\(\s*"\s*((?:[^"]|\\")*)\s*"\s*\)/
desc = $1.gsub(/\\"/, '"')
end
# If no description, try to pull it from the method name
if desc.blank? && name =~ /^(?:test)?(.*)(?:_*[0-9]+)?$/
namedesc = $1
if !namedesc.blank?
namedesc = namedesc.sub(/^_+/, '').sub(/_+$/, '')
if !namedesc.blank?
if namedesc =~ /^((?:(?:example|screening|hidden)_)+)([^_].*)$/
prefix = $1
suffix = $2
else
prefix = ''
suffix = namedesc
end
if suffix.blank?
if !prefix.blank?
desc = prefix
end
else
desc = prefix.gsub(/_/, ':') + suffix.underscore.split(/_+/).join(' ').capitalize
end
end
end
end
tc.parse_description_specifier(desc)
puts(tc.description)
# look for "example" tag in comments or attribute
if comment =~ /example\s*:\s*true\s*(?:\*\/\s*)?$/i || attrs =~ /@Example\b/
tc.example = true
end
# look for "hidden" tag in comments or attribute
if comment =~ /hidden\s*:\s*true\s*(?:\*\/\s*)?$/i || attrs =~ /@Hidden\b/
tc.hidden = true
end
# look for "screening" tag in comments or attribute
if comment =~ /screening\s*:\s*true\s*(?:\*\/\s*)?$/i || attrs =~ /@Screening\b/
tc.screening = true
end
# Attempt to pull negative feedback string from comments
nfb = nil
if comment =~ /negative\s*feedback\s*:\s*((?:[^*\r\n]|(?:\*+[^*\/\r\n]))*)(?:\*\/\s*)?$/i
nfb = $1
end
# Attempt to pull negative feedback string from attributes
if attrs =~ /@NegativeFeedback\s*\(\s*"\s*((?:[^"]|\\")*)\s*"\s*\)/
nfb = $1.gsub(/\\"/, '"')
end
tc.parse_negative_feedback_specifier(nfb)
# Attempt to pull test case weight from comments
if comment =~ /(?:scoring\s*)?weight\s*:\s*([0-9]+(?:\.[0-9]*)?)\s*(?:\*\/\s*)?$/i
tc.weight = $1.to_f
end
# Attempt to pull test case weight from attributes
if attrs =~ /@(?:Scoring)?Weight\s*\(\s*([0-9]+(?:\.[0-9]*)?)\s*\)/
tc.weight = $1.to_f
end
if tc.save
self.test_cases << tc
# Rename test method to include TestCase id
name = "#{name}_#{tc.id}"
else
puts "error saving test case: #{tc.errors.full_messages.to_s}"
end
end
rewrite = "#{comment}#{attrs}#{publicvoid}#{name}#{args}"
# puts "rewritten test decl:\n#{rewrite}\n\n"
rewrite
end
# puts "junit after rewrite:\n#{junit}"
File.write(test_file_name, junit)
end
# -------------------------------------------------------------
def parse_CxxTest_tests
junit = self.test_script.gsub(/\r\n/, "\n")
# First, collect any embedded static tests
junit.scan(
/(?:\/\/\p{Blank}*static\p{Blank}*tests\p{Blank}*:\p{Blank}*(.*\n(?:\p{Blank}*\/\/.*\n)*))|(?:\/\*\s*static\s*tests\s*:\s*((?:[^*]|(?:\*+[^*\/]))*)\*+\/)/i
) do |tests1, tests2|
if tests2.blank?
tests = tests1.gsub(/^\p{Blank}*\/\/\p{Blank}*/, '').gsub(/\p{Blank}*$/, '')
else
tests = tests2.gsub(/^\p{Blank}*(\*\p{Blank}*)?/, '').gsub(/\p{Blank}*$/, '')
end
tests.sub!(/\n*$/m, "\n")
parse_CSV_tests(tests)
end
# Now, extract metadata about and rename each test method
junit.gsub!(
/((?:\p{Blank}*\/\/.*\n)|(?:\p{Blank}*\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/\p{Blank}*\n?))*((?:\s*@(?:Test|Example|Hidden|Screening|(?:Weight|ScoringWeight)\s*\(\s*[0-9]+(?:\.[0-9]*)?\s*\)|(?:Description|Hint|NegativeFeedback)\s*\(\s*"\s*(?:[^"]|\\")*\s*"\s*\)))*)(\s*void\s+)([a-zA-Z0-9_]+)(\s*\(\s*\))/
) do |match|
comment = Regexp.last_match(1)
attrs = Regexp.last_match(2)
publicvoid = Regexp.last_match(3)
name = Regexp.last_match(4)
args = Regexp.last_match(5)
if name =~ /^test/ || attrs =~ /@Test\b/
tc = TestCase.new(
weight: 1.0,
coding_prompt: self,
input: '',
expected_output: '',
example: false,
hidden: false,
static: false,
screening: false)
tc.description = '<No Test Description Provided!>'
desc = nil
# Attempt to pull description string from comments
if comment =~ /description\s*:\s*((?:[^*\r\n]|(?:\*+[^*\/\r\n]))*)(?:\*\/\s*)?$/i
desc = $1
end
# Attempt to pull description string from attribute, which overrides
if attrs =~ /@(?:Description|Hint)\s*\(\s*"\s*((?:[^"]|\\")*)\s*"\s*\)/
desc = $1.gsub(/\\"/, '"')
end
# If no description, try to pull it from the method name
if desc.blank? && name =~ /^(?:test)?(.*)(?:_*[0-9]+)?$/
namedesc = $1
if !namedesc.blank?
namedesc = namedesc.sub(/^_+/, '').sub(/_+$/, '')
if !namedesc.blank?
if namedesc =~ /^((?:(?:example|screening|hidden)_)+)([^_].*)$/
prefix = $1
suffix = $2
else
prefix = ''
suffix = namedesc
end
if suffix.blank?
if !prefix.blank?
desc = prefix
end
else
desc = prefix.gsub(/_/, ':') + suffix.underscore.split(/_+/).join(' ').capitalize
end
end
end
end
tc.parse_description_specifier(desc)
# look for "example" tag in comments or attribute
if comment =~ /example\s*:\s*true\s*(?:\*\/\s*)?$/i || attrs =~ /@Example\b/
tc.example = true
end
# look for "hidden" tag in comments or attribute
if comment =~ /hidden\s*:\s*true\s*(?:\*\/\s*)?$/i || attrs =~ /@Hidden\b/
tc.hidden = true
end
# look for "screening" tag in comments or attribute
if comment =~ /screening\s*:\s*true\s*(?:\*\/\s*)?$/i || attrs =~ /@Screening\b/
tc.screening = true
end
# Attempt to pull negative feedback string from comments
nfb = nil
if comment =~ /negative\s*feedback\s*:\s*((?:[^*\r\n]|(?:\*+[^*\/\r\n]))*)(?:\*\/\s*)?$/i
nfb = $1
end
# Attempt to pull negative feedback string from attributes
if attrs =~ /@NegativeFeedback\s*\(\s*"\s*((?:[^"]|\\")*)\s*"\s*\)/
nfb = $1.gsub(/\\"/, '"')
end
tc.parse_negative_feedback_specifier(nfb)
# Attempt to pull test case weight from comments
if comment =~ /(?:scoring\s*)?weight\s*:\s*([0-9]+(?:\.[0-9]*)?)\s*(?:\*\/\s*)?$/i
tc.weight = $1.to_f
end
# Attempt to pull test case weight from attributes
if attrs =~ /@(?:Scoring)?Weight\s*\(\s*([0-9]+(?:\.[0-9]*)?)\s*\)/
tc.weight = $1.to_f
end
if tc.save
self.test_cases << tc
# Rename test method to include TestCase id
name = "#{name}_#{tc.id}"
else
puts "error saving test case: #{tc.errors.full_messages.to_s}"
end
end
rewrite = "#{comment}#{attrs}#{publicvoid}#{name}#{args}"
# puts "rewritten test decl:\n#{rewrite}\n\n"
rewrite
end
# puts "junit after rewrite:\n#{junit}"
File.write(test_file_name, junit)
end
end