Skip to content

Commit

Permalink
Add simple test
Browse files Browse the repository at this point in the history
  • Loading branch information
InversionSpaces committed Jul 1, 2024
1 parent 1a5c786 commit 1082ed4
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 71 deletions.
75 changes: 4 additions & 71 deletions compiler/src/test/scala/aqua/compiler/AquaCompilerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,14 @@
package aqua.compiler

import aqua.compiler.FileIdString.given
import aqua.model.AquaContext
import aqua.model.CallServiceModel
import aqua.model.FlattenModel
import aqua.model.transform.ModelBuilder
import aqua.model.transform.Transform
import aqua.model.transform.TransformConfig
import aqua.model.{CallModel, ForModel, FunctorModel, LiteralModel, ValueModel, VarModel}
import aqua.parser.Ast
import aqua.parser.Parser
import aqua.parser.ParserError
import aqua.parser.lift.Span
import aqua.parser.lift.Span.S
import aqua.model.*
import aqua.model.transform.{ModelBuilder, Transform, TransformConfig}
import aqua.raw.ConstantRaw
import aqua.raw.value.{LiteralRaw, ValueRaw, VarRaw}
import aqua.res.*
import aqua.res.ResBuilder
import aqua.semantics.FileId
import aqua.types.{ArrayType, CanonStreamType, LiteralType, ScalarType, StreamType, Type}
import aqua.types.*

import cats.Eval
import cats.Id
Expand All @@ -45,68 +35,11 @@ import cats.syntax.either.*
import cats.syntax.flatMap.*
import cats.syntax.option.*
import cats.syntax.show.*
import org.scalatest.Inside
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import scala.annotation.tailrec

class AquaCompilerSpec extends AnyFlatSpec with Matchers with Inside {
class AquaCompilerSpec extends CompilerSpec {
import ModelBuilder.*

private def aquaSource(src: Map[String, String], imports: Map[String, String]) = {
new AquaSources[Id, String, String] {

override def sources: Id[ValidatedNec[String, Chain[(String, String)]]] =
Validated.validNec(Chain.fromSeq(src.toSeq))

override def resolveImport(from: String, imp: String): Id[ValidatedNec[String, String]] =
Validated.validNec(imp)

override def load(file: String): Id[ValidatedNec[String, String]] =
Validated.fromEither(
(imports ++ src)
.get(file)
.toRight(NonEmptyChain.one(s"Cannot load imported file $file"))
)
}
}

private def insideContext(
src: Map[String, String],
imports: Map[String, String] = Map.empty
)(
test: AquaContext => Any
) = {
val compiled = CompilerAPI
.compileToContext[Id, String, String, Span.S](
aquaSource(src, imports),
id => txt => Parser.parse(Parser.parserSchema)(txt),
AquaCompilerConf(ConstantRaw.defaultConstants(None))
)
.value
.value
.toValidated

inside(compiled) { case Validated.Valid(contexts) =>
inside(contexts.headOption) { case Some(ctx) =>
test(ctx)
}
}
}

private def insideRes(
src: Map[String, String],
imports: Map[String, String] = Map.empty,
transformCfg: TransformConfig = TransformConfig()
)(funcNames: String*)(
test: PartialFunction[List[FuncRes], Any]
) = insideContext(src, imports)(ctx =>
val aquaRes = Transform.contextRes(ctx, transformCfg)
// To preserve order as in funcNames do flatMap
val funcs = funcNames.flatMap(name => aquaRes.funcs.find(_.funcName == name)).toList
inside(funcs)(test)
)

def through(peer: ValueModel) =
MakeRes.hop(peer)

Expand Down
64 changes: 64 additions & 0 deletions compiler/src/test/scala/aqua/compiler/AquaModulesSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2024 Fluence DAO
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package aqua.compiler

import aqua.model.*
import aqua.model.transform.{ModelBuilder, TransformConfig}
import aqua.res.*
import aqua.types.*

import org.scalatest.Inside
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class AquaModulesSpec extends CompilerSpec {
import ModelBuilder.*

it should "sucessfully import empty module" in {
val module = s"""aqua Module
|""".stripMargin
val main = s"""aqua Main
|
|use "module"
|
|export main
|
|func main() -> i32:
| <- 42
|""".stripMargin

val src = Map("main.aqua" -> main)
val imports = Map("module.aqua" -> module)

val transformCfg = TransformConfig(relayVarName = None)

insideRes(src, imports, transformCfg)(
"main"
) { case main :: _ =>
val ap = CallModel.Export("literal_ap", LiteralType.unsigned)
val props = ap.copy(name = "literal_props")
val expected = XorRes.wrap(
respCall(transformCfg, LiteralModel.number(42), initPeer),
errorCall(transformCfg, 0, initPeer)
)

main.body.equalsOrShowDiff(expected) should be(true)
}

}

}
88 changes: 88 additions & 0 deletions compiler/src/test/scala/aqua/compiler/CompilerSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2024 Fluence DAO
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package aqua.compiler

import aqua.compiler.FileIdString.given
import aqua.model.AquaContext
import aqua.model.transform.{Transform, TransformConfig}
import aqua.parser.lift.Span
import aqua.parser.{Parser, ParserError}
import aqua.raw.ConstantRaw
import aqua.res.FuncRes

import cats.Id
import cats.data.{Chain, NonEmptyChain, Validated, ValidatedNec}
import cats.syntax.either.*
import org.scalatest.Inside
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

trait CompilerSpec extends AnyFlatSpec with Matchers with Inside {

private def aquaSource(src: Map[String, String], imports: Map[String, String]) = {
new AquaSources[Id, String, String] {

override def sources: Id[ValidatedNec[String, Chain[(String, String)]]] =
Validated.validNec(Chain.fromSeq(src.toSeq))

override def resolveImport(from: String, imp: String): Id[ValidatedNec[String, String]] =
Validated.validNec(imp)

override def load(file: String): Id[ValidatedNec[String, String]] =
Validated.fromEither(
(imports ++ src)
.get(file)
.toRight(NonEmptyChain.one(s"Cannot load imported file $file"))
)
}
}

protected def insideContext(
src: Map[String, String],
imports: Map[String, String] = Map.empty
)(test: AquaContext => Any) = {
val compiled = CompilerAPI
.compileToContext[Id, String, String, Span.S](
aquaSource(src, imports),
id => txt => Parser.parse(Parser.parserSchema)(txt),
AquaCompilerConf(ConstantRaw.defaultConstants(None))
)
.value
.value
.toValidated

inside(compiled) { case Validated.Valid(contexts) =>
inside(contexts.headOption) { case Some(ctx) =>
test(ctx)
}
}
}

protected def insideRes(
src: Map[String, String],
imports: Map[String, String] = Map.empty,
transformCfg: TransformConfig = TransformConfig()
)(funcNames: String*)(
test: PartialFunction[List[FuncRes], Any]
) = insideContext(src, imports)(ctx =>
val aquaRes = Transform.contextRes(ctx, transformCfg)
// To preserve order as in funcNames do flatMap
val funcs = funcNames.flatMap(name => aquaRes.funcs.find(_.funcName == name)).toList
inside(funcs)(test)
)

}

0 comments on commit 1082ed4

Please sign in to comment.