forked from stephens2424/php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope_test.go
106 lines (90 loc) · 2.24 KB
/
scope_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
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
package php
import (
"testing"
"github.com/stephens2424/php/ast"
)
func TestScope(t *testing.T) {
src := `<?php
$var1 = "a";
function simple() {
$var2 = "b";
$var3 = "c";
}
class fizz {
const buzz = "fizzbuzz";
function notsimple() {
$var4 = "d";
}
}
`
p := NewParser()
a, err := p.Parse("test.php", src)
if err != nil {
t.Fatal(err)
}
ExpectFunctions(a.Namespace, []string{"simple"}, t)
ExpectClasses(a.Namespace, []string{"fizz"}, t)
ExpectVariables(p.FileSet.GlobalScope.Scope, []string{"var1"}, t)
ExpectVariables(a.Namespace.Functions["simple"].Body.Scope, []string{"var2", "var3"}, t)
ExpectVariables(a.Namespace.ClassesAndInterfaces["fizz"].(*ast.Class).Methods[0].FunctionStmt.Body.Scope, []string{"var4"}, t)
}
func ExpectVariables(s *ast.Scope, variables []string, t *testing.T) {
expected := map[string]struct{}{}
hasError := false
for _, v := range variables {
expected[v] = struct{}{}
if _, ok := s.Identifiers[v]; !ok {
t.Errorf("expected identifier $%q, but didn't find it", v)
hasError = true
}
}
for v := range s.Identifiers {
if _, ok := expected[v]; !ok {
t.Errorf("found identifier $%q, but didn't expect it", v)
hasError = true
}
}
if hasError {
t.FailNow()
}
}
func ExpectFunctions(ns *ast.Namespace, functions []string, t *testing.T) {
expected := map[string]struct{}{}
hasError := false
for _, fn := range functions {
expected[fn] = struct{}{}
if _, ok := ns.Functions[fn]; !ok {
t.Errorf("expected function %q, but didn't find it", fn)
hasError = true
}
}
for fn := range ns.Functions {
if _, ok := expected[fn]; !ok {
t.Errorf("found function %q, but didn't expect it", fn)
hasError = true
}
}
if hasError {
t.FailNow()
}
}
func ExpectClasses(ns *ast.Namespace, classes []string, t *testing.T) {
expected := map[string]struct{}{}
hasError := false
for _, class := range classes {
expected[class] = struct{}{}
if _, ok := ns.ClassesAndInterfaces[class]; !ok {
t.Errorf("expected class %q, but didn't find it", class)
hasError = true
}
}
for class := range ns.ClassesAndInterfaces {
if _, ok := expected[class]; !ok {
t.Errorf("found class %q, but didn't expect it", class)
hasError = true
}
}
if hasError {
t.FailNow()
}
}