Skip to content

Commit befe1db

Browse files
Dave Brophyflimzy
Dave Brophy
authored andcommitted
Test for different order files
1 parent ed0c57b commit befe1db

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

compiler/compiler_test.go

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package compiler
2+
3+
import (
4+
"bytes"
5+
"go/ast"
6+
"go/build"
7+
"go/parser"
8+
"go/token"
9+
"go/types"
10+
"testing"
11+
12+
"fmt"
13+
14+
"github.com/sergi/go-diff/diffmatchpatch"
15+
"golang.org/x/tools/go/loader"
16+
)
17+
18+
func TestOrder(t *testing.T) {
19+
fileA := `
20+
package foo
21+
22+
var Avar = "a"
23+
24+
type Atype struct{}
25+
26+
func Afunc() int {
27+
var varA = 1
28+
var varB = 2
29+
return varA+varB
30+
}
31+
`
32+
33+
fileB := `
34+
package foo
35+
36+
var Bvar = "b"
37+
38+
type Btype struct{}
39+
40+
func Bfunc() int {
41+
var varA = 1
42+
var varB = 2
43+
return varA+varB
44+
}
45+
`
46+
files := []source{{"fileA.go", []byte(fileA)}, {"fileB.go", []byte(fileB)}}
47+
48+
compare(t, "foo", files, false)
49+
compare(t, "foo", files, true)
50+
51+
}
52+
53+
func compare(t *testing.T, path string, sourceFiles []source, minify bool) {
54+
outputNormal, err := compile(path, sourceFiles, minify)
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
59+
// reverse the array
60+
for i, j := 0, len(sourceFiles)-1; i < j; i, j = i+1, j-1 {
61+
sourceFiles[i], sourceFiles[j] = sourceFiles[j], sourceFiles[i]
62+
}
63+
64+
outputReversed, err := compile(path, sourceFiles, minify)
65+
if err != nil {
66+
t.Fatal(err)
67+
}
68+
69+
if string(outputNormal) != string(outputReversed) {
70+
dmp := diffmatchpatch.New()
71+
diffs := dmp.DiffMain(string(outputNormal), string(outputReversed), true)
72+
fmt.Println(dmp.DiffPrettyText(diffs))
73+
t.Fatal("files in different order produces differens JS")
74+
}
75+
}
76+
77+
type source struct {
78+
name string
79+
contents []byte
80+
}
81+
82+
func compile(path string, sourceFiles []source, minify bool) ([]byte, error) {
83+
84+
conf := loader.Config{}
85+
conf.Fset = token.NewFileSet()
86+
conf.ParserMode = parser.ParseComments
87+
88+
context := build.Default // make a copy of build.Default
89+
conf.Build = &context
90+
conf.Build.BuildTags = []string{"js"}
91+
92+
var astFiles []*ast.File
93+
for _, sourceFile := range sourceFiles {
94+
astFile, err := parser.ParseFile(conf.Fset, sourceFile.name, sourceFile.contents, parser.ParseComments)
95+
if err != nil {
96+
return nil, err
97+
}
98+
astFiles = append(astFiles, astFile)
99+
}
100+
conf.CreateFromFiles(path, astFiles...)
101+
prog, err := conf.Load()
102+
if err != nil {
103+
return nil, err
104+
}
105+
106+
archiveCache := map[string]*Archive{}
107+
var importContext *ImportContext
108+
importContext = &ImportContext{
109+
Packages: make(map[string]*types.Package),
110+
Import: func(path string) (*Archive, error) {
111+
112+
// find in local cache
113+
if a, ok := archiveCache[path]; ok {
114+
return a, nil
115+
}
116+
117+
pi := prog.Package(path)
118+
importContext.Packages[path] = pi.Pkg
119+
120+
// compile package
121+
a, err := Compile(path, pi.Files, prog.Fset, importContext, minify)
122+
if err != nil {
123+
return nil, err
124+
}
125+
archiveCache[path] = a
126+
return a, nil
127+
},
128+
}
129+
130+
a, err := importContext.Import(path)
131+
if err != nil {
132+
return nil, err
133+
}
134+
b, err := renderPackage(a)
135+
if err != nil {
136+
return nil, err
137+
}
138+
return b, nil
139+
}
140+
141+
func renderPackage(archive *Archive) ([]byte, error) {
142+
143+
selection := make(map[*Decl]struct{})
144+
for _, d := range archive.Declarations {
145+
selection[d] = struct{}{}
146+
}
147+
148+
buf := &bytes.Buffer{}
149+
150+
if err := WritePkgCode(archive, selection, false, &SourceMapFilter{Writer: buf}); err != nil {
151+
return nil, err
152+
}
153+
154+
return buf.Bytes(), nil
155+
}

0 commit comments

Comments
 (0)