Class: Belajar::Test

Inherits:
Object
  • Object
show all
Defined in:
lib/belajar/test.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Test

Returns a new instance of Test.



9
10
11
12
# File 'lib/belajar/test.rb', line 9

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/belajar/test.rb', line 7

def path
  @path
end

Instance Method Details

#run(solution_code) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/belajar/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