From 8b3b033fadfd0908bf7714fb0367a113204886ee Mon Sep 17 00:00:00 2001 From: Remy Date: Tue, 21 Sep 2021 08:53:37 +0200 Subject: [PATCH] LF: Test preprocessor resuming (#10936) CHANGELOG_BEGIN CHANGELOG_END --- .../daml/lf/engine/EngineTest.scala | 21 ------ .../daml/lf/engine/PreprocessorSpec.scala | 74 +++++++++++++++++++ 2 files changed, 74 insertions(+), 21 deletions(-) create mode 100644 daml-lf/engine/src/test/scala/com/digitalasset/daml/lf/engine/PreprocessorSpec.scala diff --git a/daml-lf/engine/src/test/scala/com/digitalasset/daml/lf/engine/EngineTest.scala b/daml-lf/engine/src/test/scala/com/digitalasset/daml/lf/engine/EngineTest.scala index 53182d2dbbbe..876f79f2a823 100644 --- a/daml-lf/engine/src/test/scala/com/digitalasset/daml/lf/engine/EngineTest.scala +++ b/daml-lf/engine/src/test/scala/com/digitalasset/daml/lf/engine/EngineTest.scala @@ -179,27 +179,6 @@ class EngineTest } } - "command translation" should { - "returns correct error when resuming" in { - val translator = - new preprocessing.Preprocessor( - ConcurrentCompiledPackages(suffixLenientEngine.config.getCompilerConfig) - ) - val id = Identifier(basicTestsPkgId, "BasicTests:MyRec") - val wrongRecord = - ValueRecord(Some(id), ImmArray(Some[Name]("wrongLbl") -> ValueText("foo"))) - val res = translator - .translateValue( - TTyConApp(id, ImmArray.Empty), - wrongRecord, - ) - .consume(lookupContract, lookupPackage, lookupKey) - inside(res) { case Left(Error.Preprocessing(error)) => - error shouldBe a[Error.Preprocessing.TypeMismatch] - } - } - } - "minimal create command" should { val id = Identifier(basicTestsPkgId, "BasicTests:Simple") val let = Time.Timestamp.now() diff --git a/daml-lf/engine/src/test/scala/com/digitalasset/daml/lf/engine/PreprocessorSpec.scala b/daml-lf/engine/src/test/scala/com/digitalasset/daml/lf/engine/PreprocessorSpec.scala new file mode 100644 index 000000000000..d63531053c0e --- /dev/null +++ b/daml-lf/engine/src/test/scala/com/digitalasset/daml/lf/engine/PreprocessorSpec.scala @@ -0,0 +1,74 @@ +// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.daml.lf +package engine + +import com.daml.lf.data.{FrontStack, ImmArray, Ref} +import com.daml.lf.language.Ast +import com.daml.lf.value.Value.{ValueInt64, ValueList, ValueParty, ValueRecord} +import org.scalatest.Inside +import org.scalatest.matchers.should.Matchers +import org.scalatest.wordspec.AnyWordSpec + +class CommandPreprocessorSpec extends AnyWordSpec with Inside with Matchers { + + import com.daml.lf.testing.parser.Implicits._ + import com.daml.lf.transaction.test.TransactionBuilder.Implicits.{defaultPackageId => _, _} + + private[this] implicit val defaultPackageId: Ref.PackageId = + defaultParserParameters.defaultPackageId + + private[this] lazy val pkg = + p""" + module Mod { + + record @serializable Record = { owners: List Party, data : Int64 }; + + template (this : Record) = { + precondition True, + signatories Mod:Record {owners} this, + observers Mod:Record {owners} this, + agreement "Agreement", + choices { }, + key @(List Party) (Mod:Record {owners} this) (\ (parties: List Party) -> parties) + }; + + } + """ + + private[this] val pkgs = Map(defaultPackageId -> pkg) + private[this] val parties = ValueList(FrontStack(ValueParty("Alice"))) + + "preprocessor" should { + "returns correct result when resuming" in { + val preporcessor = new preprocessing.Preprocessor(ConcurrentCompiledPackages()) + val intermediaryResult = preporcessor + .translateValue( + Ast.TTyCon("Mod:Record"), + ValueRecord("", ImmArray("owners" -> parties, "data" -> ValueInt64(42))), + ) + intermediaryResult shouldBe a[ResultNeedPackage[_]] + val finalResult = intermediaryResult.consume(_ => None, pkgs.get, _ => None) + finalResult shouldBe a[Right[_, _]] + } + + "returns correct error when resuming" in { + val preporcessor = new preprocessing.Preprocessor(ConcurrentCompiledPackages()) + val intermediaryResult = preporcessor + .translateValue( + Ast.TTyCon("Mod:Record"), + ValueRecord( + "", + ImmArray("owners" -> parties, "wrong_field" -> ValueInt64(42)), + ), + ) + intermediaryResult shouldBe a[ResultNeedPackage[_]] + val finalResult = intermediaryResult.consume(_ => None, pkgs.get, _ => None) + inside(finalResult) { case Left(Error.Preprocessing(error)) => + error shouldBe a[Error.Preprocessing.TypeMismatch] + } + } + } + +}