functions_test.go 4.3 KB

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