-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
124 additions
and
0 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
modules/parser/src/test/scala/playground/smithyql/parser/v3/TreeSitterParserTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |