github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume2/section3/gopherfaceform/validationkit/checkusername_test.go (about)

     1  package validationkit
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  // Test for minimum number of characters
     8  func TestCheckUsernameSyntaxMinimumCharacterLength(t *testing.T) {
     9  
    10  	result := CheckUsernameSyntax("")
    11  
    12  	if result != false {
    13  		t.Errorf("Failed the minimum character check.")
    14  
    15  	}
    16  
    17  }
    18  
    19  // Test for maximum number of characters
    20  func TestCheckUsernameSyntaxMaximumCharacterLength(t *testing.T) {
    21  
    22  	result := CheckUsernameSyntax("aaaaaaaaaaaaaaaa")
    23  
    24  	if result != false {
    25  		t.Errorf("Failed the maximum character check.")
    26  
    27  	}
    28  
    29  }
    30  
    31  func TestCheckUsernameSyntaxSymbols(t *testing.T) {
    32  
    33  	result := CheckUsernameSyntax("g-@p!h#r")
    34  	if result != false {
    35  		t.Errorf("Failed the special character check.")
    36  
    37  	}
    38  
    39  }
    40  
    41  func TestCheckUsernameSyntaxUnderscore(t *testing.T) {
    42  
    43  	// Underscores are permissible
    44  	result := CheckUsernameSyntax("the_gopher")
    45  	if result != true {
    46  		t.Errorf("Failed the check to allow underscore characters.")
    47  
    48  	}
    49  
    50  }
    51  
    52  func TestCheckUsernameSyntaxAtSignInsideUsername(t *testing.T) {
    53  
    54  	// The @ sign can only be placed at the start of the username string and is invalid anyhwere else
    55  	result := CheckUsernameSyntax("the@gopher")
    56  	if result != false {
    57  		t.Errorf("Failed the @ sign check. The @ sign was found in another place besides the start of the string.")
    58  	}
    59  }
    60  
    61  func TestCheckUsernameSyntaxRandomUsernames(t *testing.T) {
    62  
    63  	for i := 0; i < 10008; i++ {
    64  
    65  		username := GenerateRandomUsername()
    66  		//fmt.Println("username: ", username)
    67  		result := CheckUsernameSyntax(username)
    68  		if result != true {
    69  			t.Errorf("The random username, ", username, ", failed to pass the username check.")
    70  			t.Fatal("Quitting!")
    71  		}
    72  	}
    73  
    74  }