Skip to content

Commit 03e6620

Browse files
committed
chore: add test
1 parent 24c3528 commit 03e6620

File tree

10 files changed

+165
-7
lines changed

10 files changed

+165
-7
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
17+
/vendor

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
module github.com/anc95/golang-enum-to-ts
22

33
go 1.17
4+
5+
require github.com/gkampitakis/go-snaps v0.2.3
6+
7+
require (
8+
github.com/gkampitakis/ciinfo v0.1.0 // indirect
9+
github.com/kr/pretty v0.3.0 // indirect
10+
github.com/kr/text v0.2.0 // indirect
11+
github.com/rogpeppe/go-internal v1.8.0 // indirect
12+
github.com/sergi/go-diff v1.2.0 // indirect
13+
)

go.sum

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/gkampitakis/ciinfo v0.1.0 h1:hPvgzhg7q6xlTNl4hWgEy13esbW847lWTREf0GoezRQ=
5+
github.com/gkampitakis/ciinfo v0.1.0/go.mod h1:PoTj49S/eZLL7PvEdBeu9D1qpd3JsEHOCyf5Y5hVYOs=
6+
github.com/gkampitakis/go-snaps v0.2.3 h1:4pQiQ9GbxlWps/LSR/I72CDWXVQs2lpxEHqhZi9pw4s=
7+
github.com/gkampitakis/go-snaps v0.2.3/go.mod h1:0HSAq6psKeEEgZJpcBcYjEwYVqCh4ZDHbSV2+SH0V84=
8+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
9+
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
10+
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
11+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
12+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
13+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
14+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
15+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
16+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
17+
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
18+
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
19+
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
20+
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
21+
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
22+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
23+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
24+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
25+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
26+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
27+
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
28+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
29+
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

src/ast/ast.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package ast

src/generator/generator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package generator

src/test-cases/normal.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package some
2+
3+
type Status int
4+
5+
const (
6+
Todo Status = iota
7+
Done
8+
)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
[TestNormal - 1]
3+
[]token.Token{
4+
{
5+
Value: "package",
6+
Type: 20,
7+
Start: {0, 6},
8+
End: {0, 0},
9+
},
10+
{
11+
Value: "some",
12+
Type: 20,
13+
Start: {0, 11},
14+
End: {0, 0},
15+
},
16+
{
17+
Value: "type",
18+
Type: 1,
19+
Start: {2, 3},
20+
End: {0, 0},
21+
},
22+
{
23+
Value: "Status",
24+
Type: 20,
25+
Start: {2, 10},
26+
End: {0, 0},
27+
},
28+
{
29+
Value: "int",
30+
Type: 4,
31+
Start: {2, 14},
32+
End: {0, 0},
33+
},
34+
{
35+
Value: "const",
36+
Type: 3,
37+
Start: {4, 5},
38+
End: {0, 0},
39+
},
40+
{
41+
Value: "(",
42+
Type: 18,
43+
Start: {4, 6},
44+
End: {0, 0},
45+
},
46+
{
47+
Value: "Todo",
48+
Type: 20,
49+
Start: {5, 4},
50+
End: {0, 0},
51+
},
52+
{
53+
Value: "Status",
54+
Type: 20,
55+
Start: {5, 11},
56+
End: {0, 0},
57+
},
58+
{
59+
Value: "=",
60+
Type: 9,
61+
Start: {5, 13},
62+
End: {0, 0},
63+
},
64+
{
65+
Value: "iota",
66+
Type: 6,
67+
Start: {5, 18},
68+
End: {0, 0},
69+
},
70+
{
71+
Value: "Done",
72+
Type: 20,
73+
Start: {6, 4},
74+
End: {0, 0},
75+
},
76+
{
77+
Value: ")",
78+
Type: 19,
79+
Start: {7, 0},
80+
End: {0, 0},
81+
},
82+
}
83+
---

src/token/parse.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (parser *Parser) collectString() string {
9595
for {
9696
charInByte, err := parser.Reader.Next()
9797

98-
if parser.Reader.char == "\n" {
98+
if IsIllegalChar(parser.Reader.charInByte) {
9999
parser.Reader.ReportLineError()
100100
}
101101

@@ -182,6 +182,7 @@ func NewParser(s string) Parser {
182182

183183
func (parser *Parser) Parse() []Token {
184184
for {
185+
parser.Reader.SkipSpace()
185186
charInByte, err := parser.Reader.Next()
186187

187188
if err != nil {

src/token/reader.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (reader *Reader) SkipSpace() {
2020
row, col := reader.row, reader.col
2121
char, err := reader.Next()
2222

23-
if err != nil || string(char) != " " {
23+
if err != nil || (string(char) != " " && !IsIllegalChar(reader.charInByte)) {
2424
reader.row, reader.col = row, col
2525
break
2626
}
@@ -39,9 +39,13 @@ func (reader *Reader) Next() (byte, error) {
3939

4040
reader.col += 1
4141

42-
if reader.col == len(reader.lines[reader.row]) {
42+
for reader.col >= len(reader.lines[reader.row]) || len(reader.lines[reader.row]) == 0 {
4343
reader.row += 1
4444
reader.col = 0
45+
46+
if reader.isOverflow() {
47+
return 0, errors.New("Overflow")
48+
}
4549
}
4650

4751
if reader.isOverflow() {
@@ -69,7 +73,7 @@ func (reader *Reader) Back() {
6973
}
7074

7175
func (reader *Reader) isOverflow() bool {
72-
return reader.row >= len(reader.lines)
76+
return reader.row >= len(reader.lines) || (reader.row == len(reader.lines)-1 && reader.col >= len(reader.lines[reader.row]))
7377
}
7478

7579
func (reader *Reader) ReportLineError() bool {
@@ -83,11 +87,11 @@ func readLines(s []byte) [][]byte {
8387
line := []byte{}
8488

8589
for ; i < len(s); i++ {
86-
if s[i] == "\n"[0] {
90+
line = append(line, s[i])
91+
92+
if string(s[i]) == "\n" {
8793
break
8894
}
89-
90-
line = append(line, s[i])
9195
}
9296

9397
lines = append(lines, line)

src/token/token_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package token
2+
3+
import (
4+
"os"
5+
"path"
6+
"testing"
7+
8+
"github.com/gkampitakis/go-snaps/snaps"
9+
)
10+
11+
func TestNormal(t *testing.T) {
12+
wd, _ := os.Getwd()
13+
14+
a := path.Join(wd, "../test-cases/normal.go")
15+
parser := NewParser(a)
16+
result := parser.Parse()
17+
18+
snaps.MatchSnapshot(t, result)
19+
}

0 commit comments

Comments
 (0)