Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maurofran committed Apr 3, 2022
0 parents commit fa51eed
Show file tree
Hide file tree
Showing 21 changed files with 721 additions and 0 deletions.
29 changes: 29 additions & 0 deletions comparator/equals_to.go
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)
}
30 changes: 30 additions & 0 deletions comparator/greater_than.go
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)
}
30 changes: 30 additions & 0 deletions comparator/greater_than_or_equal_to.go
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)
}
30 changes: 30 additions & 0 deletions comparator/less_than.go
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)
}
30 changes: 30 additions & 0 deletions comparator/less_than_or_equal_to.go
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)
}
21 changes: 21 additions & 0 deletions constraints/constraints.go
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
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/maurofran/hamcrest4go

go 1.18
31 changes: 31 additions & 0 deletions is/equal.go
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)
}
30 changes: 30 additions & 0 deletions is/nil_or_zero.go
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)
}
25 changes: 25 additions & 0 deletions is/not.go
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)
}
Loading

0 comments on commit fa51eed

Please sign in to comment.