-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMethods_test.go
133 lines (123 loc) · 2.96 KB
/
Methods_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package CataasAPI
import (
"errors"
"math/rand"
"os"
"path"
"strconv"
"strings"
"testing"
"time"
)
func print_test(t *testing.T, want interface{}, got interface{}) {
t.Errorf(`want: %v ; got: %v`, want, got)
}
func TestEncode(t *testing.T) {
// want := "https://"
// got := c.Encode().String()
const (
gif = false
tag = "cute,fluffy"
text = "hello"
width = 200
height = 200
textSize = 20
textColor = "Blue"
size = SIZE_SMALL
filter = FILTER_BLUR
)
c := &Cataas{
Gif: gif,
Tag: tag,
Text: text,
Width: width,
Height: height,
TextSize: textSize,
TextColor: textColor,
Size: SIZE_SMALL,
Filter: FILTER_BLUR,
}
uri := c.Encode()
if _text := path.Base(uri.Path); _text != text {
print_test(t, text, _text)
}
if _tag := strings.Split(path.Dir(uri.Path), "/")[1]; _tag != tag {
print_test(t, tag, _tag)
}
if _width := uri.Query().Get("width"); _width != strconv.Itoa(width) {
print_test(t, width, _width)
}
if _height := uri.Query().Get("height"); _height != strconv.Itoa(height) {
print_test(t, height, _height)
}
if _text_size := uri.Query().Get("size"); _text_size != strconv.Itoa(textSize) {
print_test(t, textSize, _text_size)
}
if _text_color := uri.Query().Get("color"); _text_color != textColor {
print_test(t, textColor, _text_color)
}
if _size := uri.Query().Get("type"); _size != string(size) {
print_test(t, string(size), _size)
}
if _filter := uri.Query().Get("filter"); _filter != string(filter) {
print_test(t, string(filter), _filter)
}
if _gif := uri.Query().Get("gif"); _gif != "" {
t.Error("Gif option not be set when tag is")
}
}
func TestGet(t *testing.T) {
c := &Cataas{Text: "Hello", TextSize: 20}
img, err := c.Get()
if err != nil {
t.Error(err)
}
if len(img) < 1 {
t.Error("Empty image")
}
}
func TestDownload(t *testing.T) {
c := &Cataas{}
const file_path = "__img__test__.png"
err := c.Download(file_path)
if err != nil {
t.Error(err)
}
if _, err := os.Stat(file_path); err == nil {
os.Remove(file_path)
} else if errors.Is(err, os.ErrNotExist) {
t.Error("File was not created. download failed.")
} else {
t.Error(err)
}
}
func TestEncodeById(t *testing.T) {
c := &Cataas{}
id := rand.New(rand.NewSource(time.Now().Unix())).Int()
uri := c.EncodeById(strconv.Itoa(id))
if _id, err := strconv.Atoi(path.Base(uri.Path)); err != nil {
t.Error(err)
} else if _id != id {
print(t, id, _id)
}
}
func TestGetAllTags(t *testing.T) {
c := &Cataas{}
if tags, err := c.GetAllTags(); err != nil {
t.Error(err)
} else if len(tags) < 1 {
t.Error("Empty tags list")
}
}
// Todo
// func TestGetCats(t *testing.T) { }
// Benchmarks //
func BenchmarkTestGet_5(b *testing.B) {
c := &Cataas{}
for i := 0; i < 5; i++ {
_, err := c.Get()
if err != nil {
_ = err.Error()
}
}
}