File: messagevalidator_test.go

package info (click to toggle)
golang-github-einride-aip-go 0.80.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,108 kB
  • sloc: makefile: 147
file content (49 lines) | stat: -rw-r--r-- 1,140 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package validation

import (
	"errors"
	"testing"

	"gotest.tools/v3/assert"
)

func TestMessageValidator(t *testing.T) {
	t.Parallel()

	t.Run("no violation", func(t *testing.T) {
		t.Parallel()
		var v MessageValidator
		assert.NilError(t, v.Err())
	})

	t.Run("add single violation", func(t *testing.T) {
		t.Parallel()
		var v MessageValidator
		v.AddFieldViolation("foo", "bar")
		assert.Error(t, v.Err(), "field violation on foo: bar")
	})

	t.Run("add single violation with parent", func(t *testing.T) {
		t.Parallel()
		var v MessageValidator
		v.SetParentField("foo")
		v.AddFieldViolation("bar", "baz")
		assert.Error(t, v.Err(), "field violation on foo.bar: baz")
	})

	t.Run("add nested violations", func(t *testing.T) {
		t.Parallel()
		var inner MessageValidator
		inner.AddFieldViolation("b", "c")
		var outer MessageValidator
		outer.AddFieldError("a", inner.Err())
		assert.Error(t, outer.Err(), "field violation on a.b: c")
	})

	t.Run("add field error", func(t *testing.T) {
		t.Parallel()
		var v MessageValidator
		v.AddFieldError("a", errors.New("boom"))
		assert.Error(t, v.Err(), "field violation on a: boom")
	})
}