class Gobstones::Checker

Public Class Methods

new(options) click to toggle source
# File lib/checker.rb, line 5
def initialize(options)
  @options = options
end

Public Instance Methods

check_error(output, expected) click to toggle source
# File lib/checker.rb, line 35
def check_error(output, expected)
  status = output[:status]
  result = output[:result]

  return if is_expected_timeout(result)

  fail_with status: :check_error_failed_expected_boom,
            result: {
              initial: result[:initialBoard],
              expected: :boom,
              final: result[:finalBoard]
            } if status.passed?

  reason_code = convert_known_reason_code result[:finalBoardError][:reason][:code]
  fail_with status: :check_error_failed_another_reason,
            result: {
              reason: result[:finalBoardError],
              expected_code: I18n.t("code_#{expected}")
            } if reason_code != expected
end
check_final_board(output, expected) click to toggle source
# File lib/checker.rb, line 9
def check_final_board(output, expected)
  status = output[:status]
  result = output[:result]

  return if is_expected_timeout(result)
  assert_not_boom status, result

  expected_board = result[:extraBoard]
  actual_board = result[:finalBoard]
  boards_match = board_json(expected_board).eql? board_json(actual_board)
  headers_match = expected_board[:head].eql?(actual_board[:head]) || !@options[:check_head_position]

  if !boards_match || !headers_match
    status = boards_match && !headers_match ?
      :check_final_board_failed_different_headers :
      :check_final_board_failed_different_boards

    fail_with status: status,
              result: {
                initial: result[:initialBoard],
                expected: expected_board,
                actual: actual_board
              }
  end
end
check_return(output, expected) click to toggle source
# File lib/checker.rb, line 56
def check_return(output, expected)
  status = output[:status]
  result = output[:result]

  assert_not_boom status, result
  return_value = result[:finalBoard][:returnValue]

  fail_with status: :check_return_failed_no_return,
            result: {
              initial: result[:initialBoard],
              expected_value: expected
            } if return_value.nil?

  final_value = adapt_value return_value
  final_expected_value = expected

  if return_value[:type] == 'Number'
    final_value = final_value.to_s
    final_expected_value = final_expected_value.to_s
    # TODO: This is not ok but it's here for retrocompatibility issues.
  end

  fail_with status: :check_return_failed_different_values,
            result: {
              initial: result[:initialBoard],
              expected_value: expected,
              actual_value: final_value
            } if final_value != final_expected_value
end

Private Instance Methods

adapt_value(return_value) click to toggle source
# File lib/checker.rb, line 127
def adapt_value(return_value)
  type = return_value[:type]
  value = return_value[:value]

  return value.to_s.capitalize if type == 'Bool'

  value
end
assert_not_boom(status, result) click to toggle source
# File lib/checker.rb, line 88
def assert_not_boom(status, result)
  fail_with status: :check_failed_unexpected_boom,
            result: {
              initial: result[:initialBoard],
              expected: result[:extraBoard],
              actual: :boom,
              reason: result[:finalBoardError]
            } if status == :runtime_error
end
board_json(board) click to toggle source
# File lib/checker.rb, line 108
def board_json(board)
  board[:table][:json]
end
convert_known_reason_code(code) click to toggle source
# File lib/checker.rb, line 112
def convert_known_reason_code(code)
  return "no_stones" if code == 'cannot-remove-stone'
  return "out_of_board" if code == 'cannot-move-to'
  return "unassigned_variable" if code == 'undefined-variable'
  return "boom_called" if code == 'boom-called'
  return "wrong_argument_type" if has_wrong_argument_type? code
  return "wrong_arguments_quantity" if code.include? 'arity-mismatch'

  code
end
fail_with(error) click to toggle source
# File lib/checker.rb, line 104
def fail_with(error)
  fail error[:status], details: error[:result]
end
has_wrong_argument_type?(code) click to toggle source
# File lib/checker.rb, line 123
def has_wrong_argument_type?(code)
  code.match Regexp.union('type-mismatch', 'expected-value-of-type')
end
is_expected_timeout(result) click to toggle source
# File lib/checker.rb, line 98
def is_expected_timeout(result)
  result[:finalBoardError] &&
  result[:finalBoardError][:reason][:code] == 'timeout' &&
  @options[:expect_endless_while]
end