util_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package util
  2. import (
  3. "github.com/stretchr/testify/require"
  4. "io/ioutil"
  5. "path/filepath"
  6. "testing"
  7. "time"
  8. )
  9. func TestDurationToHuman_SevenDays(t *testing.T) {
  10. d := 7 * 24 * time.Hour
  11. require.Equal(t, "7d", DurationToHuman(d))
  12. }
  13. func TestDurationToHuman_MoreThanOneDay(t *testing.T) {
  14. d := 49 * time.Hour
  15. require.Equal(t, "2d1h", DurationToHuman(d))
  16. }
  17. func TestDurationToHuman_LessThanOneDay(t *testing.T) {
  18. d := 17*time.Hour + 15*time.Minute
  19. require.Equal(t, "17h15m", DurationToHuman(d))
  20. }
  21. func TestDurationToHuman_TenOfThings(t *testing.T) {
  22. d := 10*time.Hour + 10*time.Minute + 10*time.Second
  23. require.Equal(t, "10h10m10s", DurationToHuman(d))
  24. }
  25. func TestDurationToHuman_Zero(t *testing.T) {
  26. require.Equal(t, "0", DurationToHuman(0))
  27. }
  28. func TestRandomString(t *testing.T) {
  29. s1 := RandomString(10)
  30. s2 := RandomString(10)
  31. s3 := RandomString(12)
  32. require.Equal(t, 10, len(s1))
  33. require.Equal(t, 10, len(s2))
  34. require.Equal(t, 12, len(s3))
  35. require.NotEqual(t, s1, s2)
  36. }
  37. func TestFileExists(t *testing.T) {
  38. filename := filepath.Join(t.TempDir(), "somefile.txt")
  39. require.Nil(t, ioutil.WriteFile(filename, []byte{0x25, 0x86}, 0600))
  40. require.True(t, FileExists(filename))
  41. require.False(t, FileExists(filename+".doesnotexist"))
  42. }
  43. func TestInStringList(t *testing.T) {
  44. s := []string{"one", "two"}
  45. require.True(t, InStringList(s, "two"))
  46. require.False(t, InStringList(s, "three"))
  47. }
  48. func TestInStringListAll(t *testing.T) {
  49. s := []string{"one", "two", "three", "four"}
  50. require.True(t, InStringListAll(s, []string{"two", "four"}))
  51. require.False(t, InStringListAll(s, []string{"three", "five"}))
  52. }
  53. func TestInIntList(t *testing.T) {
  54. s := []int{1, 2}
  55. require.True(t, InIntList(s, 2))
  56. require.False(t, InIntList(s, 3))
  57. }
  58. func TestSplitNoEmpty(t *testing.T) {
  59. require.Equal(t, []string{}, SplitNoEmpty("", ","))
  60. require.Equal(t, []string{}, SplitNoEmpty(",,,", ","))
  61. require.Equal(t, []string{"tag1", "tag2"}, SplitNoEmpty("tag1,tag2", ","))
  62. require.Equal(t, []string{"tag1", "tag2"}, SplitNoEmpty("tag1,tag2,", ","))
  63. }
  64. func TestParsePriority(t *testing.T) {
  65. priorities := []string{"", "1", "2", "3", "4", "5", "min", "LOW", " default ", "HIgh", "max", "urgent"}
  66. expected := []int{0, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 5}
  67. for i, priority := range priorities {
  68. actual, err := ParsePriority(priority)
  69. require.Nil(t, err)
  70. require.Equal(t, expected[i], actual)
  71. }
  72. }
  73. func TestParsePriority_Invalid(t *testing.T) {
  74. priorities := []string{"-1", "6", "aa", "-"}
  75. for _, priority := range priorities {
  76. _, err := ParsePriority(priority)
  77. require.Equal(t, errInvalidPriority, err)
  78. }
  79. }
  80. func TestPriorityString(t *testing.T) {
  81. priorities := []int{0, 1, 2, 3, 4, 5}
  82. expected := []string{"default", "min", "low", "default", "high", "max"}
  83. for i, priority := range priorities {
  84. actual, err := PriorityString(priority)
  85. require.Nil(t, err)
  86. require.Equal(t, expected[i], actual)
  87. }
  88. }
  89. func TestPriorityString_Invalid(t *testing.T) {
  90. _, err := PriorityString(99)
  91. require.Equal(t, err, errInvalidPriority)
  92. }
  93. func TestShortTopicURL(t *testing.T) {
  94. require.Equal(t, "ntfy.sh/mytopic", ShortTopicURL("https://ntfy.sh/mytopic"))
  95. require.Equal(t, "ntfy.sh/mytopic", ShortTopicURL("http://ntfy.sh/mytopic"))
  96. require.Equal(t, "lalala", ShortTopicURL("lalala"))
  97. }
  98. func TestParseSize_10GSuccess(t *testing.T) {
  99. s, err := ParseSize("10G")
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. require.Equal(t, int64(10*1024*1024*1024), s)
  104. }
  105. func TestParseSize_10MUpperCaseSuccess(t *testing.T) {
  106. s, err := ParseSize("10M")
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. require.Equal(t, int64(10*1024*1024), s)
  111. }
  112. func TestParseSize_10kLowerCaseSuccess(t *testing.T) {
  113. s, err := ParseSize("10k")
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. require.Equal(t, int64(10*1024), s)
  118. }
  119. func TestParseSize_FailureInvalid(t *testing.T) {
  120. _, err := ParseSize("not a size")
  121. if err == nil {
  122. t.Fatalf("expected error, but got none")
  123. }
  124. }
  125. func TestSplitKV(t *testing.T) {
  126. key, value := SplitKV(" key = value ", "=")
  127. require.Equal(t, "key", key)
  128. require.Equal(t, "value", value)
  129. key, value = SplitKV(" value ", "=")
  130. require.Equal(t, "", key)
  131. require.Equal(t, "value", value)
  132. key, value = SplitKV("mykey=value=with=separator ", "=")
  133. require.Equal(t, "mykey", key)
  134. require.Equal(t, "value=with=separator", value)
  135. }