Skip to content

Commit

Permalink
Merge pull request #2 from storozhukBM/chain_methods
Browse files Browse the repository at this point in the history
Support chain methods and specific errors
  • Loading branch information
storozhukBM authored Jul 24, 2018
2 parents 4e657f1 + 96339d1 commit 00ea352
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 18 deletions.
54 changes: 40 additions & 14 deletions verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,42 +51,68 @@ type Verify struct {
checked bool
}

// WithError verifies condition passed as first argument.
// If `positiveCondition == true`, verification will proceed for other checks.
// If `positiveCondition == false`, internal state will be filled with error specified as second argument.
// After the first failed verification all others won't count and predicates won't be evaluated.
func (v *Verify) WithError(positiveCondition bool, err error) *Verify {
vObj := v
if v == nil {
vObj = &Verify{}
}

vObj.checked = false
if vObj.err != nil {
return vObj
}
if positiveCondition {
return vObj
}
vObj.err = err
return vObj
}

// That verifies condition passed as first argument.
// If `positiveCondition == true`, verification will proceed for other checks.
// If `positiveCondition == false`, internal state will be filled with error,
// using message argument as format in fmt.Errorf(message, args...).
// After the first failed verification all others won't count and predicates won't be evaluated.
func (v *Verify) That(positiveCondition bool, message string, args ...interface{}) {
func (v *Verify) That(positiveCondition bool, message string, args ...interface{}) *Verify {
vObj := v
if v == nil {
return
vObj = &Verify{}
}
v.checked = false
if v.err != nil {
return

vObj.checked = false
if vObj.err != nil {
return vObj
}
if positiveCondition {
return
return vObj
}
v.err = fmt.Errorf(message, args...)
vObj.err = fmt.Errorf(message, args...)
return vObj
}

// That evaluates predicate passed as first argument.
// If `predicate() == true`, verification will proceed for other checks.
// If `predicate() == false`, internal state will be filled with error,
// using message argument as format in fmt.Errorf(message, args...).
// After the first failed verification all others won't count and predicates won't be evaluated.
func (v *Verify) Predicate(predicate func() bool, message string, args ...interface{}) {
func (v *Verify) Predicate(predicate func() bool, message string, args ...interface{}) *Verify {
vObj := v
if v == nil {
return
vObj = &Verify{}
}
v.checked = false
if v.err != nil {
return
vObj.checked = false
if vObj.err != nil {
return vObj
}
if predicate() {
return
return vObj
}
v.err = fmt.Errorf(message, args...)
vObj.err = fmt.Errorf(message, args...)
return vObj
}

// GetError extracts error from internal state to check if there where any during verification process.
Expand Down
34 changes: 30 additions & 4 deletions verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package verifier_test

import (
"bytes"
"errors"
"github.com/storozhukBM/verifier"
"math/rand"
"os"
Expand All @@ -13,10 +14,11 @@ import (
)

func TestVerifier_positive_conditions(t *testing.T) {
verify := verifier.Offensive()
verify.That(rand.Float32() >= 0.0, "random should be positive")
verify.That(rand.Float32() < 1.0, "random should less then 1.0")
verify.That(true, "some other check with format %s; %d", "testCheck", 35)
var verify *verifier.Verify
verify = verify.
WithError(rand.Float32() < 1.0, errors.New("random should less then 1.0")).
That(rand.Float32() >= 0.0, "random should be positive").
That(true, "some other check with format %s; %d", "testCheck", 35)
if verify.GetError() != nil {
t.Error("verifier should be empty")
}
Expand All @@ -33,6 +35,30 @@ func TestVerifier_positive_conditions(t *testing.T) {
}
}

func TestVerifier_positive_conditions_with_error(t *testing.T) {
verify := verifier.Offensive()
verificationErr := verify.
WithError(rand.Float32() >= 0.0, errors.New("random should be positive")).
That(rand.Float32() < 1.0, "random should less then 1.0").
That(true, "some other check with format %s; %d", "testCheck", 35).
GetError()
if verificationErr != nil {
t.Error("verifier should be empty")
}
expectedErr := errors.New("expect error here")
verify.WithError(rand.Float32() < 0.0, expectedErr)
verify.WithError(rand.Float32() == 0.0, errors.New("should not have any deference"))
if verify.GetError() == nil {
t.Fatal("verifier should be filled")
}
if verify.GetError() != expectedErr {
t.Errorf("unexpected error message: %s", verify.GetError())
}
if verify.String() != "verification failure: expect error here" {
t.Errorf("unexpected verifier string representation: %s", verify)
}
}

func TestVerifier_positive_not_evaluate_after_failure(t *testing.T) {
counter := 0
verify := verifier.New()
Expand Down

0 comments on commit 00ea352

Please sign in to comment.