Skip to content

Commit

Permalink
increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyoichiro Yamada committed Oct 23, 2017
1 parent cdc7b4d commit 4d982e3
Show file tree
Hide file tree
Showing 19 changed files with 768 additions and 243 deletions.
2 changes: 1 addition & 1 deletion _bin/cover.sh
Expand Up @@ -12,5 +12,5 @@ done
rm -f profile.cover
if [ -n "${COVERALLS_TOKEN}" ]; then
goveralls -coverprofile=acc.cover -repotoken=$COVERALLS_TOKEN -service=wercker.com
rm -f acc.cover
fi
rm -f acc.cover
39 changes: 20 additions & 19 deletions config/color.go
Expand Up @@ -26,6 +26,8 @@ type Color struct {
// MarshalYAML implements Marshaler
func (c Color) MarshalYAML() (interface{}, error) {
switch c.Type {
case ColorTypeNone:
return "", nil
case ColorTypeName:
return c.Name.String(), nil
case ColorType8Bit:
Expand All @@ -39,6 +41,8 @@ func (c Color) MarshalYAML() (interface{}, error) {
// MarshalJSON implements Marshaler
func (c Color) MarshalJSON() ([]byte, error) {
switch c.Type {
case ColorTypeNone:
return []byte(`""`), nil
case ColorTypeName:
return []byte(fmt.Sprintf(`"%s"`, c.Name.String())), nil
case ColorType8Bit:
Expand Down Expand Up @@ -145,6 +149,10 @@ func (c *Color) unmarshal(str string, unquote bool) error {
str = unquoted
}
}
if str == "" {
c.Type = ColorTypeNone
return nil
}
if err := c.unmarshalAs8Bit(str); err == nil {
return nil
}
Expand Down Expand Up @@ -185,23 +193,6 @@ func atoiCore(s string) (uint64, error) {
return strconv.ParseUint(s, 10, 8)
}

func concatColor(a, b *Color) *Color {
if a == nil {
return b
}
return a
}

func actualColor(c *Color) *Color {
if c == nil {
return &Color{
Type: ColorTypeName,
Name: DefaultColor,
}
}
return c
}

var backColors = map[ColorName]aec.ANSI{
Black: aec.BlackB,
Red: aec.RedB,
Expand All @@ -221,9 +212,17 @@ var backColors = map[ColorName]aec.ANSI{
LightWhite: aec.LightWhiteB,
}

var emptyColor aec.ANSI

func init() {
emptyColor = aec.EmptyBuilder.ANSI
}

// B gets background ANSI color
func (c *Color) B() aec.ANSI {
switch c.Type {
case ColorTypeNone:
return emptyColor
case ColorTypeName:
b, ok := backColors[c.Name]
if ok {
Expand All @@ -234,7 +233,7 @@ func (c *Color) B() aec.ANSI {
case ColorType24Bit:
return aec.FullColorB(c.ValueR, c.ValueG, c.ValueB)
}
return aec.DefaultB
return emptyColor
}

var frontColors = map[ColorName]aec.ANSI{
Expand All @@ -259,6 +258,8 @@ var frontColors = map[ColorName]aec.ANSI{
// F gets foreground ANSI color
func (c *Color) F() aec.ANSI {
switch c.Type {
case ColorTypeNone:
return emptyColor
case ColorTypeName:
f, ok := frontColors[c.Name]
if ok {
Expand All @@ -269,5 +270,5 @@ func (c *Color) F() aec.ANSI {
case ColorType24Bit:
return aec.FullColorF(c.ValueR, c.ValueG, c.ValueB)
}
return aec.DefaultF
return emptyColor
}
74 changes: 68 additions & 6 deletions config/color_test.go
Expand Up @@ -3,17 +3,21 @@ package config
import (
"encoding/json"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
yaml "gopkg.in/yaml.v2"
)

type testStruct struct {
C Color `json:"c" yaml:"c"`
C Color `json:"c" yaml:"c,omitempty"`
}

func TestMarshalYAML(t *testing.T) {
for exp, color := range map[string]testStruct{
`{}`: {},
`c: ""`: {Color{Type: ColorTypeNone}},
`c: black`: {Color{Type: ColorTypeName, Name: Black}},
`c: '#ffeedd'`: {Color{Type: ColorType24Bit, ValueR: 0xff, ValueG: 0xee, ValueB: 0xdd}},
`c: 31`: {Color{Type: ColorType8Bit, Value8: 31}},
Expand All @@ -26,7 +30,7 @@ func TestMarshalYAML(t *testing.T) {
exp += "\n" // NOTE: yaml will have trailing newline
act := string(buf)
if exp != act {
t.Errorf("expected a marshaled color %q, but %q", exp, act)
t.Errorf("expect that a marshaled color be %q, but %q", exp, act)
}
}

Expand All @@ -38,18 +42,19 @@ func TestMarshalYAML(t *testing.T) {

func TestMarshalJSON(t *testing.T) {
for exp, color := range map[string]testStruct{
`{"c":""}`: {C: Color{Type: ColorTypeNone}},
`{"c":"black"}`: {C: Color{Type: ColorTypeName, Name: Black}},
`{"c":"#ffeedd"}`: {C: Color{Type: ColorType24Bit, ValueR: 0xff, ValueG: 0xee, ValueB: 0xdd}},
`{"c":31}`: {C: Color{Type: ColorType8Bit, Value8: 31}},
} {
buf, err := json.Marshal(&color)
if err != nil {
t.Errorf("failed to marshal a color %q to json with error %q", "black", err)
t.Errorf("failed to marshal a color to json with error %q", err)
t.FailNow()
}
act := string(buf)
if exp != act {
t.Errorf("expected a marshaled color %q, but %q", exp, act)
t.Errorf("expect that a marshaled color be %q, but %q", exp, act)
}
}

Expand All @@ -62,6 +67,7 @@ func TestMarshalJSON(t *testing.T) {
func TestUnmarshalYAML(t *testing.T) {
const yamlTemplate = "act: %s"
for value, exp := range map[string]Color{
`""`: Color{Type: ColorTypeNone},
`0x1F`: Color{Type: ColorType8Bit, Value8: 0x1F},
`"0x1F"`: Color{Type: ColorType8Bit, Value8: 0x1F},
`25`: Color{Type: ColorType8Bit, Value8: 25},
Expand All @@ -80,10 +86,21 @@ func TestUnmarshalYAML(t *testing.T) {
continue
}
if obj.Act != exp {
t.Errorf("expected a marshaled color %#v, but %#v", exp, obj.Act)
t.Errorf("expect that a marshaled color be %#v, but %#v", exp, obj.Act)
}
}

var obj struct {
Act *Color `yaml:"act"`
}
err := yaml.Unmarshal([]byte{}, &obj)
if err != nil {
t.Errorf("failed to unmarshal a empty color as yaml with error %q", err)
}
if obj.Act != nil {
t.Errorf("expect that a marshaled color be nil, but %#v", obj.Act)
}

for _, value := range []string{
`{}`,
`[]`,
Expand All @@ -105,6 +122,7 @@ func TestUnmarshalYAML(t *testing.T) {
func TestUnmarshalJSON(t *testing.T) {
const jsonTemplate = `{"act":%s}`
for value, exp := range map[string]Color{
`""`: Color{Type: ColorTypeNone},
`"0x1F"`: Color{Type: ColorType8Bit, Value8: 0x1F},
`25`: Color{Type: ColorType8Bit, Value8: 25},
`"red"`: Color{Type: ColorTypeName, Name: Red},
Expand All @@ -120,7 +138,7 @@ func TestUnmarshalJSON(t *testing.T) {
continue
}
if obj.Act != exp {
t.Errorf("expected a marshaled color %#v, but %#v", exp, obj.Act)
t.Errorf("expect that a marshaled color be %#v, but %#v", exp, obj.Act)
}
}
for _, value := range []string{
Expand All @@ -140,3 +158,47 @@ func TestUnmarshalJSON(t *testing.T) {
}
}
}

func TestB(t *testing.T) {
const esc = "\x1b["
for _, c := range []Color{
{Type: ColorTypeName, Name: Black},
{Type: ColorType8Bit, Value8: 11},
{Type: ColorType24Bit, ValueR: 33, ValueG: 22, ValueB: 11},
} {
// B() func must return Back Color (formed ESC+[+3x)
if !strings.HasPrefix(c.B().String(), esc+"4") {
t.Errorf("invalid front color: %s", strings.TrimPrefix(c.B().String(), esc))
}
}

for _, c := range []Color{
{},
{Type: ColorTypeName, Name: "invalidColor"},
} {
c := c
assert.Equal(t, "", c.B().String())
}
}

func TestF(t *testing.T) {
const esc = "\x1b["
for _, c := range []Color{
{Type: ColorTypeName, Name: Black},
{Type: ColorType8Bit, Value8: 11},
{Type: ColorType24Bit, ValueR: 33, ValueG: 22, ValueB: 11},
} {
// F() func must return Front Color (formed ESC+[+3x)
if !strings.HasPrefix(c.F().String(), esc+"3") {
t.Errorf("invalid front color: %s", strings.TrimPrefix(c.F().String(), esc))
}
}

for _, c := range []Color{
{},
{Type: ColorTypeName, Name: "invalidColor"},
} {
c := c
assert.Equal(t, "", c.F().String())
}
}
2 changes: 2 additions & 0 deletions config/color_type.go
Expand Up @@ -4,6 +4,8 @@ package config
type ColorType string

const (
// ColorTypeNone defines empty
ColorTypeNone = ColorType("none")
// ColorType8Bit defines 8-bit (256) colors
ColorType8Bit = ColorType("8bit")
// ColorType24Bit defines 24-bit (R: 8bit + G: 8bit + B: 8bit ; full) colors
Expand Down

0 comments on commit 4d982e3

Please sign in to comment.