regex_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. package com
  15. import (
  16. "testing"
  17. )
  18. func TestIsEmail(t *testing.T) {
  19. emails := map[string]bool{
  20. `test@example.com`: true,
  21. `single-character@b.org`: true,
  22. `uncommon_address@test.museum`: true,
  23. `local@sld.UPPER`: true,
  24. `@missing.org`: false,
  25. `missing@.com`: false,
  26. `missing@qq.`: false,
  27. `wrong-ip@127.1.1.1.26`: false,
  28. }
  29. for e, r := range emails {
  30. b := IsEmail(e)
  31. if b != r {
  32. t.Errorf("IsEmail:\n Expect => %v\n Got => %v\n", r, b)
  33. }
  34. }
  35. }
  36. func TestIsUrl(t *testing.T) {
  37. urls := map[string]bool{
  38. "http://www.example.com": true,
  39. "http://example.com": true,
  40. "http://example.com?user=test&password=test": true,
  41. "http://example.com?user=test#login": true,
  42. "ftp://example.com": true,
  43. "https://example.com": true,
  44. "htp://example.com": false,
  45. "http//example.com": false,
  46. "http://example": true,
  47. }
  48. for u, r := range urls {
  49. b := IsUrl(u)
  50. if b != r {
  51. t.Errorf("IsUrl:\n Expect => %v\n Got => %v\n", r, b)
  52. }
  53. }
  54. }
  55. func BenchmarkIsEmail(b *testing.B) {
  56. for i := 0; i < b.N; i++ {
  57. IsEmail("test@example.com")
  58. }
  59. }
  60. func BenchmarkIsUrl(b *testing.B) {
  61. for i := 0; i < b.N; i++ {
  62. IsEmail("http://example.com")
  63. }
  64. }