import_export_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. package ut
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "testing"
  6. "os"
  7. "github.com/go-playground/locales"
  8. "github.com/go-playground/locales/en"
  9. "github.com/go-playground/locales/nl"
  10. )
  11. // NOTES:
  12. // - Run "go test" to run tests
  13. // - Run "gocov test | gocov report" to report on test converage by file
  14. // - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called
  15. //
  16. // or
  17. //
  18. // -- may be a good idea to change to output path to somewherelike /tmp
  19. // go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html
  20. //
  21. func TestExportImportBasic(t *testing.T) {
  22. e := en.New()
  23. uni := New(e, e)
  24. en, found := uni.GetTranslator("en") // or fallback if fails to find 'en'
  25. if !found {
  26. t.Fatalf("Expected '%t' Got '%t'", true, found)
  27. }
  28. translations := []struct {
  29. key interface{}
  30. trans string
  31. expected error
  32. expectedError bool
  33. override bool
  34. }{
  35. {
  36. key: "test_trans",
  37. trans: "Welcome {0}",
  38. expected: nil,
  39. },
  40. {
  41. key: -1,
  42. trans: "Welcome {0}",
  43. expected: nil,
  44. },
  45. {
  46. key: "test_trans2",
  47. trans: "{0} to the {1}.",
  48. expected: nil,
  49. },
  50. {
  51. key: "test_trans3",
  52. trans: "Welcome {0} to the {1}",
  53. expected: nil,
  54. },
  55. {
  56. key: "test_trans4",
  57. trans: "{0}{1}",
  58. expected: nil,
  59. },
  60. {
  61. key: "test_trans",
  62. trans: "{0}{1}",
  63. expected: &ErrConflictingTranslation{locale: en.Locale(), key: "test_trans", text: "{0}{1}"},
  64. expectedError: true,
  65. },
  66. {
  67. key: -1,
  68. trans: "{0}{1}",
  69. expected: &ErrConflictingTranslation{locale: en.Locale(), key: -1, text: "{0}{1}"},
  70. expectedError: true,
  71. },
  72. {
  73. key: "test_trans",
  74. trans: "Welcome {0} to the {1}.",
  75. expected: nil,
  76. override: true,
  77. },
  78. }
  79. for _, tt := range translations {
  80. err := en.Add(tt.key, tt.trans, tt.override)
  81. if err != tt.expected {
  82. if !tt.expectedError {
  83. t.Errorf("Expected '%s' Got '%s'", tt.expected, err)
  84. } else {
  85. if err.Error() != tt.expected.Error() {
  86. t.Errorf("Expected '%s' Got '%s'", tt.expected.Error(), err.Error())
  87. }
  88. }
  89. }
  90. }
  91. dirname := "testdata/translations"
  92. defer os.RemoveAll(dirname)
  93. err := uni.Export(FormatJSON, dirname)
  94. if err != nil {
  95. t.Fatalf("Expected '%v' Got '%s'", nil, err)
  96. }
  97. uni = New(e, e)
  98. err = uni.Import(FormatJSON, dirname)
  99. if err != nil {
  100. t.Fatalf("Expected '%v' Got '%s'", nil, err)
  101. }
  102. en, found = uni.GetTranslator("en") // or fallback if fails to find 'en'
  103. if !found {
  104. t.Fatalf("Expected '%t' Got '%t'", true, found)
  105. }
  106. tests := []struct {
  107. key interface{}
  108. params []string
  109. expected string
  110. expectedError bool
  111. }{
  112. {
  113. key: "test_trans",
  114. params: []string{"Joeybloggs", "The Test"},
  115. expected: "Welcome Joeybloggs to the The Test.",
  116. },
  117. {
  118. key: "test_trans2",
  119. params: []string{"Joeybloggs", "The Test"},
  120. expected: "Joeybloggs to the The Test.",
  121. },
  122. {
  123. key: "test_trans3",
  124. params: []string{"Joeybloggs", "The Test"},
  125. expected: "Welcome Joeybloggs to the The Test",
  126. },
  127. {
  128. key: "test_trans4",
  129. params: []string{"Joeybloggs", "The Test"},
  130. expected: "JoeybloggsThe Test",
  131. },
  132. // bad translation
  133. {
  134. key: "non-existant-key",
  135. params: []string{"Joeybloggs", "The Test"},
  136. expected: "",
  137. expectedError: true,
  138. },
  139. }
  140. for _, tt := range tests {
  141. s, err := en.T(tt.key, tt.params...)
  142. if s != tt.expected {
  143. if !tt.expectedError || (tt.expectedError && err != ErrUnknowTranslation) {
  144. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  145. }
  146. }
  147. }
  148. }
  149. func TestExportImportCardinal(t *testing.T) {
  150. e := en.New()
  151. uni := New(e, e)
  152. en, found := uni.GetTranslator("en")
  153. if !found {
  154. t.Fatalf("Expected '%t' Got '%t'", true, found)
  155. }
  156. translations := []struct {
  157. key interface{}
  158. trans string
  159. rule locales.PluralRule
  160. expected error
  161. expectedError bool
  162. override bool
  163. }{
  164. // bad translation
  165. {
  166. key: "cardinal_test",
  167. trans: "You have a day left.",
  168. rule: locales.PluralRuleOne,
  169. expected: &ErrCardinalTranslation{text: fmt.Sprintf("error: parameter '%s' not found, may want to use 'Add' instead of 'AddCardinal'. locale: '%s' key: '%v' text: '%s'", paramZero, en.Locale(), "cardinal_test", "You have a day left.")},
  170. expectedError: true,
  171. },
  172. {
  173. key: "cardinal_test",
  174. trans: "You have {0} day",
  175. rule: locales.PluralRuleOne,
  176. expected: nil,
  177. },
  178. {
  179. key: "cardinal_test",
  180. trans: "You have {0} days left.",
  181. rule: locales.PluralRuleOther,
  182. expected: nil,
  183. },
  184. {
  185. key: "cardinal_test",
  186. trans: "You have {0} days left.",
  187. rule: locales.PluralRuleOther,
  188. expected: &ErrConflictingTranslation{locale: en.Locale(), key: "cardinal_test", rule: locales.PluralRuleOther, text: "You have {0} days left."},
  189. expectedError: true,
  190. },
  191. {
  192. key: "cardinal_test",
  193. trans: "You have {0} day left.",
  194. rule: locales.PluralRuleOne,
  195. expected: nil,
  196. override: true,
  197. },
  198. }
  199. for _, tt := range translations {
  200. err := en.AddCardinal(tt.key, tt.trans, tt.rule, tt.override)
  201. if err != tt.expected {
  202. if !tt.expectedError || err.Error() != tt.expected.Error() {
  203. t.Errorf("Expected '%s' Got '%s'", tt.expected, err)
  204. }
  205. }
  206. }
  207. dirname := "testdata/translations"
  208. defer os.RemoveAll(dirname)
  209. err := uni.Export(FormatJSON, dirname)
  210. if err != nil {
  211. t.Fatalf("Expected '%v' Got '%s'", nil, err)
  212. }
  213. uni = New(e, e)
  214. err = uni.Import(FormatJSON, dirname)
  215. if err != nil {
  216. t.Fatalf("Expected '%v' Got '%s'", nil, err)
  217. }
  218. en, found = uni.GetTranslator("en") // or fallback if fails to find 'en'
  219. if !found {
  220. t.Fatalf("Expected '%t' Got '%t'", true, found)
  221. }
  222. tests := []struct {
  223. key interface{}
  224. num float64
  225. digits uint64
  226. param string
  227. expected string
  228. expectedError bool
  229. }{
  230. {
  231. key: "cardinal_test",
  232. num: 1,
  233. digits: 0,
  234. param: string(en.FmtNumber(1, 0)),
  235. expected: "You have 1 day left.",
  236. },
  237. // bad translation key
  238. {
  239. key: "non-existant",
  240. num: 1,
  241. digits: 0,
  242. param: string(en.FmtNumber(1, 0)),
  243. expected: "",
  244. expectedError: true,
  245. },
  246. }
  247. for _, tt := range tests {
  248. s, err := en.C(tt.key, tt.num, tt.digits, tt.param)
  249. if err != nil {
  250. if !tt.expectedError && err != ErrUnknowTranslation {
  251. t.Errorf("Expected '<nil>' Got '%s'", err)
  252. }
  253. }
  254. if s != tt.expected {
  255. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  256. }
  257. }
  258. }
  259. func TestExportImportOrdinal(t *testing.T) {
  260. e := en.New()
  261. uni := New(e, e)
  262. en, found := uni.GetTranslator("en")
  263. if !found {
  264. t.Fatalf("Expected '%t' Got '%t'", true, found)
  265. }
  266. translations := []struct {
  267. key interface{}
  268. trans string
  269. rule locales.PluralRule
  270. expected error
  271. expectedError bool
  272. override bool
  273. }{
  274. // bad translation
  275. {
  276. key: "day",
  277. trans: "st",
  278. rule: locales.PluralRuleOne,
  279. expected: &ErrOrdinalTranslation{text: fmt.Sprintf("error: parameter '%s' not found, may want to use 'Add' instead of 'AddOrdinal'. locale: '%s' key: '%v' text: '%s'", paramZero, en.Locale(), "day", "st")},
  280. expectedError: true,
  281. },
  282. {
  283. key: "day",
  284. trans: "{0}sfefewt",
  285. rule: locales.PluralRuleOne,
  286. expected: nil,
  287. },
  288. {
  289. key: "day",
  290. trans: "{0}nd",
  291. rule: locales.PluralRuleTwo,
  292. expected: nil,
  293. },
  294. {
  295. key: "day",
  296. trans: "{0}rd",
  297. rule: locales.PluralRuleFew,
  298. expected: nil,
  299. },
  300. {
  301. key: "day",
  302. trans: "{0}th",
  303. rule: locales.PluralRuleOther,
  304. expected: nil,
  305. },
  306. // bad translation
  307. {
  308. key: "day",
  309. trans: "{0}th",
  310. rule: locales.PluralRuleOther,
  311. expected: &ErrConflictingTranslation{locale: en.Locale(), key: "day", rule: locales.PluralRuleOther, text: "{0}th"},
  312. expectedError: true,
  313. },
  314. {
  315. key: "day",
  316. trans: "{0}st",
  317. rule: locales.PluralRuleOne,
  318. expected: nil,
  319. override: true,
  320. },
  321. }
  322. for _, tt := range translations {
  323. err := en.AddOrdinal(tt.key, tt.trans, tt.rule, tt.override)
  324. if err != tt.expected {
  325. if !tt.expectedError || err.Error() != tt.expected.Error() {
  326. t.Errorf("Expected '<nil>' Got '%s'", err)
  327. }
  328. }
  329. }
  330. dirname := "testdata/translations"
  331. defer os.RemoveAll(dirname)
  332. err := uni.Export(FormatJSON, dirname)
  333. if err != nil {
  334. t.Fatalf("Expected '%v' Got '%s'", nil, err)
  335. }
  336. uni = New(e, e)
  337. err = uni.Import(FormatJSON, dirname)
  338. if err != nil {
  339. t.Fatalf("Expected '%v' Got '%s'", nil, err)
  340. }
  341. en, found = uni.GetTranslator("en") // or fallback if fails to find 'en'
  342. if !found {
  343. t.Fatalf("Expected '%t' Got '%t'", true, found)
  344. }
  345. tests := []struct {
  346. key interface{}
  347. num float64
  348. digits uint64
  349. param string
  350. expected string
  351. expectedError bool
  352. }{
  353. {
  354. key: "day",
  355. num: 1,
  356. digits: 0,
  357. param: string(en.FmtNumber(1, 0)),
  358. expected: "1st",
  359. },
  360. {
  361. key: "day",
  362. num: 2,
  363. digits: 0,
  364. param: string(en.FmtNumber(2, 0)),
  365. expected: "2nd",
  366. },
  367. {
  368. key: "day",
  369. num: 3,
  370. digits: 0,
  371. param: string(en.FmtNumber(3, 0)),
  372. expected: "3rd",
  373. },
  374. {
  375. key: "day",
  376. num: 4,
  377. digits: 0,
  378. param: string(en.FmtNumber(4, 0)),
  379. expected: "4th",
  380. },
  381. {
  382. key: "day",
  383. num: 10258.43,
  384. digits: 0,
  385. param: string(en.FmtNumber(10258.43, 0)),
  386. expected: "10,258th",
  387. },
  388. // bad translation
  389. {
  390. key: "d-day",
  391. num: 10258.43,
  392. digits: 0,
  393. param: string(en.FmtNumber(10258.43, 0)),
  394. expected: "",
  395. expectedError: true,
  396. },
  397. }
  398. for _, tt := range tests {
  399. s, err := en.O(tt.key, tt.num, tt.digits, tt.param)
  400. if err != nil {
  401. if !tt.expectedError && err != ErrUnknowTranslation {
  402. t.Errorf("Expected '<nil>' Got '%s'", err)
  403. }
  404. }
  405. if s != tt.expected {
  406. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  407. }
  408. }
  409. }
  410. func TestExportImportRange(t *testing.T) {
  411. n := nl.New()
  412. uni := New(n, n)
  413. // dutch
  414. nl, found := uni.GetTranslator("nl")
  415. if !found {
  416. t.Fatalf("Expected '%t' Got '%t'", true, found)
  417. }
  418. translations := []struct {
  419. key interface{}
  420. trans string
  421. rule locales.PluralRule
  422. expected error
  423. expectedError bool
  424. override bool
  425. }{
  426. // bad translation
  427. {
  428. key: "day",
  429. trans: "er -{1} dag vertrokken",
  430. rule: locales.PluralRuleOne,
  431. expected: &ErrRangeTranslation{text: fmt.Sprintf("error: parameter '%s' not found, are you sure you're adding a Range Translation? locale: '%s' key: '%s' text: '%s'", paramZero, nl.Locale(), "day", "er -{1} dag vertrokken")},
  432. expectedError: true,
  433. },
  434. // bad translation
  435. {
  436. key: "day",
  437. trans: "er {0}- dag vertrokken",
  438. rule: locales.PluralRuleOne,
  439. expected: &ErrRangeTranslation{text: fmt.Sprintf("error: parameter '%s' not found, a Range Translation requires two parameters. locale: '%s' key: '%s' text: '%s'", paramOne, nl.Locale(), "day", "er {0}- dag vertrokken")},
  440. expectedError: true,
  441. },
  442. {
  443. key: "day",
  444. trans: "er {0}-{1} dag",
  445. rule: locales.PluralRuleOne,
  446. expected: nil,
  447. },
  448. {
  449. key: "day",
  450. trans: "er zijn {0}-{1} dagen over",
  451. rule: locales.PluralRuleOther,
  452. expected: nil,
  453. },
  454. // bad translation
  455. {
  456. key: "day",
  457. trans: "er zijn {0}-{1} dagen over",
  458. rule: locales.PluralRuleOther,
  459. expected: &ErrConflictingTranslation{locale: nl.Locale(), key: "day", rule: locales.PluralRuleOther, text: "er zijn {0}-{1} dagen over"},
  460. expectedError: true,
  461. },
  462. {
  463. key: "day",
  464. trans: "er {0}-{1} dag vertrokken",
  465. rule: locales.PluralRuleOne,
  466. expected: nil,
  467. override: true,
  468. },
  469. }
  470. for _, tt := range translations {
  471. err := nl.AddRange(tt.key, tt.trans, tt.rule, tt.override)
  472. if err != tt.expected {
  473. if !tt.expectedError || err.Error() != tt.expected.Error() {
  474. t.Errorf("Expected '%#v' Got '%s'", tt.expected, err)
  475. }
  476. }
  477. }
  478. dirname := "testdata/translations"
  479. defer os.RemoveAll(dirname)
  480. err := uni.Export(FormatJSON, dirname)
  481. if err != nil {
  482. t.Fatalf("Expected '%v' Got '%s'", nil, err)
  483. }
  484. uni = New(n, n)
  485. err = uni.Import(FormatJSON, dirname)
  486. if err != nil {
  487. t.Fatalf("Expected '%v' Got '%s'", nil, err)
  488. }
  489. nl, found = uni.GetTranslator("nl") // or fallback if fails to find 'en'
  490. if !found {
  491. t.Fatalf("Expected '%t' Got '%t'", true, found)
  492. }
  493. tests := []struct {
  494. key interface{}
  495. num1 float64
  496. digits1 uint64
  497. num2 float64
  498. digits2 uint64
  499. param1 string
  500. param2 string
  501. expected string
  502. expectedError bool
  503. }{
  504. {
  505. key: "day",
  506. num1: 1,
  507. digits1: 0,
  508. num2: 2,
  509. digits2: 0,
  510. param1: string(nl.FmtNumber(1, 0)),
  511. param2: string(nl.FmtNumber(2, 0)),
  512. expected: "er zijn 1-2 dagen over",
  513. },
  514. {
  515. key: "day",
  516. num1: 0,
  517. digits1: 0,
  518. num2: 1,
  519. digits2: 0,
  520. param1: string(nl.FmtNumber(0, 0)),
  521. param2: string(nl.FmtNumber(1, 0)),
  522. expected: "er 0-1 dag vertrokken",
  523. },
  524. {
  525. key: "day",
  526. num1: 0,
  527. digits1: 0,
  528. num2: 2,
  529. digits2: 0,
  530. param1: string(nl.FmtNumber(0, 0)),
  531. param2: string(nl.FmtNumber(2, 0)),
  532. expected: "er zijn 0-2 dagen over",
  533. },
  534. // bad translations from here
  535. {
  536. key: "d-day",
  537. num1: 0,
  538. digits1: 0,
  539. num2: 2,
  540. digits2: 0,
  541. param1: string(nl.FmtNumber(0, 0)),
  542. param2: string(nl.FmtNumber(2, 0)),
  543. expected: "",
  544. expectedError: true,
  545. },
  546. }
  547. for _, tt := range tests {
  548. s, err := nl.R(tt.key, tt.num1, tt.digits1, tt.num2, tt.digits2, tt.param1, tt.param2)
  549. if err != nil {
  550. if !tt.expectedError && err != ErrUnknowTranslation {
  551. t.Errorf("Expected '<nil>' Got '%s'", err)
  552. }
  553. }
  554. if s != tt.expected {
  555. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  556. }
  557. }
  558. }
  559. func TestImportRecursive(t *testing.T) {
  560. e := en.New()
  561. uni := New(e, e)
  562. dirname := "testdata/nested1"
  563. err := uni.Import(FormatJSON, dirname)
  564. if err != nil {
  565. t.Fatalf("Expected '%v' Got '%s'", nil, err)
  566. }
  567. en, found := uni.GetTranslator("en") // or fallback if fails to find 'en'
  568. if !found {
  569. t.Fatalf("Expected '%t' Got '%t'", true, found)
  570. }
  571. tests := []struct {
  572. key interface{}
  573. params []string
  574. expected string
  575. expectedError bool
  576. }{
  577. {
  578. key: "test_trans",
  579. params: []string{"Joeybloggs", "The Test"},
  580. expected: "Welcome Joeybloggs to the The Test.",
  581. },
  582. {
  583. key: "test_trans2",
  584. params: []string{"Joeybloggs", "The Test"},
  585. expected: "Joeybloggs to the The Test.",
  586. },
  587. {
  588. key: "test_trans3",
  589. params: []string{"Joeybloggs", "The Test"},
  590. expected: "Welcome Joeybloggs to the The Test",
  591. },
  592. {
  593. key: "test_trans4",
  594. params: []string{"Joeybloggs", "The Test"},
  595. expected: "JoeybloggsThe Test",
  596. },
  597. // bad translation
  598. {
  599. key: "non-existant-key",
  600. params: []string{"Joeybloggs", "The Test"},
  601. expected: "",
  602. expectedError: true,
  603. },
  604. }
  605. for _, tt := range tests {
  606. s, err := en.T(tt.key, tt.params...)
  607. if s != tt.expected {
  608. if !tt.expectedError || (tt.expectedError && err != ErrUnknowTranslation) {
  609. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  610. }
  611. }
  612. }
  613. }
  614. func TestBadImport(t *testing.T) {
  615. // test non existant file
  616. e := en.New()
  617. uni := New(e, e)
  618. filename := "testdata/non-existant-file.json"
  619. expected := "stat testdata/non-existant-file.json: no such file or directory"
  620. err := uni.Import(FormatJSON, filename)
  621. if err == nil || err.Error() != expected {
  622. t.Fatalf("Expected '%s' Got '%s'", expected, err)
  623. }
  624. // test bad parameter basic translation
  625. filename = "testdata/bad-translation1.json"
  626. expected = "error: bad parameter syntax, missing parameter '{0}' in translation. locale: 'en' key: 'test_trans3' text: 'Welcome {lettersnotpermitted} to the {1}'"
  627. err = uni.Import(FormatJSON, filename)
  628. if err == nil || err.Error() != expected {
  629. t.Fatalf("Expected '%s' Got '%s'", expected, err)
  630. }
  631. // test missing bracket basic translation
  632. filename = "testdata/bad-translation2.json"
  633. expected = "error: missing bracket '{}', in translation. locale: 'en' key: 'test_trans3' text: 'Welcome {0 to the {1}'"
  634. err = uni.Import(FormatJSON, filename)
  635. if err == nil || err.Error() != expected {
  636. t.Fatalf("Expected '%s' Got '%s'", expected, err)
  637. }
  638. // test missing locale basic translation
  639. filename = "testdata/bad-translation3.json"
  640. expected = "error: locale 'nl' not registered."
  641. err = uni.Import(FormatJSON, filename)
  642. if err == nil || err.Error() != expected {
  643. t.Fatalf("Expected '%s' Got '%s'", expected, err)
  644. }
  645. // test bad plural definition
  646. filename = "testdata/bad-translation4.json"
  647. expected = "error: bad plural definition 'ut.translation{Locale:\"en\", Key:\"cardinal_test\", Translation:\"You have {0} day left.\", PluralType:\"NotAPluralType\", PluralRule:\"One\", OverrideExisting:false}'"
  648. err = uni.Import(FormatJSON, filename)
  649. if err == nil || err.Error() != expected {
  650. t.Fatalf("Expected '%s' Got '%s'", expected, err)
  651. }
  652. // test bad plural rule for locale
  653. filename = "testdata/bad-translation5.json"
  654. expected = "error: cardinal plural rule 'Many' does not exist for locale 'en' key: 'cardinal_test' text: 'You have {0} day left.'"
  655. err = uni.Import(FormatJSON, filename)
  656. if err == nil || err.Error() != expected {
  657. t.Fatalf("Expected '%s' Got '%s'", expected, err)
  658. }
  659. // test invalid JSON
  660. filename = "testdata/bad-translation6.json"
  661. expected = "invalid character ']' after object key:value pair"
  662. err = uni.Import(FormatJSON, filename)
  663. if err == nil || err.Error() != expected {
  664. t.Fatalf("Expected '%s' Got '%s'", expected, err)
  665. }
  666. // test bad io.Reader
  667. f, err := os.Open(filename)
  668. if err != nil {
  669. t.Fatalf("Expected '%v' Got '%s'", nil, err)
  670. }
  671. f.Close()
  672. expected = "read testdata/bad-translation6.json: bad file descriptor"
  673. err = uni.ImportByReader(FormatJSON, f)
  674. if err == nil || err.Error() != expected {
  675. t.Fatalf("Expected '%s' Got '%s'", expected, err)
  676. }
  677. }
  678. func TestBadExport(t *testing.T) {
  679. // test readonly directory
  680. e := en.New()
  681. uni := New(e, e)
  682. en, found := uni.GetTranslator("en") // or fallback if fails to find 'en'
  683. if !found {
  684. t.Fatalf("Expected '%t' Got '%t'", true, found)
  685. }
  686. dirname := "testdata/readonly"
  687. err := os.Mkdir(dirname, 0444)
  688. if err != nil {
  689. t.Fatalf("Expected '%v' Got '%s'", nil, err)
  690. }
  691. defer os.RemoveAll(dirname)
  692. en.Add("day", "this is a day", false)
  693. expected := "open testdata/readonly/en.json: permission denied"
  694. err = uni.Export(FormatJSON, dirname)
  695. if err == nil || err.Error() != expected {
  696. t.Fatalf("Expected '%s' Got '%s'", expected, err)
  697. }
  698. // test exporting into directory inside readonly directory
  699. expected = "stat testdata/readonly/inner: permission denied"
  700. err = uni.Export(FormatJSON, filepath.Join(dirname, "inner"))
  701. if err == nil || err.Error() != expected {
  702. t.Fatalf("Expected '%s' Got '%s'", expected, err)
  703. }
  704. }