-
Notifications
You must be signed in to change notification settings - Fork 10
/
roundtrip_test.go
153 lines (131 loc) · 2.74 KB
/
roundtrip_test.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package bel
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"testing"
"github.com/go-test/deep"
)
func TestRoundtrip(t *testing.T) {
if !isNpxAvailable() {
t.Skip("npm is not available - skipping round trip")
return
} else if testing.Short() {
t.Skip("runnig short test - skipping round trip test")
return
}
ws, err := ioutil.TempDir("", "")
if !installTsNode(t, ws) {
return
}
defer os.RemoveAll(ws)
testdata := MyTestStruct{
StringField: "hello world",
// OptionalField string `json:",omitempty"`
NamedField: 42,
// NamedOptionalField int32 `json:"thisIsOptional,omitempty"`
SkipThisField: "has-a-value",
Containment: AnotherTestStruct{
Bar: true,
Foo: "baz",
},
Referece: &AnotherTestStruct{
Bar: false,
Foo: "abc",
},
}
if err != nil {
t.Error(err)
return
}
if !generateTypescript(t, ws, testdata) {
return
}
fromTS := executeTypescript(t, ws)
if fromTS == nil {
return
}
testdata.SkipThisField = ""
diff := deep.Equal(&testdata, fromTS)
for _, d := range diff {
t.Error(d)
}
}
func installTsNode(t *testing.T, ws string) bool {
cmd := exec.Command("/bin/sh", "-c", "npm install typescript ts-node")
cmd.Dir = ws
if out, err := cmd.CombinedOutput(); err != nil {
t.Skip("cannot install ts-node: " + string(out))
return false
}
return true
}
func generateTypescript(t *testing.T, ws string, testdata MyTestStruct) bool {
handler, err := NewParsedSourceEnumHandler(".")
if err != nil {
t.Error(err)
return false
}
extract, err := Extract(MyTestStruct{},
WithEnumerations(handler),
FollowStructs,
)
if err != nil {
t.Error(err)
return false
}
f, err := os.Create(path.Join(ws, "struct.ts"))
if err != nil {
t.Error(err)
return false
}
err = Render(extract, GenerateOutputTo(f))
if err != nil {
f.Close()
t.Error(err)
return false
}
f.Close()
testjson, err := json.Marshal(testdata)
if err != nil {
t.Error(err)
return false
}
indexts := []byte(fmt.Sprintf(`
import { MyTestStruct } from "./struct";
const data: MyTestStruct = %s;
console.log(JSON.stringify(data));
`, string(testjson)))
err = ioutil.WriteFile(path.Join(ws, "index.ts"), indexts, 0744)
if err != nil {
t.Error(err)
return false
}
return true
}
func executeTypescript(t *testing.T, ws string) *MyTestStruct {
cmd := exec.Command("./node_modules/.bin/ts-node", "index.ts")
cmd.Dir = ws
out, err := cmd.CombinedOutput()
if err != nil {
t.Error(string(out))
return nil
}
result := MyTestStruct{}
err = json.Unmarshal(out, &result)
if err != nil {
t.Error(err)
return nil
}
return &result
}
func isNpxAvailable() bool {
cmd := exec.Command("/bin/sh", "-c", "which npx")
if err := cmd.Run(); err != nil {
return false
}
return true
}