You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.6KB

  1. /*
  2. * Copyright (c) 2018 DeineAgentur UG https://www.deineagentur.com. All rights reserved.
  3. * Licensed under the MIT License. See LICENSE file in the project root for full license information.
  4. */
  5. package gotext
  6. import (
  7. "reflect"
  8. "testing"
  9. )
  10. func TestSimplifiedLocale(t *testing.T) {
  11. tr :=SimplifiedLocale("de_DE@euro")
  12. if tr != "de_DE" {
  13. t.Errorf("Expected 'de_DE' but got '%s'", tr)
  14. }
  15. tr =SimplifiedLocale("de_DE.UTF-8")
  16. if tr != "de_DE" {
  17. t.Errorf("Expected 'de_DE' but got '%s'", tr)
  18. }
  19. tr =SimplifiedLocale("de_DE:latin1")
  20. if tr != "de_DE" {
  21. t.Errorf("Expected 'de_DE' but got '%s'", tr)
  22. }
  23. }
  24. func TestReformattingSingleNamedPattern(t *testing.T) {
  25. pat := "%(name_me)x"
  26. f, n := reformatSprintf(pat)
  27. if f != "%x" {
  28. t.Errorf("pattern should be %%x but %v", f)
  29. }
  30. if !reflect.DeepEqual(n, []string{"name_me"}) {
  31. t.Errorf("named var should be {name_me} but %v", n)
  32. }
  33. }
  34. func TestReformattingMultipleNamedPattern(t *testing.T) {
  35. pat := "%(name_me)x and %(another_name)v"
  36. f, n := reformatSprintf(pat)
  37. if f != "%x and %v" {
  38. t.Errorf("pattern should be %%x and %%v but %v", f)
  39. }
  40. if !reflect.DeepEqual(n, []string{"name_me", "another_name"}) {
  41. t.Errorf("named var should be {name_me, another_name} but %v", n)
  42. }
  43. }
  44. func TestReformattingRepeatedNamedPattern(t *testing.T) {
  45. pat := "%(name_me)x and %(another_name)v and %(name_me)v"
  46. f, n := reformatSprintf(pat)
  47. if f != "%x and %v and %v" {
  48. t.Errorf("pattern should be %%x and %%v and %%v but %v", f)
  49. }
  50. if !reflect.DeepEqual(n, []string{"name_me", "another_name", "name_me"}) {
  51. t.Errorf("named var should be {name_me, another_name, name_me} but %v", n)
  52. }
  53. }
  54. func TestSprintf(t *testing.T) {
  55. pat := "%(brother)s loves %(sister)s. %(sister)s also loves %(brother)s."
  56. params := map[string]interface{}{
  57. "sister": "Susan",
  58. "brother": "Louis",
  59. }
  60. s := Sprintf(pat, params)
  61. if s != "Louis loves Susan. Susan also loves Louis." {
  62. t.Errorf("result should be Louis loves Susan. Susan also love Louis. but %v", s)
  63. }
  64. }
  65. func TestNPrintf(t *testing.T) {
  66. pat := "%(brother)s loves %(sister)s. %(sister)s also loves %(brother)s.\n"
  67. params := map[string]interface{}{
  68. "sister": "Susan",
  69. "brother": "Louis",
  70. }
  71. NPrintf(pat, params)
  72. }
  73. func TestSprintfFloatsWithPrecision(t *testing.T) {
  74. pat := "%(float)f / %(floatprecision).1f / %(long)g / %(longprecision).3g"
  75. params := map[string]interface{}{
  76. "float": 5.034560,
  77. "floatprecision": 5.03456,
  78. "long": 5.03456,
  79. "longprecision": 5.03456,
  80. }
  81. s := Sprintf(pat, params)
  82. expectedresult := "5.034560 / 5.0 / 5.03456 / 5.03"
  83. if s != expectedresult {
  84. t.Errorf("result should be (%v) but is (%v)", expectedresult, s)
  85. }
  86. }