bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/name/basic_test.go (about)

     1  package name
     2  
     3  import (
     4  	"testing"
     5  	"unicode"
     6  )
     7  
     8  func createBasicVaidator(t *testing.T) RuneLevelValidator {
     9  	isValidTest := func(r rune) bool {
    10  		return unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-' || r == '_' || r == '.' || r == '/'
    11  	}
    12  	validator, err := NewBasicValidator(false, isValidTest)
    13  	if err != nil {
    14  		t.Error(err)
    15  		return nil
    16  	}
    17  
    18  	return validator
    19  }
    20  
    21  func TestBasicValidation(t *testing.T) {
    22  	testCases := []struct {
    23  		testString string
    24  		expectPass bool
    25  	}{
    26  		{"abc", true},
    27  		{"one.two.three", true},
    28  		{"1/2/3/4", true},
    29  		{"abc-123/456_xyz", true},
    30  		{"", false},
    31  		{" ", false},
    32  		{"abc$", false},
    33  		{"abc!xyz", false},
    34  	}
    35  
    36  	validator := createBasicVaidator(t)
    37  
    38  	for _, testCase := range testCases {
    39  		if validator.IsValid(testCase.testString) != testCase.expectPass {
    40  			t.Errorf("Expected IsValid test for '%s' to yeild '%t' not '%t'",
    41  				testCase.testString, testCase.expectPass, !testCase.expectPass)
    42  		}
    43  	}
    44  }
    45  
    46  func TestRuneLevelValidation(t *testing.T) {
    47  	testCases := []struct {
    48  		testRune   rune
    49  		expectPass bool
    50  	}{
    51  		{'a', true},
    52  		{'Z', true},
    53  		{'0', true},
    54  		{'9', true},
    55  		{'-', true},
    56  		{'_', true},
    57  		{'.', true},
    58  		{'/', true},
    59  
    60  		{'£', false},
    61  		{'$', false},
    62  		{'?', false},
    63  	}
    64  
    65  	validator := createBasicVaidator(t)
    66  
    67  	for _, testCase := range testCases {
    68  		if validator.IsRuneValid(testCase.testRune) != testCase.expectPass {
    69  			t.Errorf("Expected rune '%c' to be [valid=%t] but it was [valid=%t]",
    70  				testCase.testRune, testCase.expectPass, !testCase.expectPass)
    71  		}
    72  	}
    73  }