Repository navigation
Expand file tree
/
Copy pathformat_print_original_test.go
More file actions
126 lines (111 loc) · 1.97 KB
/
Copy pathformat_print_original_test.go
File metadata and controls
126 lines (111 loc) · 1.97 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
package javascript_test
import (
"bytes"
"fmt"
"reflect"
"testing"
"vimagination.zapto.org/javascript"
"vimagination.zapto.org/javascript/walk"
"vimagination.zapto.org/parser"
)
func TestPrintingOriginal(t *testing.T) {
for n, test := range [...]struct {
Input, Output string
}{
{ // 1
"a = 1",
"a=1",
},
{ // 2
"a = 1;",
"a=1",
},
{ // 3
"a = 1;b=2",
"a=1;b=2",
},
{ // 4
"import a from './b'",
"import a from'./b'",
},
{ // 5
"import {a as b} from './b'",
"import{a as b}from'./b'",
},
{ // 6
"export {a as b}",
"export{a as b}",
},
{ // 7
"for (a in b) {}",
"for(a in b){}",
},
{ // 8
"for (const [a] of [b]) {}",
"for(const[a]of[b]){}",
},
{ // 9
"#a in b",
"#a in b",
},
{ // 10
"{a=1}",
"{a=1}",
},
{ // 11
"throw 1",
"throw 1",
},
{ // 12
"class A{a;b;c;d(){}e}",
"class A{a;b;c;d(){}e}",
},
{ // 13
"({a})",
"({a})",
},
{ // 14
"({a,b})",
"({a,b})",
},
{ // 15
"({a:d,b:c})",
"({a:d,b:c})",
},
{ // 16
"({a,b:c})",
"({a,b:c})",
},
{ // 17
"for(const{a,b}in c){}",
"for(const{a,b}in c){}",
},
} {
tk := parser.NewStringTokeniser(test.Input)
m, err := javascript.ParseModule(&tk)
if err != nil {
t.Errorf("test %d.1: unexpected error: %s", n+1, err)
continue
}
var h walk.Handler
h = walk.HandlerFunc(func(t javascript.Type) error {
v := reflect.ValueOf(t)
if v.Type().Kind() != reflect.Pointer || v.Type().Elem().Kind() != reflect.Struct {
return nil
}
if f, ok := v.Type().Elem().FieldByName("Tokens"); ok {
v.Elem().FieldByIndex(f.Index).SetZero()
}
return walk.Walk(t, h)
})
if err = h.Handle(m); err != nil {
t.Errorf("test %d.2: unexpected error: %s", n+1, err)
continue
}
var buf bytes.Buffer
fmt.Fprintf(&buf, "%#s", m)
if str := buf.String(); str != test.Output {
t.Errorf("test %d.3: expecting %q, got %q", n+1, test.Output, str)
}
}
}