lunarmodules / luassert

Assertion library for Lua
MIT License
206 stars 77 forks source link

feature request: string pattern matching #87

Closed djerius closed 9 years ago

djerius commented 9 years ago

I'm testing code which uses the common

ok, retval = func( ... )

paradigm, where retval is an error message/object if ok is false. Just as with error messages tossed via exceptions (and handled by has_error) it would be nice to be able to compare the result with a match rather than an exact equality, e.g.

assert.are.matches( '.*: missing file', retval )

Thanks for a great testing framework! Diab

djerius commented 9 years ago

It seems that I'm mistaken about has_error After perusing the code I see that it only supports exact substring matching and matching numbers at ends of strings.

A generic pattern match would be nice. I have errors which include keywords taken from tables, so their order in the error message are non-deterministic. I need to be able to check for strings like

 a and b are excluded
 b and a are excluded

and ensure that the a and b keywords are in the error message (else I could just look for the string are excluded )

Here's some example code:

https://gist.github.com/djerius/7879cbd6d9ac66257e32

It allows one to write

 assert.matches( "[ab] and [ab] are excluded",  retval )
o-lim commented 9 years ago

@djerius I think this went into v1.7.7