-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_test.go
81 lines (64 loc) · 1.92 KB
/
function_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
package jot
import (
"bytes"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)
func TestFunction(t *testing.T) {
b := new(bytes.Buffer)
spec := Function("testFunction")
err := spec.Write(b)
assert.Nil(t, err)
output := b.String()
expect := "func testFunction() {\n}\n"
assert.Equal(t, expect, output, "Output mismatch.")
}
func TestFunctionParameter(t *testing.T) {
b := new(bytes.Buffer)
typeSpec := Type(reflect.TypeOf((*string)(nil)).Elem())
spec := Function("testFunction").AddParameter("s1", typeSpec)
err := spec.Write(b)
assert.Nil(t, err)
output := b.String()
expect := "func testFunction(s1 string) {\n}\n"
assert.Equal(t, expect, output, "Output mismatch.")
spec.AddParameter("s2", typeSpec)
b.Reset()
err = spec.Write(b)
output = b.String()
expect = "func testFunction(s1 string, s2 string) {\n}\n"
assert.Equal(t, expect, output, "Output mismatch.")
}
func TestFunctionReturns(t *testing.T) {
b := new(bytes.Buffer)
typeSpec := Type(reflect.TypeOf((*string)(nil)).Elem())
spec := Function("testFunction").AddReturnType(typeSpec)
err := spec.Write(b)
assert.Nil(t, err)
output := b.String()
expect := "func testFunction() string {\n}\n"
assert.Equal(t, expect, output, "Output mismatch.")
spec.AddReturnType(typeSpec)
b.Reset()
err = spec.Write(b)
assert.Nil(t, err)
output = b.String()
expect = "func testFunction() (string, string) {\n}\n"
assert.Equal(t, expect, output, "Output mismatch.")
}
func TestFunctionCode(t *testing.T) {
b := new(bytes.Buffer)
spec := Function("testFunction").AddCode("println(1 + 2)")
err := spec.Write(b)
assert.Nil(t, err)
output := b.String()
expect := "func testFunction() {\nprintln(1 + 2)\n}\n"
assert.Equal(t, expect, output, "Output mismatch.")
spec = Function("testFunction").AddCodeFmt("println({0} + {1})", 1, 2)
b.Reset()
err = spec.Write(b)
assert.Nil(t, err)
output = b.String()
assert.Equal(t, expect, output, "Output mismatch.")
}