Sha256: 160ac37c073d483e82c46f6f0bb0592ba83324e73768f15ce35f7c62c6cf9f84

Contents?: true

Size: 1005 Bytes

Versions: 1

Compression:

Stored size: 1005 Bytes

Contents

module Belajar
  require 'fileutils'

  class Test

    attr_reader :path

    CODE_REGEX = /\[\['solution::code'\]\]/

    def initialize(path)
      @unit_path = path
      @path = Dir[File.join(path, '*spec.rb')].first
    end

    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 = %x{ rspec --color --format j #{temp_spec} }
      remove_file(temp_spec)

      TestResult.new(result)
    end

    private

    def insert_code(spec, code)
      spec.gsub(CODE_REGEX, code)
    end

    def create_temp_spec(path, content)
      base_path = File.dirname(path)
      FileUtils.mkdir_p(base_path) unless Dir.exist?(base_path)
      File.open(path, 'w') { |f| f.puts content }
    end

    def remove_file(path)
      FileUtils.rm(path) if File.exist?(path)
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
belajar-0.1.1 lib/belajar/test.rb