add: tests and code cov

This commit is contained in:
Weilin Shi 2017-09-19 19:17:25 +08:00
parent 25375da284
commit 955f985f22
4 changed files with 84 additions and 1 deletions

View File

@ -1,4 +1,7 @@
language: go
go:
- 1.9
- 1.9
after_success:
- bash <(curl -s https://codecov.io/bash)

View File

@ -4,6 +4,7 @@
[![GoDoc](https://godoc.org/github.com/steambap/captcha?status.svg)](https://godoc.org/github.com/steambap/captcha)
[![Build Status](https://travis-ci.org/steambap/captcha.svg)](https://travis-ci.org/steambap/captcha)
[![codecov](https://codecov.io/gh/steambap/captcha/branch/master/graph/badge.svg)](https://codecov.io/gh/steambap/captcha)
[![Go Report Card](https://goreportcard.com/badge/github.com/steambap/captcha)](https://goreportcard.com/report/github.com/steambap/captcha)
</div>

15
captcha_test.go Normal file
View File

@ -0,0 +1,15 @@
package captcha
import (
"bytes"
"testing"
)
func TestNewCaptcha(t *testing.T) {
data, err := New(150, 50)
if err != nil {
t.Fatal(err)
}
buf := new(bytes.Buffer)
data.WriteTo(buf)
}

View File

@ -1,6 +1,7 @@
package captcha
import (
"fmt"
"image/color"
"testing"
)
@ -8,3 +9,66 @@ import (
func TestHSVAInterface(t *testing.T) {
var _ color.Color = hsva{}
}
func TestConversionGray(t *testing.T) {
var white1 color.Color = hsva{h: 0.0, s: 0.0, v: 1.0, a: uint8(255)}
var white2 color.Color = color.RGBA{R: uint8(255), G: uint8(255), B: uint8(255), A: uint8(255)}
if err := eq(white1, white2); err != nil {
t.Fatal(err)
}
var black1 color.Color = hsva{h: 0.0, s: 0.0, v: 0.0, a: uint8(255)}
var black2 color.Color = color.RGBA{R: uint8(0), G: uint8(0), B: uint8(0), A: uint8(255)}
if err := eq(black1, black2); err != nil {
t.Fatal(err)
}
}
func TestConversionRGB(t *testing.T) {
var yellow1 color.Color = hsva{h: 60.0 / 360.0, s: 0.6, v: 1.0, a: uint8(255)}
var yellow2 color.Color = color.RGBA{R: uint8(255), G: uint8(255), B: uint8(102), A: uint8(255)}
if err := eq(yellow1, yellow2); err != nil {
t.Fatal(err)
}
var green1 color.Color = hsva{h: 120.0 / 360.0, s: 1.0, v: 0.8, a: uint8(255)}
var green2 color.Color = color.RGBA{R: uint8(0), G: uint8(204), B: uint8(0), A: uint8(255)}
if err := eq(green1, green2); err != nil {
t.Fatal(err)
}
var teal1 color.Color = hsva{h: 180.0 / 360.0, s: 0.5, v: 0.8, a: uint8(255)}
var teal2 color.Color = color.RGBA{R: uint8(102), G: uint8(204), B: uint8(204), A: uint8(255)}
if err := eq(teal1, teal2); err != nil {
t.Fatal(err)
}
var blue1 color.Color = hsva{h: 240.0 / 360.0, s: 0.75, v: 0.8, a: uint8(255)}
var blue2 color.Color = color.RGBA{R: uint8(51), G: uint8(51), B: uint8(204), A: uint8(255)}
if err := eq(blue1, blue2); err != nil {
t.Fatal(err)
}
var pink1 color.Color = hsva{h: 300.0 / 360.0, s: 0.2, v: 1.0, a: uint8(255)}
var pink2 color.Color = color.RGBA{R: uint8(255), G: uint8(204), B: uint8(255), A: uint8(255)}
if err := eq(pink1, pink2); err != nil {
t.Fatal(err)
}
}
func eq(c0, c1 color.Color) error {
r0, g0, b0, a0 := c0.RGBA()
r1, g1, b1, a1 := c1.RGBA()
if r0 != r1 || g0 != g1 || b0 != b1 || a0 != a1 {
return fmt.Errorf("got 0x%04x 0x%04x 0x%04x 0x%04x\nwant 0x%04x 0x%04x 0x%04x 0x%04x",
r0, g0, b0, a0, r1, g1, b1, a1)
}
return nil
}