This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/ant-antlibs-antunit.git
commit 0d5bb05c190c89fda45a15f272e2bf30b950ff1d Author: Stefan Bodewig <[email protected]> AuthorDate: Sun May 17 13:07:05 2026 +0200 provide built-in negations for remaining assertions --- changes.xml | 4 +- docs/assertions.html | 222 ++++++++++++++++++++- src/etc/testcases/assert.xml | 59 ++++++ src/main/org/apache/ant/antunit/antlib.xml | 81 +++++++- .../junit/org/apache/ant/antunit/AssertTest.java | 45 ++++- 5 files changed, 392 insertions(+), 19 deletions(-) diff --git a/changes.xml b/changes.xml index ad29a93..56194b0 100644 --- a/changes.xml +++ b/changes.xml @@ -46,7 +46,9 @@ Only works properly with Ant 1.10.13 or later. </action> <action type="add"> - Add a new assertPropertyNotSet assertion. + Add new assertNotEquals, assertPropertyNotSet, + assertPropertyNotEquals, assertPropertyDoesntContain and + assertReferenceIsNotSet assertions. </action> </release> <release version="1.4.1" date="2021-07-07"> diff --git a/docs/assertions.html b/docs/assertions.html index f7123f0..23bcc26 100644 --- a/docs/assertions.html +++ b/docs/assertions.html @@ -89,7 +89,54 @@ <h3>Examples</h3> <pre> <assertEquals message="foo is not bar" expected="bar" - actual="${foo}" casesensitive="false"/> + actual="${foo}" casesensitive="false"/> + </pre> + + <h2><a name="assertNotEquals">assertNotEquals</a></h2> + + <p>Asserts that its two arguments are not equal.</p> + + <p><em>Since AntUnit 1.5</em></p> + + <table border="1" cellpadding="2" cellspacing="0"> + <tr> + <td valign="top"><b>Attribute</b></td> + <td valign="top"><b>Description</b></td> + <td align="center" valign="top"><b>Required</b></td> + </tr> + <tr> + <td valign="top">notExpected</td> + <td valign="top">First string to test.</td> + <td valign="top" align="center">Yes</td> + </tr> + <tr> + <td valign="top">actual</td> + <td valign="top">Second string to test.</td> + <td valign="top" align="center">Yes</td> + </tr> + <tr> + <td valign="top">message</td> + <td valign="top">Message for the exception if the condition + doesn't hold true. Defaults to "Didn't expect + '<em>notExpected</em>' to be '<em>actual</em>'".</td> + <td align="center">No.</td> + </tr> + <tr> + <td valign="top">casesensitive</td> + <td valign="top">Perform a case sensitive comparision. + Default is true.</td> + <td valign="top" align="center">No</td> + </tr> + </table> + + <h3>Examples</h3> + + <p>Make the build fail with the message "foo is bar" if the + property foo doesn't hold the value bar regardless of case:</p> + + <pre> + <assertNotEquals message="foo is bar" notExpected="bar" + actual="${foo}" casesensitive="false"/> </pre> <h2><a name="assertPropertySet">assertPropertySet</a></h2> @@ -125,6 +172,42 @@ <h3>Examples</h3> <assertPropertySet name="foo"/> </pre> + <h2><a name="assertPropertyNotSet">assertPropertyNotSet</a></h2> + + <p><em>Since AntUnit 1.5</em></p> + + <p>Asserts that a property hasn't got a value.</p> + + <table border="1" cellpadding="2" cellspacing="0"> + <tr> + <td valign="top"><b>Attribute</b></td> + <td valign="top"><b>Description</b></td> + <td align="center" valign="top"><b>Required</b></td> + </tr> + <tr> + <td valign="top">name</td> + <td valign="top">Name of the property.</td> + <td valign="top" align="center">Yes</td> + </tr> + <tr> + <td valign="top">message</td> + <td valign="top">Message for the exception if the condition + doesn't hold true. Defaults to "Didn't expect property + '<em>name</em>' to be set but was '<em>actual value</em>'".</td> + <td align="center">No.</td> + </tr> + </table> + + <h3>Examples</h3> + + <p>Make the build fail with the message "Didn't expect property + 'foo' but was 'bar'" if the property foo is defined and has + value bar:</p> + + <pre> + <assertPropertyNotSet name="foo"/> + </pre> + <h2><a name="assertPropertyEquals">assertPropertyEquals</a></h2> <p>Asserts that a given property is set and holds a certain @@ -169,7 +252,54 @@ <h3>Examples</h3> <pre> <assertPropertyEquals message="foo is not bar" value="bar" - name="foo" casesensitive="false"/> + name="foo" casesensitive="false"/> + </pre> + + <h2><a name="assertPropertyNotEquals">assertPropertyNotEquals</a></h2> + + <p><em>Since AntUnit 1.5</em></p> + + <p>Asserts that a given property is set but doesn't hold a certain + value.</p> + + <table border="1" cellpadding="2" cellspacing="0"> + <tr> + <td valign="top"><b>Attribute</b></td> + <td valign="top"><b>Description</b></td> + <td align="center" valign="top"><b>Required</b></td> + </tr> + <tr> + <td valign="top">name</td> + <td valign="top">Name of the property.</td> + <td valign="top" align="center">Yes</td> + </tr> + <tr> + <td valign="top">value</td> + <td valign="top">Not expected value.</td> + <td valign="top" align="center">Yes</td> + </tr> + <tr> + <td valign="top">message</td> + <td valign="top">Message for the exception if the condition + doesn't hold true. Defaults to "Didn't expect property '<em>name</em>' to have value '<em>value</em>'".</td> + <td align="center">No.</td> + </tr> + <tr> + <td valign="top">casesensitive</td> + <td valign="top">Perform a case sensitive comparision. + Default is true.</td> + <td valign="top" align="center">No</td> + </tr> + </table> + + <h3>Examples</h3> + + <p>Make the build fail with the message "foo is bar" if the + property foo holds the value bar regardless of case:</p> + + <pre> + <assertPropertyNotEquals message="foo is bar" value="bar" + name="foo" casesensitive="false"/> </pre> <h2><a name="assertPropertyContains">assertPropertyContains</a></h2> @@ -219,6 +349,55 @@ <h3>Examples</h3> name="foo" casesensitive="false"/> </pre> + <h2><a name="assertPropertyDoesntContain">assertPropertyDoesntContain</a></h2> + + <p>Asserts that a given property is set and doesn't hold a certain + value as substring.</p> + + <p><em>Since AntUnit 1.5</em></p> + + <table border="1" cellpadding="2" cellspacing="0"> + <tr> + <td valign="top"><b>Attribute</b></td> + <td valign="top"><b>Description</b></td> + <td align="center" valign="top"><b>Required</b></td> + </tr> + <tr> + <td valign="top">name</td> + <td valign="top">Name of the property.</td> + <td valign="top" align="center">Yes</td> + </tr> + <tr> + <td valign="top">value</td> + <td valign="top">Not expected substring value.</td> + <td valign="top" align="center">Yes</td> + </tr> + <tr> + <td valign="top">message</td> + <td valign="top">Message for the exception if the condition + doesn't hold true. Defaults to "Didn't expect property + '<em>name</em>' to contain value '<em>value</em>' but was + '<em>value of property name</em>'".</td> + <td align="center">No.</td> + </tr> + <tr> + <td valign="top">casesensitive</td> + <td valign="top">Perform a case sensitive comparision. + Default is true.</td> + <td valign="top" align="center">No</td> + </tr> + </table> + + <h3>Examples</h3> + + <p>Make the build fail with the message "foo contains bar" if the + property foo contains the value bar regardless of case:</p> + + <pre> + <assertPropertyDoesntContain message="foo contains bar" value="bar" + name="foo" casesensitive="false"/> + </pre> + <h2><a name="assertFileExists">assertFileExists</a></h2> <p>Asserts that a given file exists.</p> @@ -607,7 +786,7 @@ <h2><a name="assertFilesMatch">assertFilesMatch</a></h2> <td align="center">No.</td> </tr> </table> - + <h3>Example</h3> <p>Make the build fail if the file build.xml and backups/build.xml @@ -645,7 +824,7 @@ <h2><a name="assertFilesDiffer">assertFilesDiffer</a></h2> <td align="center">No.</td> </tr> </table> - + <h3>Example</h3> <p>Make the build fail if the file build.xml and backups/build.xml @@ -688,6 +867,41 @@ <h3>Examples</h3> <assertReferenceSet name="foo"/> </pre> + <h2><a name="assertReferenceIsNotSet">assertReferenceIsNotSet</a></h2> + + <p>Asserts that a reference hasn't got any value.</p> + + <p><em>Since AntUnit 1.5</em></p> + + <table border="1" cellpadding="2" cellspacing="0"> + <tr> + <td valign="top"><b>Attribute</b></td> + <td valign="top"><b>Description</b></td> + <td align="center" valign="top"><b>Required</b></td> + </tr> + <tr> + <td valign="top">refid</td> + <td valign="top">Id of the reference.</td> + <td valign="top" align="center">Yes</td> + </tr> + <tr> + <td valign="top">message</td> + <td valign="top">Message for the exception if the condition + doesn't hold true. Defaults to "Didn't expect reference + '<em>id</em>'".</td> + <td align="center">No.</td> + </tr> + </table> + + <h3>Examples</h3> + + <p>Make the build fail with the message "Didn't expect reference 'foo'" + if the reference foo is defined:</p> + + <pre> + <assertReferenceIsNotSet name="foo"/> + </pre> + <h2><a name="assertReferenceIsType">assertReferenceIsType</a></h2> <p>Asserts that a reference has a value of a given type.</p> diff --git a/src/etc/testcases/assert.xml b/src/etc/testcases/assert.xml index ca33ada..3677571 100644 --- a/src/etc/testcases/assert.xml +++ b/src/etc/testcases/assert.xml @@ -77,6 +77,22 @@ under the License. <au:assertEquals expected="bar" actual="BAR"/> </target> + <target name="assertNotEqualsPass1"> + <au:assertNotEquals notexpected="bar" actual="baz"/> + </target> + + <target name="assertNotEqualsPass2"> + <au:assertNotEquals notexpected="bar" actual="BAR"/> + </target> + + <target name="assertNotEqualsFail"> + <au:assertNotEquals notexpected="bar" actual="bar"/> + </target> + + <target name="assertNotEqualsCaseFail"> + <au:assertNotEquals notexpected="bar" actual="BAR" casesensitive="false"/> + </target> + <target name="assertPropertySetPass"> <property name="foo" value="bar"/> <au:assertPropertySet name="foo"/> @@ -119,6 +135,30 @@ under the License. <au:assertPropertyEquals name="foo" value="BAR"/> </target> + <target name="assertPropertyNotEqualsPass1"> + <property name="foo" value="bar"/> + <au:assertPropertyNotEquals name="foo" value="baz"/> + </target> + + <target name="assertPropertyNotEqualsPass2"> + <property name="foo" value="bar"/> + <au:assertPropertyNotEquals name="foo" value="BAR"/> + </target> + + <target name="assertPropertyNotEqualsFail1"> + <au:assertPropertyNotEquals name="foo" value="bar"/> + </target> + + <target name="assertPropertyNotEqualsFail2"> + <property name="foo" value="bar"/> + <au:assertPropertyNotEquals name="foo" value="bar"/> + </target> + + <target name="assertPropertyNotEqualsCaseFail"> + <property name="foo" value="bar"/> + <au:assertPropertyNotEquals name="foo" value="BAR" casesensitive="false"/> + </target> + <target name="assertPropertyContains"> <property name="foo" value="bar"/> <au:assertPropertyContains name="foo" value="ba"/> @@ -129,6 +169,16 @@ under the License. <au:assertPropertyContains name="foo" value="foo"/> </target> + <target name="assertPropertyDoesntContain"> + <property name="foo" value="bar"/> + <au:assertPropertyDoesntContain name="foo" value="foo"/> + </target> + + <target name="assertPropertyDoesntContainFail"> + <property name="foo" value="bar"/> + <au:assertPropertyDoesntContain name="foo" value="ba"/> + </target> + <target name="assertFileExistsPass"> <au:assertFileExists file="assert.xml"/> </target> @@ -214,6 +264,15 @@ under the License. <au:assertReferenceSet refid="foo2"/> </target> + <target name="assertReferenceIsNotSetPass"> + <au:assertReferenceIsNotSet refid="foo3"/> + </target> + + <target name="assertReferenceIsNotSetFail"> + <path id="foo4"/> + <au:assertReferenceIsNotSet refid="foo4"/> + </target> + <target name="assertReferenceIsTypePass"> <path id="foo3"/> <au:assertReferenceIsType refid="foo3" type="path"/> diff --git a/src/main/org/apache/ant/antunit/antlib.xml b/src/main/org/apache/ant/antunit/antlib.xml index 115387c..c6e2f76 100644 --- a/src/main/org/apache/ant/antunit/antlib.xml +++ b/src/main/org/apache/ant/antunit/antlib.xml @@ -88,6 +88,22 @@ under the License. </sequential> </macrodef> + <macrodef name="assertNotEquals" backtrace="false"> + <attribute name="notExpected"/> + <attribute name="actual"/> + <attribute name="casesensitive" default="true"/> + <attribute name="message" + default="Didn't expect '@{notExpected}' to be '@{actual}'"/> + <sequential> + <au:fail message="@{message}"> + <not> + <equals arg1="@{notExpected}" arg2="@{actual}" + casesensitive="@{casesensitive}"/> + </not> + </au:fail> + </sequential> + </macrodef> + <macrodef name="assertPropertySet" backtrace="false"> <attribute name="name"/> <attribute name="message" @@ -102,7 +118,7 @@ under the License. <macrodef name="assertPropertyNotSet" backtrace="false"> <attribute name="name"/> <attribute name="message" - default="Expected property '@{name}' to not be set but was '${@{name}}'"/> + default="Didn't expect property '@{name}' to be set but was '${@{name}}'"/> <sequential> <au:fail message="@{message}"> <not> @@ -125,6 +141,19 @@ under the License. </sequential> </macrodef> + <macrodef name="assertPropertyNotEquals" backtrace="false"> + <attribute name="name"/> + <attribute name="value"/> + <attribute name="casesensitive" default="true"/> + <attribute name="message" + default="Didn't expect property '@{name}' to have value '@{value}'"/> + <sequential> + <au:assertPropertySet message="@{message}" name="@{name}"/> + <au:assertNotEquals message="@{message}" notexpected="@{value}" + actual="${@{name}}" casesensitive="@{casesensitive}"/> + </sequential> + </macrodef> + <macrodef name="assertPropertyContains" backtrace="false"> <attribute name="name"/> <attribute name="value"/> @@ -139,7 +168,24 @@ under the License. </au:fail> </sequential> </macrodef> - + + <macrodef name="assertPropertyDoesntContain" backtrace="false"> + <attribute name="name"/> + <attribute name="value"/> + <attribute name="casesensitive" default="true"/> + <attribute name="message" + default="Didn't expect property '@{name}' to contain value '@{value}' but was '${@{name}}'"/> + <sequential> + <au:assertPropertySet message="@{message}" name="@{name}"/> + <au:fail message="@{message}"> + <not> + <contains string="${@{name}}" substring="@{value}" + casesensitive="@{casesensitive}"/> + </not> + </au:fail> + </sequential> + </macrodef> + <macrodef name="assertFileExists" backtrace="false"> <attribute name="file"/> <attribute name="message" @@ -189,11 +235,11 @@ under the License. <attribute name="value"/> <attribute name="casesensitive" default="true"/> <attribute name="message" - default="Expected resource '@{resource}' to contain value '@{value}' but was '${@{resource}}'"/> + default="Expected resource '@{resource}' to contain value '@{value}' but was '${@{resource}}'"/> <sequential> <au:assertResourceExists resource="@{resource}"/> <au:fail message="@{message}"> - <resourcecontains resource="@{resource}" substring="@{value}" + <resourcecontains resource="@{resource}" substring="@{value}" casesensitive="@{casesensitive}"/> </au:fail> </sequential> @@ -204,10 +250,10 @@ under the License. <attribute name="value"/> <attribute name="casesensitive" default="true"/> <attribute name="message" - default="Didn't expect resource '@{resource}' to contain value '@{value}' but was '${@{resource}}'"/> + default="Didn't expect resource '@{resource}' to contain value '@{value}' but was '${@{resource}}'"/> <sequential> <au:assertFalse message="@{message}"> - <resourcecontains resource="@{resource}" substring="@{value}" + <resourcecontains resource="@{resource}" substring="@{value}" casesensitive="@{casesensitive}"/> </au:assertFalse> </sequential> @@ -264,11 +310,11 @@ under the License. <attribute name="value"/> <attribute name="casesensitive" default="true"/> <attribute name="message" - default="Expected resource '@{refid}' to contain value '@{value}'"/> + default="Expected resource '@{refid}' to contain value '@{value}'"/> <sequential> <au:assertRefResourceExists refid="@{refid}"/> <au:fail message="@{message}"> - <resourcecontains refid="@{refid}" substring="@{value}" + <resourcecontains refid="@{refid}" substring="@{value}" casesensitive="@{casesensitive}"/> </au:fail> </sequential> @@ -279,10 +325,10 @@ under the License. <attribute name="value"/> <attribute name="casesensitive" default="true"/> <attribute name="message" - default="Didn't expect resource '@{refid}' to contain value '@{value}'"/> + default="Didn't expect resource '@{refid}' to contain value '@{value}'"/> <sequential> <au:assertFalse message="@{message}"> - <resourcecontains refid="@{refid}" substring="@{value}" + <resourcecontains refid="@{refid}" substring="@{value}" casesensitive="@{casesensitive}"/> </au:assertFalse> </sequential> @@ -347,6 +393,19 @@ under the License. </sequential> </macrodef> + <macrodef name="assertReferenceIsNotSet" backtrace="false"> + <attribute name="refid"/> + <attribute name="message" + default="Didn't expect reference '@{refid}'"/> + <sequential> + <au:fail message="@{message}"> + <not> + <isreference refid="@{refid}"/> + </not> + </au:fail> + </sequential> + </macrodef> + <macrodef name="assertReferenceIsType" backtrace="false"> <attribute name="refid"/> <attribute name="type"/> @@ -398,7 +457,7 @@ under the License. default="Expected '@{string}' to match pattern '@{pattern}'"/> <sequential> <au:fail message="@{message}"> - <matches string="@{string}" pattern="@{pattern}" + <matches string="@{string}" pattern="@{pattern}" casesensitive="@{casesensitive}" singleline="@{singleline}" multiline="@{multiline}"/> diff --git a/src/tests/junit/org/apache/ant/antunit/AssertTest.java b/src/tests/junit/org/apache/ant/antunit/AssertTest.java index d56b010..0c7ec78 100644 --- a/src/tests/junit/org/apache/ant/antunit/AssertTest.java +++ b/src/tests/junit/org/apache/ant/antunit/AssertTest.java @@ -49,6 +49,12 @@ public class AssertTest extends BuildFileTest { public void testEqualsCasePass() { testPass("assertEqualsCasePass"); } + public void testNotEqualsPass1() { + testPass("assertNotEqualsPass1"); + } + public void testNotEqualsPass2() { + testPass("assertNotEqualsPass2"); + } public void testPropertySetPass() { testPass("assertPropertySetPass"); } @@ -61,6 +67,18 @@ public class AssertTest extends BuildFileTest { public void testPropertyEqualsCasePass() { testPass("assertPropertyEqualsCasePass"); } + public void testPropertyNotEqualsPass1() { + testPass("assertPropertyNotEqualsPass1"); + } + public void testPropertyNotEqualsPass2() { + testPass("assertPropertyNotEqualsPass2"); + } + public void testPropertyContains() { + testPass("assertPropertyContains"); + } + public void testPropertyDoesntContain() { + testPass("assertPropertyDoesntContain"); + } public void testFileExistsPass() { testPass("assertFileExistsPass"); } @@ -88,6 +106,9 @@ public class AssertTest extends BuildFileTest { public void testReferenceSetPass() { testPass("assertReferenceSetPass"); } + public void testReferenceIsNotSetPass() { + testPass("assertReferenceIsNotSetPass"); + } public void testReferenceIsTypePass() { testPass("assertReferenceIsTypePass"); } @@ -104,11 +125,17 @@ public class AssertTest extends BuildFileTest { public void testEqualsFail2() { testFail("assertEqualsFail2", "Expected 'bar' but was 'BAR'"); } + public void testNotEqualsFail() { + testFail("assertNotEqualsFail", "Didn't expect 'bar' to be 'bar'"); + } + public void testNotEqualsCaseFail() { + testFail("assertNotEqualsCaseFail", "Didn't expect 'bar' to be 'BAR'"); + } public void testPropertySetFail() { testFail("assertPropertySetFail", "Expected property 'foo'"); } public void testPropertyNotSetFail() { - testFail("assertPropertyNotSetFail", "Expected property 'foo' to not be set but was 'bar'"); + testFail("assertPropertyNotSetFail", "Didn't expect property 'foo' to be set but was 'bar'"); } public void testPropertyEqualsFail1() { testFail("assertPropertyEqualsFail1", "Expected property 'foo' to have value 'bar' but was '${foo}'"); @@ -119,12 +146,21 @@ public class AssertTest extends BuildFileTest { public void testPropertyEqualsFail3() { testFail("assertPropertyEqualsFail3", "Expected property 'foo' to have value 'BAR' but was 'bar'"); } - public void testPropertyContains() { - testPass("assertPropertyContains"); + public void testPropertyNotEqualsFail1() { + testFail("assertPropertyNotEqualsFail1", "Didn't expect property 'foo' to have value 'bar'"); + } + public void testPropertyNotEqualsFail2() { + testFail("assertPropertyNotEqualsFail2", "Didn't expect property 'foo' to have value 'bar'"); + } + public void testPropertyNotEqualsCaseFail() { + testFail("assertPropertyNotEqualsCaseFail", "Didn't expect property 'foo' to have value 'BAR'"); } public void testPropertyContainsFail() { testFail("assertPropertyContainsFail", "Expected property 'foo' to contain value 'foo' but was 'bar'"); } + public void testPropertyDoesntContainFail() { + testFail("assertPropertyDoesntContainFail", "Didn't expect property 'foo' to contain value 'ba' but was 'bar'"); + } public void testFileExistsFail() { testFail("assertFileExistsFail", "Expected file 'assert.txt' to exist"); @@ -160,6 +196,9 @@ public class AssertTest extends BuildFileTest { public void testReferenceSetFail() { testFail("assertReferenceSetFail", "Expected reference 'foo2'"); } + public void testReferenceIsNotSetFail() { + testFail("assertReferenceIsNotSetFail", "Didn't expect reference 'foo4'"); + } public void testReferenceIsTypeFailNotSet() { testFail("assertReferenceIsTypeFailNotSet", "Expected reference 'foo4'");
