Skip to content

Commit

Permalink
Adding rest of ModuleModule and test (#62)
Browse files Browse the repository at this point in the history
* adding rest of ModuleModule
  • Loading branch information
michelchan authored Mar 14, 2022
1 parent f0a018d commit 648db65
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 4 deletions.
33 changes: 29 additions & 4 deletions morphir-ir/shared/src/main/scala/zio/morphir/ir/ModuleModule.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package zio.morphir.ir

import zio.Chunk
import zio.morphir.ir.TypeModule.Definition.{CustomType, TypeAlias}

object ModuleModule {

Expand Down Expand Up @@ -37,10 +39,33 @@ object ModuleModule {

def mapAttributes: Definition[Annotations] = ???

def collectTypeReferences: Set[FQName] = ???
def collectValueReferences: Set[FQName] = ???
def collectReferences: Set[FQName] = ???
def dependsOnModules: Set[QualifiedModuleName] = ???
def collectTypeReferences: Set[FQName] = self.types.flatMap {
case (_, AccessControlled.WithPrivateAccess(definition)) =>
definition.value match {
case TypeAlias(_, typeExp) => typeExp.collectReferences
case CustomType(_, ctors) => ctors.withPrivateAccess.collectReferences
}
case (_, AccessControlled.WithPublicAccess(definition)) =>
definition.value match {
case TypeAlias(_, typeExp) => typeExp.collectReferences
case CustomType(_, ctors) => ctors.withPrivateAccess.collectReferences
}
case _ => Nil

}.toSet

def collectValueReferences: Set[FQName] = self.values.flatMap {
case (_, AccessControlled.WithPrivateAccess(definition)) =>
definition.body.collectReferences
case (_, AccessControlled.WithPublicAccess(definition)) =>
definition.body.collectReferences
case _ => Nil
}.toSet

def collectReferences: Set[FQName] = collectTypeReferences ++ collectValueReferences
def dependsOnModules: Set[QualifiedModuleName] = self.collectReferences.map { case FQName(pp, mp, _) =>
QualifiedModuleName(pp.toPath, mp.toPath)
}
}

object Definition {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ object TypeModule extends TypeModuleSyntax {
def toUnannotated: Constructors[Any] = Constructors(items.map { case (ctor, args) =>
(ctor, args.map { case (paramName, paramType) => (paramName, paramType.toUnannotated) })
})

def collectReferences: Set[FQName] = {
items.values.flatMap {
case Chunk((_, tpe)) =>
tpe.collectReferences
case _ => Nil
}.toSet
}
}

sealed trait Definition[+Annotations] { self =>
Expand Down
112 changes: 112 additions & 0 deletions morphir-ir/shared/src/test/scala/zio/morphir/ir/ModuleModuleSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package zio.morphir.ir

import zio.morphir.ir.ModuleModule.{Definition, Specification}
import zio.{Chunk, ZEnvironment}
import zio.morphir.ir.TypeModule.Definition.{CustomType, TypeAlias}
import zio.morphir.ir.TypeModule.Specification.OpaqueTypeSpecification
import zio.morphir.ir.TypeModule.Constructors
import zio.morphir.syntax.AllSyntax
import zio.morphir.testing.MorphirBaseSpec
import zio.test.*

object ModuleModuleSpec extends MorphirBaseSpec with AllSyntax {
val items = Map {
Name("type") -> Chunk((Name("var"), defineVariable("var1")))
Name("rainbow") -> Chunk((Name("red"), defineVariable("red")))
}

val typeAlias = Documented(
"doc",
TypeAlias(Chunk(Name.fromString("hello")), defineVariable("type1"))
)

val customType = Documented(
"doc",
CustomType(Chunk(Name.fromString("world")), AccessControlled.publicAccess(Constructors(items)))
)

val definitionTypes = Map {
Name("hello") -> AccessControlled.publicAccess(typeAlias)
Name("world") -> AccessControlled.publicAccess(customType)
}

val definitionValues = Map {
Name("val") -> AccessControlled.publicAccess(
ValueModule.Definition.fromLiteral(string("string"))
)
}

val moduleDef = Definition(definitionTypes, definitionValues)

val specTypes = Map {
Name("hello") -> Documented(
"doc",
OpaqueTypeSpecification(Chunk(Name("name1")), ZEnvironment.empty)
)
Name("world") -> Documented(
"doc",
OpaqueTypeSpecification(Chunk(Name("name2")), ZEnvironment.empty)
)
}

val specValues = Map {
Name("spec1") -> ValueModule.Specification(
Chunk(
(Name("type1"), defineVariable("Float")),
(Name("type2"), defineVariable("Decimal"))
),
defineVariable("WholeNumbers")
)
}

val moduleSpec = Specification(specTypes, specValues)

def spec = suite("Type")(
suite("Module Definition")(
test("Can be turned to Specification") {
// val expected = Specification(types = Map{
// Name("hello") -> typeAlias.map(_.toSpecification)
// Name("world") -> customType.map(_.toSpecification)
// },
// Map{
// Name("val") -> ValueModule.Definition.fromLiteral(string("string")).toSpecification
// }
// )
// assertTrue(moduleDef.toSpecification == expected)
// todo add when TypeModule toSpec is added
assertTrue(1 == 1)
},
test("Can look up values") {
val result = moduleDef.lookupValue(Name("val"))
assertTrue(result.isDefined && result.get == ValueModule.Definition.fromLiteral(string("string")))
},
test("Can be erased") {
assertTrue(moduleDef.eraseAttributes == Definition.empty)
},
test("Can collect all references") {
assertTrue(
moduleDef.collectTypeReferences.size == 0 &&
moduleDef.collectValueReferences.size == 0 &&
moduleDef.collectValueReferences.size == 0
)
}
),
suite("Module Specification")(
test("Can look up values") {
val result = moduleSpec.lookupValue(Name("spec1"))
assertTrue(
result.isDefined && result.get.inputs.size == 2 && result.get.output == defineVariable("WholeNumbers")
)
},
test("Can look up types") {
val result = moduleSpec.lookupType(Name("world"))
assertTrue(
result.isDefined && !moduleSpec.lookupType(Name("notHere")).isDefined
)
},
test("Can be erased") {
assertTrue(moduleSpec.eraseAttributes == Specification.empty)
}
)
)
}

0 comments on commit 648db65

Please sign in to comment.