-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_test.go
95 lines (78 loc) · 1.79 KB
/
php_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
package strlang
import (
"fmt"
"testing"
)
func TestPHP_If(t *testing.T) {
testPHPInit(func(b *PHP) (*testing.T, string, string) {
b.If("true", func() {
b.WriteStringln("return false;")
})
expected := "" +
"if (true) {\n" +
" return false;\n" +
"}\n"
return t, expected, b.String()
})
}
func TestPHP_Else(t *testing.T) {
testPHPInit(func(b *PHP) (*testing.T, string, string) {
b.If("true", func() {
b.WriteStringln("return false;")
})
b.Else(func() {
b.WriteStringln("return true;")
})
expected := "" +
"if (true) {\n" +
" return false;\n" +
"} else {\n" +
" return true;\n" +
"}\n"
return t, expected, b.String()
})
}
func TestPHP_ElseIf(t *testing.T) {
testPHPInit(func(b *PHP) (*testing.T, string, string) {
b.If("true", func() {
b.WriteStringln("return false;")
})
b.ElseIf("false", func() {
b.WriteStringln("return true;")
})
expected := "" +
"if (true) {\n" +
" return false;\n" +
"} elseif (false) {\n" +
" return true;\n" +
"}\n"
return t, expected, b.String()
})
}
func TestPHP_Class(t *testing.T) {
testPHPInit(func(b *PHP) (*testing.T, string, string) {
b.Class("User", func() {
b.ClassFunc("public", "getEmail", "", "string", func() {
b.WriteStringln("return $this->email;")
})
})
expected := "" +
"class User {\n" +
" public function getEmail(): string {\n" +
" return $this->email;\n" +
" }\n\n" +
"}\n\n"
return t, expected, b.String()
})
}
func testPHPInit(fn func(b *PHP) (t *testing.T, expected, given string)) {
b := NewPHP("App")
b.SetIndentChar(" ")
t, expected, given := fn(b)
expected = "<?php\n\nnamespace App;\n\n" + expected
if given != expected {
fmt.Println(expected)
fmt.Println(given)
t.Errorf("Expected %q but got %q", expected, given)
}
}