Class: Linecook::Test::CommandParser
- Inherits:
-
Object
- Object
- Linecook::Test::CommandParser
- Defined in:
- lib/linecook/test/command_parser.rb
Instance Attribute Summary collapse
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#ps1 ⇒ Object
readonly
Returns the value of attribute ps1.
-
#ps2 ⇒ Object
readonly
Returns the value of attribute ps2.
-
#suffix ⇒ Object
readonly
Returns the value of attribute suffix.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ CommandParser
constructor
A new instance of CommandParser.
- #parse(script) ⇒ Object
- #parse_cmd(cmd) ⇒ Object
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(={}) = { :ps1 => '% ', :ps2 => '> ', :prefix => '0<&- 2>&1 ', :suffix => '' }.merge() @ps1 = [:ps1] @ps2 = [:ps2] @prefix = [:prefix] @suffix = [:suffix] end |
Instance Attribute Details
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
6 7 8 |
# File 'lib/linecook/test/command_parser.rb', line 6 def prefix @prefix end |
#ps1 ⇒ Object (readonly)
Returns the value of attribute ps1.
4 5 6 |
# File 'lib/linecook/test/command_parser.rb', line 4 def ps1 @ps1 end |
#ps2 ⇒ Object (readonly)
Returns the value of attribute ps2.
5 6 7 |
# File 'lib/linecook/test/command_parser.rb', line 5 def ps2 @ps2 end |
#suffix ⇒ Object (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 |