utils_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package utils
  2. import (
  3. "errors"
  4. "testing"
  5. "time"
  6. "golang-fave/consts"
  7. )
  8. func Expect(t *testing.T, actual, expect interface{}) {
  9. if actual != expect {
  10. t.Fatalf("\033[0;33mExpected \033[0;32m`%v`\033[0;33m but got \033[0;31m`%v`\033[0m",
  11. expect, actual)
  12. }
  13. }
  14. func TestIsFileExists(t *testing.T) {
  15. Expect(t, IsFileExists("./../testdata/some-file.txt"), true)
  16. Expect(t, IsFileExists("./../testdata/no-existed-file"), false)
  17. }
  18. func TestIsDir(t *testing.T) {
  19. Expect(t, IsDir("./../testdata"), true)
  20. Expect(t, IsDir("./../testdata/some-file.txt"), false)
  21. Expect(t, IsDir("./../testdata/no-existed-dir"), false)
  22. }
  23. func TestIsDirExists(t *testing.T) {
  24. Expect(t, IsDirExists("./../testdata"), true)
  25. Expect(t, IsDirExists("./../testdata/some-file.txt"), false)
  26. Expect(t, IsDirExists("./../testdata/no-existed-dir"), false)
  27. }
  28. func TestIsNumeric(t *testing.T) {
  29. Expect(t, IsNumeric("12345"), true)
  30. Expect(t, IsNumeric("string"), false)
  31. }
  32. func TestIsValidEmail(t *testing.T) {
  33. Expect(t, IsValidEmail("test@gmail.com"), true)
  34. Expect(t, IsValidEmail("test@yandex.ru"), true)
  35. Expect(t, IsValidEmail("test@ya.ru"), true)
  36. Expect(t, IsValidEmail("test@test"), false)
  37. }
  38. func TestIsValidAlias(t *testing.T) {
  39. Expect(t, IsValidAlias("/"), true)
  40. Expect(t, IsValidAlias("/some-page/"), true)
  41. Expect(t, IsValidAlias("/some-page.html"), true)
  42. Expect(t, IsValidAlias("/some-page.html/"), true)
  43. Expect(t, IsValidAlias(""), false)
  44. Expect(t, IsValidAlias("some-page"), false)
  45. Expect(t, IsValidAlias("/some page/"), false)
  46. }
  47. func TestFixPath(t *testing.T) {
  48. Expect(t, FixPath(""), "")
  49. Expect(t, FixPath("/"), "")
  50. Expect(t, FixPath("./dir"), "./dir")
  51. Expect(t, FixPath("./dir/"), "./dir")
  52. Expect(t, FixPath("\\dir"), "\\dir")
  53. Expect(t, FixPath("\\dir\\"), "\\dir")
  54. }
  55. func TestExtractHostPort(t *testing.T) {
  56. h, p := ExtractHostPort("localhost:8080", false)
  57. Expect(t, h, "localhost")
  58. Expect(t, p, "8080")
  59. h, p = ExtractHostPort("localhost:80", false)
  60. Expect(t, h, "localhost")
  61. Expect(t, p, "80")
  62. h, p = ExtractHostPort("localhost", false)
  63. Expect(t, h, "localhost")
  64. Expect(t, p, "80")
  65. h, p = ExtractHostPort("localhost", true)
  66. Expect(t, h, "localhost")
  67. Expect(t, p, "443")
  68. }
  69. func TestGetAssetsUrl(t *testing.T) {
  70. Expect(t, GetAssetsUrl("style.css"), "/style.css?v="+consts.AssetsVersion)
  71. }
  72. func TestGetTmplSystemData(t *testing.T) {
  73. Expect(t, GetTmplSystemData(), consts.TmplSystem{
  74. PathIcoFav: "/assets/sys/fave.ico?v=" + consts.AssetsVersion,
  75. PathSvgLogo: "/assets/sys/logo.svg?v=" + consts.AssetsVersion,
  76. PathCssStyles: "/assets/sys/styles.css?v=" + consts.AssetsVersion,
  77. PathCssCpStyles: "/assets/cp/styles.css?v=" + consts.AssetsVersion,
  78. PathCssBootstrap: "/assets/bootstrap.css?v=" + consts.AssetsVersion,
  79. PathJsJquery: "/assets/jquery.js?v=" + consts.AssetsVersion,
  80. PathJsPopper: "/assets/popper.js?v=" + consts.AssetsVersion,
  81. PathJsBootstrap: "/assets/bootstrap.js?v=" + consts.AssetsVersion,
  82. PathJsCpScripts: "/assets/cp/scripts.js?v=" + consts.AssetsVersion,
  83. })
  84. }
  85. func TestGetTmplError(t *testing.T) {
  86. Expect(t, GetTmplError(errors.New("some error")), consts.TmplError{
  87. ErrorMessage: "some error",
  88. })
  89. }
  90. func TestGetMd5(t *testing.T) {
  91. Expect(t, GetMd5("some string"), "5ac749fbeec93607fc28d666be85e73a")
  92. }
  93. func TestGetCurrentUnixTimestamp(t *testing.T) {
  94. Expect(t, GetCurrentUnixTimestamp(), int64(time.Now().Unix()))
  95. }
  96. func TestSystemRenderTemplate(t *testing.T) {
  97. //
  98. }
  99. func TestSystemErrorPageEngine(t *testing.T) {
  100. //
  101. }
  102. func TestSystemErrorPageTemplate(t *testing.T) {
  103. //
  104. }
  105. func TestSystemErrorPage404(t *testing.T) {
  106. //
  107. }
  108. func TestUrlToArray(t *testing.T) {
  109. a := UrlToArray("/some/url")
  110. Expect(t, len(a), 2)
  111. Expect(t, a[0], "some")
  112. Expect(t, a[1], "url")
  113. a = UrlToArray("/some/url/")
  114. Expect(t, len(a), 2)
  115. Expect(t, a[0], "some")
  116. Expect(t, a[1], "url")
  117. a = UrlToArray("/some/url?a=1&b=2")
  118. Expect(t, len(a), 2)
  119. Expect(t, a[0], "some")
  120. Expect(t, a[1], "url")
  121. a = UrlToArray("/some/url/?a=1&b=2")
  122. Expect(t, len(a), 2)
  123. Expect(t, a[0], "some")
  124. Expect(t, a[1], "url")
  125. }
  126. func TestIntToStr(t *testing.T) {
  127. Expect(t, IntToStr(2000), "2000")
  128. }
  129. func TestStrToInt(t *testing.T) {
  130. Expect(t, StrToInt("2000"), 2000)
  131. Expect(t, StrToInt("string"), 0)
  132. }
  133. func TestGenerateAlias(t *testing.T) {
  134. Expect(t, GenerateAlias(""), "")
  135. Expect(t, GenerateAlias("Some page name"), "/some-page-name/")
  136. Expect(t, GenerateAlias("Some page name 2"), "/some-page-name-2/")
  137. Expect(t, GenerateAlias("Какая то страница"), "/kakaya-to-stranica/")
  138. Expect(t, GenerateAlias("Какая то страница 2"), "/kakaya-to-stranica-2/")
  139. }
  140. func TestUnixTimestampToMySqlDateTime(t *testing.T) {
  141. Expect(t, UnixTimestampToMySqlDateTime(1551741275), "2019-03-05 01:14:35")
  142. }
  143. func TestUnixTimestampToFormat(t *testing.T) {
  144. Expect(t, UnixTimestampToFormat(1551741275, "2006/01/02 15:04"), "2019/03/05 01:14")
  145. }
  146. func TestExtractGetParams(t *testing.T) {
  147. Expect(t, ExtractGetParams("/some-url"), "")
  148. Expect(t, ExtractGetParams("/some-url/"), "")
  149. Expect(t, ExtractGetParams("/some-url?a=1&b=2"), "?a=1&b=2")
  150. Expect(t, ExtractGetParams("/some-url/?a=1&b=2"), "?a=1&b=2")
  151. }
  152. func TestJavaScriptVarValue(t *testing.T) {
  153. Expect(t, JavaScriptVarValue(`It's "string"`), "It’s ”string”")
  154. Expect(t, JavaScriptVarValue(`It is string`), "It is string")
  155. }