Passed
Push — main ( 2a46cd...12059c )
by Igor
01:41
created

test.assertHasOneViolation   B

Complexity

Conditions 6

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
dl 0
loc 13
c 0
b 0
f 0
rs 8.6666
nop 3
1
package test
2
3
import (
4
	"errors"
5
	"testing"
6
7
	"github.com/muonsoft/validation"
8
	"github.com/muonsoft/validation/validationtest"
9
	"github.com/stretchr/testify/assert"
10
)
11
12
func assertHasOneViolation(code, message, path string) func(t *testing.T, err error) {
13
	return func(t *testing.T, err error) {
14
		t.Helper()
15
		validationtest.AssertIsViolationList(t, err, func(t *testing.T, violations validation.ViolationList) bool {
16
			t.Helper()
17
18
			if assert.Len(t, violations, 1) {
19
				return assert.Equal(t, code, violations[0].Code()) &&
20
					assert.Equal(t, message, violations[0].Message()) &&
21
					assert.Equal(t, path, violations[0].PropertyPath().String())
22
			}
23
24
			return false
25
		})
26
	}
27
}
28
29
func assertNoError(t *testing.T, err error) {
30
	t.Helper()
31
	assert.NoError(t, err)
32
}
33
34
func assertIsInapplicableConstraintError(t *testing.T, err error, valueType string) {
35
	t.Helper()
36
	var inapplicableConstraint *validation.InapplicableConstraintError
37
38
	if !errors.As(err, &inapplicableConstraint) {
39
		t.Errorf("failed asserting that error is InapplicableConstraintError")
40
		return
41
	}
42
43
	assert.Equal(t, valueType, inapplicableConstraint.ValueType)
44
}
45