utils_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. PathThemeStyles: "/assets/theme/styles.css",
  84. PathThemeScripts: "/assets/theme/scripts.js",
  85. })
  86. }
  87. func TestGetTmplError(t *testing.T) {
  88. Expect(t, GetTmplError(errors.New("some error")), consts.TmplError{
  89. ErrorMessage: "some error",
  90. })
  91. }
  92. func TestGetMd5(t *testing.T) {
  93. Expect(t, GetMd5("some string"), "5ac749fbeec93607fc28d666be85e73a")
  94. }
  95. func TestGetCurrentUnixTimestamp(t *testing.T) {
  96. Expect(t, GetCurrentUnixTimestamp(), int64(time.Now().Unix()))
  97. }
  98. func TestSystemRenderTemplate(t *testing.T) {
  99. //
  100. }
  101. func TestSystemErrorPageEngine(t *testing.T) {
  102. //
  103. }
  104. func TestSystemErrorPageTemplate(t *testing.T) {
  105. //
  106. }
  107. func TestSystemErrorPage404(t *testing.T) {
  108. //
  109. }
  110. func TestUrlToArray(t *testing.T) {
  111. a := UrlToArray("/some/url")
  112. Expect(t, len(a), 2)
  113. Expect(t, a[0], "some")
  114. Expect(t, a[1], "url")
  115. a = UrlToArray("/some/url/")
  116. Expect(t, len(a), 2)
  117. Expect(t, a[0], "some")
  118. Expect(t, a[1], "url")
  119. a = UrlToArray("/some/url?a=1&b=2")
  120. Expect(t, len(a), 2)
  121. Expect(t, a[0], "some")
  122. Expect(t, a[1], "url")
  123. a = UrlToArray("/some/url/?a=1&b=2")
  124. Expect(t, len(a), 2)
  125. Expect(t, a[0], "some")
  126. Expect(t, a[1], "url")
  127. }
  128. func TestIntToStr(t *testing.T) {
  129. Expect(t, IntToStr(2000), "2000")
  130. }
  131. func TestStrToInt(t *testing.T) {
  132. Expect(t, StrToInt("2000"), 2000)
  133. Expect(t, StrToInt("string"), 0)
  134. }
  135. func TestGenerateAlias(t *testing.T) {
  136. Expect(t, GenerateAlias(""), "")
  137. Expect(t, GenerateAlias("Some page name"), "/some-page-name/")
  138. Expect(t, GenerateAlias("Some page name 2"), "/some-page-name-2/")
  139. Expect(t, GenerateAlias("Какая то страница"), "/kakaya-to-stranica/")
  140. Expect(t, GenerateAlias("Какая то страница 2"), "/kakaya-to-stranica-2/")
  141. }
  142. func TestUnixTimestampToMySqlDateTime(t *testing.T) {
  143. Expect(t, UnixTimestampToMySqlDateTime(1551741275), "2019-03-05 01:14:35")
  144. }
  145. func TestUnixTimestampToFormat(t *testing.T) {
  146. Expect(t, UnixTimestampToFormat(1551741275, "2006/01/02 15:04"), "2019/03/05 01:14")
  147. }
  148. func TestExtractGetParams(t *testing.T) {
  149. Expect(t, ExtractGetParams("/some-url"), "")
  150. Expect(t, ExtractGetParams("/some-url/"), "")
  151. Expect(t, ExtractGetParams("/some-url?a=1&b=2"), "?a=1&b=2")
  152. Expect(t, ExtractGetParams("/some-url/?a=1&b=2"), "?a=1&b=2")
  153. }
  154. func TestJavaScriptVarValue(t *testing.T) {
  155. Expect(t, JavaScriptVarValue(`It's "string"`), "It’s ”string”")
  156. Expect(t, JavaScriptVarValue(`It is string`), "It is string")
  157. }