This repository has been archived on 2024-08-25. You can view files and clone it, but cannot push or open issues or pull requests.
captcha/captcha_test.go

121 lines
2.4 KiB
Go
Raw Normal View History

2017-09-19 12:17:25 +01:00
package captcha
import (
"bytes"
2017-09-22 03:01:40 +01:00
"golang.org/x/image/font/gofont/goregular"
2017-09-20 02:51:27 +01:00
"image/color"
2017-09-27 06:07:08 +01:00
"math/rand"
2017-09-28 01:18:29 +01:00
"testing"
2017-09-19 12:17:25 +01:00
)
func TestNewCaptcha(t *testing.T) {
data, err := New(150, 50)
if err != nil {
t.Fatal(err)
}
buf := new(bytes.Buffer)
data.WriteTo(buf)
}
2017-09-20 02:51:27 +01:00
func TestSmallCaptcha(t *testing.T) {
_, err := New(36, 12)
if err != nil {
t.Fatal(err)
}
}
2017-09-20 02:51:27 +01:00
func TestNewCaptchaOptions(t *testing.T) {
New(100, 34, func(options *Options) {
options.BackgroundColor = color.Opaque
options.CharPreset = "1234567890"
options.CurveNumber = 0
options.TextLength = 6
})
2017-09-28 01:18:29 +01:00
NewMathExpr(100, 34, func(options *Options) {
options.BackgroundColor = color.Black
})
}
func TestNewMathExpr(t *testing.T) {
_, err := NewMathExpr(150, 50)
if err != nil {
t.Fatal(err)
}
2017-09-20 02:51:27 +01:00
}
func TestCovNilFontError(t *testing.T) {
temp := ttfFont
ttfFont = nil
_, err := New(150, 50)
if err == nil {
t.Fatal("Expect to get nil font error")
}
2017-09-28 01:18:29 +01:00
_, err = NewMathExpr(150, 50)
if err == nil {
t.Fatal("Expect to get nil font error")
}
2017-09-20 02:51:27 +01:00
ttfFont = temp
}
2017-09-22 03:01:40 +01:00
func TestLoadFont(t *testing.T) {
err := LoadFont(goregular.TTF)
if err != nil {
t.Fatal("Fail to load go font")
}
err = LoadFont([]byte("invalid"))
if err == nil {
t.Fatal("LoadFont incorrectly parse an invalid font")
2017-09-22 03:01:40 +01:00
}
}
2017-09-27 06:07:08 +01:00
func TestMaxColor(t *testing.T) {
var result uint32
result = maxColor()
if result != 0 {
t.Fatalf("Expect max color to be 0, got %v", result)
}
result = maxColor(1)
if result != 1 {
t.Fatalf("Expect max color to be 1, got %v", result)
}
result = maxColor(52428, 65535)
if result != 255 {
t.Fatalf("Expect max color to be 255, got %v", result)
}
var rng = rand.New(rand.NewSource(0))
for i := 0; i < 10; i++ {
result = maxColor(rng.Uint32(), rng.Uint32(), rng.Uint32())
if result > 255 {
t.Fatalf("Number out of range: %v", result)
}
}
}
func TestMinColor(t *testing.T) {
var result uint32
result = minColor()
if result != 255 {
t.Fatalf("Expect min color to be 255, got %v", result)
}
result = minColor(1)
if result != 1 {
t.Fatalf("Expect min color to be 1, got %v", result)
}
result = minColor(52428, 65535)
if result != 204 {
t.Fatalf("Expect min color to be 1, got %v", result)
}
var rng = rand.New(rand.NewSource(0))
for i := 0; i < 10; i++ {
result = minColor(rng.Uint32(), rng.Uint32(), rng.Uint32())
if result > 255 {
t.Fatalf("Number out of range: %v", result)
}
}
}