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

Commit

Permalink
Merge pull request #1 from maurofran/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
maurofran authored Apr 4, 2022
2 parents c0b4d1f + 0ca449f commit b606aba
Show file tree
Hide file tree
Showing 41 changed files with 1,213 additions and 156 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Hamcrest for GO
30 changes: 0 additions & 30 deletions comparator/greater_than_or_equal_to.go

This file was deleted.

30 changes: 0 additions & 30 deletions comparator/less_than_or_equal_to.go

This file was deleted.

2 changes: 1 addition & 1 deletion comparator/equals_to.go → compare/equals_to.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package comparator
package compare

import (
"github.com/maurofran/hamcrest4go/matcher"
Expand Down
51 changes: 51 additions & 0 deletions compare/equals_to_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package compare

import (
"github.com/maurofran/hamcrest4go/matcher"
"github.com/stretchr/testify/assert"
"testing"
)

func TestEqualsTo_Matches(t *testing.T) {
tests := []struct {
name string
ref int
value int
expected bool
}{
{"equals", 5, 5, true},
{"lesser", 5, 4, false},
{"greater", 5, 6, false},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Given
fixture := EqualsTo(test.ref)
// When
actual := fixture.Matches(test.value)
// Then
assert.Equal(t, test.expected, actual)
})
}
}

func TestEqualsTo_DescribeTo(t *testing.T) {
// Given
fixture := EqualsTo(5)
description := matcher.StringDescription()
// When
fixture.DescribeTo(description)
// Then
assert.Equal(t, `a value equal to 5`, description.String())
}

func TestEqualsTo_DescribeMismatch(t *testing.T) {
// Given
fixture := EqualsTo(5)
description := matcher.StringDescription()
// When
fixture.DescribeMismatch(8, description)
// Then
assert.Equal(t, `8 was equal to 5`, description.String())
}
2 changes: 1 addition & 1 deletion comparator/greater_than.go → compare/greater_than.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package comparator
package compare

import (
"github.com/maurofran/hamcrest4go/constraints"
Expand Down
30 changes: 30 additions & 0 deletions compare/greater_than_or_equals_to.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package compare

import (
"github.com/maurofran/hamcrest4go/constraints"
"github.com/maurofran/hamcrest4go/matcher"
)

// GreaterThanOrEqualsTo creates a new matcher for grater than comparison.
func GreaterThanOrEqualsTo[T constraints.Ordered](value T) matcher.Matcher[T] {
return greaterThanOrEqualsTo[T]{ref: value}
}

type greaterThanOrEqualsTo[T constraints.Ordered] struct {
ref T
}

func (e greaterThanOrEqualsTo[T]) Matches(value T) bool {
return value >= e.ref
}

func (e greaterThanOrEqualsTo[T]) DescribeTo(description matcher.Description) {
description.AppendText("a value greater than or equals to ")
description.AppendValue(e.ref)
}

func (e greaterThanOrEqualsTo[T]) DescribeMismatch(actual T, description matcher.Description) {
description.AppendValue(actual)
description.AppendText(" was greater than or equals to ")
description.AppendValue(e.ref)
}
51 changes: 51 additions & 0 deletions compare/greater_than_or_equals_to_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package compare

import (
"github.com/maurofran/hamcrest4go/matcher"
"github.com/stretchr/testify/assert"
"testing"
)

func TestGreaterThanOrEqualsTo_Matches(t *testing.T) {
tests := []struct {
name string
ref int
value int
expected bool
}{
{"equals", 5, 5, true},
{"lesser", 5, 4, false},
{"greater", 5, 6, true},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Given
fixture := GreaterThanOrEqualsTo(test.ref)
// When
actual := fixture.Matches(test.value)
// Then
assert.Equal(t, test.expected, actual)
})
}
}

func TestGreaterOrEqualsToThan_DescribeTo(t *testing.T) {
// Given
fixture := GreaterThanOrEqualsTo(5)
description := matcher.StringDescription()
// When
fixture.DescribeTo(description)
// Then
assert.Equal(t, `a value greater than or equals to 5`, description.String())
}

