@@ -10,7 +10,29 @@ import (
10
10
"testing"
11
11
)
12
12
13
- func TestMainFunction (t * testing.T ) {
13
+ // Constant tests
14
+
15
+ func TestVersionString (t * testing.T ) {
16
+ r , _ := regexp .Compile (`\d{1,2}.\d{1,2}.\d{1,2}` )
17
+ // match expected format of version string
18
+ if r .MatchString (version ) != true {
19
+ t .Errorf ("[FAIL] Unexpected version string format identified." )
20
+ }
21
+ }
22
+
23
+ func TestUsageString (t * testing.T ) {
24
+ if strings .HasPrefix (usage , "Usage:" ) == false {
25
+ t .Errorf ("[FAIL] Unexpected usage string format." )
26
+ }
27
+ }
28
+
29
+ func TestHelpString (t * testing.T ) {
30
+ if strings .HasPrefix (help , "=====" ) == false {
31
+ t .Errorf ("[FAIL] Unexpected help string format." )
32
+ }
33
+ }
34
+
35
+ func TestMainFunctionSingleGlyph (t * testing.T ) {
14
36
os .Args = []string {"uni" , "j" }
15
37
old := os .Stdout // keep backup of stdout
16
38
r , w , err := os .Pipe ()
@@ -37,28 +59,115 @@ func TestMainFunction(t *testing.T) {
37
59
38
60
if len (out ) == 0 {
39
61
t .Errorf ("[FAIL] Test of main() function did not return standard output response" )
40
- } else if ! strings .HasPrefix (out , "U+006A" ) {
41
- t .Errorf ("[FAIL] Expected execution of 'uni f' to return string that begins with 'U+006A', but instead it returned %s" , out )
62
+ }
63
+
64
+ if ! (out == "U+006A 'j'\n " ) {
65
+ t .Errorf ("[FAIL] Expected execution of 'uni f' to return string U+006A 'j', but instead it returned %s" , out )
42
66
}
43
67
44
68
}
45
69
46
- func TestVersionString (t * testing.T ) {
47
- r , _ := regexp .Compile (`\d{1,2}.\d{1,2}.\d{1,2}` )
48
- // match expected format of version string
49
- if r .MatchString (version ) != true {
50
- t .Errorf ("[FAIL] Unexpected version string format identified." )
70
+ func TestMainFunctionMultiGlyph (t * testing.T ) {
71
+ os .Args = []string {"uni" , "jj" }
72
+ old := os .Stdout // keep backup of stdout
73
+ r , w , err := os .Pipe ()
74
+ if err != nil {
75
+ log .Fatal (err )
51
76
}
77
+ os .Stdout = w
78
+
79
+ outC := make (chan string )
80
+
81
+ // copy the output in a separate goroutine
82
+ go func () {
83
+ var buf bytes.Buffer
84
+ io .Copy (& buf , r )
85
+ outC <- buf .String ()
86
+ }()
87
+
88
+ main () // call main function with mock os.Args defined above
89
+
90
+ // back to normal state
91
+ w .Close ()
92
+ os .Stdout = old // restoring the real stdout
93
+ out := <- outC
94
+
95
+ if len (out ) == 0 {
96
+ t .Errorf ("[FAIL] Test of main() function did not return standard output response" )
97
+ }
98
+
99
+ if ! (out == "U+006A 'j'\n U+006A 'j'\n " ) {
100
+ t .Errorf ("[FAIL] Expected execution of 'uni f' to return string U+006A 'j' * 2, but instead it returned %s" , out )
101
+ }
102
+
52
103
}
53
104
54
- func TestUsageString (t * testing.T ) {
55
- if strings .HasPrefix (usage , "Usage:" ) == false {
56
- t .Errorf ("[FAIL] Unexpected usage string format." )
105
+ func TestMainFunctionSingleUnicodeCodePoint (t * testing.T ) {
106
+ os .Args = []string {"uni" , "-g" , "006A" }
107
+ old := os .Stdout // keep backup of stdout
108
+ r , w , err := os .Pipe ()
109
+ if err != nil {
110
+ log .Fatal (err )
111
+ }
112
+ os .Stdout = w
113
+
114
+ outC := make (chan string )
115
+
116
+ // copy the output in a separate goroutine
117
+ go func () {
118
+ var buf bytes.Buffer
119
+ io .Copy (& buf , r )
120
+ outC <- buf .String ()
121
+ }()
122
+
123
+ main () // call main function with mock os.Args defined above
124
+
125
+ // back to normal state
126
+ w .Close ()
127
+ os .Stdout = old // restoring the real stdout
128
+ out := <- outC
129
+
130
+ if len (out ) == 0 {
131
+ t .Errorf ("[FAIL] Test of main() function did not return standard output response" )
132
+ }
133
+
134
+ if ! (out == "U+006A 'j'\n " ) {
135
+ t .Errorf ("[FAIL] Expected execution of 'uni f' to return string U+006A 'j', but instead it returned %s" , out )
57
136
}
137
+
58
138
}
59
139
60
- func TestHelpString (t * testing.T ) {
61
- if strings .HasPrefix (help , "=====" ) == false {
62
- t .Errorf ("[FAIL] Unexpected help string format." )
140
+ func TestMainFunctionMultiUnicodeCodePoint (t * testing.T ) {
141
+ os .Args = []string {"uni" , "-g" , "006A" , "006A" }
142
+ old := os .Stdout // keep backup of stdout
143
+ r , w , err := os .Pipe ()
144
+ if err != nil {
145
+ log .Fatal (err )
63
146
}
147
+ os .Stdout = w
148
+
149
+ outC := make (chan string )
150
+
151
+ // copy the output in a separate goroutine
152
+ go func () {
153
+ var buf bytes.Buffer
154
+ io .Copy (& buf , r )
155
+ outC <- buf .String ()
156
+ }()
157
+
158
+ main () // call main function with mock os.Args defined above
159
+
160
+ // back to normal state
161
+ w .Close ()
162
+ os .Stdout = old // restoring the real stdout
163
+ out := <- outC
164
+
165
+ if len (out ) == 0 {
166
+ t .Errorf ("[FAIL] Test of main() function did not return standard output response" )
167
+ }
168
+
169
+ if ! (out == "U+006A 'j'\n U+006A 'j'\n " ) {
170
+ t .Errorf ("[FAIL] Expected execution of 'uni f' to return string U+006A 'j' * 2, but instead it returned %s" , out )
171
+ }
172
+
64
173
}
0 commit comments