Class: Linecook::Test::CommandParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CommandParser

Returns a new instance of CommandParser.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/linecook/test/command_parser.rb', line 9

def initialize(options={})
  options = {
    :ps1 => '% ',
    :ps2 => '> ',
    :prefix => '0<&- 2>&1 ',
    :suffix => ''
  }.merge(options)
  
  @ps1 = options[:ps1]
  @ps2 = options[:ps2]
  @prefix = options[:prefix]
  @suffix = options[:suffix]
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



6
7
8
# File 'lib/linecook/test/command_parser.rb', line 6

def prefix
  @prefix
end

#ps1Object (readonly)

Returns the value of attribute ps1.



4
5
6
# File 'lib/linecook/test/command_parser.rb', line 4

def ps1
  @ps1
end

#ps2Object (readonly)

Returns the value of attribute ps2.



5
6
7
# File 'lib/linecook/test/command_parser.rb', line 5

def ps2
  @ps2
end

#suffixObject (readonly)

Returns the value of attribute suffix.



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

def suffix
  @suffix
end

Instance Method Details

#parse(script) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/linecook/test/command_parser.rb', line 31

def parse(script)
  commands = []
  
  command, output, exit_status = nil, "", 0
  script.each_line do |line|
    case
    when line.index(ps1) == 0
      if command
        commands << ["#{prefix}#{command}#{suffix}", output, exit_status]
      end
      
      command, output, exit_status = parse_cmd lchomp(ps1, line)
      
    when command.nil?
      unless line.strip.empty?
        command, output, exit_status = parse_cmd(line)
      end
      
    when line.index(ps2) == 0
      command << lchomp(ps2, line)
    
    when output.nil?
      output = line
    
    else
      output << line
    end
  end
  
  if command
    commands << ["#{prefix}#{command}#{suffix}", output, exit_status]
  end
  
  commands
end

#parse_cmd(cmd) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/linecook/test/command_parser.rb', line 23

def parse_cmd(cmd)
  cmd =~ /.*?#\s*(?:\[(\d+)\])?\s*(\.{3})?/
  exit_status = $1 ? $1.to_i : 0
  output = $2 ? nil : ""
  
  [cmd, output, exit_status]
end