-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
79 lines (65 loc) · 1.91 KB
/
main_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
package main
import (
"bytes"
"os"
"strings"
"super-code-gen/pkg/generate"
"testing"
)
func TestGenRoute(t *testing.T) {
getUserFileWant :=
[]byte(`import { RequestHandler } from "express";
interface GetUserReqBody {
}
export const getUser:RequestHandler=async (req,res)=>{
const body = req.body as GetUserReqBody
}`)
addUserFileWant :=
[]byte(`import { RequestHandler } from "express";
interface AddUserReqBody {
}
export const addUser:RequestHandler=async (req,res)=>{
const body = req.body as AddUserReqBody
}`)
indexFileWant := []byte(`export { getUser } from "./getUser";
export { addUser } from "./addUser";
`)
userRoutesFileWant := []byte(`import { Router } from "express";
import { getUser, addUser, } from "../controllers/user-controllers";
import { body } from "express-validator";
const userRouter = Router();
userRouter.post("/getUser", getUser);
userRouter.post("/addUser", addUser);
export default user;`)
name := "user"
routes := []string{"user/getUser", "user/addUser"}
validate, body := true, true
paths := make([]string, 0)
for _, arg := range routes {
paths = append(paths, strings.Split(arg, "/")[1])
}
generate.GenRoute(name, paths, validate)
generate.GenController(name, paths, body)
controllerRoot := "controllers/user-controllers/"
getUserFile, e := os.ReadFile(controllerRoot + "getUser.ts")
addUserFile, _ := os.ReadFile(controllerRoot + "addUser.ts")
indexFile, _ := os.ReadFile(controllerRoot + "index.ts")
userRoutesFile, _ := os.ReadFile("routes/user-routes.ts")
files := [][][]byte{
{getUserFile, getUserFileWant},
{addUserFile, addUserFileWant},
{indexFile, indexFileWant},
{userRoutesFile, userRoutesFileWant},
}
for _, fileGroup := range files {
res := bytes.Compare(fileGroup[0], []byte(fileGroup[1]))
if res == 1 {
t.Fail()
}
if e != nil {
t.Fatalf("File: getUser, Error %v", e)
}
}
os.RemoveAll(controllerRoot)
os.Remove("routes/user-routes.ts")
}