mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 12:18:16 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			125 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			125 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
/*
 | 
						|
Copyright 2014 Google Inc. All rights reserved.
 | 
						|
 | 
						|
Licensed under the Apache License, Version 2.0 (the "License");
 | 
						|
you may not use this file except in compliance with the License.
 | 
						|
You may obtain a copy of the License at
 | 
						|
 | 
						|
    http://www.apache.org/licenses/LICENSE-2.0
 | 
						|
 | 
						|
Unless required by applicable law or agreed to in writing, software
 | 
						|
distributed under the License is distributed on an "AS IS" BASIS,
 | 
						|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
						|
See the License for the specific language governing permissions and
 | 
						|
limitations under the License.
 | 
						|
*/
 | 
						|
 | 
						|
package errors
 | 
						|
 | 
						|
import (
 | 
						|
	"strings"
 | 
						|
	"testing"
 | 
						|
)
 | 
						|
 | 
						|
func TestMakeFuncs(t *testing.T) {
 | 
						|
	testCases := []struct {
 | 
						|
		fn       func() ValidationError
 | 
						|
		expected ValidationErrorType
 | 
						|
	}{
 | 
						|
		{
 | 
						|
			func() ValidationError { return NewFieldInvalid("f", "v") },
 | 
						|
			ValidationErrorTypeInvalid,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			func() ValidationError { return NewFieldNotSupported("f", "v") },
 | 
						|
			ValidationErrorTypeNotSupported,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			func() ValidationError { return NewFieldDuplicate("f", "v") },
 | 
						|
			ValidationErrorTypeDuplicate,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			func() ValidationError { return NewFieldNotFound("f", "v") },
 | 
						|
			ValidationErrorTypeNotFound,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			func() ValidationError { return NewFieldRequired("f", "v") },
 | 
						|
			ValidationErrorTypeRequired,
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	for _, testCase := range testCases {
 | 
						|
		err := testCase.fn()
 | 
						|
		if err.Type != testCase.expected {
 | 
						|
			t.Errorf("expected Type %q, got %q", testCase.expected, err.Type)
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func TestValidationError(t *testing.T) {
 | 
						|
	s := NewFieldInvalid("foo", "bar").Error()
 | 
						|
	if !strings.Contains(s, "foo") || !strings.Contains(s, "bar") || !strings.Contains(s, ValueOf(ValidationErrorTypeInvalid)) {
 | 
						|
		t.Errorf("error message did not contain expected values, got %s", s)
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func TestErrListPrefix(t *testing.T) {
 | 
						|
	testCases := []struct {
 | 
						|
		Err      ValidationError
 | 
						|
		Expected string
 | 
						|
	}{
 | 
						|
		{
 | 
						|
			NewFieldNotFound("[0].bar", "value"),
 | 
						|
			"foo[0].bar",
 | 
						|
		},
 | 
						|
		{
 | 
						|
			NewFieldInvalid("field", "value"),
 | 
						|
			"foo.field",
 | 
						|
		},
 | 
						|
		{
 | 
						|
			NewFieldDuplicate("", "value"),
 | 
						|
			"foo",
 | 
						|
		},
 | 
						|
	}
 | 
						|
	for _, testCase := range testCases {
 | 
						|
		errList := ErrorList{testCase.Err}
 | 
						|
		prefix := errList.Prefix("foo")
 | 
						|
		if prefix == nil || len(prefix) != len(errList) {
 | 
						|
			t.Errorf("Prefix should return self")
 | 
						|
		}
 | 
						|
		if e, a := testCase.Expected, errList[0].(ValidationError).Field; e != a {
 | 
						|
			t.Errorf("expected %s, got %s", e, a)
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func TestErrListPrefixIndex(t *testing.T) {
 | 
						|
	testCases := []struct {
 | 
						|
		Err      ValidationError
 | 
						|
		Expected string
 | 
						|
	}{
 | 
						|
		{
 | 
						|
			NewFieldNotFound("[0].bar", "value"),
 | 
						|
			"[1][0].bar",
 | 
						|
		},
 | 
						|
		{
 | 
						|
			NewFieldInvalid("field", "value"),
 | 
						|
			"[1].field",
 | 
						|
		},
 | 
						|
		{
 | 
						|
			NewFieldDuplicate("", "value"),
 | 
						|
			"[1]",
 | 
						|
		},
 | 
						|
	}
 | 
						|
	for _, testCase := range testCases {
 | 
						|
		errList := ErrorList{testCase.Err}
 | 
						|
		prefix := errList.PrefixIndex(1)
 | 
						|
		if prefix == nil || len(prefix) != len(errList) {
 | 
						|
			t.Errorf("PrefixIndex should return self")
 | 
						|
		}
 | 
						|
		if e, a := testCase.Expected, errList[0].(ValidationError).Field; e != a {
 | 
						|
			t.Errorf("expected %s, got %s", e, a)
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 |