Skip to content

Commit

Permalink
chore: refactor shoulds to remove the need for explicit negated condi…
Browse files Browse the repository at this point in the history
…tions, introducing should.Not()
  • Loading branch information
omissis committed Aug 10, 2022
1 parent 24e4062 commit 7cbe11b
Show file tree
Hide file tree
Showing 21 changed files with 163 additions and 255 deletions.
2 changes: 1 addition & 1 deletion internal/arch/file/rule_one_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func Test_It_Checks_A_File_Does_Not_Exist(t *testing.T) {
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
vs, errs := file.One(tC.filename).
Should(fs.NotExist()).
Should(fs.Not(fs.Exist())).
Because("testing reasons")

if !cmp.Equal(vs, tC.wantViolations, cmp.AllowUnexported(rule.Violation{}), cmpopts.EquateEmpty()) {
Expand Down
2 changes: 1 addition & 1 deletion internal/arch/file/rule_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func Test_It_Checks_A_Set_Of_Files_Do_Not_Exist(t *testing.T) {
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
vs, errs := file.Set(tC.filenames...).
Should(fs.NotExist()).
Should(fs.Not(fs.Exist())).
Because("testing reasons")

if !cmp.Equal(vs, tC.wantViolations, cmp.AllowUnexported(rule.Violation{}), cmpopts.EquateEmpty()) {
Expand Down
13 changes: 7 additions & 6 deletions internal/arch/file/should/end_with.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ func EndWith(suffix string) *Expression {

return ls <= lf && fileName[lf-ls:] != suffix
},
getViolation: func(filePath string) rule.Violation {
getViolation: func(filePath string, negated bool) rule.Violation {
format := "file's name '%s' does not end with '%s'"
if negated {
format = "file's name '%s' does end with '%s'"
}

return rule.NewViolation(
fmt.Sprintf(
"file's name '%s' does not end with '%s'",
filepath.Base(filePath),
suffix,
),
fmt.Sprintf(format, filepath.Base(filePath), suffix),
)
},
}
Expand Down
35 changes: 35 additions & 0 deletions internal/arch/file/should/end_with_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,41 @@ func Test_EndWith(t *testing.T) {
t.Run(tC.desc, func(t *testing.T) {
ew := should.EndWith(tC.suffix)
got := ew.Evaluate(tC.ruleBuilder)

if !cmp.Equal(got, tC.want, cmp.AllowUnexported(rule.Violation{}), cmpopts.EquateEmpty()) {
t.Errorf("want = %+v, got = %+v", tC.want, got)
}
})
}
}

func Test_NotEndWith(t *testing.T) {
testCases := []struct {
desc string
ruleBuilder *file.RuleBuilder
suffix string
want []rule.Violation
}{
{
desc: "foobar does not end with baz",
ruleBuilder: file.One("foobar"),
suffix: "baz",
want: nil,
},
{
desc: "foobar ends with bar",
ruleBuilder: file.One("foobar"),
suffix: "bar",
want: []rule.Violation{
rule.NewViolation("file's name 'foobar' does end with 'bar'"),
},
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
ew := should.Not(should.EndWith(tC.suffix))
got := ew.Evaluate(tC.ruleBuilder)

if !cmp.Equal(got, tC.want, cmp.AllowUnexported(rule.Violation{}), cmpopts.EquateEmpty()) {
t.Errorf("want = %+v, got = %+v", tC.want, got)
}
Expand Down
18 changes: 10 additions & 8 deletions internal/arch/file/should/exist.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@ func Exist() *Expression {
return &Expression{
evaluate: func(rb rule.Builder, filePath string) bool {
if _, err := os.Stat(filePath); err != nil {
if os.IsNotExist(err) {
return true
if !os.IsNotExist(err) {
rb.AddError(err)
}

rb.AddError(err)
return true
}

return false
},
getViolation: func(filePath string) rule.Violation {
getViolation: func(filePath string, negated bool) rule.Violation {
format := "file '%s' does not exist"
if negated {
format = "file '%s' does exist"
}

return rule.NewViolation(
fmt.Sprintf(
"file '%s' does not exist",
filepath.Base(filePath),
),
fmt.Sprintf(format, filepath.Base(filePath)),
)
},
}
Expand Down
31 changes: 31 additions & 0 deletions internal/arch/file/should/exist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,37 @@ func Test_Exist(t *testing.T) {
t.Run(tC.desc, func(t *testing.T) {
e := should.Exist()
got := e.Evaluate(tC.ruleBuilder)

if !cmp.Equal(got, tC.want, cmp.AllowUnexported(rule.Violation{}), cmpopts.EquateEmpty()) {
t.Errorf("want = %+v, got = %+v", tC.want, got)
}
})
}
}

func Test_NotExist(t *testing.T) {
testCases := []struct {
desc string
ruleBuilder *file.RuleBuilder
want []rule.Violation
}{
{
desc: "exist.go exists",
ruleBuilder: file.One("exist.go"),
want: []rule.Violation{
rule.NewViolation("file 'exist.go' does exist"),
},
},
{
desc: "abc.xyz does not exist",
ruleBuilder: file.One("abc.xyz"),
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
e := should.Not(should.Exist())
got := e.Evaluate(tC.ruleBuilder)

if !cmp.Equal(got, tC.want, cmp.AllowUnexported(rule.Violation{}), cmpopts.EquateEmpty()) {
t.Errorf("want = %+v, got = %+v", tC.want, got)
}
Expand Down
9 changes: 7 additions & 2 deletions internal/arch/file/should/match_glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ func MatchGlob(glob string, basePath string) *Expression {

return !match
},
getViolation: func(filePath string) rule.Violation {
getViolation: func(filePath string, negated bool) rule.Violation {
format := "file's path '%s' does not match glob pattern '%s'"
if negated {
format = "file's path '%s' does match glob pattern '%s'"
}

return rule.NewViolation(
fmt.Sprintf(
"file's path '%s' does not match glob pattern '%s'",
format,
filepath.Base(filePath),
glob,
),
Expand Down
1 change: 1 addition & 0 deletions internal/arch/file/should/match_glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func Test_MatchGlob(t *testing.T) {
t.Run(tC.desc, func(t *testing.T) {
mg := should.MatchGlob(tC.glob, basePath)
got := mg.Evaluate(tC.ruleBuilder)

if !cmp.Equal(got, tC.want, cmp.AllowUnexported(rule.Violation{}), cmpopts.EquateEmpty()) {
t.Errorf("want = %+v, got = %+v", tC.want, got)
}
Expand Down
9 changes: 7 additions & 2 deletions internal/arch/file/should/match_regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ func MatchRegex(res string) *Expression {
filepath.Base(filePath),
)
},
getViolation: func(filePath string) rule.Violation {
getViolation: func(filePath string, negated bool) rule.Violation {
format := "file's name '%s' does not match regex '%s'"
if negated {
format = "file's name '%s' does match regex '%s'"
}

return rule.NewViolation(
fmt.Sprintf(
"file's name '%s' does not match regex '%s'",
format,
filepath.Base(filePath),
res,
),
Expand Down
43 changes: 43 additions & 0 deletions internal/arch/file/should/match_regex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,49 @@ func Test_MatchRegex(t *testing.T) {
t.Run(tC.desc, func(t *testing.T) {
ew := should.MatchRegex(tC.regexp)
got := ew.Evaluate(tC.ruleBuilder)

if !cmp.Equal(got, tC.want, cmp.AllowUnexported(rule.Violation{}), cmpopts.EquateEmpty()) {
t.Errorf("want = %+v, got = %+v", tC.want, got)
}
})
}
}

func Test_NotMatchRegex(t *testing.T) {
testCases := []struct {
desc string
ruleBuilder *file.RuleBuilder
regexp string
want []rule.Violation
}{
{
desc: "foobar matches '[a-z]+'",
ruleBuilder: file.One("foobar"),
regexp: "[a-z]+",
want: []rule.Violation{
rule.NewViolation("file's name 'foobar' does match regex '[a-z]+'"),
},
},
{
desc: "foobar matches 'foobar'",
ruleBuilder: file.One("foobar"),
regexp: "foobar",
want: []rule.Violation{
rule.NewViolation("file's name 'foobar' does match regex 'foobar'"),
},
},
{
desc: "foobar does not match '[0-9]+'",
ruleBuilder: file.One("foobar"),
regexp: "[0-9]+",
want: nil,
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
nmr := should.Not(should.MatchRegex(tC.regexp))
got := nmr.Evaluate(tC.ruleBuilder)

if !cmp.Equal(got, tC.want, cmp.AllowUnexported(rule.Violation{}), cmpopts.EquateEmpty()) {
t.Errorf("want = %+v, got = %+v", tC.want, got)
}
Expand Down
30 changes: 0 additions & 30 deletions internal/arch/file/should/not_end_with.go

This file was deleted.

44 changes: 0 additions & 44 deletions internal/arch/file/should/not_end_with_test.go

This file was deleted.

31 changes: 0 additions & 31 deletions internal/arch/file/should/not_exist.go

This file was deleted.

40 changes: 0 additions & 40 deletions internal/arch/file/should/not_exist_test.go

This file was deleted.

Loading

0 comments on commit 7cbe11b

Please # to comment.