unit testing generated content

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.
 
B

Brian Candler

sylvain said:
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

You can match a single regexp across a multi-line string; using /m
option is clearer since . then matches any character including newline.
=> 4
 
7

7stud --

sylvain said:
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.

How about:


def test_it(str)
num_lines = 4
regexs = [/^start/, /something .*/, /^\s*$/, /^end/]

lines = str.split("\n")
return false if lines.length != num_lines

match_results = lines.zip(regexs).collect{|line, regex| line =~ regex}
match_results.all?
end





s1 =<<ENDOFSTRING
start
something else

end of data
ENDOFSTRING

s2 =<<ENDOFSTRING
start
something else

end of data
too many lines
ENDOFSTRING

s3 =<<ENDOFSTRING
start
something else
no match here
end of data
ENDOFSTRING

[s1, s2, s3].each do |str|
puts test_it(str)
end

--output:--
true
false
false
 
S

Sylvain Viart

[Note: parts of this message were removed to make it a legal post.]

Hi,

Hey cool seems great.
I've tried to find a way of passing regexps through the block (Proc) without
success.

But by array it's fine too.

def test_it(str)
num_lines = 4
regexs = [/^start/, /something .*/, /^\s*$/, /^end/]

lines = str.split("\n")
return false if lines.length != num_lines

match_results = lines.zip(regexs).collect{|line, regex| line =~ regex}
match_results.all?
end



Thanks, I will post the final assert, when It will be ready.

Do you think I can group my own assertion in a module or something and load
it into my testcase ?
For the moment I've just added a method into the testcase, was used only
here.


Regards,
Sylvain Viart.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top