-
Notifications
You must be signed in to change notification settings - Fork 4
/
igo_test.go
77 lines (60 loc) · 1.45 KB
/
igo_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
package main
import (
"os"
"regexp"
"testing"
"github.com/rocketlaunchr/igo/cmds"
"github.com/spf13/cobra"
)
var buildCmd = &cobra.Command{
Use: "build",
Short: "Transpile igo files to go files",
Run: cmds.BuildCmd,
}
const chunkSize = 64000
func fileCompare(expected, generated string) (bool, error) {
exp, err := os.Open(expected)
if err != nil {
return false, err
}
defer exp.Close()
gen, err := os.Open(generated)
if err != nil {
return false, err
}
defer gen.Close()
expByte := make([]byte, chunkSize)
_, err = exp.Read(expByte)
if err != nil {
return false, err
}
genByte := make([]byte, chunkSize)
_, err = gen.Read(genByte)
if err != nil {
return false, err
}
// Comparing files using regexp patterns from expected_code file
matched, err := regexp.MatchString(string(expByte), string(genByte))
if err != nil {
return false, err
}
return matched, nil
}
func TestIgoBuild(t *testing.T) {
igoFile := "./test_files/test_sample.igo"
genFile := "./test_files/gen_test_sample.go"
expCode := "./test_files/expected.tgo"
// Generating go code from igo sample file
cmds.BuildCmd(buildCmd, []string{igoFile})
confirm, err := fileCompare(expCode, genFile)
if err != nil {
t.Errorf("Test failed with error: %s\n", err)
}
if !confirm {
t.Error("Generated Code is not same with expected code.")
}
// Delete generated file
if err := os.Remove(genFile); err != nil {
t.Errorf("Test failed with error:%s\n", err)
}
}