utils_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2025 BackendServerTemplate Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package resource
  5. import "testing"
  6. func TestSemanticVersion(t *testing.T) {
  7. if !utilsIsSemanticVersion("0.0.0") {
  8. t.Errorf("SemanticVersion test failed: 0.0.0 (must be true, but return false)")
  9. }
  10. if !utilsIsSemanticVersion("1.0.0") {
  11. t.Errorf("SemanticVersion test failed: 1.0.0 (must be true, but return false)")
  12. }
  13. if !utilsIsSemanticVersion("1.2.3") {
  14. t.Errorf("SemanticVersion test failed: 1.2.3 (must be true, but return false)")
  15. }
  16. if !utilsIsSemanticVersion("1.0.0+dev") {
  17. t.Errorf("SemanticVersion test failed: 1.0.0+dev (must be true, but return false)")
  18. }
  19. if !utilsIsSemanticVersion("1.0.0+dev-123") {
  20. t.Errorf("SemanticVersion test failed: 1.0.0+dev-123 (must be true, but return false)")
  21. }
  22. if !utilsIsSemanticVersion("1.0.0+dev.123") {
  23. t.Errorf("SemanticVersion test failed: 1.0.0+dev-123 (must be true, but return false)")
  24. }
  25. if !utilsIsSemanticVersion("1.0.0+dev-123.abc") {
  26. t.Errorf("SemanticVersion test failed: 1.0.0+dev-123-456 (must be true, but return false)")
  27. }
  28. if !utilsIsSemanticVersion("1.0.0-123.456") {
  29. t.Errorf("SemanticVersion test failed: 1.0.0-123-456 (must be true, but return false)")
  30. }
  31. if !utilsIsSemanticVersion("1.0.0-123-456+dev") {
  32. t.Errorf("SemanticVersion test failed: 1.0.0-123-456+dev (must be true, but return false)")
  33. }
  34. if !utilsIsSemanticVersion("1.0.0-123-456+dev-127") {
  35. t.Errorf("SemanticVersion test failed: 1.0.0-123-456+dev-127 (must be true, but return false)")
  36. }
  37. if utilsIsSemanticVersion("v0.0.0") {
  38. t.Errorf("SemanticVersion test failed: v0.0.0 (must be false, but return true)")
  39. }
  40. if utilsIsSemanticVersion("1.0.0.0") {
  41. t.Errorf("SemanticVersion test failed: 1.0.0.0 (must be false, but return true)")
  42. }
  43. if utilsIsSemanticVersion("1.0.0-123+dev-234+prod") {
  44. t.Errorf("SemanticVersion test failed: 1.0.0-123+dev-234+prod (must be false, but return true)")
  45. }
  46. }
  47. func TestCheckAndRemoveBOM(t *testing.T) {
  48. hasBOM := string([]byte{0xEF, 0xBB, 0xBF}) + "Hello"
  49. noBOM := "Hello"
  50. if utilsCheckAndRemoveBOM(noBOM) != noBOM {
  51. t.Errorf("No BOM check error")
  52. }
  53. if utilsCheckAndRemoveBOM(hasBOM) == hasBOM {
  54. t.Errorf("Has BOM check error")
  55. }
  56. if utilsCheckAndRemoveBOM(hasBOM) != noBOM {
  57. t.Errorf("Has BOM remove error")
  58. }
  59. }
  60. func TestClenFileData(t *testing.T) {
  61. t.Run("Text-OnlyLine", func(t *testing.T) {
  62. text := "Hello"
  63. if utilsClenFileData(text) != "Hello" {
  64. t.Errorf("ClenFileData OnlyLine error")
  65. }
  66. })
  67. t.Run("Text-OnlyLine-WithSpace", func(t *testing.T) {
  68. text := "Hello "
  69. if utilsClenFileData(text) != "Hello" {
  70. t.Errorf("ClenFileData OnlyLine error")
  71. }
  72. })
  73. t.Run("Text-With-CRLF", func(t *testing.T) {
  74. text := "Hello\r\n"
  75. if utilsClenFileData(text) != "Hello" {
  76. t.Errorf("ClenFileData OnlyLine error")
  77. }
  78. })
  79. t.Run("Text-With-More-CRLF", func(t *testing.T) {
  80. text := "Hello\r\n\r\n\r\n"
  81. if utilsClenFileData(text) != "Hello" {
  82. t.Errorf("ClenFileData OnlyLine error")
  83. }
  84. })
  85. t.Run("Text-With-CRLF-WithSpace", func(t *testing.T) {
  86. text := "Hello \r\n"
  87. if utilsClenFileData(text) != "Hello" {
  88. t.Errorf("ClenFileData OnlyLine error")
  89. }
  90. })
  91. t.Run("Text-MoreLine", func(t *testing.T) {
  92. text := "Hello\r\nWorld"
  93. if utilsClenFileData(text) != "Hello" {
  94. t.Errorf("ClenFileData OnlyLine error")
  95. }
  96. })
  97. }
  98. func TestClenFileDataMoreLine(t *testing.T) {
  99. t.Run("Text-OnlyLine", func(t *testing.T) {
  100. text := "Hello"
  101. if utilsClenFileDataMoreLine(text) != "Hello" {
  102. t.Errorf("ClenFileData OnlyLine error")
  103. }
  104. })
  105. t.Run("Text-OnlyLine-WithSpace", func(t *testing.T) {
  106. text := " Hello "
  107. if utilsClenFileDataMoreLine(text) != "Hello" {
  108. t.Errorf("ClenFileData OnlyLine error")
  109. }
  110. })
  111. t.Run("Text-With-CRLF", func(t *testing.T) {
  112. text := "Hello\r\n"
  113. if utilsClenFileDataMoreLine(text) != "Hello" {
  114. t.Errorf("ClenFileData OnlyLine error")
  115. }
  116. })
  117. t.Run("Text-With-More-CRLF", func(t *testing.T) {
  118. text := "Hello\r\n\r\n\r\n"
  119. if utilsClenFileDataMoreLine(text) != "Hello" {
  120. t.Errorf("ClenFileData OnlyLine error")
  121. }
  122. })
  123. t.Run("Text-With-CRLF-WithSpace", func(t *testing.T) {
  124. text := "Hello \r\n"
  125. if utilsClenFileDataMoreLine(text) != "Hello" {
  126. t.Errorf("ClenFileData OnlyLine error")
  127. }
  128. })
  129. t.Run("Text-MoreLine", func(t *testing.T) {
  130. text := "Hello\r\nWorld"
  131. if utilsClenFileDataMoreLine(text) != "Hello\nWorld" {
  132. t.Errorf("ClenFileData OnlyLine error")
  133. }
  134. })
  135. }