-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathcoding_prompt_answer.rb
More file actions
102 lines (86 loc) · 3.04 KB
/
coding_prompt_answer.rb
File metadata and controls
102 lines (86 loc) · 3.04 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
# == Schema Information
#
# Table name: coding_prompt_answers
#
# id :integer not null, primary key
# answer :text(65535)
# error :text(65535)
# error_line_no :integer
#
# =============================================================================
# Represents one coding prompt answer in an exercise attempt. In spirit,
# this is a subclass of PromptAnswer, and inherits all of the fields of
# PromptAnswer via acts_as (see the documentation on-line for the
# activerecord-acts_as gem).
#
class CodingPromptAnswer < ActiveRecord::Base
#~ Relationships ............................................................
acts_as :prompt_answer
has_many :student_test_cases
has_many :student_test_case_results
has_many :test_case_results,
#-> { includes :test_case },
-> { order('test_case_id ASC').includes(:test_case) },
inverse_of: :coding_prompt_answer, dependent: :destroy
#~ Validation ...............................................................
# Note: there is no validates :answer, presence: true here, intentionally.
# There may be cases where a user attempts an exercise but does not
# answer all prompts, and that would constitute an empty answer. We
# want to allow that, so do not add validations preventing it.
def prompt_dir
'usr/resources/' + self.language + '/tests/' + self.id.to_s
end
def test_file_name
prompt_dir + '/' + self.class_name + 'Test.' +
Exercise.extension_of(self.language)
end
#~ Instance methods .........................................................
# -------------------------------------------------------------
def parse_student_tests!(answer_text, language, id)
testList = CodeWorker.get_tests_from_answer_text(answer_text, language)
testList.each do |test|
tc = StudentTestCase.new(
input: test[0],
expected_output: test[1],
description: test[2],
coding_prompt_answer_id: self.id
)
unless tc.save
puts "error saving test case: #{tc.errors.full_messages.to_s}"
end
end
end
# -------------------------------------------------------------
def execute_static_tests
result = nil
code = self.without_comments
self.test_cases.only_static.each do |test_case|
this_result = test_case.apply_static_check(self, code)
if !result && this_result
result = this_result
end
end
result
end
# -------------------------------------------------------------
def without_comments
result = answer
lang = prompt.specific.language
if lang
regex = REMOVE_COMMENTS_REGEX[lang]
if regex
# Replace all comments with just the line endings in the
# comments, in order to preserve line numbering
result.gsub!(regex) do |cmt|
cmt.scan(/\n/).join('')
end
end
end
return result
end
#~ Private instance methods .................................................
private
REMOVE_COMMENTS_REGEX = {
'Java' => /(\/\*([^*]|(\*+[^*\/]))*\*+\/)|(\/\/[^\r\n]*)/
}
end