Module: Course::Assessment::Answer::ProgrammingTestCaseHelper

Included in:
Submission::SubmissionsHelper
Defined in:
app/helpers/course/assessment/answer/programming_test_case_helper.rb

Instance Method Summary collapse

Instance Method Details

#get_failed_test_cases_by_type(test_cases_and_results) ⇒ Hash

If the test case type has a failed test case, return the first one.

Parameters:

  • test_cases_by_type (Hash)

    The test cases and their results keyed by type

Returns:

  • (Hash)

    Failed test case and its result, if any


34
35
36
37
38
39
40
# File 'app/helpers/course/assessment/answer/programming_test_case_helper.rb', line 34

def get_failed_test_cases_by_type(test_cases_and_results)
  {}.tap do |result|
    test_cases_and_results.each do |test_case_type, test_cases_and_results_of_type|
      result[test_case_type] = get_first_failed_test(test_cases_and_results_of_type)
    end
  end
end

#get_hint(test_case, test_case_result) ⇒ String

Get a hint message. Use the one from test_result if available, else fallback to the one from the test case.

Parameters:

Returns:

  • (String)

    The hint, or an empty string if there isn't one


9
10
11
12
13
# File 'app/helpers/course/assessment/answer/programming_test_case_helper.rb', line 9

def get_hint(test_case, test_case_result)
  hint = test_case_result.messages['hint'] if test_case_result
  hint ||= test_case.hint
  hint || ''
end

#get_output(test_case_result) ⇒ String

Get the output message for the tutors to see when grading. Use the output meta attribute if available, else fallback to the failure message, error message, and finally empty string.

Parameters:

Returns:

  • (String)

    The output, failure message, error message or empty string if the previous 3 don't exist.


21
22
23
24
25
26
27
28
# File 'app/helpers/course/assessment/answer/programming_test_case_helper.rb', line 21

def get_output(test_case_result)
  if test_case_result
    output = test_case_result.messages['output']
    output = test_case_result.messages['failure'] if output.blank?
    output = test_case_result.messages['error'] if output.blank?
  end
  output || ''
end

#get_test_cases_and_results(test_cases_by_type, auto_grading) ⇒ Hash

Organize the test cases and test results into a hash, keyed by test case type. If there is no test result, the test case key points to nil. nil is needed to make sure test cases are still displayed before they have a test result. Currently test_cases are ordered by sorting on the identifier of the ProgrammingTestCase. e.g. { 'public_test': { test_case_1: result_1, test_case_2: result_2, test_case_3: nil }, 'private_test': { priv_case_1: priv_result_1 }, 'evaluation_test': { eval_case1: eval_result_1 } }

Parameters:

Returns:

  • (Hash)

    The hash structure described above


53
54
55
56
57
58
59
60
# File 'app/helpers/course/assessment/answer/programming_test_case_helper.rb', line 53

def get_test_cases_and_results(test_cases_by_type, auto_grading)
  results_hash = auto_grading ? auto_grading.test_results.group_by(&:test_case) : {}
  test_cases_by_type.each do |type, test_cases|
    test_cases_by_type[type] =
      test_cases.map { |test_case| [test_case, results_hash[test_case]&.first] }.
      sort_by { |test_case, _| test_case.identifier }.to_h
  end
end