func head tests

This commit is contained in:
DieMyst 2021-03-17 13:34:35 +03:00
parent 5fa8dde601
commit 4b4509868c

View File

@ -1,15 +1,15 @@
package aqua.parser
import aqua.ast.algebra.types.ScalarType.{bool, u64}
import aqua.ast.algebra.types.{LiteralType, ScalarType}
import aqua.ast.expr.FuncExpr
import aqua.parser.lexer.{Ability, ArrowTypeToken, BasicTypeToken, CustomTypeToken, Literal, Name, VarLambda}
import aqua.parser.lexer.{Ability, Arg, ArrowTypeToken, BasicTypeToken, CustomTypeToken, Literal, Name, TypeToken, VarLambda}
import cats.data.NonEmptyList
import org.scalatest.EitherValues
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import aqua.parser.lift.LiftParser.Implicits.idLiftParser
import cats.Id
import shapeless.HNil
import scala.language.implicitConversions
@ -18,123 +18,90 @@ class FuncSpec extends AnyFlatSpec with Matchers with EitherValues {
import aqua.ast.algebra.types.ScalarType.{string, u32}
implicit def scToBt(sc: ScalarType): BasicTypeToken[Id] = BasicTypeToken[Id](sc)
implicit def strToAb(str: String): Ability[Id] = Ability[Id](str)
implicit def strToVar(str: String): Name[Id] = Name[Id](str)
/*
private val getTimeHead = FuncHead[Id](
"getTime",
List(
("peer", "peer", CustomTypeToken[Id]("PeerId")),
("ret", "ret", ArrowTypeToken[Id]((), (u32: BasicTypeToken[Id]) :: Nil, None))
),
Some(string: BasicTypeToken[Id])
)
*/
implicit def toName(str: String): Name[Id] = Name[Id](str)
implicit def toCustomType(str: String): CustomTypeToken[Id] = CustomTypeToken[Id](str)
implicit def toCustomArg(str: String, customType: String): Arg[Id] = Arg[Id](toName(str), toCustomType(customType))
implicit def toArg(str: String, typeToken: TypeToken[Id]): Arg[Id] = Arg[Id](toName(str), typeToken)
def funcExpr(str: String): FuncExpr[Id] = FuncExpr.p[Id].parseAll(str).value
"func header" should "parse" in {
/*
func tryGen(in1: u32, in2: string) -> bool:
on in2:
Local.onIn(in2)
Local.gt(in1, 25)
*/
println(FuncExpr.p[Id].parseAll("func some() -> bool:\n").right.value)
println(FuncExpr.p[Id].parseAll("func some():\n").right.value)
/*DefFunc.`funchead`.parseAll("func some(peer: u32)").right.value should be(
FuncHead[Id]("some", List(("peer", "peer", (u32: BasicTypeToken[Id]))), None)
)*/
funcExpr("func some() -> bool:\n") should be(FuncExpr(toName("some"), List(), Some(bool: BasicTypeToken[Id])))
funcExpr("func some():\n") should be(FuncExpr(toName("some"), List(), None))
val arrowToken = ArrowTypeToken[Id]((), List(BasicTypeToken[Id](u32)), Some(BasicTypeToken[Id](bool)))
funcExpr("func some(peer: PeerId, other: u32 -> bool):\n") should be(
FuncExpr(toName("some"), List(toCustomArg("peer", "PeerId"), toArg("other", arrowToken)), None)
)
val arrowToken2 = ArrowTypeToken[Id]((), List(BasicTypeToken[Id](u32), BasicTypeToken[Id](u64)), Some(BasicTypeToken[Id](bool)))
funcExpr("func some(peer: PeerId, other: u32, u64 -> bool):\n") should be(
FuncExpr(toName("some"), List(toCustomArg("peer", "PeerId"), toArg("other", arrowToken2)), None)
)
val arrowToken3 = ArrowTypeToken[Id]((), List(BasicTypeToken[Id](u32)), None)
funcExpr("func getTime(peer: PeerId, ret: u32 -> ()) -> string:\n") should be(
FuncExpr(toName("getTime"), List(toCustomArg("peer", "PeerId"), toArg("ret", arrowToken3)), Some(BasicTypeToken[Id](string)))
)
}
/*
DefFunc.`funchead`.parseAll("func some(peer: PeerId)").right.value should be(
FuncHead[Id]("some", List(("peer", "peer", CustomTypeToken[Id]("PeerId"))), None)
)
DefFunc.`funchead`.parseAll("func some(peer: PeerId, other: u32)").right.value should be(
FuncHead[Id](
"some",
List(("peer", "peer", CustomTypeToken[Id]("PeerId")), ("other", "other", (u32: BasicTypeToken[Id]))),
None
)
)
DefFunc.`funchead`.parseAll("func some(peer: PeerId, other: u32 -> u32)").right.value should be(
FuncHead[Id](
"some",
List(
("peer", "peer", CustomTypeToken[Id]("PeerId")),
("other", "other", ArrowTypeToken[Id]((), (u32: BasicTypeToken[Id]) :: Nil, Some(u32: BasicTypeToken[Id])))
),
None
)
)
/*
DefFunc.`funchead`.parseAll("func getTime(peer: PeerId, ret: u32 -> ()) -> string").right.value should be(
getTimeHead
)
}
"function" should "parse getTime as a whole" in {
val func =
"""func getTime(peer: PeerId, ret: u32 -> ()) -> string:
| on peer:
| Peer "peer"
| t <- Peer.timestamp()
| ret(t)""".stripMargin
"function" should "parse single line fn" in {
val func =
"""func getTime(peer: PeerId, ret: u32 -> ()) -> string:
| ret(43)""".stripMargin
DefFunc.`deffunc`.parseAll(func).right.value should be(
DefFunc[Id, HNil](
getTimeHead,
NonEmptyList.of(
FuncCall[Id, HNil]("ret", Literal[Id]("43", LiteralType.number) :: Nil, HNil)
),
HNil
)
)
}
"function" should "parse getTime as a whole" in {
val func =
"""func getTime(peer: PeerId, ret: u32 -> ()) -> string:
| on peer:
| Peer "peer"
| t <- Peer.timestamp()
| ret(t)""".stripMargin
DefFunc.`deffunc`.parseAll(func).right.value should be(
DefFunc[Id, HNil](
getTimeHead,
NonEmptyList.of(
On[Id, HNil](
VarLambda[Id]("peer", Nil),
NonEmptyList.of(
AbilityId[Id, HNil]("Peer", Literal[Id]("\"peer\"", LiteralType.string), HNil),
Extract[Id, HNil]("t", AbilityFuncCall[Id, HNil]("Peer", "timestamp", "Peer.timestamp", Nil, HNil), HNil)
DefFunc.`deffunc`.parseAll(func).right.value should be(
DefFunc[Id, HNil](
getTimeHead,
NonEmptyList.of(
On[Id, HNil](
VarLambda[Id]("peer", Nil),
NonEmptyList.of(
AbilityId[Id, HNil]("Peer", Literal[Id]("\"peer\"", LiteralType.string), HNil),
Extract[Id, HNil]("t", AbilityFuncCall[Id, HNil]("Peer", "timestamp", "Peer.timestamp", Nil, HNil), HNil)
),
HNil
),
HNil
FuncCall[Id, HNil]("ret", VarLambda[Id]("t", Nil) :: Nil, HNil)
),
FuncCall[Id, HNil]("ret", VarLambda[Id]("t", Nil) :: Nil, HNil)
),
HNil
HNil
)
)
)
}
}
"function" should "parse getTime with no return" in {
val func =
"""func getTime(peer: PeerId, ret: u32 -> ()) -> string:
| on peer:
| Peer "peer"
| t <- Peer.timestamp()""".stripMargin
"function" should "parse getTime with no return" in {
val func =
"""func getTime(peer: PeerId, ret: u32 -> ()) -> string:
| on peer:
| Peer "peer"
| t <- Peer.timestamp()""".stripMargin
DefFunc.`deffunc`.parseAll(func).right.value should be(
DefFunc[Id, HNil](
getTimeHead,
NonEmptyList.of(
On[Id, HNil](
VarLambda[Id]("peer", Nil),
NonEmptyList.of(
AbilityId[Id, HNil]("Peer", Literal[Id]("\"peer\"", LiteralType.string), HNil),
Extract[Id, HNil]("t", AbilityFuncCall[Id, HNil]("Peer", "timestamp", "Peer.timestamp", Nil, HNil), HNil)
),
HNil
)
),
HNil
DefFunc.`deffunc`.parseAll(func).right.value should be(
DefFunc[Id, HNil](
getTimeHead,
NonEmptyList.of(
On[Id, HNil](
VarLambda[Id]("peer", Nil),
NonEmptyList.of(
AbilityId[Id, HNil]("Peer", Literal[Id]("\"peer\"", LiteralType.string), HNil),
Extract[Id, HNil]("t", AbilityFuncCall[Id, HNil]("Peer", "timestamp", "Peer.timestamp", Nil, HNil), HNil)
),
HNil
)
),
HNil
)
)
)
}*/
}*/
}