-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator_test.go
43 lines (36 loc) · 1.19 KB
/
validator_test.go
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
// Copyright 2023 Christoph Fichtmüller. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package jug
import "testing"
func TestNewValidator(t *testing.T) {
v := NewValidator()
if v == nil {
t.Fatal("Expected to get a validator")
}
}
func TestValidator_Require(t *testing.T) {
e1 := NewValidator().Require(false, "message").Validate()
if e1 == nil {
t.Fatal("Require(false) should return an error")
}
if e1.Error() != "message" {
t.Fatal("error should contain the provided message, got", e1.Error())
}
e2 := NewValidator().Require(true, "m").Validate()
if e2 != nil {
t.Fatal("Require(true) should not return an error, got", e2)
}
}
func TestValidator_RequireEnum(t *testing.T) {
if err := NewValidator().RequireEnum("a", "message", "a", "b").Validate(); err != nil {
t.Fatal("RequireEnum() should not fail when given an enum value, got", err)
}
err := NewValidator().RequireEnum("c", "message", "a", "b").Validate()
if err == nil {
t.Fatal("RequireEnum() should fail when given a non enum value")
}
if err.Error() != "message" {
t.Fatal("error should contain the provided message, got", err.Error())
}
}