Class: TestBuffer

Inherits:
Array
  • Object
show all
Defined in:
lib/orb/test_buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_file, source_line) ⇒ TestBuffer

Returns a new instance of TestBuffer.



6
7
8
9
# File 'lib/orb/test_buffer.rb', line 6

def initialize(source_file, source_line)
  @source_file = source_file
  @source_line = source_line
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/orb/test_buffer.rb', line 4

def name
  @name
end

Instance Method Details

#edit_contents!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/orb/test_buffer.rb', line 11

def edit_contents!
  path = "#{Dir::tmpdir}/orb-#{Kernel.rand 1000000}.rb"
  begin
    File.open(path, "w") do |f|
      f.write(join("\n"))
      f.flush
      system("#{ENV['EDITOR'] || '/usr/bin/vim'} #{f.path}")
      replace(File.read(f.path).lines.map(&:chomp))
    end
  ensure
    File.unlink(path)
  end
end

#generate_test(indentation_level) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/orb/test_buffer.rb', line 35

def generate_test(indentation_level)
  indent = "  " * indentation_level
  "".tap do |test|
    test << indent << "test \"#{name}\" do\n"
    each do |line|
      test << indent << "  " << line << "\n"
    end
    test << indent << "end\n"
  end
end

#indentation_levelObject



46
47
48
49
# File 'lib/orb/test_buffer.rb', line 46

def indentation_level
  orb_line = File.readlines(@source_file)[@source_line - 1]
  orb_line.scan(/(\s*).*/).flatten.first.size / 2
end

#run(context) ⇒ Object



25
26
27
28
29
# File 'lib/orb/test_buffer.rb', line 25

def run(context)
  each do |line|
    context.eval(line)
  end
end

#splice_into_file(content) ⇒ Object



51
52
53
54
55
56
# File 'lib/orb/test_buffer.rb', line 51

def splice_into_file(content)
  lines = File.readlines(@source_file)
  index = @source_line - 1
  lines[index] = content
  File.open(@source_file, "w") { |f| f.write lines.join("") }
end

#writeObject



31
32
33
# File 'lib/orb/test_buffer.rb', line 31

def write
  splice_into_file(generate_test(indentation_level))
end