S
sylvain
Hi,
I'm writing unit test.
I've programmed already an assert method, which purpose it to compare
a string or a file with unix diff.
The goal is to assert that a generated content multiline is exactly
what I expect.
All the beginning of the code is a kind of polymorphism, treating
argument as string or filename
All the assert job is really done in the 4 last line, with unix diff.
def assert_content_equal(expected, actual, message =3D nil)
# compare avec diff
expected_f =3D "/tmp/expected#{$$}"
actual_f =3D "/tmp/actual#{$$}"
remove_expected_f =3D true
remove_actual_f =3D true
if expected.kind_of? String
if (expected.size < 255 and FileTest.exists? expected)
expected_f =3D expected
remove_expected_f =3D false
else
# cr=E9ation du fichier avec le contenu de la string
expected
File.open(expected_f, "w") { |f| f.print expected }
end
else
raise "expected String ou nom de fichier"
end
# code idem avec s/expected/actual/g
# besoin de matchit : ?if expected^MV%%"cy/# code idem^M2jV%
%x"cPV%%:s/expected/actual/g^M
if actual.kind_of? String
if (actual.size < 255 and FileTest.exists? actual)
actual_f =3D actual
remove_actual_f =3D false
else
# cr=E9ation du fichier avec le contenu de la string
actual
File.open(actual_f, "w") { |f| f.print actual }
end
else
raise "actual String ou nom de fichier"
end
diff =3D `diff #{expected_f} #{actual_f}`
File.unlink(expected_f) if remove_expected_f
File.unlink(actual_f) if remove_actual_f
assert_block("=E9chec diff <expected >actual\n#{diff}") { $? =3D=3D
0 }
end
What I would like to do now, is to have a regexp assert multiline
match, some thing like :
assert_match_regexp(actual) {
/^start/
/something .*/
/^\s*$/
/^end/
}
Which mean I expect 4 lines, individually matching the associated
regexp
Do you see a want to achieve?
I mean, reading the blockgiven, counting the regexp, and asserting the
whole...
More that that a full regexpdiff in fact ! ;-)
Any link or clue/hint, would be nice.
Regards,
Sylvain.
I'm writing unit test.
I've programmed already an assert method, which purpose it to compare
a string or a file with unix diff.
The goal is to assert that a generated content multiline is exactly
what I expect.
All the beginning of the code is a kind of polymorphism, treating
argument as string or filename
All the assert job is really done in the 4 last line, with unix diff.
def assert_content_equal(expected, actual, message =3D nil)
# compare avec diff
expected_f =3D "/tmp/expected#{$$}"
actual_f =3D "/tmp/actual#{$$}"
remove_expected_f =3D true
remove_actual_f =3D true
if expected.kind_of? String
if (expected.size < 255 and FileTest.exists? expected)
expected_f =3D expected
remove_expected_f =3D false
else
# cr=E9ation du fichier avec le contenu de la string
expected
File.open(expected_f, "w") { |f| f.print expected }
end
else
raise "expected String ou nom de fichier"
end
# code idem avec s/expected/actual/g
# besoin de matchit : ?if expected^MV%%"cy/# code idem^M2jV%
%x"cPV%%:s/expected/actual/g^M
if actual.kind_of? String
if (actual.size < 255 and FileTest.exists? actual)
actual_f =3D actual
remove_actual_f =3D false
else
# cr=E9ation du fichier avec le contenu de la string
actual
File.open(actual_f, "w") { |f| f.print actual }
end
else
raise "actual String ou nom de fichier"
end
diff =3D `diff #{expected_f} #{actual_f}`
File.unlink(expected_f) if remove_expected_f
File.unlink(actual_f) if remove_actual_f
assert_block("=E9chec diff <expected >actual\n#{diff}") { $? =3D=3D
0 }
end
What I would like to do now, is to have a regexp assert multiline
match, some thing like :
assert_match_regexp(actual) {
/^start/
/something .*/
/^\s*$/
/^end/
}
Which mean I expect 4 lines, individually matching the associated
regexp
Do you see a want to achieve?
I mean, reading the blockgiven, counting the regexp, and asserting the
whole...
More that that a full regexpdiff in fact ! ;-)
Any link or clue/hint, would be nice.
Regards,
Sylvain.