new method 'output_newline'

This commit is contained in:
Johan Holmberg 2017-11-09 16:14:33 +01:00
parent dae53f993b
commit 0880d93778
3 changed files with 171 additions and 6 deletions

View File

@ -81,6 +81,7 @@ module Cmdtest
@_env_path = @_runner.orig_env_path
@_t1 = @_t2 = 0
@_output_encoding = 'ascii'
@_output_newline = Util.windows? ? "\r\n" : "\n"
end
def output_encoding(encoding)
@ -97,6 +98,20 @@ module Cmdtest
end
end
def output_newline(newline)
if block_given?
saved_newline = @_output_newline
begin
@_output_newline = newline
yield
ensure
@_output_newline = saved_newline
end
else
@_output_newline = newline
end
end
#------------------------------
# Import file into the "workdir" from the outside world.
# The source is found relative to the current directory when "cmdtest"
@ -556,7 +571,7 @@ module Cmdtest
"error reading file: '#{file}'"
end
else
_xxx_equal(what, positive, actual.text(@_output_encoding), expected)
_xxx_equal(what, positive, actual.text(@_output_encoding, @_output_newline), expected)
end
end
end
@ -596,7 +611,7 @@ module Cmdtest
def _stdxxx_contain_aux(stdxxx, positive, expected)
_process_after do
@_checked[stdxxx] = true
actual = @_effects.send(stdxxx).text(@_output_encoding)
actual = @_effects.send(stdxxx).text(@_output_encoding, @_output_newline)
_xxx_contain(stdxxx, positive, actual, expected)
end
end
@ -665,7 +680,7 @@ module Cmdtest
def _stdxxx_equal_aux(stdxxx, positive, expected)
_process_after do
@_checked[stdxxx] = true
actual = @_effects.send(stdxxx).text(@_output_encoding)
actual = @_effects.send(stdxxx).text(@_output_encoding, @_output_newline)
_xxx_equal(stdxxx, positive, actual, expected)
end
end

View File

@ -85,7 +85,7 @@ module Cmdtest
@bytes = bytes
end
def text(encoding)
def text(encoding, newline)
extern_text = @bytes.dup
extern_text.force_encoding(encoding)
if ! extern_text.valid_encoding?
@ -96,7 +96,8 @@ module Cmdtest
n_crnl = str.scan("\r\n").size
n_windows = n_crnl
n_unix = n_nl - n_crnl
if Util.windows?
case newline
when "\r\n"
if n_unix > 0 && n_windows == 0
raise AssertFailed, "ERROR: UNIX line ending: #{@name}"
elsif n_unix > 0 && n_windows > 0
@ -104,7 +105,7 @@ module Cmdtest
else
return str.gsub("\r\n", "\n")
end
else
when "\n"
if n_unix == 0 && n_windows > 0
raise AssertFailed, "ERROR: Windows line ending: #{@name}"
elsif n_unix > 0 && n_windows > 0
@ -112,6 +113,14 @@ module Cmdtest
else
return str
end
when :consistent
if n_unix > 0 && n_windows > 0
raise AssertFailed, "ERROR: mixed line ending: #{@name}"
else
return str.gsub("\r\n", "\n")
end
else
raise RuntimeError, "unkown newline type: #{newline.inspect}"
end
end
end

View File

@ -72,4 +72,145 @@ class CMDTEST_crnl < Cmdtest::Testcase
end
end
def test_crnl_EXPECTED
create_CMDTEST_foo [
'cmd "echo_crnl.rb 1:rn 2:rn" do',
' comment "windows line endings"',
' output_newline "\r\n" do',
' stdout_equal "1\n2\n"',
' end',
'end',
]
cmd_cmdtest do
stdout_equal [
"### windows line endings",
]
end
end
def test_crnl_NOT_EXPECTED
create_CMDTEST_foo [
'cmd "echo_crnl.rb 1:rn 2:rn" do',
' comment "windows line endings"',
' output_newline "\n" do',
' stdout_equal "1\n2\n"',
' end',
'end',
]
cmd_cmdtest do
stdout_equal [
"### windows line endings",
"--- ERROR: Windows line ending: STDOUT",
]
exit_nonzero
end
end
def test_nl_EXPECTED
create_CMDTEST_foo [
'cmd "echo_crnl.rb 1:n 2:n" do',
' comment "linux line endings"',
' output_newline "\n" do',
' stdout_equal "1\n2\n"',
' end',
'end',
]
cmd_cmdtest do
stdout_equal [
"### linux line endings",
]
end
end
def test_nl_NOT_EXPECTED
create_CMDTEST_foo [
'cmd "echo_crnl.rb 1:n 2:n" do',
' comment "linux line endings"',
' output_newline "\r\n" do',
' stdout_equal "1\n2\n"',
' end',
'end',
]
cmd_cmdtest do
stdout_equal [
"### linux line endings",
"--- ERROR: UNIX line ending: STDOUT",
]
exit_nonzero
end
end
def test_unknown_OUTPUT_NEWLINE
create_CMDTEST_foo [
'cmd "echo_crnl.rb 1:n 2:n" do',
' comment "linux line endings"',
' output_newline "foobar" do',
' stdout_equal "1\n2\n"',
' end',
'end',
]
cmd_cmdtest do
stdout_equal /unkown newline type: "foobar"/
exit_nonzero
end
end
def test_CONSISTENT_EXPECTED_nl
create_CMDTEST_foo [
'cmd "echo_crnl.rb 1:n 2:n" do',
' comment "consistent line endings"',
' output_newline :consistent do',
' stdout_equal "1\n2\n"',
' end',
'end',
]
cmd_cmdtest do
stdout_equal [
"### consistent line endings",
]
end
end
def test_CONSISTENT_EXPECTED_crnl
create_CMDTEST_foo [
'cmd "echo_crnl.rb 1:rn 2:rn" do',
' comment "consistent line endings"',
' output_newline :consistent do',
' stdout_equal "1\n2\n"',
' end',
'end',
]
cmd_cmdtest do
stdout_equal [
"### consistent line endings",
]
end
end
def test_CONSISTENT_EXPECTED_mixed
create_CMDTEST_foo [
'cmd "echo_crnl.rb 1:rn 2:n" do',
' comment "consistent line endings"',
' output_newline :consistent do',
' stdout_equal "1\n2\n"',
' end',
'end',
]
cmd_cmdtest do
stdout_equal [
"### consistent line endings",
"--- ERROR: mixed line ending: STDOUT",
]
exit_nonzero
end
end
end