Skip to content

Commit

Permalink
Add test cases for SymbolsOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
reid-spencer committed May 11, 2024
1 parent 477c90b commit f50746a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,29 +115,26 @@ case class SymbolsOutput(
require(id.nonEmpty, "No name elements provided to lookupSymbol")
val clazz = classTag[D].runtimeClass
val nameList = id.reverse
nameList.headOption match
val leafName = id.head
symTab.get(leafName) match {
case Some(set) =>
set
.filter { case (_: NamedValue, parents: Parents) =>
// whittle down the list of matches to the ones whose parents names
// have the same as the nameList provided
hasSameParentNames(nameList, parents)
}
.map { case (d: NamedValue, _: Parents) =>
// If a name match is also the same type as desired by the caller
// then give them the definition in the requested type, optionally
if clazz.isInstance(d) then { (d, Option(d.asInstanceOf[D])) }
else { (d, None) }
}
.toList
case None =>
// Symbol wasn't found
List.empty
case Some(leafName) =>
symTab.get(leafName) match {
case Some(set) =>
set
.filter { case (_: NamedValue, parents: Parents) =>
// whittle down the list of matches to the ones whose parents names
// have the same as the nameList provided
hasSameParentNames(nameList, parents)
}
.map { case (d: NamedValue, _: Parents) =>
// If a name match is also the same type as desired by the caller
// then give them the definition in the requested type, optionally
if clazz.isInstance(d) then { (d, Option(d.asInstanceOf[D])) }
else { (d, None) }
}
.toList
case None =>
// Symbol wasn't found
List.empty
}
}
}

/** Look up a symbol in the table
Expand All @@ -149,54 +146,44 @@ case class SymbolsOutput(
* requested type
*/
def lookupParentage(
names: Seq[String]
names: PathNames
): List[Symbols.SymTabItem] = {
names.headOption match
require(names.nonEmpty, "No name elements provided to lookupSymbol")
val leafName = names.head
symTab.get(leafName) match {
case Some(set) =>
set.filter { case (_: Definition, parents: Parents) =>
// whittle down the list of matches to the ones whose parents names
// have the same as the nameList provided
hasSameParentNames(names, parents)
}.toList
case None =>
require(names.nonEmpty, "No name elements provided to lookupSymbol")
// Symbol wasn't found
List.empty[Symbols.SymTabItem]
case Some(leafName) =>
symTab.get(leafName) match {
case Some(set) =>
set.filter { case (_: Definition, parents: Parents) =>
// whittle down the list of matches to the ones whose parents names
// have the same as the nameList provided
hasSameParentNames(names, parents)
}.toList
case None =>
// Symbol wasn't found
List.empty[Symbols.SymTabItem]
}
}
}

def lookup[D <: Definition: ClassTag](
ref: Reference[D]
): List[D] = { lookup[D](ref.pathId.value) }

def lookup[D <: Definition: ClassTag](
id: PathNames
): List[D] = {
require(id.nonEmpty, "No name elements provided to lookup")
val clazz = classTag[D].runtimeClass
id.headOption match
case None =>
require(id.nonEmpty, "Provided id is empty")
List.empty[D]
case Some(leafName) =>
symTab.get(leafName) match {
case Some(set) =>
val result = set
.filter { case (d: Definition, parents: Symbols.Parents) =>
if clazz.isInstance(d) then {
// It is in the result set as long as the container names
// given in the provided id are the same as the container
// names in the symbol table.
hasSameParentNames(id, parents)
} else { false }
}
.map(_._1.asInstanceOf[D])
result.toList
case None => List.empty[D]
}
val leafName = id.head
symTab.get(leafName) match {
case Some(set) =>
val result = set
.filter { case (d: Definition, parents: Symbols.Parents) =>
if clazz.isInstance(d) then {
// It is in the result set as long as the container names
// given in the provided id are the same as the container
// names in the symbol table.
hasSameParentNames(id, parents)
} else { false }
}
.map(_._1.asInstanceOf[D])
result.toList
case None => List.empty[D]
}
}

def foreachOverloadedSymbol(f: Seq[Seq[NamedValue]] => Unit): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.ossuminc.riddl.passes.symbols

import com.ossuminc.riddl.language.AST.*
import com.ossuminc.riddl.language.parsing.ParsingTest
import com.ossuminc.riddl.language.CommonOptions
import com.ossuminc.riddl.language.{At,CommonOptions}
import com.ossuminc.riddl.passes.{Pass, PassInput, PassesOutput}
import org.scalatest.Assertion

Expand All @@ -11,11 +11,11 @@ import scala.reflect.ClassTag
/** Unit Tests For SymbolsPassTest */
class SymbolsPassTest extends ParsingTest {

val st: SymbolsOutput = {
val (root: Root, st: SymbolsOutput) = {
val root = checkFile("everything", "everything.riddl")
val input: PassInput = PassInput(root, CommonOptions())
val outputs = PassesOutput()
Pass.runSymbols(input, outputs)
root -> Pass.runSymbols(input, outputs)
}

def assertRefWithParent[T <: Definition: ClassTag, P <: Definition: ClassTag](
Expand All @@ -33,6 +33,41 @@ class SymbolsPassTest extends ParsingTest {
}
}

"SymbolsOutput" must {
"return empty list for non-existent namedValue" in {
val nv: NamedValue = Domain(At(), Identifier(At(), "not-in-root"))
st.parentsOf(nv) must be(Seq.empty[Definition])
}
"lookupSymbol(id: PathNames) should fail if no names" in {
val xcption = intercept[IllegalArgumentException] { st.lookupSymbol[Context](Seq.empty[String]) }
xcption.isInstanceOf[IllegalArgumentException] must be(true)
}
"lookupSymbol() should return None for non-matching type" in {
val list = st.lookupSymbol[Context](Seq("Everything"))
list must not be(empty)
list.head._1.isInstanceOf[Domain] must be(true)
list.head._2 must be(None)
}
"lookupParentage(id: PathNames) should fail on no names" in {
val xcption = intercept[IllegalArgumentException] {
val parents = st.lookupParentage(Seq.empty[String])
parents must be(empty)
}
xcption.isInstanceOf[IllegalArgumentException] must be(true)
}
"lookup should fail on no names" in {
val xcption = intercept[IllegalArgumentException] {
val parents = st.lookup[Context](Seq.empty[String])
parents must be(empty)
}
xcption.isInstanceOf[IllegalArgumentException] must be(true)
}
"lookup should return empty when nothing found" in {
val list = st.lookup[Epic](Seq("SomeType", "Everything"))
list must be(empty)
}
}

"SymbolsPass" must {
"capture all expected symbol references and parents" in {
st.lookup[Domain](Seq("Everything")).headOption mustBe defined
Expand Down

0 comments on commit f50746a

Please sign in to comment.