github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/Unknwon/com/regex_test.go (about)

     1  // Copyright 2013 com authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"): you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package com
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestIsEmail(t *testing.T) {
    22  	emails := map[string]bool{
    23  		`test@example.com`:             true,
    24  		`single-character@b.org`:       true,
    25  		`uncommon_address@test.museum`: true,
    26  		`local@sld.UPPER`:              true,
    27  		`@missing.org`:                 false,
    28  		`missing@.com`:                 false,
    29  		`missing@qq.`:                  false,
    30  		`wrong-ip@127.1.1.1.26`:        false,
    31  	}
    32  	for e, r := range emails {
    33  		b := IsEmail(e)
    34  		if b != r {
    35  			t.Errorf("IsEmail:\n Expect => %v\n Got => %v\n", r, b)
    36  		}
    37  	}
    38  }
    39  
    40  func TestIsUrl(t *testing.T) {
    41  	urls := map[string]bool{
    42  		"http://www.example.com":                     true,
    43  		"http://example.com":                         true,
    44  		"http://example.com?user=test&password=test": true,
    45  		"http://example.com?user=test#login":         true,
    46  		"ftp://example.com":                          true,
    47  		"https://example.com":                        true,
    48  		"htp://example.com":                          false,
    49  		"http//example.com":                          false,
    50  		"http://example":                             true,
    51  	}
    52  	for u, r := range urls {
    53  		b := IsUrl(u)
    54  		if b != r {
    55  			t.Errorf("IsUrl:\n Expect => %v\n Got => %v\n", r, b)
    56  		}
    57  	}
    58  }
    59  
    60  func BenchmarkIsEmail(b *testing.B) {
    61  	for i := 0; i < b.N; i++ {
    62  		IsEmail("test@example.com")
    63  	}
    64  }
    65  
    66  func BenchmarkIsUrl(b *testing.B) {
    67  	for i := 0; i < b.N; i++ {
    68  		IsEmail("http://example.com")
    69  	}
    70  }