func TestGreaterThanOrEqualsTo_DescribeMismatch(t *testing.T) {
// Given
fixture := GreaterThanOrEqualsTo(5)
description := matcher.StringDescription()
// When
fixture.DescribeMismatch(8, description)
// Then
assert.Equal(t, `8 was greater than or equals to 5`, description.String())
}
51 changes: 51 additions & 0 deletions compare/greater_than_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package compare

import (
"github.com/maurofran/hamcrest4go/matcher"
"github.com/stretchr/testify/assert"
"testing"
)

func TestGreaterThan_Matches(t *testing.T) {
tests := []struct {
name string
ref int
value int
expected bool
}{
{"equals", 5, 5, false},
{"lesser", 5, 4, false},
{"greater", 5, 6, true},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Given
fixture := GreaterThan(test.ref)
// When
actual := fixture.Matches(test.value)
// Then
assert.Equal(t, test.expected, actual)
})
}
}

func TestGreaterThan_DescribeTo(t *testing.T) {
// Given
fixture := GreaterThan(5)
description := matcher.StringDescription()
// When
fixture.DescribeTo(description)
// Then
assert.Equal(t, `a value greater than 5`, description.String())
}

func TestGreaterThan_DescribeMismatch(t *testing.T) {
// Given
fixture := GreaterThan(5)
description := matcher.StringDescription()
// When
fixture.DescribeMismatch(8, description)
// Then
assert.Equal(t, `8 was greater than 5`, description.String())
}
4 changes: 2 additions & 2 deletions comparator/less_than.go → compare/less_than.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package comparator
package compare

import (
"github.com/maurofran/hamcrest4go/constraints"
Expand All @@ -19,7 +19,7 @@ func (e lessThan[T]) Matches(value T) bool {
}

func (e lessThan[T]) DescribeTo(description matcher.Description) {
description.AppendText("a value lesser than to ")
description.AppendText("a value lesser than ")
description.AppendValue(e.ref)
}

Expand Down
30 changes: 30 additions & 0 deletions compare/less_than_or_equals_to.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package compare

import (
"github.com/maurofran/hamcrest4go/constraints"
"github.com/maurofran/hamcrest4go/matcher"
)

// LessThanOrEqualsTo creates a new matcher for grater than comparison.
func LessThanOrEqualsTo[T constraints.Ordered](value T) matcher.Matcher[T] {
return lessThanOrEqualsTo[T]{ref: value}
}

type lessThanOrEqualsTo[T constraints.Ordered] struct {
ref T
}

func (e lessThanOrEqualsTo[T]) Matches(value T) bool {
return value <= e.ref
}

func (e lessThanOrEqualsTo[T]) DescribeTo(description matcher.Description) {
description.AppendText("a value less than or equals to ")
description.AppendValue(e.ref)
}

func (e lessThanOrEqualsTo[T]) DescribeMismatch(actual T, description matcher.Description) {
description.AppendValue(actual)
description.AppendText(" was less than or equals to ")
description.AppendValue(e.ref)
}
51 changes: 51 additions & 0 deletions compare/less_than_or_equals_to_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package compare

import (
"github.com/maurofran/hamcrest4go/matcher"
"github.com/stretchr/testify/assert"
"testing"
)

func TestLessThanOrEqualsTo_Matches(t *testing.T) {
tests := []struct {
name string
ref int
value int
expected bool
}{
{"equals", 5, 5, true},
{"lesser", 5, 4, true},
{"greater", 5, 6, false},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Given
fixture := LessThanOrEqualsTo(test.ref)
// When
actual := fixture.Matches(test.value)
// Then
assert.Equal(t, test.expected, actual)
})
}
}

func TestLessOrEqualsToThan_DescribeTo(t *testing.T) {
// Given
fixture := LessThanOrEqualsTo(5)
description := matcher.StringDescription()
// When
fixture.DescribeTo(description)
// Then
assert.Equal(t, `a value less than or equals to 5`, description.String())
}

func TestLessThanOrEqualsTo_DescribeMismatch(t *testing.T) {
// Given
fixture := LessThanOrEqualsTo(5)
description := matcher.StringDescription()
// When
fixture.DescribeMismatch(8, description)
// Then
assert.Equal(t, `8 was less than or equals to 5`, description.String())
}
Loading

0 comments on commit b606aba

Please sign in to comment.