Skip to content

Commit 1671daf

Browse files
committed
new Unicode code point search tests
1 parent f565dfb commit 1671daf

File tree

1 file changed

+123
-14
lines changed

1 file changed

+123
-14
lines changed

uni_test.go

Lines changed: 123 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,29 @@ import (
1010
"testing"
1111
)
1212

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) {
1436
os.Args = []string{"uni", "j"}
1537
old := os.Stdout // keep backup of stdout
1638
r, w, err := os.Pipe()
@@ -37,28 +59,115 @@ func TestMainFunction(t *testing.T) {
3759

3860
if len(out) == 0 {
3961
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)
4266
}
4367

4468
}
4569

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)
5176
}
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'\nU+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+
52103
}
53104

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)
57136
}
137+
58138
}
59139

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)
63146
}
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'\nU+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+
64173
}

0 commit comments

Comments
 (0)