You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
badges/badge_test.go

156 lines
4.0 KiB
Go

package badge
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2021 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"io/ioutil"
"testing"
. "pkg.re/essentialkaos/check.v1"
)
// ////////////////////////////////////////////////////////////////////////////////// //
func Test(t *testing.T) { TestingT(t) }
type BadgeSuite struct {
generator *Generator
}
// ////////////////////////////////////////////////////////////////////////////////// //
var _ = Suite(&BadgeSuite{})
// ////////////////////////////////////////////////////////////////////////////////// //
func (s *BadgeSuite) SetUpTest(c *C) {
var err error
3 years ago
s.generator, err = NewGenerator("Verdana.ttf", 11)
if err != nil {
c.Fatal(err.Error())
}
}
func (s *BadgeSuite) TestErrors(c *C) {
3 years ago
_, err := NewGenerator("unknown.ttf", 0)
c.Assert(err, NotNil)
3 years ago
_, err = NewGenerator("badge.go", 0)
c.Assert(err, NotNil)
}
func (s *BadgeSuite) TestPlastic(c *C) {
srcBadge, err := ioutil.ReadFile("testdata/plastic.svg")
if err != nil {
c.Fatal(err.Error())
}
3 years ago
ourBadge := s.generator.GeneratePlastic("label", "message", COLOR_RED)
3 years ago
c.Assert(string(ourBadge), Equals, string(srcBadge))
}
func (s *BadgeSuite) TestFlat(c *C) {
srcBadge, err := ioutil.ReadFile("testdata/flat.svg")
if err != nil {
c.Fatal(err.Error())
}
3 years ago
ourBadge := s.generator.GenerateFlat("label", "message", COLOR_RED)
3 years ago
c.Assert(string(ourBadge), Equals, string(srcBadge))
}
func (s *BadgeSuite) TestFlatSquare(c *C) {
3 years ago
srcBadge, err := ioutil.ReadFile("testdata/square.svg")
if err != nil {
c.Fatal(err.Error())
}
3 years ago
ourBadge := s.generator.GenerateFlatSquare("label", "message", COLOR_RED)
3 years ago
c.Assert(string(ourBadge), Equals, string(srcBadge))
}
func (s *BadgeSuite) TestPlasticSimple(c *C) {
srcBadge, err := ioutil.ReadFile("testdata/plastic_simple.svg")
if err != nil {
c.Fatal(err.Error())
}
ourBadge := s.generator.GeneratePlastic("", "message", COLOR_RED)
c.Assert(string(ourBadge), Equals, string(srcBadge))
ourBadge = s.generator.GeneratePlasticSimple("message", COLOR_RED)
c.Assert(string(ourBadge), Equals, string(srcBadge))
}
func (s *BadgeSuite) TestFlatSimple(c *C) {
srcBadge, err := ioutil.ReadFile("testdata/flat_simple.svg")
if err != nil {
c.Fatal(err.Error())
}
ourBadge := s.generator.GenerateFlat("", "message", COLOR_RED)
c.Assert(string(ourBadge), Equals, string(srcBadge))
ourBadge = s.generator.GenerateFlatSimple("message", COLOR_RED)
c.Assert(string(ourBadge), Equals, string(srcBadge))
}
func (s *BadgeSuite) TestFlatSquareSimple(c *C) {
srcBadge, err := ioutil.ReadFile("testdata/square_simple.svg")
if err != nil {
c.Fatal(err.Error())
}
ourBadge := s.generator.GenerateFlatSquare("", "message", COLOR_RED)
c.Assert(string(ourBadge), Equals, string(srcBadge))
ourBadge = s.generator.GenerateFlatSquareSimple("message", COLOR_RED)
c.Assert(string(ourBadge), Equals, string(srcBadge))
}
func (s *BadgeSuite) TestBlackAndWhite(c *C) {
bb, err := ioutil.ReadFile("testdata/black.svg")
if err != nil {
c.Fatal(err.Error())
}
wb, err := ioutil.ReadFile("testdata/white.svg")
if err != nil {
c.Fatal(err.Error())
}
ourBadge := s.generator.GenerateFlatSimple("message", "#FFFFFF")
c.Assert(string(ourBadge), Equals, string(wb))
ourBadge = s.generator.GenerateFlatSimple("message", "#000000")
c.Assert(string(ourBadge), Equals, string(bb))
}
func (s *BadgeSuite) TestAux(c *C) {
c.Assert(formatColor("000000"), Equals, "000000")
c.Assert(formatColor("#000000"), Equals, "000000")
c.Assert(formatColor("#000"), Equals, "000000")
c.Assert(calcLumColor(0.7), Equals, 0.4479884124418833)
c.Assert(calcLumColor(0.01), Equals, 0.0007739938080495357)
}