This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fa51eed
Showing
21 changed files
with
721 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package comparator | ||
|
||
import ( | ||
"github.com/maurofran/hamcrest4go/matcher" | ||
) | ||
|
||
// EqualsTo creates a new matcher for equality comparison. | ||
func EqualsTo[T comparable](value T) matcher.Matcher[T] { | ||
return equalsTo[T]{ref: value} | ||
} | ||
|
||
type equalsTo[T comparable] struct { | ||
ref T | ||
} | ||
|
||
func (e equalsTo[T]) Matches(value T) bool { | ||
return e.ref == value | ||
} | ||
|
||
func (e equalsTo[T]) DescribeTo(description matcher.Description) { | ||
description.AppendText("a value equal to ") | ||
description.AppendValue(e.ref) | ||
} | ||
|
||
func (e equalsTo[T]) DescribeMismatch(actual T, description matcher.Description) { | ||
description.AppendValue(actual) | ||
description.AppendText(" was equal to ") | ||
description.AppendValue(e.ref) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package comparator | ||
|
||
import ( | ||
"github.com/maurofran/hamcrest4go/constraints" | ||
"github.com/maurofran/hamcrest4go/matcher" | ||
) | ||
|
||
// GreaterThan creates a new matcher for grater than comparison. | ||
func GreaterThan[T constraints.Ordered](value T) matcher.Matcher[T] { | ||
return greaterThan[T]{ref: value} | ||
} | ||
|
||
type greaterThan[T constraints.Ordered] struct { | ||
ref T | ||
} | ||
|
||
func (e greaterThan[T]) Matches(value T) bool { | ||
return value > e.ref | ||
} | ||
|
||
func (e greaterThan[T]) DescribeTo(description matcher.Description) { | ||
description.AppendText("a value greater than ") | ||
description.AppendValue(e.ref) | ||
} | ||
|
||
func (e greaterThan[T]) DescribeMismatch(actual T, description matcher.Description) { | ||
description.AppendValue(actual) | ||
description.AppendText(" was greater than ") | ||
description.AppendValue(e.ref) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package comparator | ||
|
||
import ( | ||
"github.com/maurofran/hamcrest4go/constraints" | ||
"github.com/maurofran/hamcrest4go/matcher" | ||
) | ||
|
||
// GreaterThanOrEqualTo creates a new matcher for grater than comparison. | ||
func GreaterThanOrEqualTo[T constraints.Ordered](value T) matcher.Matcher[T] { | ||
return greaterThanOrEqualTo[T]{ref: value} | ||
} | ||
|
||
type greaterThanOrEqualTo[T constraints.Ordered] struct { | ||
ref T | ||
} | ||
|
||
func (e greaterThanOrEqualTo[T]) Matches(value T) bool { | ||
return value >= e.ref | ||
} | ||
|
||
func (e greaterThanOrEqualTo[T]) DescribeTo(description matcher.Description) { | ||
description.AppendText("a value greater than or equal to ") | ||
description.AppendValue(e.ref) | ||
} | ||
|
||
func (e greaterThanOrEqualTo[T]) DescribeMismatch(actual T, description matcher.Description) { | ||
description.AppendValue(actual) | ||
description.AppendText(" was greater than or equal to ") | ||
description.AppendValue(e.ref) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package comparator | ||
|
||
import ( | ||
"github.com/maurofran/hamcrest4go/constraints" | ||
"github.com/maurofran/hamcrest4go/matcher" | ||
) | ||
|
||
// LessThan creates a new matcher for grater than comparison. | ||
func LessThan[T constraints.Ordered](value T) matcher.Matcher[T] { | ||
return lessThan[T]{ref: value} | ||
} | ||
|
||
type lessThan[T constraints.Ordered] struct { | ||
ref T | ||
} | ||
|
||
func (e lessThan[T]) Matches(value T) bool { | ||
return value < e.ref | ||
} | ||
|
||
func (e lessThan[T]) DescribeTo(description matcher.Description) { | ||
description.AppendText("a value lesser than to ") | ||
description.AppendValue(e.ref) | ||
} | ||
|
||
func (e lessThan[T]) DescribeMismatch(actual T, description matcher.Description) { | ||
description.AppendValue(actual) | ||
description.AppendText(" was lesser than ") | ||
description.AppendValue(e.ref) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package comparator | ||
|
||
import ( | ||
"github.com/maurofran/hamcrest4go/constraints" | ||
"github.com/maurofran/hamcrest4go/matcher" | ||
) | ||
|
||
// LessThanOrEqualTo creates a new matcher for grater than comparison. | ||
func LessThanOrEqualTo[T constraints.Ordered](value T) matcher.Matcher[T] { | ||
return lessThanOrEqualTo[T]{ref: value} | ||
} | ||
|
||
type lessThanOrEqualTo[T constraints.Ordered] struct { | ||
ref T | ||
} | ||
|
||
func (e lessThanOrEqualTo[T]) Matches(value T) bool { | ||
return value <= e.ref | ||
} | ||
|
||
func (e lessThanOrEqualTo[T]) DescribeTo(description matcher.Description) { | ||
description.AppendText("a value lesser than to ") | ||
description.AppendValue(e.ref) | ||
} | ||
|
||
func (e lessThanOrEqualTo[T]) DescribeMismatch(actual T, description matcher.Description) { | ||
description.AppendValue(actual) | ||
description.AppendText(" was lesser than ") | ||
description.AppendValue(e.ref) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package constraints | ||
|
||
type Signed interface { | ||
~int | ~int8 | ~int16 | ~int32 | ~int64 | ||
} | ||
|
||
type Unsigned interface { | ||
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ||
} | ||
|
||
type Integer interface { | ||
Signed | Unsigned | ||
} | ||
|
||
type Float interface { | ||
~float32 | ~float64 | ||
} | ||
|
||
type Ordered interface { | ||
Integer | Float | ~string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/maurofran/hamcrest4go | ||
|
||
go 1.18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package is | ||
|
||
import "github.com/maurofran/hamcrest4go/matcher" | ||
|
||
// Equaler is the constraint interface for an object that has the Equal method. | ||
type Equaler[O any] interface { | ||
Equal(other O) bool | ||
} | ||
|
||
// EqualTo gets a matcher for nil value. | ||
func EqualTo[T Equaler[T]](ref T) matcher.Matcher[T] { | ||
return equalTo[T]{ref: ref} | ||
} | ||
|
||
type equalTo[T Equaler[T]] struct { | ||
ref T | ||
} | ||
|
||
func (e equalTo[T]) Matches(value T) bool { | ||
return value.Equal(e.ref) | ||
} | ||
|
||
func (e equalTo[T]) DescribeTo(description matcher.Description) { | ||
description.AppendText("equal to ") | ||
description.AppendValue(e.ref) | ||
} | ||
|
||
func (equalTo[T]) DescribeMismatch(actual T, description matcher.Description) { | ||
description.AppendText("was ") | ||
description.AppendValue(actual) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package is | ||
|
||
import "github.com/maurofran/hamcrest4go/matcher" | ||
|
||
// NotNilOrZero gets a matcher for not nilOrZero. | ||
func NotNilOrZero[T comparable]() matcher.Matcher[T] { | ||
return Not[T](NilOrZero[T]()) | ||
} | ||
|
||
// NilOrZero gets a matcher for nil value. | ||
func NilOrZero[T comparable]() matcher.Matcher[T] { | ||
return nilOrZero[T]{} | ||
} | ||
|
||
type nilOrZero[T comparable] struct { | ||
} | ||
|
||
func (nilOrZero[T]) Matches(value T) bool { | ||
var z T | ||
return value == z | ||
} | ||
|
||
func (nilOrZero[T]) DescribeTo(description matcher.Description) { | ||
description.AppendText("nilOrZero") | ||
} | ||
|
||
func (nilOrZero[T]) DescribeMismatch(actual T, description matcher.Description) { | ||
description.AppendText("was ") | ||
description.AppendValue(actual) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package is | ||
|
||
import "github.com/maurofran/hamcrest4go/matcher" | ||
|
||
// Not implements the negation of a given mathcer. | ||
func Not[T any](original matcher.Matcher[T]) matcher.Matcher[T] { | ||
return not[T]{original: original} | ||
} | ||
|
||
type not[T any] struct { | ||
original matcher.Matcher[T] | ||
} | ||
|
||
func (n not[T]) Matches(value T) bool { | ||
return !n.original.Matches(value) | ||
} | ||
|
||
func (n not[T]) DescribeTo(description matcher.Description) { | ||
description.AppendText("not ") | ||
description.AppendDescriptionOf(n.original) | ||
} | ||
|
||
func (n not[T]) DescribeMismatch(actual T, description matcher.Description) { | ||
n.original.DescribeMismatch(actual, description) | ||
} |
Oops, something went wrong.