Skip to content

Commit

Permalink
rename Assert.raisesCondition to Assert.exception (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
RealyUniqueName committed Dec 20, 2023
1 parent 6ff73d8 commit a7e1eab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/utest/Assert.hx
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,8 @@ class Assert {
}

/**
* DEPRECATED: use `utest.Assert.exception` instead.
*
* It is used to test an application that under certain circumstances must
* react throwing an error. This assert guarantees that the error is of the
* correct type (or any type if non is specified).
Expand Down Expand Up @@ -698,8 +700,8 @@ class Assert {
* @param pos Code position where the Assert call has been executed. Don't fill it
* unless you know what you are doing.
*/
public static function raisesCondition<T>(method:() -> Void, type:Class<T>, condition:(e:T)->Bool, ?msgNotThrown : String , ?msgWrongType : String, ?msgWrongCondition : String, ?pos : PosInfos) : Bool {
var cond = e -> {
public static function exception<T>(method:() -> Void, ?type:Class<T>, ?condition:(e:T)->Bool, ?msgNotThrown : String , ?msgWrongType : String, ?msgWrongCondition : String, ?pos : PosInfos) : Bool {
var cond = condition == null ? _ -> true : e -> {
if(null == msgWrongCondition)
msgWrongCondition = 'exception of ${Type.getClassName(type)} is raised, but condition failed';
isTrue(condition(e), msgWrongCondition, pos);
Expand All @@ -712,6 +714,7 @@ class Assert {
inline function handleCatch(ex:Any):Bool {
return if(null == type) {
pass(pos);
condition(ex);
} else {
if (null == msgWrongType)
msgWrongType = "expected throw " + typeDescr + " but it is " + ex;
Expand Down
19 changes: 14 additions & 5 deletions test/utest/TestAssert.hx
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,20 @@ class TestAssert extends Test {
}
}

public function testRaisesCondition() {
success(() -> raisesCondition(() -> throw new SampleException('haxe.Exception-based'), SampleException, e -> e.message.indexOf('haxe') >= 0));
failure(() -> raisesCondition(() -> throw new SampleException('haxe.Exception-based'), SampleException, e -> e.message == 'fail'));
success(() -> raisesCondition(() -> throw 'Non-haxe.Exception', String, e -> e.indexOf('haxe') >= 0));
failure(() -> raisesCondition(() -> throw 'Non-haxe.Exception', String, e -> e == 'fail'));
public function testException() {
//check for any exception
success(() -> exception(() -> throw new Exception('error')));
success(() -> exception(() -> throw Sample.Some('error')));
success(() -> exception(() -> throw 'error'));
failure(() -> exception(() -> {}));
//check for enum-based exceptions
success(() -> exception(() -> throw Sample.Some('error'), e -> Std.isOfType(e, Sample)));
failure(() -> exception(() -> throw Sample.Some('error'), e -> Std.isOfType(e, haxe.ds.Option)));
//check for specific class-based exceptions
success(() -> exception(() -> throw new SampleException('haxe.Exception-based'), SampleException, e -> e.message.indexOf('haxe') >= 0));
failure(() -> exception(() -> throw new SampleException('haxe.Exception-based'), SampleException, e -> e.message == 'fail'));
success(() -> exception(() -> throw 'Non-haxe.Exception', String, e -> e.indexOf('haxe') >= 0));
failure(() -> exception(() -> throw 'Non-haxe.Exception', String, e -> e == 'fail'));
}

public function testIs() {
Expand Down

0 comments on commit a7e1eab

Please sign in to comment.