Skip to content

Commit

Permalink
Merge pull request #123 from wikiti/match-assertions
Browse files Browse the repository at this point in the history
Regular expression matcher assertions
  • Loading branch information
elsassph committed Oct 23, 2016
2 parents 8351249 + 915b67e commit ca11b8f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/massive/munit/Assert.hx
Expand Up @@ -215,7 +215,43 @@ class Assert
assertionCount++;
if (expected == actual) fail("Value [" + actual +"] was the same as expected value [" + expected + "]", info);
}


/**
* Assert that a string matches a regular expression. The internal state of the given regular expression
* may be modified by this assertion.
*
* @param string value expected to match the regular expression
* @param regex a regular expression that should match the string value
* @throws AssertionException if regex does not match string
*/
public static function doesMatch(string:String, regexp:EReg, ?info:PosInfos)
{
assertionCount++;

var matches:Bool = regexp.match(string);
if (matches) return;

fail("Value [" + string +"] was expected to match [" + regexp + "]", info);
}

/**
* Assert that a string does not match a regular expression. The internal state of the given regular expression
* may be modified by this assertion.
*
* @param string value expected to not match the regular expression
* @param regex a regular expression that should not match the string value
* @throws AssertionException if regex matches string
*/
public static function doesNotMatch(string:String, regexp:EReg, ?info:PosInfos)
{
assertionCount++;

var matches:Bool = regexp.match(string);
if (!matches) return;

fail("Value [" + string +"] was expected to not match [" + regexp + "], and matched at [" + regexp.matchedPos().pos + "]", info);
}

/**
* Assert that an expectation was thrown. Can expect strings and non-strings.
*
Expand Down
33 changes: 33 additions & 0 deletions test/massive/munit/AssertTest.hx
Expand Up @@ -388,6 +388,22 @@ class AssertTest
Assert.fail("Invalid assertion not captured");
}

@Test
public function testDoesMatch():Void
{
Assert.doesMatch("regular_example_45-", ~/^regular_example_\d+\-$/);

try
{
Assert.doesMatch("regular_example_45", ~/^regular_\d+$/);
}
catch (e:AssertionException)
{
return;
}
Assert.fail("Invalid assertion not captured");
}

@Test
public function testAreNotSameString():Void
{
Expand Down Expand Up @@ -465,6 +481,23 @@ class AssertTest
Assert.fail("Invalid assertion not captured");
}

@Test
public function testDoesNotMatch():Void
{
Assert.doesNotMatch("this is a string", ~/^This is a string$/);
Assert.doesNotMatch("fff", ~/^\d+$/);

try
{
Assert.doesNotMatch("#198c19", ~/^#[0-9a-c]+$/i);
}
catch (e:AssertionException)
{
return;
}
Assert.fail("Invalid assertion not captured");
}

@Test
public function testFail():Void
{
Expand Down

0 comments on commit ca11b8f

Please sign in to comment.