File: not_nil_test.go

package info (click to toggle)
golang-github-go-ozzo-ozzo-validation.v4 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, trixie
  • size: 336 kB
  • sloc: makefile: 2
file content (62 lines) | stat: -rw-r--r-- 1,299 bytes parent folder | download
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
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2016 Qiang Xue. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package validation

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

type MyInterface interface {
	Hello()
}

func TestNotNil(t *testing.T) {
	var v1 []int
	var v2 map[string]int
	var v3 *int
	var v4 interface{}
	var v5 MyInterface
	tests := []struct {
		tag   string
		value interface{}
		err   string
	}{
		{"t1", v1, "is required"},
		{"t2", v2, "is required"},
		{"t3", v3, "is required"},
		{"t4", v4, "is required"},
		{"t5", v5, "is required"},
		{"t6", "", ""},
		{"t7", 0, ""},
	}

	for _, test := range tests {
		r := NotNil
		err := r.Validate(test.value)
		assertError(t, test.err, err, test.tag)
	}
}

func Test_notNilRule_Error(t *testing.T) {
	r := NotNil
	assert.Equal(t, "is required", r.Validate(nil).Error())
	r2 := r.Error("123")
	assert.Equal(t, "is required", r.Validate(nil).Error())
	assert.Equal(t, "123", r2.err.Message())
}

func TestNotNilRule_ErrorObject(t *testing.T) {
	r := NotNil

	err := NewError("code", "abc")
	r = r.ErrorObject(err)

	assert.Equal(t, err, r.err)
	assert.Equal(t, err.Code(), r.err.Code())
	assert.Equal(t, err.Message(), r.err.Message())
	assert.NotEqual(t, err, NotNil.err)
}