functions_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package template // import "miniflux.app/v2/internal/template"
  4. import (
  5. "testing"
  6. "time"
  7. "miniflux.app/v2/internal/locale"
  8. )
  9. func TestDict(t *testing.T) {
  10. d, err := dict("k1", "v1", "k2", "v2")
  11. if err != nil {
  12. t.Fatalf(`The dict should be valid: %v`, err)
  13. }
  14. if value, found := d["k1"]; found {
  15. if value != "v1" {
  16. t.Fatalf(`Unexpected value for k1: got %q`, value)
  17. }
  18. }
  19. if value, found := d["k2"]; found {
  20. if value != "v2" {
  21. t.Fatalf(`Unexpected value for k2: got %q`, value)
  22. }
  23. }
  24. }
  25. func TestDictWithInvalidNumberOfArguments(t *testing.T) {
  26. _, err := dict("k1")
  27. if err == nil {
  28. t.Fatal(`An error should be returned if the number of arguments are not even`)
  29. }
  30. }
  31. func TestDictWithInvalidMap(t *testing.T) {
  32. _, err := dict(1, 2)
  33. if err == nil {
  34. t.Fatal(`An error should be returned if the dict keys are not string`)
  35. }
  36. }
  37. func TestHasKey(t *testing.T) {
  38. input := map[string]string{"k": "v"}
  39. if !hasKey(input, "k") {
  40. t.Fatal(`This key exists in the map and should returns true`)
  41. }
  42. if hasKey(input, "missing") {
  43. t.Fatal(`This key doesn't exists in the given map and should returns false`)
  44. }
  45. }
  46. func TestTruncateWithShortTexts(t *testing.T) {
  47. scenarios := []string{"Short text", "Короткий текст"}
  48. for _, input := range scenarios {
  49. result := truncate(input, 25)
  50. if result != input {
  51. t.Fatalf(`Unexpected output, got %q instead of %q`, result, input)
  52. }
  53. result = truncate(input, len(input))
  54. if result != input {
  55. t.Fatalf(`Unexpected output, got %q instead of %q`, result, input)
  56. }
  57. }
  58. }
  59. func TestTruncateWithLongTexts(t *testing.T) {
  60. scenarios := map[string]string{
  61. "This is a really pretty long English text": "This is a really pretty l…",
  62. "Это реально очень длинный русский текст": "Это реально очень длинный…",
  63. }
  64. for input, expected := range scenarios {
  65. result := truncate(input, 25)
  66. if result != expected {
  67. t.Fatalf(`Unexpected output, got %q instead of %q`, result, expected)
  68. }
  69. }
  70. }
  71. func TestIsEmail(t *testing.T) {
  72. if !isEmail("user@domain.tld") {
  73. t.Fatal(`This email is valid and should returns true`)
  74. }
  75. if isEmail("invalid") {
  76. t.Fatal(`This email is not valid and should returns false`)
  77. }
  78. }
  79. func TestDuration(t *testing.T) {
  80. now := time.Now()
  81. var dt = []struct {
  82. in time.Time
  83. out string
  84. }{
  85. {time.Time{}, ""},
  86. {now.Add(time.Hour), "1h0m0s"},
  87. {now.Add(time.Minute), "1m0s"},
  88. {now.Add(time.Minute * 40), "40m0s"},
  89. {now.Add(time.Millisecond * 40), "0s"},
  90. {now.Add(time.Millisecond * 80), "0s"},
  91. {now.Add(time.Millisecond * 400), "0s"},
  92. {now.Add(time.Millisecond * 800), "1s"},
  93. {now.Add(time.Millisecond * 4321), "4s"},
  94. {now.Add(time.Millisecond * 8765), "9s"},
  95. {now.Add(time.Microsecond * 12345678), "12s"},
  96. {now.Add(time.Microsecond * 87654321), "1m28s"},
  97. }
  98. for i, tt := range dt {
  99. if out := durationImpl(tt.in, now); out != tt.out {
  100. t.Errorf(`%d. content mismatch for "%v": expected=%q got=%q`, i, tt.in, tt.out, out)
  101. }
  102. }
  103. }
  104. func TestElapsedTime(t *testing.T) {
  105. printer := locale.NewPrinter("en_US")
  106. var dt = []struct {
  107. in time.Time
  108. out string
  109. }{
  110. {time.Time{}, printer.Print("time_elapsed.not_yet")},
  111. {time.Now().Add(time.Hour), printer.Print("time_elapsed.not_yet")},
  112. {time.Now(), printer.Print("time_elapsed.now")},
  113. {time.Now().Add(-time.Minute), printer.Plural("time_elapsed.minutes", 1, 1)},
  114. {time.Now().Add(-time.Minute * 40), printer.Plural("time_elapsed.minutes", 40, 40)},
  115. {time.Now().Add(-time.Hour), printer.Plural("time_elapsed.hours", 1, 1)},
  116. {time.Now().Add(-time.Hour * 3), printer.Plural("time_elapsed.hours", 3, 3)},
  117. {time.Now().Add(-time.Hour * 32), printer.Print("time_elapsed.yesterday")},
  118. {time.Now().Add(-time.Hour * 24 * 3), printer.Plural("time_elapsed.days", 3, 3)},
  119. {time.Now().Add(-time.Hour * 24 * 14), printer.Plural("time_elapsed.days", 14, 14)},
  120. {time.Now().Add(-time.Hour * 24 * 15), printer.Plural("time_elapsed.days", 15, 15)},
  121. {time.Now().Add(-time.Hour * 24 * 21), printer.Plural("time_elapsed.weeks", 3, 3)},
  122. {time.Now().Add(-time.Hour * 24 * 32), printer.Plural("time_elapsed.months", 1, 1)},
  123. {time.Now().Add(-time.Hour * 24 * 60), printer.Plural("time_elapsed.months", 2, 2)},
  124. {time.Now().Add(-time.Hour * 24 * 366), printer.Plural("time_elapsed.years", 1, 1)},
  125. {time.Now().Add(-time.Hour * 24 * 365 * 3), printer.Plural("time_elapsed.years", 3, 3)},
  126. }
  127. for i, tt := range dt {
  128. if out := elapsedTime(printer, "Local", tt.in); out != tt.out {
  129. t.Errorf(`%d. content mismatch for "%v": expected=%q got=%q`, i, tt.in, tt.out, out)
  130. }
  131. }
  132. }
  133. func TestFormatFileSize(t *testing.T) {
  134. scenarios := []struct {
  135. input int64
  136. expected string
  137. }{
  138. {0, "0 B"},
  139. {1, "1 B"},
  140. {500, "500 B"},
  141. {1024, "1.0 KiB"},
  142. {43520, "42.5 KiB"},
  143. {5000 * 1024 * 1024, "4.9 GiB"},
  144. }
  145. for _, scenario := range scenarios {
  146. result := formatFileSize(scenario.input)
  147. if result != scenario.expected {
  148. t.Errorf(`Unexpected result, got %q instead of %q for %d`, result, scenario.expected, scenario.input)
  149. }
  150. }
  151. }