-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
176 lines (144 loc) · 5.21 KB
/
Copy pathmain_test.go
File metadata and controls
176 lines (144 loc) · 5.21 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"ella.to/ella/compiler"
)
func TestGenCmd_GeneratesRuntimeTSForConsts(t *testing.T) {
tmpDir := t.TempDir()
schemaPath := filepath.Join(tmpDir, "schema.ella")
outDTS := filepath.Join(tmpDir, "schema.gen.d.ts")
outTS := filepath.Join(tmpDir, "schema.gen.ts")
source := `const TopicUserCreated = "jetdrive.user.created"`
if err := os.WriteFile(schemaPath, []byte(withModule(source)), 0o644); err != nil {
t.Fatalf("failed writing schema: %v", err)
}
genCmd([]string{schemaPath}, "schema", outDTS, false, false)
if _, err := os.Stat(outDTS); err != nil {
t.Fatalf("expected declaration output file to exist: %v", err)
}
if _, err := os.Stat(outTS); err != nil {
t.Fatalf("expected runtime output file to exist: %v", err)
}
}
func TestGenCmd_SkipsRuntimeTSWhenNoConsts(t *testing.T) {
tmpDir := t.TempDir()
schemaPath := filepath.Join(tmpDir, "schema.ella")
outDTS := filepath.Join(tmpDir, "schema.gen.d.ts")
outTS := filepath.Join(tmpDir, "schema.gen.ts")
source := `model User {
Id: string
}`
if err := os.WriteFile(schemaPath, []byte(withModule(source)), 0o644); err != nil {
t.Fatalf("failed writing schema: %v", err)
}
genCmd([]string{schemaPath}, "schema", outDTS, false, false)
if _, err := os.Stat(outDTS); err != nil {
t.Fatalf("expected declaration output file to exist: %v", err)
}
if _, err := os.Stat(outTS); !os.IsNotExist(err) {
t.Fatalf("expected runtime output file to be absent, got err=%v", err)
}
}
func TestGenCmd_GeneratesRuntimeTSForErrors(t *testing.T) {
tmpDir := t.TempDir()
schemaPath := filepath.Join(tmpDir, "schema.ella")
outDTS := filepath.Join(tmpDir, "schema.gen.d.ts")
outTS := filepath.Join(tmpDir, "schema.gen.ts")
source := `error ErrNotFound { Msg = "resource not found" }`
if err := os.WriteFile(schemaPath, []byte(withModule(source)), 0o644); err != nil {
t.Fatalf("failed writing schema: %v", err)
}
genCmd([]string{schemaPath}, "schema", outDTS, false, false)
if _, err := os.Stat(outDTS); err != nil {
t.Fatalf("expected declaration output file to exist: %v", err)
}
if _, err := os.Stat(outTS); err != nil {
t.Fatalf("expected runtime output file to exist: %v", err)
}
}
func TestGenCmd_GeneratesTypeScriptClientForTSOutput(t *testing.T) {
tmpDir := t.TempDir()
schemaPath := filepath.Join(tmpDir, "schema.ella")
outTS := filepath.Join(tmpDir, "schema.gen.ts")
source := `model User {
Id: string
}
service UserService {
GetById (id: string) => (user: User)
}
error ErrUserNotFound { Msg = "user not found" }
`
if err := os.WriteFile(schemaPath, []byte(withModule(source)), 0o644); err != nil {
t.Fatalf("failed writing schema: %v", err)
}
genCmd([]string{schemaPath}, "schema", outTS, false, false)
b, err := os.ReadFile(outTS)
if err != nil {
t.Fatalf("expected ts output file to exist: %v", err)
}
code := string(b)
if !strings.Contains(code, `export function createFetchJsonRpc(host: string, options: FetchJsonRpcOptions = {}): EllaRpcConnection {`) {
t.Fatalf("expected fetch helper in ts client output, got:\n%s", code)
}
if !strings.Contains(code, `export function createUserService(conn: EllaRpcConnection): UserService {`) {
t.Fatalf("expected createUserService factory in ts client output, got:\n%s", code)
}
}
func TestHasConstDeclarations(t *testing.T) {
progWithConst := parseProgramFromSource(t, `const Topic = "x"`)
if !hasConstDeclarations(progWithConst) {
t.Fatal("expected hasConstDeclarations to return true")
}
progWithoutConst := parseProgramFromSource(t, `model User {
Id: string
}`)
if hasConstDeclarations(progWithoutConst) {
t.Fatal("expected hasConstDeclarations to return false")
}
}
func TestHasErrorDeclarations(t *testing.T) {
progWithError := parseProgramFromSource(t, `error ErrNotFound { Msg = "x" }`)
if !hasErrorDeclarations(progWithError) {
t.Fatal("expected hasErrorDeclarations to return true")
}
progWithoutError := parseProgramFromSource(t, `const Topic = "x"`)
if hasErrorDeclarations(progWithoutError) {
t.Fatal("expected hasErrorDeclarations to return false")
}
}
func TestHasRuntimeTypeScriptExports(t *testing.T) {
progWithConst := parseProgramFromSource(t, `const Topic = "x"`)
if !hasRuntimeTypeScriptExports(progWithConst) {
t.Fatal("expected hasRuntimeTypeScriptExports to return true for const")
}
progWithError := parseProgramFromSource(t, `error ErrNotFound { Msg = "x" }`)
if !hasRuntimeTypeScriptExports(progWithError) {
t.Fatal("expected hasRuntimeTypeScriptExports to return true for error")
}
progWithoutBoth := parseProgramFromSource(t, `model User {
Id: string
}`)
if hasRuntimeTypeScriptExports(progWithoutBoth) {
t.Fatal("expected hasRuntimeTypeScriptExports to return false")
}
}
// withModule prepends the now-required module declaration to a test source.
func withModule(source string) string {
if strings.HasPrefix(source, "\n") {
return "module test" + source
}
return "module test\n" + source
}
func parseProgramFromSource(t *testing.T, source string) *compiler.Program {
t.Helper()
scanner := compiler.NewScanner(strings.NewReader(withModule(source)), "test.ella")
parser := compiler.NewParser(scanner)
program, err := parser.Parse()
if err != nil {
t.Fatalf("parse error: %v", err)
}
return program
}