From a7e1eab8d73eb953fdb43f4855bc59298ad369a4 Mon Sep 17 00:00:00 2001 From: Aleksandr Kuzmenko Date: Wed, 20 Dec 2023 10:00:41 +0300 Subject: [PATCH] rename Assert.raisesCondition to Assert.exception (#127) --- src/utest/Assert.hx | 7 +++++-- test/utest/TestAssert.hx | 19 ++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/utest/Assert.hx b/src/utest/Assert.hx index 53d89b5..8e7e57f 100644 --- a/src/utest/Assert.hx +++ b/src/utest/Assert.hx @@ -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). @@ -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(method:() -> Void, type:Class, condition:(e:T)->Bool, ?msgNotThrown : String , ?msgWrongType : String, ?msgWrongCondition : String, ?pos : PosInfos) : Bool { - var cond = e -> { + public static function exception(method:() -> Void, ?type:Class, ?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); @@ -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; diff --git a/test/utest/TestAssert.hx b/test/utest/TestAssert.hx index 1e1e631..f946e12 100644 --- a/test/utest/TestAssert.hx +++ b/test/utest/TestAssert.hx @@ -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() {