functions_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. )
  8. func TestTruncate(t *testing.T) {
  9. fm := funcMap{}
  10. if f, ok := fm.Map()["truncate"]; ok {
  11. if truncate := f.(func(str string, max int) string); ok {
  12. shortEnglishText := "Short text"
  13. shortUnicodeText := "Короткий текст"
  14. // edge case
  15. if truncate(shortEnglishText, len(shortEnglishText)) != shortEnglishText {
  16. t.Fatal("Invalid truncation")
  17. }
  18. // real case
  19. if truncate(shortEnglishText, 25) != shortEnglishText {
  20. t.Fatal("Invalid truncation")
  21. }
  22. if truncate(shortUnicodeText, len(shortUnicodeText)) != shortUnicodeText {
  23. t.Fatal("Invalid truncation")
  24. }
  25. if truncate(shortUnicodeText, 25) != shortUnicodeText {
  26. t.Fatal("Invalid truncation")
  27. }
  28. longEnglishText := "This is really pretty long English text"
  29. longRussianText := "Это реально очень длинный русский текст"
  30. if truncate(longEnglishText, 25) != "This is really pretty lon…" {
  31. t.Fatal("Invalid truncation")
  32. }
  33. if truncate(longRussianText, 25) != "Это реально очень длинный…" {
  34. t.Fatal("Invalid truncation")
  35. }
  36. } else {
  37. t.Fatal("Type assetion for this func is failed, check func, maybe it was changed")
  38. }
  39. } else {
  40. t.Fatal("There is no such function in this map, check key, maybe it was changed")
  41. }
  42. }