Skip to content

Commit

Permalink
LF: Exhaustive test for valueTranslator. (#10927)
Browse files Browse the repository at this point in the history
* LF: Exhaustive test for value translator.

CHANGELOG_BEGIN
CHANGELOG_END

* cosmetic
  • Loading branch information
remyhaemmerle-da authored Sep 20, 2021
1 parent 409c0b4 commit ac02dbd
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 300 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ private[engine] final class ValueTranslator(
throw Error.Preprocessing.ValueNesting(value)
} else {
val newNesting = nesting + 1
def typeError(msg: String = s"mismatching type: $ty and value: $value0") =
throw Error.Preprocessing.TypeMismatch(ty, value0, msg)
def typeError(msg: String = s"mismatching type: ${ty0.pretty} and value: $value0") =
throw Error.Preprocessing.TypeMismatch(ty0, value0, msg)
val (ty1, tyArgs) = AstUtil.destructApp(ty0)
ty1 match {
case TBuiltin(bt) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CommandPreprocessorSpec
import com.daml.lf.transaction.test.TransactionBuilder.Implicits.{defaultPackageId => _, _}
private implicit val defaultPackageId = defaultParserParameters.defaultPackageId

lazy val pkg =
private[this] val pkg =
p"""
module Mod {

Expand Down Expand Up @@ -71,7 +71,7 @@ class CommandPreprocessorSpec
private[this] val compiledPackage = ConcurrentCompiledPackages()
assert(compiledPackage.addPackage(defaultPackageId, pkg) == ResultDone.Unit)

val valueParties = ValueList(FrontStack(ValueParty("Alice")))
private[this] val valueParties = ValueList(FrontStack(ValueParty("Alice")))

"preprocessCommand" should {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,278 +180,6 @@ class EngineTest
}

"command translation" should {
"translate create commands argument including labels" in {
val id = Identifier(basicTestsPkgId, "BasicTests:Simple")
val command =
CreateCommand(id, ValueRecord(Some(id), ImmArray((Some[Name]("p"), ValueParty(party)))))

val res = preprocessor
.preprocessCommands(ImmArray(command))
.consume(lookupContract, lookupPackage, lookupKey)
res shouldBe a[Right[_, _]]

}

"translate create commands argument without labels" in {
val id = Identifier(basicTestsPkgId, "BasicTests:Simple")
val command =
CreateCommand(id, ValueRecord(Some(id), ImmArray((None, ValueParty(party)))))

val res = preprocessor
.preprocessCommands(ImmArray(command))
.consume(lookupContract, lookupPackage, lookupKey)
res shouldBe a[Right[_, _]]
}

"not translate create commands argument wrong label" in {
val id = Identifier(basicTestsPkgId, "BasicTests:Simple")
val command =
CreateCommand(
id,
ValueRecord(Some(id), ImmArray((Some[Name]("this_is_not_the_one"), ValueParty(party)))),
)

val res = preprocessor
.preprocessCommands(ImmArray(command))
.consume(lookupContract, lookupPackage, lookupKey)
inside(res) { case Left(Error.Preprocessing(error)) =>
error shouldBe a[Error.Preprocessing.TypeMismatch]
}
}

"translate exercise commands argument including labels" in {
val originalCoid = toContractId("BasicTests:CallablePayout:1")
val templateId = Identifier(basicTestsPkgId, "BasicTests:CallablePayout")
val command = ExerciseCommand(
templateId,
originalCoid,
"Transfer",
ValueRecord(None, ImmArray((Some[Name]("newReceiver"), ValueParty(clara)))),
)

val res = preprocessor
.preprocessCommands(ImmArray(command))
.consume(lookupContract, lookupPackage, lookupKey)
res shouldBe a[Right[_, _]]
}

"translate exercise commands argument without labels" in {
val originalCoid = toContractId("BasicTests:CallablePayout:1")
val templateId = Identifier(basicTestsPkgId, "BasicTests:CallablePayout")
val command = ExerciseCommand(
templateId,
originalCoid,
"Transfer",
ValueRecord(None, ImmArray((None, ValueParty(clara)))),
)

val res = preprocessor
.preprocessCommands(ImmArray(command))
.consume(lookupContract, lookupPackage, lookupKey)
res shouldBe a[Right[_, _]]
}

"translate exercise-by-key commands with argument with labels" in {
val templateId = Identifier(basicTestsPkgId, "BasicTests:WithKey")
val command = ExerciseByKeyCommand(
templateId,
ValueRecord(None, ImmArray((None, ValueParty(alice)), (None, ValueInt64(42)))),
"SumToK",
ValueRecord(None, ImmArray((Some[Name]("n"), ValueInt64(5)))),
)

val res = preprocessor
.preprocessCommands(ImmArray(command))
.consume(lookupContract, lookupPackage, lookupKey)
res shouldBe a[Right[_, _]]
}

"translate exercise-by-key commands with argument without labels" in {
val templateId = Identifier(basicTestsPkgId, "BasicTests:WithKey")
val command = ExerciseByKeyCommand(
templateId,
ValueRecord(None, ImmArray((None, ValueParty(alice)), (None, ValueInt64(42)))),
"SumToK",
ValueRecord(None, ImmArray((None, ValueInt64(5)))),
)

val res = preprocessor
.preprocessCommands(ImmArray(command))
.consume(lookupContract, lookupPackage, lookupKey)
res shouldBe a[Right[_, _]]
}

"not translate exercise-by-key commands with argument with wrong labels" in {
val templateId = Identifier(basicTestsPkgId, "BasicTests:WithKey")
val command = ExerciseByKeyCommand(
templateId,
ValueRecord(None, ImmArray((None, ValueParty(alice)), (None, ValueInt64(42)))),
"SumToK",
ValueRecord(None, ImmArray((Some[Name]("WRONG"), ValueInt64(5)))),
)

val res = preprocessor
.preprocessCommands(ImmArray(command))
.consume(lookupContract, lookupPackage, lookupKey)
inside(res) { case Left(Error.Preprocessing(error)) =>
error shouldBe a[Error.Preprocessing.TypeMismatch]
error.message should startWith("Missing record field 'n' for record")
}
}

"not translate exercise-by-key commands if the template specifies no key" in {
val templateId = Identifier(basicTestsPkgId, "BasicTests:CallablePayout")
val command = ExerciseByKeyCommand(
templateId,
ValueRecord(None, ImmArray((None, ValueParty(alice)), (None, ValueInt64(42)))),
"Transfer",
ValueRecord(None, ImmArray((None, ValueParty(clara)))),
)

val res = preprocessor
.preprocessCommands(ImmArray(command))
.consume(lookupContract, lookupPackage, lookupKey)
inside(res) {
case Left(Error.Preprocessing(Error.Preprocessing.Lookup(language.LookupError(ref, _)))) =>
ref shouldBe a[language.Reference.TemplateKey]
}
}

"not translate exercise-by-key commands if the given key does not match the type specified in the template" in {
val templateId = Identifier(basicTestsPkgId, "BasicTests:WithKey")
val command = ExerciseByKeyCommand(
templateId,
ValueRecord(None, ImmArray((None, ValueInt64(42)), (None, ValueInt64(42)))),
"SumToK",
ValueRecord(None, ImmArray((None, ValueInt64(5)))),
)

val res = preprocessor
.preprocessCommands(ImmArray(command))
.consume(lookupContract, lookupPackage, lookupKey)
inside(res) { case Left(Error.Preprocessing(error)) =>
error shouldBe a[Error.Preprocessing.TypeMismatch]
}
}

"translate create-and-exercise commands argument including labels" in {
val id = Identifier(basicTestsPkgId, "BasicTests:CallablePayout")
val command =
CreateAndExerciseCommand(
id,
ValueRecord(
Some(Identifier(basicTestsPkgId, "BasicTests:CallablePayout")),
ImmArray(
(Some[Ref.Name]("giver"), ValueParty(clara)),
(Some[Ref.Name]("receiver"), ValueParty(clara)),
),
),
"Transfer",
ValueRecord(None, ImmArray((Some[Name]("newReceiver"), ValueParty(clara)))),
)

val res = preprocessor
.preprocessCommands(ImmArray(command))
.consume(lookupContract, lookupPackage, lookupKey)
res shouldBe a[Right[_, _]]

}

"translate create-and-exercise commands argument without labels" in {
val id = Identifier(basicTestsPkgId, "BasicTests:CallablePayout")
val command =
CreateAndExerciseCommand(
id,
ValueRecord(
Some(Identifier(basicTestsPkgId, "BasicTests:CallablePayout")),
ImmArray((None, ValueParty(clara)), (None, ValueParty(clara))),
),
"Transfer",
ValueRecord(None, ImmArray((None, ValueParty(clara)))),
)

val res = preprocessor
.preprocessCommands(ImmArray(command))
.consume(lookupContract, lookupPackage, lookupKey)
res shouldBe a[Right[_, _]]
}

"not translate create-and-exercise commands argument wrong label in create arguments" in {
val id = Identifier(basicTestsPkgId, "BasicTests:CallablePayout")
val command =
CreateAndExerciseCommand(
id,
ValueRecord(
Some(Identifier(basicTestsPkgId, "BasicTests:CallablePayout")),
ImmArray(
(None, ValueParty(clara)),
(Some[Ref.Name]("this_is_not_the_one"), ValueParty(clara)),
),
),
"Transfer",
ValueRecord(None, ImmArray((None, ValueParty(clara)))),
)

val res = preprocessor
.preprocessCommands(ImmArray(command))
.consume(lookupContract, lookupPackage, lookupKey)
inside(res) { case Left(Error.Preprocessing(error)) =>
error shouldBe a[Error.Preprocessing.TypeMismatch]
}
}

"not translate create-and-exercise commands argument wrong label in choice arguments" in {
val id = Identifier(basicTestsPkgId, "BasicTests:CallablePayout")
val command =
CreateAndExerciseCommand(
id,
ValueRecord(
Some(Identifier(basicTestsPkgId, "BasicTests:CallablePayout")),
ImmArray((None, ValueParty(clara)), (None, ValueParty(clara))),
),
"Transfer",
ValueRecord(None, ImmArray((Some[Name]("this_is_not_the_one"), ValueParty(clara)))),
)

val res = preprocessor
.preprocessCommands(ImmArray(command))
.consume(lookupContract, lookupPackage, lookupKey)
inside(res) { case Left(Error.Preprocessing(error)) =>
error shouldBe a[Error.Preprocessing.TypeMismatch]
}
}

"translate Optional values" in {
val (optionalPkgId, _, allOptionalPackages) =
loadPackage("daml-lf/tests/Optional.dar")

val translator =
new preprocessing.Preprocessor(
ConcurrentCompiledPackages(suffixLenientEngine.config.getCompilerConfig)
)

val id = Identifier(optionalPkgId, "Optional:Rec")
val someValue =
ValueRecord(
Some(id),
ImmArray(Some[Name]("recField") -> ValueOptional(Some(ValueText("foo")))),
)
val noneValue =
ValueRecord(Some(id), ImmArray(Some[Name]("recField") -> ValueOptional(None)))
val typ = TTyConApp(id, ImmArray.Empty)

translator
.translateValue(typ, someValue)
.consume(lookupContract, allOptionalPackages.get, lookupKey) shouldEqual
Right(SRecord(id, ImmArray("recField"), ArrayList(SOptional(Some(SText("foo"))))))

translator
.translateValue(typ, noneValue)
.consume(lookupContract, allOptionalPackages.get, lookupKey) shouldEqual
Right(SRecord(id, ImmArray("recField"), ArrayList(SOptional(None))))

}

"returns correct error when resuming" in {
val translator =
new preprocessing.Preprocessor(
Expand Down
Loading

0 comments on commit ac02dbd

Please sign in to comment.