Class: Tap::Test::ScriptTester

Inherits:
Object
  • Object
show all
Includes:
Support::ShellUtils
Defined in:
lib/tap/test/script_tester.rb

Constant Summary collapse

NIL_VALIDATION =
lambda {|*args|}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::ShellUtils

#capture_sh, #redirect_sh, #sh

Constructor Details

#initialize(command_path = nil, stepwise = false, &run_block) ⇒ ScriptTester

Returns a new instance of ScriptTester.



21
22
23
24
25
26
# File 'lib/tap/test/script_tester.rb', line 21

def initialize(command_path=nil, stepwise=false, &run_block)
  @command_path = command_path
  @commands = []
  @stepwise = stepwise
  @run_block = run_block
end

Instance Attribute Details

#command_pathObject

The command path for self, returned by to_s



11
12
13
# File 'lib/tap/test/script_tester.rb', line 11

def command_path
  @command_path
end

#commandsObject (readonly)

An array of (command, message, expected, validation) entries, representing the accumulated test commands.



15
16
17
# File 'lib/tap/test/script_tester.rb', line 15

def commands
  @commands
end

#run_blockObject (readonly)

Returns the value of attribute run_block.



17
18
19
# File 'lib/tap/test/script_tester.rb', line 17

def run_block
  @run_block
end

#stepwiseObject (readonly)

Returns the value of attribute stepwise.



17
18
19
# File 'lib/tap/test/script_tester.rb', line 17

def stepwise
  @stepwise
end

Instance Method Details

#check(msg, command, use_regexp_escapes = true, &validation) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/tap/test/script_tester.rb', line 58

def check(msg, command, use_regexp_escapes=true, &validation)
  new_commands = split(command)
  commands = new_commands.collect do |cmd, expected|
    expected = RegexpEscape.new(expected) if expected && use_regexp_escapes
    [cmd, msg, expected, validation]
  end
  
  run(*commands)
end

#match(msg, command, regexp = nil, &validation) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/tap/test/script_tester.rb', line 68

def match(msg, command, regexp=nil, &validation)
  new_commands = split(command)
  commands = new_commands.collect do |cmd, expected|
    raise "expected text specified in match command" unless expected == nil
    [cmd, msg, regexp, validation]
  end
  
  run(*commands)
end

#run(*commands) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/tap/test/script_tester.rb', line 78

def run(*commands)
  commands.each_with_index do |(cmd, msg, expected, validation), i|
    unless expected || validation
      raise ArgumentError, "no expectation or validation set for: #{cmd}"
    end
    
    start = Time.now
    result = capture_sh(cmd) {|ok, status, tempfile_path| }
    elapsed = Time.now - start
    
    cmd_msg = commands.length > 1 ? "#{msg} (#{i})" : msg
    run_block.call(expected, result, %Q{#{cmd_msg}\n% #{cmd}}) if expected
    validation.call(result) if validation

    if stepwise
      print %Q{
------------------------------------
%s
> %s
%s
Time Elapsed: %.3fs} % [cmd_msg, cmd, result, elapsed]

      print "\nContinue? (y/n): "
      break if gets.strip =~ /^no?$/i
    else
      puts "%.3fs : %s" % [elapsed, cmd_msg]
    end
  end
end

#split(str) ⇒ Object

Splits the input string, collecting single-line commands and expected results. Nil will be used as the expected result if the result is whitespace, or not present.

cmd = ScriptTest.new
cmd.split %Q{
% command one
expected text for command one
% command two
% command three
expected text for command three
}  
# => [
# ["command one", "expected text for command one\n"],
# ["command two", nil],
# ["command three", "expected text for command three\n"]]


45
46
47
48
49
50
51
52
# File 'lib/tap/test/script_tester.rb', line 45

def split(str)
  str.split(/^%\s*/).collect do |s| 
    next(nil) if s.strip.empty?
    command, expected = s.split(/\n/, 2)
    expected = nil if expected && expected.strip.empty?
    [command.strip, expected]
  end.compact
end

#time(msg, command) ⇒ Object



54
55
56
# File 'lib/tap/test/script_tester.rb', line 54

def time(msg, command)
  run([command, msg, nil, NIL_VALIDATION])
end

#to_sObject

Returns the command path.



109
110
111
# File 'lib/tap/test/script_tester.rb', line 109

def to_s
  command_path
end