Skip to content

Commit

Permalink
Add a couple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Nov 5, 2024
1 parent c2757f3 commit b9ca07e
Showing 1 changed file with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package playground.smithyql.parser.v3

import org.polyvariant.treesitter4s.Node
import org.polyvariant.treesitter4s.TreeSitterAPI
import playground.generated.nodes.SourceFile
import weaver.*

object TreeSitterParserTests extends FunSuite {

private def parse(s: String): SourceFile = {
val p = TreeSitterAPI.make("smithyql")
SourceFile.unsafeApply(p.parse(s).rootNode.get)
}

test("SourceFile fields") {
val in = parse("""use service foo.bar.baz.bax#Baz
|GetBaz{}""".stripMargin)

assert(in.use_clause.nonEmpty) &&
assert(in.statements.nonEmpty)
}

test("All parents of deep child") {
val allNodes = parse("""use service foo.bar.baz.bax#Baz
|GetBaz { a = { x = 42 }}""".stripMargin)
.fold[List[Node]](_ :: _.flatten.toList)

val parentTypesAndSources = allNodes
.find(_.source == "x")
.get
.selfAndParents
.map(n => n.tpe -> n.source)
.mkString("\n")

val expected = List(
"identifier" -> "x",
"binding" -> "x = 42",
"bindings" -> "x = 42",
"struct" -> "{ x = 42 }",
"binding" -> "a = { x = 42 }",
"bindings" -> "a = { x = 42 }",
"struct" -> "{ a = { x = 42 }}",
"operation_call" -> "GetBaz { a = { x = 42 }}",
"top_level_statement" -> "GetBaz { a = { x = 42 }}",
"source_file" -> "use service foo.bar.baz.bax#Baz\nGetBaz { a = { x = 42 }}",
).mkString("\n")

assert.same(expected, parentTypesAndSources)
}

test("Deep insight into field") {
val in = parse("""use service foo.bar.baz.bax#Baz
|GetBaz { a = { x = 42 } }""".stripMargin)

// this ain't pretty huh
// watch out for the upcoming lookup DSL
val valueOfX =
in.statements
.get
.operation_call
.get
.input
.get
.bindings
.get
.binding
.find(_.key.get.source == "a")
.get
.value
.get
.asStruct
.get
.bindings
.get
.binding
.find(_.key.get.source == "x")
.get
.value
.get
.asNumber
.get
.source
.toInt

assert.eql(42, valueOfX)
}

test("Deep insight into field, but the file isn't valid") {
val in = parse("""use service fo o.b ar.b/az.bax/#//B//,,{}az
|GetBa z { a = { x = 42, 50 }, z, 42 }""".stripMargin)

// this ain't pretty huh
// watch out for the upcoming lookup DSL
val valueOfX =
in.statements
.get
.operation_call
.get
.input
.get
.bindings
.get
.binding
.find(_.key.get.source == "a")
.get
.value
.get
.asStruct
.get
.bindings
.get
.binding
.find(_.key.get.source == "x")
.get
.value
.get
.asNumber
.get
.source
.toInt

assert.eql(42, valueOfX)
}
}

0 comments on commit b9ca07e

Please sign in to comment.