Skip to content

Commit

Permalink
[FEAT] Make ErrMap public instead of private
Browse files Browse the repository at this point in the history
This is required to capture this error at unit
testing.
  • Loading branch information
mdm-code committed Nov 1, 2022
1 parent f6fc89b commit 7d80e10
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
11 changes: 6 additions & 5 deletions mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ var colorMap map[string]SgrAttr = map[string]SgrAttr{
}

var (
errMap = errors.New("Color mapping error")
// ErrMap indicates that there were issues with disambiguating color names.
ErrMap = errors.New("Color mapping error")
)

// MapColors attempts to interpret string elements of the ss slice as a set of
Expand All @@ -80,7 +81,7 @@ func MapColors(ss []string) ([]SgrAttr, error) {
for _, s := range ss {
attr, err := MapColor(s)
if err != nil {
return []SgrAttr{}, errMap
return []SgrAttr{}, ErrMap
}
result = append(result, attr)
}
Expand All @@ -105,7 +106,7 @@ func MapColor(s string) (SgrAttr, error) {
if matchRegexp(re8, s) {
col, ok := collateRgb8(re8, s)
if !ok {
return "", errMap
return "", ErrMap
}
return col, nil
}
Expand All @@ -115,11 +116,11 @@ func MapColor(s string) (SgrAttr, error) {
if matchRegexp(re24, s) {
col, ok := collateRgb24(re24, s)
if !ok {
return "", errMap
return "", ErrMap
}
return col, nil
}
return "", errMap
return "", ErrMap
}

// MatchRegexp checks if val matches the provided regex r.
Expand Down
34 changes: 17 additions & 17 deletions mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func TestMapColors(t *testing.T) {
{[]string{"rgb24=bg:255:255:255", "blink", "magentafg"}, nil},

// Failing colors
{[]string{"italics", "strike"}, errMap},
{[]string{"italic", "redgb"}, errMap},
{[]string{"italic", "rgb8=gf:240"}, errMap},
{[]string{"italics", "strike"}, ErrMap},
{[]string{"italic", "redgb"}, ErrMap},
{[]string{"italic", "rgb8=gf:240"}, ErrMap},
}
for _, c := range cases {
t.Run(strings.Join(c.colors, "-"), func(t *testing.T) {
Expand Down Expand Up @@ -57,10 +57,10 @@ func TestMapColor(t *testing.T) {
{"whitebfg", nil},

// Not-implemented colors and styles
{"purplefg", errMap},
{"greybg", errMap},
{"pinkbbg", errMap},
{"orangebfg", errMap},
{"purplefg", ErrMap},
{"greybg", ErrMap},
{"pinkbbg", ErrMap},
{"orangebfg", ErrMap},

// Passing RGB patterns
{"RGB8=fg:25", nil},
Expand All @@ -71,16 +71,16 @@ func TestMapColor(t *testing.T) {
{"rgb24=fg:0:12:255", nil},

// Failing RGB patterns
{"", errMap}, // empty string
{"rgb24", errMap}, // missing parameters
{"rgb8=gb:227", errMap}, // unknown layer (8)
{"rgb24=gf:227:12:142", errMap}, // unknown layer (24)
{"rgb9=bg:227", errMap}, // unknown bit size
{"rgb8=bg:255:255", errMap}, // too many color values (8)
{"rgb24=fg:255:255:255:255", errMap}, // too many color values (24)
{"rgb24=gf:12:245:0", errMap}, // unknown layer
{"rgb8=bg:256", errMap}, // 256 > uint8 255 cap (8)
{"rgb24=bg:255:256:123", errMap}, // 256 > uint8 255 cap (24)
{"", ErrMap}, // empty string
{"rgb24", ErrMap}, // missing parameters
{"rgb8=gb:227", ErrMap}, // unknown layer (8)
{"rgb24=gf:227:12:142", ErrMap}, // unknown layer (24)
{"rgb9=bg:227", ErrMap}, // unknown bit size
{"rgb8=bg:255:255", ErrMap}, // too many color values (8)
{"rgb24=fg:255:255:255:255", ErrMap}, // too many color values (24)
{"rgb24=gf:12:245:0", ErrMap}, // unknown layer
{"rgb8=bg:256", ErrMap}, // 256 > uint8 255 cap (8)
{"rgb24=bg:255:256:123", ErrMap}, // 256 > uint8 255 cap (24)
}
for _, c := range cases {
t.Run(c.color, func(t *testing.T) {
Expand Down

0 comments on commit 7d80e10

Please sign in to comment.