Class: Slightish::TestCase

Inherits:
Object
  • Object
show all
Defined in:
lib/slightish/test_case.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_file) ⇒ TestCase

Returns a new instance of TestCase.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/slightish/test_case.rb', line 12

def initialize(source_file)
  @source_file = source_file
  @expected_exit_code = 0

  @start_line = @end_line = -1
  @raw_command = @command = nil
  @raw_expected_output = @expected_output = nil
  @raw_expected_error_output = @expected_error_output = nil
  @actual_output = @actual_error_output = nil
  @actual_exit_code = nil
end

Instance Attribute Details

#actual_error_outputObject (readonly)

Returns the value of attribute actual_error_output.



10
11
12
# File 'lib/slightish/test_case.rb', line 10

def actual_error_output
  @actual_error_output
end

#actual_exit_codeObject (readonly)

Returns the value of attribute actual_exit_code.



10
11
12
# File 'lib/slightish/test_case.rb', line 10

def actual_exit_code
  @actual_exit_code
end

#actual_outputObject (readonly)

Returns the value of attribute actual_output.



10
11
12
# File 'lib/slightish/test_case.rb', line 10

def actual_output
  @actual_output
end

#commandObject (readonly)

Returns the value of attribute command.



6
7
8
# File 'lib/slightish/test_case.rb', line 6

def command
  @command
end

#end_lineObject

Returns the value of attribute end_line.



5
6
7
# File 'lib/slightish/test_case.rb', line 5

def end_line
  @end_line
end

#expected_error_outputObject (readonly)

Returns the value of attribute expected_error_output.



8
9
10
# File 'lib/slightish/test_case.rb', line 8

def expected_error_output
  @expected_error_output
end

#expected_exit_codeObject

Returns the value of attribute expected_exit_code.



9
10
11
# File 'lib/slightish/test_case.rb', line 9

def expected_exit_code
  @expected_exit_code
end

#expected_outputObject (readonly)

Returns the value of attribute expected_output.



7
8
9
# File 'lib/slightish/test_case.rb', line 7

def expected_output
  @expected_output
end

#raw_commandObject (readonly)

Returns the value of attribute raw_command.



6
7
8
# File 'lib/slightish/test_case.rb', line 6

def raw_command
  @raw_command
end

#raw_expected_error_outputObject (readonly)

Returns the value of attribute raw_expected_error_output.



8
9
10
# File 'lib/slightish/test_case.rb', line 8

def raw_expected_error_output
  @raw_expected_error_output
end

#raw_expected_outputObject (readonly)

Returns the value of attribute raw_expected_output.



7
8
9
# File 'lib/slightish/test_case.rb', line 7

def raw_expected_output
  @raw_expected_output
end

#source_fileObject (readonly)

Returns the value of attribute source_file.



4
5
6
# File 'lib/slightish/test_case.rb', line 4

def source_file
  @source_file
end

#start_lineObject

Returns the value of attribute start_line.



5
6
7
# File 'lib/slightish/test_case.rb', line 5

def start_line
  @start_line
end

Instance Method Details

#append_command(str) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/slightish/test_case.rb', line 69

def append_command(str)
  if @raw_command.nil?
    @raw_command = str
  else
    # bash eats newlines from multiline strings, so no \n here
    # For example:
    # "echo a\
    # b"
    # produces "ab"
    @raw_command += str
  end
end

#append_expected_error_output(str) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/slightish/test_case.rb', line 90

def append_expected_error_output(str)
  if @raw_expected_error_output.nil?
    @raw_expected_error_output = str
  else
    @raw_expected_error_output += "\n" + str
  end
end

#append_expected_output(str) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/slightish/test_case.rb', line 82

def append_expected_output(str)
  if @raw_expected_output.nil?
    @raw_expected_output = str
  else
    @raw_expected_output += "\n" + str
  end
end

#failed?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/slightish/test_case.rb', line 39

def failed?
  !passed?
end

#failure_descriptionObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/slightish/test_case.rb', line 43

def failure_description
  res = ''
  res += output_failure_description('stdout', @expected_output, @actual_output) unless @expected_output == @actual_output

  if @expected_error_output != @actual_error_output
    res += "\n\n" unless res.empty?
    res += output_failure_description('stderr', @expected_error_output, @actual_error_output)
  end

  if @actual_exit_code != @expected_exit_code
    res += "\n\n" unless res.empty?
    res += 'Expected exit code: '.red.bold + @expected_exit_code.to_s.gray + "\n"
    res += 'Actual exit code: '.green.bold + @actual_exit_code.to_s.gray
  end

  res
end

#passed?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/slightish/test_case.rb', line 33

def passed?
  @actual_output == @expected_output &&
    @actual_error_output == @expected_error_output &&
    @actual_exit_code == @expected_exit_code
end

#run(sandbox) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/slightish/test_case.rb', line 24

def run(sandbox)
  expand(sandbox)

  @actual_output, @actual_error_output, process_status = Open3.capture3(@command, { chdir: sandbox.path })
  @actual_output.chomp!
  @actual_error_output.chomp!
  @actual_exit_code = process_status.exitstatus
end

#source_descriptionObject



61
62
63
64
65
66
67
# File 'lib/slightish/test_case.rb', line 61

def source_description
  if @start_line == @end_line
    "#{@source_file}:#{@start_line}"
  else
    "#{@source_file}:#{@start_line}-#{@end_line}"
  end
end