functions_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 TestTruncateWithShortTexts(t *testing.T) {
  38. scenarios := []string{"Short text", "Короткий текст"}
  39. for _, input := range scenarios {
  40. result := truncate(input, 25)
  41. if result != input {
  42. t.Fatalf(`Unexpected output, got %q instead of %q`, result, input)
  43. }
  44. result = truncate(input, len(input))
  45. if result != input {
  46. t.Fatalf(`Unexpected output, got %q instead of %q`, result, input)
  47. }
  48. }
  49. }
  50. func TestTruncateWithLongTexts(t *testing.T) {
  51. scenarios := map[string]string{
  52. "This is a really pretty long English text": "This is a really pretty l…",
  53. "Это реально очень длинный русский текст": "Это реально очень длинный…",
  54. }
  55. for input, expected := range scenarios {
  56. result := truncate(input, 25)
  57. if result != expected {
  58. t.Fatalf(`Unexpected output, got %q instead of %q`, result, expected)
  59. }
  60. }
  61. }
  62. func TestIsEmail(t *testing.T) {
  63. if !isEmail("user@domain.tld") {
  64. t.Fatal(`This email is valid and should returns true`)
  65. }
  66. if isEmail("invalid") {
  67. t.Fatal(`This email is not valid and should returns false`)
  68. }
  69. }
  70. func TestDuration(t *testing.T) {
  71. now := time.Now()
  72. var dt = []struct {
  73. in time.Time
  74. out string
  75. }{
  76. {time.Time{}, ""},
  77. {now.Add(time.Hour), "1h0m0s"},
  78. {now.Add(time.Minute), "1m0s"},
  79. {now.Add(time.Minute * 40), "40m0s"},
  80. {now.Add(time.Millisecond * 40), "0s"},
  81. {now.Add(time.Millisecond * 80), "0s"},
  82. {now.Add(time.Millisecond * 400), "0s"},
  83. {now.Add(time.Millisecond * 800), "1s"},
  84. {now.Add(time.Millisecond * 4321), "4s"},
  85. {now.Add(time.Millisecond * 8765), "9s"},
  86. {now.Add(time.Microsecond * 12345678), "12s"},
  87. {now.Add(time.Microsecond * 87654321), "1m28s"},
  88. }
  89. for i, tt := range dt {
  90. if out := durationImpl(tt.in, now); out != tt.out {
  91. t.Errorf(`%d. content mismatch for "%v": expected=%q got=%q`, i, tt.in, tt.out, out)
  92. }
  93. }
  94. }
  95. func TestElapsedTime(t *testing.T) {
  96. printer := locale.NewPrinter("en_US")
  97. var dt = []struct {
  98. in time.Time
  99. out string
  100. }{
  101. {time.Time{}, printer.Print("time_elapsed.not_yet")},
  102. {time.Now().Add(time.Hour), printer.Print("time_elapsed.not_yet")},
  103. {time.Now(), printer.Print("time_elapsed.now")},
  104. {time.Now().Add(-time.Minute), printer.Plural("time_elapsed.minutes", 1, 1)},
  105. {time.Now().Add(-time.Minute * 40), printer.Plural("time_elapsed.minutes", 40, 40)},
  106. {time.Now().Add(-time.Hour), printer.Plural("time_elapsed.hours", 1, 1)},
  107. {time.Now().Add(-time.Hour * 3), printer.Plural("time_elapsed.hours", 3, 3)},
  108. {time.Now().Add(-time.Hour * 32), printer.Print("time_elapsed.yesterday")},
  109. {time.Now().Add(-time.Hour * 24 * 3), printer.Plural("time_elapsed.days", 3, 3)},
  110. {time.Now().Add(-time.Hour * 24 * 14), printer.Plural("time_elapsed.days", 14, 14)},
  111. {time.Now().Add(-time.Hour * 24 * 15), printer.Plural("time_elapsed.days", 15, 15)},
  112. {time.Now().Add(-time.Hour * 24 * 21), printer.Plural("time_elapsed.weeks", 3, 3)},
  113. {time.Now().Add(-time.Hour * 24 * 32), printer.Plural("time_elapsed.months", 1, 1)},
  114. {time.Now().Add(-time.Hour * 24 * 60), printer.Plural("time_elapsed.months", 2, 2)},
  115. {time.Now().Add(-time.Hour * 24 * 366), printer.Plural("time_elapsed.years", 1, 1)},
  116. {time.Now().Add(-time.Hour * 24 * 365 * 3), printer.Plural("time_elapsed.years", 3, 3)},
  117. }
  118. for i, tt := range dt {
  119. if out := elapsedTime(printer, "Local", tt.in); out != tt.out {
  120. t.Errorf(`%d. content mismatch for "%v": expected=%q got=%q`, i, tt.in, tt.out, out)
  121. }
  122. }
  123. }
  124. func TestFormatFileSize(t *testing.T) {
  125. scenarios := []struct {
  126. input int64
  127. expected string
  128. }{
  129. {0, "0 B"},
  130. {1, "1 B"},
  131. {500, "500 B"},
  132. {1024, "1.0 KiB"},
  133. {43520, "42.5 KiB"},
  134. {5000 * 1024 * 1024, "4.9 GiB"},
  135. }
  136. for _, scenario := range scenarios {
  137. result := formatFileSize(scenario.input)
  138. if result != scenario.expected {
  139. t.Errorf(`Unexpected result, got %q instead of %q for %d`, result, scenario.expected, scenario.input)
  140. }
  141. }
  142. }