Files
dolt/nomdl/codegen/codegen_test.go
Chris Masone 5ce93dad2e Beginning of import support in NomDL
This patch mostly merges parse.Package and types.Package, though it
can't quite go all the way. A types.Package doesn't have 'using'
declarations, while the parsed representation of a .noms file needs to
have that information. Hence, the parse package is moved to the 'pkg'
package, and pkg.Parsed is introduced. This type embeds types.Package
and adds the necessary additional information.

To make inroads on handling imports, I enhanced ParsePackage() (now
called ParseNomDL()) to actually process the 'alias' and 'import'
statements in the input and go replace namespaced type names in the
package with refs of imported packages. For example, the TypeRef for
'Bar' generated in the following package

alias Foo = import "sha1-ffffffff"

struct Bar {
  f: Foo.RockinStruct
}

will actually return types.Ref{sha1-ffffffff} when you call PackageRef()
on it.

In addition, I've added a function to the new 'pkg' package,
which allows the caller to get the dependencies of a type package
from a chunk store.

Fixes issue #353, towards issue #294
2015-09-28 16:08:22 -07:00

138 lines
2.9 KiB
Go

package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/attic-labs/noms/Godeps/_workspace/src/golang.org/x/tools/imports"
"github.com/attic-labs/noms/chunks"
"github.com/attic-labs/noms/Godeps/_workspace/src/github.com/stretchr/testify/assert"
"github.com/attic-labs/noms/d"
"github.com/attic-labs/noms/nomdl/pkg"
)
func assertOutput(inPath, goldenPath string, t *testing.T) {
assert := assert.New(t)
emptyCS := chunks.NewMemoryStore() // Will be ChunkSource containing imports
inFile, err := os.Open(inPath)
assert.NoError(err)
defer inFile.Close()
goldenFile, err := os.Open(goldenPath)
assert.NoError(err)
defer goldenFile.Close()
goldenBytes, err := ioutil.ReadAll(goldenFile)
d.Chk.NoError(err)
var buf bytes.Buffer
pkg := pkg.ParseNomDL("", inFile, emptyCS)
gen := NewCodeGen(&buf, getBareFileName(inPath), pkg)
gen.WritePackage("test")
bs, err := imports.Process("", buf.Bytes(), nil)
d.Chk.NoError(err)
assert.Equal(string(goldenBytes), string(bs))
}
func TestGeneratedFiles(t *testing.T) {
files, err := filepath.Glob("test/gen/*.noms")
d.Chk.NoError(err)
for _, n := range files {
_, file := filepath.Split(n)
assertOutput(n, "test/"+file[:len(file)-5]+".go", t)
}
}
func TestCanUseDef(t *testing.T) {
assert := assert.New(t)
emptyCS := chunks.NewMemoryStore() // Will be ChunkSource containing imports
assertCanUseDef := func(s string, using, named bool) {
pkg := pkg.ParseNomDL("", bytes.NewBufferString(s), emptyCS)
gen := NewCodeGen(nil, "fakefile", pkg)
for _, t := range pkg.UsingDeclarations {
assert.Equal(using, gen.canUseDef(t))
}
for _, t := range pkg.NamedTypes {
assert.Equal(named, gen.canUseDef(t))
}
}
good := `
using List(Int8)
using Set(Int8)
using Map(Int8, Int8)
using Map(Int8, Set(Int8))
using Map(Int8, Map(Int8, Int8))
struct Simple {
x: Int8
}
using Set(Simple)
using Map(Simple, Int8)
using Map(Simple, Simple)
`
assertCanUseDef(good, true, true)
good = `
struct Tree {
children: List(Tree)
}
`
assertCanUseDef(good, true, true)
bad := `
struct WithList {
x: List(Int8)
}
using Set(WithList)
using Map(WithList, Int8)
struct WithSet {
x: Set(Int8)
}
using Set(WithSet)
using Map(WithSet, Int8)
struct WithMap {
x: Map(Int8, Int8)
}
using Set(WithMap)
using Map(WithMap, Int8)
`
assertCanUseDef(bad, false, true)
bad = `
struct Commit {
value: Value
parents: Set(Commit)
}
`
assertCanUseDef(bad, false, false)
bad = `
Set(Set(Int8))
Set(Map(Int, Int8))
Set(List(Int8))
Map(Set(Int8), Int8)
Map(Map(Int8, Int8), Int8)
Map(List(Int8), Int8)
`
for _, line := range strings.Split(bad, "\n") {
if strings.TrimSpace(line) == "" {
continue
}
assertCanUseDef(fmt.Sprintf("using %s", line), false, false)
assertCanUseDef(fmt.Sprintf("struct S { x: %s }", line), false, false)
}
}