-
Notifications
You must be signed in to change notification settings - Fork 35
/
tests_util.go
69 lines (57 loc) · 1.42 KB
/
tests_util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
// "fmt"
"bytes"
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"log"
"strings"
)
func extractor(xmlStrings []string) error {
log.SetFlags(log.LstdFlags | log.Lshortfile)
ex := Extractor{
namePrefix: namePrefix,
nameSuffix: nameSuffix,
useType: useType,
progress: progress,
ignoreXmlDecodingErrors: ignoreXmlDecodingErrors,
}
ex.init()
for i, _ := range xmlStrings {
//log.Println(xmlStrings[i])
err := ex.extract(strings.NewReader(xmlStrings[i]))
if err != nil {
return err
}
}
ex.done()
buf := bytes.NewBufferString("")
fps := make([]string, 1)
//fps[0] = "foo"
generateGoCode(buf, fps, &ex)
//log.Println(buf.String())
return parseAndType(buf.String())
}
// Derived from example at https://github.com/golang/example/tree/master/gotypes#an-example
func parseAndType(s string) error {
fset := token.NewFileSet()
//f, err := parser.ParseFile(fset, "hello.go", hello, 0)
f, err := parser.ParseFile(fset, "hello.go", s, 0)
if err != nil {
//t.Error("Expected 1.5, got ", v)
//log.Fatal(err) // parse error
return err
}
conf := types.Config{Importer: importer.Default()}
// Type-check the package containing only file f.
// Check returns a *types.Package.
_, err = conf.Check("cmd/hello", fset, []*ast.File{f}, nil)
if err != nil {
//log.Fatal(err) // type error
return err
}
return nil
}