Add unittests to check failures in namespace resolution

This commit is contained in:
Chris Masone
2015-09-29 10:23:33 -07:00
parent 5ce93dad2e
commit 24bc9e4831
2 changed files with 88 additions and 17 deletions
+83 -12
View File
@@ -58,20 +58,39 @@ func (suite *ImportTestSuite) TestGetDeps() {
suite.True(ok, "%s is a dep; should have been found.", suite.nestedRef.String())
}
func (suite *ImportTestSuite) TestResolveNamespace() {
deps := GetDeps(types.SetOfRefOfPackageDef{suite.importRef: true}, suite.cs)
t := resolveNamespace(types.MakeExternalTypeRef("Other", "ForeignEnum"), map[string]ref.Ref{"Other": suite.importRef}, deps)
suite.EqualValues(types.MakeTypeRef("ForeignEnum", types.Ref{R: suite.importRef}), t)
}
func (suite *ImportTestSuite) TestUnknownAlias() {
deps := GetDeps(types.SetOfRefOfPackageDef{suite.importRef: true}, suite.cs)
suite.Panics(func() {
resolveNamespace(types.MakeExternalTypeRef("Bother", "ForeignEnum"), map[string]ref.Ref{"Other": suite.importRef}, deps)
})
}
func (suite *ImportTestSuite) TestUnknownImportedType() {
deps := GetDeps(types.SetOfRefOfPackageDef{suite.importRef: true}, suite.cs)
suite.Panics(func() {
resolveNamespace(types.MakeExternalTypeRef("Other", "NotThere"), map[string]ref.Ref{"Other": suite.importRef}, deps)
})
}
func (suite *ImportTestSuite) TestDetectFreeVariable() {
ls := types.MakeStructTypeRef("Local", []types.Field{
types.Field{"b", types.MakePrimitiveTypeRef(types.BoolKind), false},
types.Field{"n", types.MakeTypeRef("OtherLocal", types.Ref{}), false},
},
types.Choices{})
suite.Panics(func() {
resolveNamespaces(map[string]types.TypeRef{"Local": ls}, map[string]ref.Ref{}, map[ref.Ref]types.PackageDef{})
})
}
func (suite *ImportTestSuite) TestImports() {
logname := "testing"
r := strings.NewReader(fmt.Sprintf(`
alias Other = import "%s"
struct Local1 {
a: Other.ForeignStruct
b: Int16
c: Local2
}
struct Local2 {
a: Bool
b: Other.ForeignEnum
}`, suite.importRef))
p := ParseNomDL(logname, r, suite.cs)
find := func(n string, tref types.TypeRef) types.Field {
suite.Equal(types.StructKind, tref.Kind())
@@ -84,6 +103,43 @@ func (suite *ImportTestSuite) TestImports() {
return types.Field{}
}
findChoice := func(n string, tref types.TypeRef) types.Field {
suite.Equal(types.StructKind, tref.Kind())
for _, f := range tref.Desc.(types.StructDesc).Union {
if f.Name == n {
return f
}
}
suite.Fail("Could not find choice", "%s not present", n)
return types.Field{}
}
r := strings.NewReader(fmt.Sprintf(`
alias Other = import "%s"
struct Local1 {
a: Other.ForeignStruct
b: Int16
c: Local2
}
struct Local2 {
a: Bool
b: Other.ForeignEnum
}
struct Union {
union {
a: Other.ForeignStruct
b: Local2
}
}
struct WithUnion {
a: Other.ForeignStruct
b: union {
s: Local1
t: Other.ForeignEnum
}
}`, suite.importRef))
p := ParseNomDL(logname, r, suite.cs)
named := p.NamedTypes["Local1"]
field := find("a", named)
suite.EqualValues(suite.importRef, field.T.PackageRef().Ref())
@@ -93,4 +149,19 @@ func (suite *ImportTestSuite) TestImports() {
named = p.NamedTypes["Local2"]
field = find("b", named)
suite.EqualValues(suite.importRef, field.T.PackageRef().Ref())
named = p.NamedTypes["Union"]
field = findChoice("a", named)
suite.EqualValues(suite.importRef, field.T.PackageRef().Ref())
field = findChoice("b", named)
suite.EqualValues(types.Ref{}, field.T.PackageRef())
named = p.NamedTypes["WithUnion"]
field = find("a", named)
suite.EqualValues(suite.importRef, field.T.PackageRef().Ref())
namedUnion := find("b", named).T
field = findChoice("s", namedUnion)
suite.EqualValues(types.Ref{}, field.T.PackageRef())
field = findChoice("t", namedUnion)
suite.EqualValues(suite.importRef, field.T.PackageRef().Ref())
}
+5 -5
View File
@@ -18,7 +18,7 @@ func ParseNomDL(logname string, r io.Reader, cs chunks.ChunkSource) Parsed {
for _, target := range aliases {
depRefs[target] = true
}
resolveAliases(i.NamedTypes, aliases, GetDeps(depRefs, cs))
resolveNamespaces(i.NamedTypes, aliases, GetDeps(depRefs, cs))
return Parsed{
types.PackageDef{Dependencies: depRefs, NamedTypes: i.NamedTypes},
i.Name,
@@ -71,7 +71,7 @@ func resolveImports(pkg intermediate) map[string]ref.Ref {
return aliases
}
func resolveAliases(namedTypes map[string]types.TypeRef, aliases map[string]ref.Ref, deps map[ref.Ref]types.PackageDef) {
func resolveNamespaces(namedTypes map[string]types.TypeRef, aliases map[string]ref.Ref, deps map[ref.Ref]types.PackageDef) {
var rec func(t types.TypeRef) types.TypeRef
resolveFields := func(fields []types.Field) {
for idx, f := range fields {
@@ -82,7 +82,7 @@ func resolveAliases(namedTypes map[string]types.TypeRef, aliases map[string]ref.
d.Exp.True(ok, "Could not find type %s in current package.", f.T.Name())
continue
}
f.T = resolveAlias(f.T, aliases, deps)
f.T = resolveNamespace(f.T, aliases, deps)
} else {
f.T = rec(f.T)
}
@@ -99,7 +99,7 @@ func resolveAliases(namedTypes map[string]types.TypeRef, aliases map[string]ref.
for n, t := range namedTypes {
if t.IsUnresolved() {
namedTypes[n] = resolveAlias(t, aliases, deps)
namedTypes[n] = resolveNamespace(t, aliases, deps)
continue
}
namedTypes[n] = rec(t)
@@ -107,7 +107,7 @@ func resolveAliases(namedTypes map[string]types.TypeRef, aliases map[string]ref.
}
func resolveAlias(t types.TypeRef, aliases map[string]ref.Ref, deps map[ref.Ref]types.PackageDef) types.TypeRef {
func resolveNamespace(t types.TypeRef, aliases map[string]ref.Ref, deps map[ref.Ref]types.PackageDef) types.TypeRef {
target, ok := aliases[t.Namespace()]
d.Exp.True(ok, "Could not find import aliased to %s", t.Namespace())
_, ok = deps[target].NamedTypes[t.Name()]