Skip to content

Commit

Permalink
Reenable AnyException tests in ExceptionTest.scala (#9861)
Browse files Browse the repository at this point in the history
* Reenable AnyException tests in ExceptionTest.scala

changelog_begin
changelog_end

* Move to SBuiltinTest

changelog_begin
changelog_end
  • Loading branch information
cocreature authored Jun 1, 2021
1 parent 1573984 commit 89bd478
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,6 @@ import org.scalatest.wordspec.AnyWordSpec

class ExceptionTest extends AnyWordSpec with Matchers with TableDrivenPropertyChecks {

// TODO https://github.com/digital-asset/daml/issues/8020
// Turn this into a test for to-any-exception / from-any-exception
// using only user-defined types, since builtin exception types are going away.

// "builtin exceptions: to/from-any-exception" should {

// // Convertion to/from AnyException works as expected:
// // specifically: we can only extract the inner value if the
// // expected-type (on: from_any_exception) matches the actual-type (on: to_any_exception)

// val pkgs: PureCompiledPackages = typeAndCompile(p"""
// module M {
// val G : AnyException = to_any_exception @GeneralError (MAKE_GENERAL_ERROR "G");
// val A : AnyException = to_any_exception @ArithmeticError (MAKE_ARITHMETIC_ERROR "A");
// val C : AnyException = to_any_exception @ContractError (MAKE_CONTRACT_ERROR "C");
// val fromG : AnyException -> Text = \(e:AnyException) -> case from_any_exception @GeneralError e of None -> "NONE" | Some x -> GENERAL_ERROR_MESSAGE x;
// val fromA : AnyException -> Text = \(e:AnyException) -> case from_any_exception @ArithmeticError e of None -> "NONE" | Some x -> ARITHMETIC_ERROR_MESSAGE x;
// val fromC : AnyException -> Text = \(e:AnyException) -> case from_any_exception @ContractError e of None -> "NONE" | Some x -> CONTRACT_ERROR_MESSAGE x;
// }
// """)

// val testCases = Table[String, String](
// ("expression", "string-result"),
// ("M:fromG M:G", "G"),
// ("M:fromA M:A", "A"),
// ("M:fromC M:C", "C"),
// ("M:fromG M:A", "NONE"),
// ("M:fromG M:C", "NONE"),
// ("M:fromA M:G", "NONE"),
// ("M:fromA M:C", "NONE"),
// ("M:fromC M:G", "NONE"),
// ("M:fromC M:A", "NONE"),
// )

// forEvery(testCases) { (exp: String, res: String) =>
// s"""eval[$exp] --> "$res"""" in {
// val expected: SResult = SResultFinalValue(SValue.SText(res))
// runExpr(pkgs)(e"$exp") shouldBe expected
// }
// }
// }

// TODO https://github.com/digital-asset/daml/issues/8020
// Add builtin errors back into this test (ArithmeticError and ContractError).
// In these cases the message should probably just be "ArithmeticError" and "ContractError".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,28 @@ class SBuiltinTest extends AnyFreeSpec with Matchers with TableDrivenPropertyChe

}

"To/FromAnyException" - {
val testCases = Table[String, String](
("expression", "string-result"),
("Mod:from1 Mod:A1", "1"),
("Mod:from2 Mod:A2", "2"),
("Mod:from3 Mod:A3", "3"),
("Mod:from1 Mod:A2", "NONE"),
("Mod:from1 Mod:A3", "NONE"),
("Mod:from2 Mod:A1", "NONE"),
("Mod:from2 Mod:A3", "NONE"),
("Mod:from3 Mod:A1", "NONE"),
("Mod:from3 Mod:A2", "NONE"),
)

forEvery(testCases) { (exp: String, res: String) =>
s"""eval[$exp] --> "$res"""" in {
val expected = Right(SValue.SText(res))
eval(e"$exp") shouldBe expected
}
}
}

}

object SBuiltinTest {
Expand All @@ -1519,6 +1541,25 @@ object SBuiltinTest {
exception Exception = {
message \(e: Mod:Exception) -> "some nice error message"
} ;

record @serializable Ex1 = { message: Text } ;
exception Ex1 = {
message \(e: Mod:Ex1) -> Mod:Ex1 {message} e
};
record @serializable Ex2 = { message: Text } ;
exception Ex2 = {
message \(e: Mod:Ex2) -> Mod:Ex2 {message} e
};
record @serializable Ex3 = { message: Text } ;
exception Ex3 = {
message \(e: Mod:Ex3) -> Mod:Ex3 {message} e
};
val A1 : AnyException = to_any_exception @Mod:Ex1 (Mod:Ex1 { message = "1" });
val A2 : AnyException = to_any_exception @Mod:Ex2 (Mod:Ex2 { message = "2" });
val A3 : AnyException = to_any_exception @Mod:Ex3 (Mod:Ex3 { message = "3" });
val from1 : AnyException -> Text = \(e:AnyException) -> case from_any_exception @Mod:Ex1 e of None -> "NONE" | Some x -> Mod:Ex1 { message} x;
val from2 : AnyException -> Text = \(e:AnyException) -> case from_any_exception @Mod:Ex2 e of None -> "NONE" | Some x -> Mod:Ex2 { message} x;
val from3 : AnyException -> Text = \(e:AnyException) -> case from_any_exception @Mod:Ex3 e of None -> "NONE" | Some x -> Mod:Ex3 { message} x;
}

"""
Expand Down

0 comments on commit 89bd478

Please sign in to comment.