util_test.go 4.5 KB

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