Skip to content

Commit

Permalink
test: improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Oct 23, 2022
1 parent aadddb3 commit d594d3d
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 23 deletions.
6 changes: 3 additions & 3 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ func TestNormalize(t *testing.T) {
m.EXPECT().IsEnabledFor(pp.Info).Return(true),
m.EXPECT().Infof(pp.EmojiEnvVars, "Checking settings . . ."),
m.EXPECT().IncIndent().Return(m),
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: wanted boolean expression; got %q", `range`, `range`),
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: wanted a boolean expression; got %q", `range`, `range`),
)
},
},
Expand All @@ -741,7 +741,7 @@ func TestNormalize(t *testing.T) {
m.EXPECT().IsEnabledFor(pp.Info).Return(true),
m.EXPECT().Infof(pp.EmojiEnvVars, "Checking settings . . ."),
m.EXPECT().IncIndent().Return(m),
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: wanted boolean expression; got %q", "999", "999"),
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: wanted a boolean expression; got %q", "999", "999"),
)
},
},
Expand All @@ -762,7 +762,7 @@ func TestNormalize(t *testing.T) {
m.EXPECT().IsEnabledFor(pp.Info).Return(true),
m.EXPECT().Infof(pp.EmojiEnvVars, "Checking settings . . ."),
m.EXPECT().IncIndent().Return(m),
m.EXPECT().Errorf(pp.EmojiUserError, `Failed to parse %q: wanted ")"; got end-of-string`, `is(12345`),
m.EXPECT().Errorf(pp.EmojiUserError, `Failed to parse %q: wanted %q; reached end of string`, `is(12345`, ")"),
)
},
},
Expand Down
11 changes: 10 additions & 1 deletion internal/config/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,16 @@ func TestReadDomains(t *testing.T) {
ds{},
false,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: invalid token %q in a list", "hi.org,(", "(")
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: unexpected token %q", "hi.org,(", "(")
},
},
"illformed4": {
true, ")",
ds{},
ds{},
false,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: unexpected token %q", ")", ")")
},
},
} {
Expand Down
4 changes: 2 additions & 2 deletions internal/domainexp/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

var (
ErrSingleAnd = fmt.Errorf("got &, but only && is allowed")
ErrSingleOr = fmt.Errorf("got |, but only || is allowed")
ErrSingleAnd = fmt.Errorf(`use "&&" instead of "&"`)
ErrSingleOr = fmt.Errorf(`use "||" instead of "|"`)
)

//nolint:funlen
Expand Down
17 changes: 9 additions & 8 deletions internal/domainexp/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func scanList(ppfmt pp.PP, input string, tokens []string) ([]string, []string) {
case ")":
return list, tokens
case "(", "&&", "||", "!":
ppfmt.Errorf(pp.EmojiUserError, `Failed to parse %q: invalid token %q in a list`, input, tokens[0])
ppfmt.Errorf(pp.EmojiUserError, `Failed to parse %q: unexpected token %q`, input, tokens[0])
return nil, nil
default:
if !readyForNext {
Expand Down Expand Up @@ -71,13 +71,13 @@ func scanConstants(_ppfmt pp.PP, _input string, tokens []string, wanted []string

func scanMustConstant(ppfmt pp.PP, input string, tokens []string, wanted string) []string {
if len(tokens) == 0 {
ppfmt.Errorf(pp.EmojiUserError, `Failed to parse %q: wanted ")"; got end-of-string`, input)
ppfmt.Errorf(pp.EmojiUserError, `Failed to parse %q: wanted %q; reached end of string`, input, wanted)
return nil
}
if wanted == tokens[0] {
return tokens[1:]
}
ppfmt.Errorf(pp.EmojiUserError, `Failed to parse %q: wanted ")"; got %q`, input, tokens[0])
ppfmt.Errorf(pp.EmojiUserError, `Failed to parse %q: wanted %q; got %q`, input, wanted, tokens[0])
return nil
}

Expand Down Expand Up @@ -163,17 +163,16 @@ func scanFactor(ppfmt pp.PP, input string, tokens []string) (predicate, []string
}
newTokens = scanMustConstant(ppfmt, input, newTokens, ")")
if newTokens == nil {
ppfmt.Errorf(pp.EmojiUserError, `Failed to parse %q: wanted ')'`, input)
return nil, nil
}
return pred, newTokens
}
}

if len(tokens) == 0 {
ppfmt.Errorf(pp.EmojiUserError, "Failed to parse %q: wanted boolean expression; got end-of-string", input)
ppfmt.Errorf(pp.EmojiUserError, "Failed to parse %q: wanted a boolean expression; reached end of string", input)
} else {
ppfmt.Errorf(pp.EmojiUserError, "Failed to parse %q: wanted boolean expression; got %q", input, tokens[0])
ppfmt.Errorf(pp.EmojiUserError, "Failed to parse %q: wanted a boolean expression; got %q", input, tokens[0])
}
return nil, nil
}
Expand Down Expand Up @@ -234,7 +233,8 @@ func ParseList(ppfmt pp.PP, input string) ([]domain.Domain, bool) {
if tokens == nil {
return nil, false
} else if len(tokens) > 0 {
ppfmt.Errorf(pp.EmojiUserError, "Parsing %q: unexpected %q", input, tokens[0])
ppfmt.Errorf(pp.EmojiUserError, "Failed to parse %q: unexpected token %q", input, tokens[0])
return nil, false
}

return list, true
Expand All @@ -250,7 +250,8 @@ func ParseExpression(ppfmt pp.PP, input string) (predicate, bool) {
if tokens == nil {
return nil, false
} else if len(tokens) > 0 {
ppfmt.Errorf(pp.EmojiUserError, "Parsing %q: unexpected %q", input, tokens[0])
ppfmt.Errorf(pp.EmojiUserError, "Failed to parse %q: unexpected token %q", input, tokens[0])
return nil, false
}

return pred, true
Expand Down
117 changes: 108 additions & 9 deletions internal/domainexp/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func TestParseList(t *testing.T) {
)
},
},
"illformed/1": {
"&", false, nil,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: %v", "&", domainexp.ErrSingleAnd)
},
},
} {
tc := tc
t.Run(name, func(t *testing.T) {
Expand All @@ -55,6 +61,7 @@ func TestParseList(t *testing.T) {
}
}

//nolint:funlen
func TestParseExpression(t *testing.T) {
t.Parallel()
type f = domain.FQDN
Expand All @@ -66,15 +73,107 @@ func TestParseExpression(t *testing.T) {
expected bool
prepareMockPP func(m *mocks.MockPP)
}{
"true": {"true", true, f(""), true, nil},
"f": {"f", true, w(""), false, nil},
"and/t-0": {"t && 0", true, f(""), false, nil},
"or/F-1": {"F || 1", true, w(""), true, nil},
"is/matched/1": {"is(example.com)", true, f("example.com"), true, nil},
"is/matched/idn/1": {"is(☕.de)", true, f("xn--53h.de"), true, nil},
"is/matched/idn/2": {"is(Xn--53H.de)", true, f("xn--53h.de"), true, nil},
"is/matched/idn/3": {"is(*.Xn--53H.de)", true, w("xn--53h.de"), true, nil},
"is/unmatched/1": {"is(example.org)", true, f("example.com"), false, nil},
"empty": {
"", false, nil, true,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError, `Failed to parse %q: wanted a boolean expression; reached end of string`, "")
},
},
"const/1": {"true", true, nil, true, nil},
"const/2": {"f", true, nil, false, nil},
"&&/1": {"t && 0", true, nil, false, nil},
"&&/2": {
"t &&", false, nil, false,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError, `Failed to parse %q: wanted a boolean expression; reached end of string`, "t &&") //nolint:lll
},
},
"&&/&/1": {
"true & true", false, nil, false,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: %v", "true & true", domainexp.ErrSingleAnd)
},
},
"&&/&/2": {
"true &", false, nil, false,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: %v", "true &", domainexp.ErrSingleAnd)
},
},
"||/1": {"F || 1", true, nil, true, nil},
"||/2": {
"F ||", false, nil, false,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError, `Failed to parse %q: wanted a boolean expression; reached end of string`, "F ||") //nolint:lll
},
},
"||/|/1": {
"false | false", false, nil, false,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: %v", "false | false", domainexp.ErrSingleOr)
},
},
"||/|/2": {
"false |", false, nil, false,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: %v", "false |", domainexp.ErrSingleOr)
},
},
"is/1": {"is(example.com)", true, f("example.com"), true, nil},
"is/2": {"is(example.com)", true, f("sub.example.com"), false, nil},
"is/3": {"is(example.org)", true, f("example.com"), false, nil},
"is/wildcard/1": {"is(example.com)", true, w("example.com"), false, nil},
"is/wildcard/2": {"is(*.example.com)", true, w("example.com"), true, nil},
"is/wildcard/3": {"is(*.example.com)", true, f("example.com"), false, nil},
"is/idn/1": {"is(☕.de)", true, f("xn--53h.de"), true, nil},
"is/idn/2": {"is(Xn--53H.de)", true, f("xn--53h.de"), true, nil},
"is/idn/3": {"is(*.Xn--53H.de)", true, w("xn--53h.de"), true, nil},
"is/error/1": {
"is)", false, nil, false,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError, `Failed to parse %q: wanted %q; got %q`, "is)", "(", ")")
},
},
"is/error/2": {
"is(&&", false, nil, false,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError, `Failed to parse %q: unexpected token %q`, "is(&&", "&&")
},
},
"sub/1": {"sub(example.com)", true, f("example.com"), true, nil},
"sub/2": {"sub(example.com)", true, w("example.com"), true, nil},
"sub/3": {"sub(example.com)", true, f("sub.example.com"), true, nil},
"sub/4": {"sub(example.com)", true, f("subexample.com"), false, nil},
"sub/idn/1": {"sub(☕.de)", true, f("xn--53h.de"), true, nil},
"sub/idn/2": {"sub(Xn--53H.de)", true, f("xn--53h.de"), true, nil},
"sub/idn/3": {"sub(*.Xn--53H.de)", true, w("xn--53h.de"), true, nil},
"not/1": {"!0", true, nil, true, nil},
"not/2": {"!!!!!!!!!!!0", true, nil, true, nil},
"not/3": {
"!(", false, nil, true,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: wanted a boolean expression; reached end of string", "!(")
},
},
"nested/1": {"((true)||(false))&&((false)||(true))", true, nil, true, nil},
"nested/2": {
"((", false, nil, true,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: wanted a boolean expression; reached end of string", "((")
},
},
"nested/3": {
"(true", false, nil, true,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: wanted %q; reached end of string", "(true", ")")
},
},
"error/extra": {
"0 1", false, nil, false,
func(m *mocks.MockPP) {
m.EXPECT().Errorf(pp.EmojiUserError, "Failed to parse %q: unexpected token %q", "0 1", "1")
},
},
} {
tc := tc
t.Run(name, func(t *testing.T) {
Expand Down

0 comments on commit d594d3d

Please sign in to comment.