From 955f985f228dd73850d600cbcd32d5d204692dbb Mon Sep 17 00:00:00 2001 From: Weilin Shi <934587911@qq.com> Date: Tue, 19 Sep 2017 19:17:25 +0800 Subject: [PATCH] add: tests and code cov --- .travis.yml | 5 +++- README.md | 1 + captcha_test.go | 15 ++++++++++++ hsva_test.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 captcha_test.go diff --git a/.travis.yml b/.travis.yml index a9e5d5f..b6f1536 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,7 @@ language: go go: - - 1.9 \ No newline at end of file + - 1.9 + +after_success: + - bash <(curl -s https://codecov.io/bash) \ No newline at end of file diff --git a/README.md b/README.md index 92dd6cd..f10a195 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/captcha_test.go b/captcha_test.go new file mode 100644 index 0000000..bb53049 --- /dev/null +++ b/captcha_test.go @@ -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) +} diff --git a/hsva_test.go b/hsva_test.go index da6b69e..dcfc1e5 100644 --- a/hsva_test.go +++ b/hsva_test.go @@ -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 +}