Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Original file line number Diff line number Diff line change
1
+ package builder
2
+
3
+ import (
4
+ "strconv"
5
+ "testing"
6
+ )
7
+
8
+ type Builder1 struct {
9
+ result string
10
+ }
11
+
12
+ func (b * Builder1 ) Part1 () {
13
+ b .result += "1"
14
+ }
15
+
16
+ func (b * Builder1 ) Part2 () {
17
+ b .result += "2"
18
+ }
19
+
20
+ func (b * Builder1 ) Part3 () {
21
+ b .result += "3"
22
+ }
23
+
24
+ func (b * Builder1 ) GetResult () string {
25
+ return b .result
26
+ }
27
+
28
+ func TestBuilder1 (t * testing.T ) {
29
+ builder := & Builder1 {}
30
+ director := NewDirector (builder )
31
+ director .Construct ()
32
+ res := builder .GetResult ()
33
+ if res != "123" {
34
+ t .Fatalf ("Builder1 fail expect 123 acture %s" , res )
35
+ }
36
+ }
37
+
38
+ type Builder2 struct {
39
+ result int
40
+ }
41
+
42
+ func (b * Builder2 ) Part1 () {
43
+ b .result += 1
44
+ }
45
+
46
+ func (b * Builder2 ) Part2 () {
47
+ b .result += 2
48
+ }
49
+
50
+ func (b * Builder2 ) Part3 () {
51
+ b .result += 3
52
+ }
53
+
54
+ func (b * Builder2 ) GetResult () string {
55
+ return strconv .Itoa (b .result )
56
+ }
57
+
58
+ func TestBuilder2 (t * testing.T ) {
59
+ builder := & Builder2 {}
60
+ director := NewDirector (builder )
61
+ director .Construct ()
62
+ res := builder .GetResult ()
63
+ if res != "6" {
64
+ t .Fatalf ("Builder2 fail expect 6 acture %s" , res )
65
+ }
66
+ }
0 commit comments