class Daigaku::Test

Constants

CODE_REGEX

Attributes

path[R]

Public Class Methods

new(path) click to toggle source
# File lib/daigaku/test.rb, line 9
def initialize(path)
  @unit_path = path
  @path      = Dir[File.join(path, '*spec.rb')].first
end

Public Instance Methods

run(solution_code) click to toggle source
# File lib/daigaku/test.rb, line 14
def run(solution_code)
  spec_code         = File.read(@path)
  patched_spec_code = insert_code(spec_code, solution_code.to_s)

  temp_spec = File.join(File.dirname(@path), "temp_#{File.basename(@path)}")
  create_temp_spec(temp_spec, patched_spec_code)

  result = `rspec --no-color --order defined --format j #{temp_spec}`
  remove_file(temp_spec)

  TestResult.new(result)
end

Private Instance Methods

create_temp_spec(path, content) click to toggle source
# File lib/daigaku/test.rb, line 33
def create_temp_spec(path, content)
  base_path = File.dirname(path)
  FileUtils.makedirs(base_path) unless Dir.exist?(base_path)
  File.open(path, 'w') { |f| f.puts content }
end
insert_code(spec, code) click to toggle source
# File lib/daigaku/test.rb, line 29
def insert_code(spec, code)
  spec.gsub(CODE_REGEX, code)
end
remove_file(path) click to toggle source
# File lib/daigaku/test.rb, line 39
def remove_file(path)
  FileUtils.rm(path) if File.exist?(path)
end