utils_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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("./../support/some-file.txt"), true)
  16. Expect(t, IsFileExists("./../support/no-existed-file"), false)
  17. }
  18. func TestIsDir(t *testing.T) {
  19. Expect(t, IsDir("./../support"), true)
  20. Expect(t, IsDir("./../support/some-file.txt"), false)
  21. Expect(t, IsDir("./../support/no-existed-dir"), false)
  22. }
  23. func TestIsDirExists(t *testing.T) {
  24. Expect(t, IsDirExists("./../support"), true)
  25. Expect(t, IsDirExists("./../support/some-file.txt"), false)
  26. Expect(t, IsDirExists("./../support/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 TestIsValidSingleAlias(t *testing.T) {
  48. Expect(t, IsValidSingleAlias("some-category"), true)
  49. Expect(t, IsValidSingleAlias("some-category-12345"), true)
  50. Expect(t, IsValidSingleAlias("some_category_12345"), true)
  51. Expect(t, IsValidSingleAlias(""), false)
  52. Expect(t, IsValidSingleAlias("/"), false)
  53. Expect(t, IsValidSingleAlias("/some-category/"), false)
  54. Expect(t, IsValidSingleAlias("some-category.html"), false)
  55. Expect(t, IsValidSingleAlias("some category"), false)
  56. }
  57. func TestFixPath(t *testing.T) {
  58. Expect(t, FixPath(""), "")
  59. Expect(t, FixPath("/"), "")
  60. Expect(t, FixPath("./dir"), "./dir")
  61. Expect(t, FixPath("./dir/"), "./dir")
  62. Expect(t, FixPath("\\dir"), "\\dir")
  63. Expect(t, FixPath("\\dir\\"), "\\dir")
  64. }
  65. func TestExtractHostPort(t *testing.T) {
  66. h, p := ExtractHostPort("localhost:8080", false)
  67. Expect(t, h, "localhost")
  68. Expect(t, p, "8080")
  69. h, p = ExtractHostPort("localhost:80", false)
  70. Expect(t, h, "localhost")
  71. Expect(t, p, "80")
  72. h, p = ExtractHostPort("localhost", false)
  73. Expect(t, h, "localhost")
  74. Expect(t, p, "80")
  75. h, p = ExtractHostPort("localhost", true)
  76. Expect(t, h, "localhost")
  77. Expect(t, p, "443")
  78. }
  79. func TestGetAssetsUrl(t *testing.T) {
  80. Expect(t, GetAssetsUrl("style.css"), "/style.css?v="+consts.AssetsVersion)
  81. }
  82. func TestGetTmplSystemData(t *testing.T) {
  83. Expect(t, GetTmplSystemData(), consts.TmplSystem{
  84. PathIcoFav: "/assets/sys/fave.ico?v=" + consts.AssetsVersion,
  85. PathSvgLogo: "/assets/sys/logo.svg?v=" + consts.AssetsVersion,
  86. PathCssStyles: "/assets/sys/styles.css?v=" + consts.AssetsVersion,
  87. PathCssCpStyles: "/assets/cp/styles.css?v=" + consts.AssetsVersion,
  88. PathCssBootstrap: "/assets/bootstrap.css?v=" + consts.AssetsVersion,
  89. PathJsJquery: "/assets/jquery.js?v=" + consts.AssetsVersion,
  90. PathJsPopper: "/assets/popper.js?v=" + consts.AssetsVersion,
  91. PathJsBootstrap: "/assets/bootstrap.js?v=" + consts.AssetsVersion,
  92. PathJsCpScripts: "/assets/cp/scripts.js?v=" + consts.AssetsVersion,
  93. PathThemeStyles: "/assets/theme/styles.css",
  94. PathThemeScripts: "/assets/theme/scripts.js",
  95. InfoVersion: consts.ServerVersion,
  96. })
  97. }
  98. func TestGetTmplError(t *testing.T) {
  99. Expect(t, GetTmplError(errors.New("some error")), consts.TmplError{
  100. ErrorMessage: "some error",
  101. })
  102. }
  103. func TestGetMd5(t *testing.T) {
  104. Expect(t, GetMd5("some string"), "5ac749fbeec93607fc28d666be85e73a")
  105. }
  106. func TestGetCurrentUnixTimestamp(t *testing.T) {
  107. Expect(t, GetCurrentUnixTimestamp(), int64(time.Now().Unix()))
  108. }
  109. func TestSystemRenderTemplate(t *testing.T) {
  110. //
  111. }
  112. func TestSystemErrorPageEngine(t *testing.T) {
  113. //
  114. }
  115. func TestSystemErrorPageTemplate(t *testing.T) {
  116. //
  117. }
  118. func TestSystemErrorPage404(t *testing.T) {
  119. //
  120. }
  121. func TestUrlToArray(t *testing.T) {
  122. a := UrlToArray("/some/url")
  123. Expect(t, len(a), 2)
  124. Expect(t, a[0], "some")
  125. Expect(t, a[1], "url")
  126. a = UrlToArray("/some/url/")
  127. Expect(t, len(a), 2)
  128. Expect(t, a[0], "some")
  129. Expect(t, a[1], "url")
  130. a = UrlToArray("/some/url?a=1&b=2")
  131. Expect(t, len(a), 2)
  132. Expect(t, a[0], "some")
  133. Expect(t, a[1], "url")
  134. a = UrlToArray("/some/url/?a=1&b=2")
  135. Expect(t, len(a), 2)
  136. Expect(t, a[0], "some")
  137. Expect(t, a[1], "url")
  138. }
  139. func TestIntToStr(t *testing.T) {
  140. Expect(t, IntToStr(2000), "2000")
  141. }
  142. func TestStrToInt(t *testing.T) {
  143. Expect(t, StrToInt("2000"), 2000)
  144. Expect(t, StrToInt("string"), 0)
  145. }
  146. func TestGenerateAlias(t *testing.T) {
  147. Expect(t, GenerateAlias(""), "")
  148. Expect(t, GenerateAlias("Some page name"), "/some-page-name/")
  149. Expect(t, GenerateAlias("Some page name 2"), "/some-page-name-2/")
  150. Expect(t, GenerateAlias("Какая-то страница"), "/kakayato-stranica/")
  151. Expect(t, GenerateAlias("Какая-то страница 2"), "/kakayato-stranica-2/")
  152. }
  153. func TestGenerateSingleAlias(t *testing.T) {
  154. Expect(t, GenerateSingleAlias(""), "")
  155. Expect(t, GenerateSingleAlias("Some category name"), "some-category-name")
  156. Expect(t, GenerateSingleAlias("Some category name 2"), "some-category-name-2")
  157. Expect(t, GenerateSingleAlias("Какая-то категория"), "kakayato-kategoriya")
  158. Expect(t, GenerateSingleAlias("Какая-то категория 2"), "kakayato-kategoriya-2")
  159. }
  160. func TestUnixTimestampToMySqlDateTime(t *testing.T) {
  161. Expect(t, UnixTimestampToMySqlDateTime(1551741275), "2019-03-05 01:14:35")
  162. }
  163. func TestUnixTimestampToFormat(t *testing.T) {
  164. Expect(t, UnixTimestampToFormat(1551741275, "2006/01/02 15:04"), "2019/03/05 01:14")
  165. }
  166. func TestExtractGetParams(t *testing.T) {
  167. Expect(t, ExtractGetParams("/some-url"), "")
  168. Expect(t, ExtractGetParams("/some-url/"), "")
  169. Expect(t, ExtractGetParams("/some-url?a=1&b=2"), "?a=1&b=2")
  170. Expect(t, ExtractGetParams("/some-url/?a=1&b=2"), "?a=1&b=2")
  171. }
  172. func TestJavaScriptVarValue(t *testing.T) {
  173. Expect(t, JavaScriptVarValue(`It's "string"`), "It’s ”string”")
  174. Expect(t, JavaScriptVarValue(`It is string`), "It is string")
  175. }