Skip to content

Commit 8b9f496

Browse files
authored
Merge pull request #45 from Code-Hex/add/tests
added tests
2 parents 78556bc + ab9aae7 commit 8b9f496

File tree

6 files changed

+131
-17
lines changed

6 files changed

+131
-17
lines changed

cow.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ type Cow struct {
1313
typ *CowFile
1414
thoughts rune
1515
thinking bool
16-
bold bool
1716
ballonWidth int
1817
disableWordWrap bool
1918

@@ -185,14 +184,6 @@ func pickCow() (*CowFile, error) {
185184
}, nil
186185
}
187186

188-
// Bold enables bold mode
189-
func Bold() Option {
190-
return func(c *Cow) error {
191-
c.bold = true
192-
return nil
193-
}
194-
}
195-
196187
// BallonWidth specifies ballon size
197188
func BallonWidth(size uint) Option {
198189
return func(c *Cow) error {

cow_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cowsay
22

33
import (
4+
"errors"
5+
"fmt"
46
"testing"
57

68
"github.com/google/go-cmp/cmp"
@@ -84,8 +86,11 @@ func TestCow_Clone(t *testing.T) {
8486

8587
t.Run("random", func(t *testing.T) {
8688
cow, _ := New(
89+
Type(""),
8790
Thinking(),
8891
Thoughts('o'),
92+
Eyes("xx"),
93+
Tongue("u"),
8994
Random(),
9095
)
9196

@@ -97,6 +102,21 @@ func TestCow_Clone(t *testing.T) {
97102
t.Errorf("(-want, +got)\n%s", diff)
98103
}
99104
})
105+
106+
t.Run("error", func(t *testing.T) {
107+
cow, err := New()
108+
if err != nil {
109+
t.Fatal(err)
110+
}
111+
112+
wantErr := errors.New("error")
113+
_, err = cow.Clone(func(*Cow) error {
114+
return wantErr
115+
})
116+
if wantErr != err {
117+
t.Fatalf("want %v, but got %v", wantErr, err)
118+
}
119+
})
100120
}
101121

102122
func Test_adjustTo2Chars(t *testing.T) {
@@ -134,3 +154,14 @@ func Test_adjustTo2Chars(t *testing.T) {
134154
})
135155
}
136156
}
157+
158+
func TestNotFound_Error(t *testing.T) {
159+
file := "test"
160+
n := &NotFound{
161+
Cowfile: file,
162+
}
163+
want := fmt.Sprintf("not found %q cowfile", file)
164+
if want != n.Error() {
165+
t.Fatalf("want %q but got %q", want, n.Error())
166+
}
167+
}

cowsay.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,6 @@ func (cow *Cow) GetCow() (string, error) {
132132
return "", err
133133
}
134134

135-
if len(cow.eyes) > 2 {
136-
cow.eyes = cow.eyes[0:2]
137-
}
138-
139-
if len(cow.tongue) > 2 {
140-
cow.tongue = cow.tongue[0:2]
141-
}
142-
143135
r := strings.NewReplacer(
144136
"\\\\", "\\",
145137
"\\@", "@",

cowsay_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package cowsay
22

33
import (
44
"bytes"
5+
"errors"
6+
"io/ioutil"
57
"os"
68
"path/filepath"
79
"testing"
@@ -152,3 +154,78 @@ func TestCowFile_ReadAll(t *testing.T) {
152154
}
153155

154156
}
157+
158+
const defaultSay = ` ________
159+
< cowsay >
160+
--------
161+
\ ^__^
162+
\ (oo)\_______
163+
(__)\ )\/\
164+
||----w |
165+
|| ||`
166+
167+
func TestSay(t *testing.T) {
168+
type args struct {
169+
phrase string
170+
options []Option
171+
}
172+
tests := []struct {
173+
name string
174+
args args
175+
wantFile string
176+
wantErr bool
177+
}{
178+
{
179+
name: "default",
180+
args: args{
181+
phrase: "hello!",
182+
},
183+
wantFile: "default.cow",
184+
wantErr: false,
185+
},
186+
{
187+
name: "nest",
188+
args: args{
189+
phrase: defaultSay,
190+
options: []Option{
191+
DisableWordWrap(),
192+
},
193+
},
194+
wantFile: "nest.cow",
195+
wantErr: false,
196+
},
197+
{
198+
name: "error",
199+
args: args{
200+
phrase: "error",
201+
options: []Option{
202+
func(*Cow) error {
203+
return errors.New("error")
204+
},
205+
},
206+
},
207+
wantErr: true,
208+
},
209+
}
210+
for _, tt := range tests {
211+
t.Run(tt.name, func(t *testing.T) {
212+
got, err := Say(tt.args.phrase, tt.args.options...)
213+
if (err != nil) != tt.wantErr {
214+
t.Errorf("Say() error = %v, wantErr %v", err, tt.wantErr)
215+
return
216+
}
217+
if tt.wantErr {
218+
return
219+
}
220+
filename := filepath.Join("testdata", tt.wantFile)
221+
content, err := ioutil.ReadFile(filename)
222+
if err != nil {
223+
t.Fatal(err)
224+
}
225+
want := string(content)
226+
if want != got {
227+
t.Fatalf("want\n%s\n\ngot\n%s", want, got)
228+
}
229+
})
230+
}
231+
}

testdata/default.cow

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
________
2+
< hello! >
3+
--------
4+
\ ^__^
5+
\ (oo)\_______
6+
(__)\ )\/\
7+
||----w |
8+
|| ||

testdata/nest.cow

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
______________________________
2+
/ ________ \
3+
| < cowsay > |
4+
| -------- |
5+
| \ ^__^ |
6+
| \ (oo)\_______ |
7+
| (__)\ )\/\ |
8+
| ||----w | |
9+
\ || || /
10+
------------------------------
11+
\ ^__^
12+
\ (oo)\_______
13+
(__)\ )\/\
14+
||----w |
15+
|| ||

0 commit comments

Comments
 (0)