functions_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 TestElapsedTime(t *testing.T) {
  80. printer := locale.NewPrinter("en_US")
  81. var dt = []struct {
  82. in time.Time
  83. out string
  84. }{
  85. {time.Time{}, printer.Printf("time_elapsed.not_yet")},
  86. {time.Now().Add(time.Hour), printer.Printf("time_elapsed.not_yet")},
  87. {time.Now(), printer.Printf("time_elapsed.now")},
  88. {time.Now().Add(-time.Minute), printer.Plural("time_elapsed.minutes", 1, 1)},
  89. {time.Now().Add(-time.Minute * 40), printer.Plural("time_elapsed.minutes", 40, 40)},
  90. {time.Now().Add(-time.Hour), printer.Plural("time_elapsed.hours", 1, 1)},
  91. {time.Now().Add(-time.Hour * 3), printer.Plural("time_elapsed.hours", 3, 3)},
  92. {time.Now().Add(-time.Hour * 32), printer.Printf("time_elapsed.yesterday")},
  93. {time.Now().Add(-time.Hour * 24 * 3), printer.Plural("time_elapsed.days", 3, 3)},
  94. {time.Now().Add(-time.Hour * 24 * 14), printer.Plural("time_elapsed.days", 14, 14)},
  95. {time.Now().Add(-time.Hour * 24 * 15), printer.Plural("time_elapsed.days", 15, 15)},
  96. {time.Now().Add(-time.Hour * 24 * 21), printer.Plural("time_elapsed.weeks", 3, 3)},
  97. {time.Now().Add(-time.Hour * 24 * 32), printer.Plural("time_elapsed.months", 1, 1)},
  98. {time.Now().Add(-time.Hour * 24 * 60), printer.Plural("time_elapsed.months", 2, 2)},
  99. {time.Now().Add(-time.Hour * 24 * 366), printer.Plural("time_elapsed.years", 1, 1)},
  100. {time.Now().Add(-time.Hour * 24 * 365 * 3), printer.Plural("time_elapsed.years", 3, 3)},
  101. }
  102. for i, tt := range dt {
  103. if out := elapsedTime(printer, "Local", tt.in); out != tt.out {
  104. t.Errorf(`%d. content mismatch for "%v": expected=%q got=%q`, i, tt.in, tt.out, out)
  105. }
  106. }
  107. }
  108. func TestFormatFileSize(t *testing.T) {
  109. scenarios := []struct {
  110. input int64
  111. expected string
  112. }{
  113. {500, "500 B"},
  114. {1024, "1.0 KiB"},
  115. {43520, "42.5 KiB"},
  116. {5000 * 1024 * 1024, "4.9 GiB"},
  117. }
  118. for _, scenario := range scenarios {
  119. result := formatFileSize(scenario.input)
  120. if result != scenario.expected {
  121. t.Errorf(`Unexpected result, got %q instead of %q for %d`, result, scenario.expected, scenario.input)
  122. }
  123. }
  124. }