PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/model/utils_test.go

https://gitlab.com/Realtyka/platform
Go | 122 lines | 102 code | 18 blank | 2 comment | 18 complexity | 662b4a57fbf6283b31451ff21083f2d7 MD5 | raw file
  1. // Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
  2. // See License.txt for license information.
  3. package model
  4. import (
  5. "strings"
  6. "testing"
  7. )
  8. func TestNewId(t *testing.T) {
  9. for i := 0; i < 1000; i++ {
  10. id := NewId()
  11. if len(id) > 26 {
  12. t.Fatal("ids shouldn't be longer than 26 chars")
  13. }
  14. }
  15. }
  16. func TestRandomString(t *testing.T) {
  17. for i := 0; i < 1000; i++ {
  18. r := NewRandomString(32)
  19. if len(r) != 32 {
  20. t.Fatal("should be 32 chars")
  21. }
  22. }
  23. }
  24. func TestAppError(t *testing.T) {
  25. err := NewLocAppError("TestAppError", "message", nil, "")
  26. json := err.ToJson()
  27. rerr := AppErrorFromJson(strings.NewReader(json))
  28. if err.Message != rerr.Message {
  29. t.Fatal()
  30. }
  31. err.Error()
  32. }
  33. func TestMapJson(t *testing.T) {
  34. m := make(map[string]string)
  35. m["id"] = "test_id"
  36. json := MapToJson(m)
  37. rm := MapFromJson(strings.NewReader(json))
  38. if rm["id"] != "test_id" {
  39. t.Fatal("map should be valid")
  40. }
  41. rm2 := MapFromJson(strings.NewReader(""))
  42. if len(rm2) > 0 {
  43. t.Fatal("make should be ivalid")
  44. }
  45. }
  46. func TestValidEmail(t *testing.T) {
  47. if !IsValidEmail("corey+test@hulen.com") {
  48. t.Error("email should be valid")
  49. }
  50. if IsValidEmail("@corey+test@hulen.com") {
  51. t.Error("should be invalid")
  52. }
  53. }
  54. func TestValidLower(t *testing.T) {
  55. if !IsLower("corey+test@hulen.com") {
  56. t.Error("should be valid")
  57. }
  58. if IsLower("Corey+test@hulen.com") {
  59. t.Error("should be invalid")
  60. }
  61. }
  62. func TestEtag(t *testing.T) {
  63. etag := Etag("hello", 24)
  64. if len(etag) <= 0 {
  65. t.Fatal()
  66. }
  67. }
  68. var hashtags map[string]string = map[string]string{
  69. "#test": "#test",
  70. "test": "",
  71. "#test123": "#test123",
  72. "#123test123": "",
  73. "#test-test": "#test-test",
  74. "#test?": "#test",
  75. "hi #there": "#there",
  76. "#bug #idea": "#bug #idea",
  77. "#bug or #gif!": "#bug #gif",
  78. "#hüllo": "#hüllo",
  79. "#?test": "",
  80. "#-test": "",
  81. "#yo_yo": "#yo_yo",
  82. "(#brakets)": "#brakets",
  83. ")#stekarb(": "#stekarb",
  84. "<#less_than<": "#less_than",
  85. ">#greater_than>": "#greater_than",
  86. "-#minus-": "#minus",
  87. "+#plus+": "#plus",
  88. "=#equals=": "#equals",
  89. "%#pct%": "#pct",
  90. "&#and&": "#and",
  91. "^#hat^": "#hat",
  92. "##brown#": "#brown",
  93. "*#star*": "#star",
  94. "|#pipe|": "#pipe",
  95. ":#colon:": "#colon",
  96. ";#semi;": "#semi",
  97. }
  98. func TestParseHashtags(t *testing.T) {
  99. for input, output := range hashtags {
  100. if o, _ := ParseHashtags(input); o != output {
  101. t.Fatal("expected=" + output + " actual=" + o)
  102. }
  103. }
  104. }