en_test.go 17 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. package en
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/go-playground/locales"
  6. "github.com/go-playground/locales/currency"
  7. )
  8. func TestLocale(t *testing.T) {
  9. trans := New()
  10. expected := "en"
  11. if trans.Locale() != expected {
  12. t.Errorf("Expected '%s' Got '%s'", expected, trans.Locale())
  13. }
  14. }
  15. func TestPluralsRange(t *testing.T) {
  16. trans := New()
  17. tests := []struct {
  18. expected locales.PluralRule
  19. }{
  20. {
  21. expected: locales.PluralRuleOther,
  22. },
  23. }
  24. rules := trans.PluralsRange()
  25. expected := 1
  26. if len(rules) != expected {
  27. t.Errorf("Expected '%d' Got '%d'", expected, len(rules))
  28. }
  29. for _, tt := range tests {
  30. r := locales.PluralRuleUnknown
  31. for i := 0; i < len(rules); i++ {
  32. if rules[i] == tt.expected {
  33. r = rules[i]
  34. break
  35. }
  36. }
  37. if r == locales.PluralRuleUnknown {
  38. t.Errorf("Expected '%s' Got '%s'", tt.expected, r)
  39. }
  40. }
  41. }
  42. func TestPluralsOrdinal(t *testing.T) {
  43. trans := New()
  44. tests := []struct {
  45. expected locales.PluralRule
  46. }{
  47. {
  48. expected: locales.PluralRuleOne,
  49. },
  50. {
  51. expected: locales.PluralRuleTwo,
  52. },
  53. {
  54. expected: locales.PluralRuleFew,
  55. },
  56. {
  57. expected: locales.PluralRuleOther,
  58. },
  59. }
  60. rules := trans.PluralsOrdinal()
  61. expected := 4
  62. if len(rules) != expected {
  63. t.Errorf("Expected '%d' Got '%d'", expected, len(rules))
  64. }
  65. for _, tt := range tests {
  66. r := locales.PluralRuleUnknown
  67. for i := 0; i < len(rules); i++ {
  68. if rules[i] == tt.expected {
  69. r = rules[i]
  70. break
  71. }
  72. }
  73. if r == locales.PluralRuleUnknown {
  74. t.Errorf("Expected '%s' Got '%s'", tt.expected, r)
  75. }
  76. }
  77. }
  78. func TestPluralsCardinal(t *testing.T) {
  79. trans := New()
  80. tests := []struct {
  81. expected locales.PluralRule
  82. }{
  83. {
  84. expected: locales.PluralRuleOne,
  85. },
  86. {
  87. expected: locales.PluralRuleOther,
  88. },
  89. }
  90. rules := trans.PluralsCardinal()
  91. expected := 2
  92. if len(rules) != expected {
  93. t.Errorf("Expected '%d' Got '%d'", expected, len(rules))
  94. }
  95. for _, tt := range tests {
  96. r := locales.PluralRuleUnknown
  97. for i := 0; i < len(rules); i++ {
  98. if rules[i] == tt.expected {
  99. r = rules[i]
  100. break
  101. }
  102. }
  103. if r == locales.PluralRuleUnknown {
  104. t.Errorf("Expected '%s' Got '%s'", tt.expected, r)
  105. }
  106. }
  107. }
  108. func TestRangePlurals(t *testing.T) {
  109. trans := New()
  110. tests := []struct {
  111. num1 float64
  112. v1 uint64
  113. num2 float64
  114. v2 uint64
  115. expected locales.PluralRule
  116. }{
  117. {
  118. num1: 1,
  119. v1: 1,
  120. num2: 2,
  121. v2: 2,
  122. expected: locales.PluralRuleOther,
  123. },
  124. }
  125. for _, tt := range tests {
  126. rule := trans.RangePluralRule(tt.num1, tt.v1, tt.num2, tt.v2)
  127. if rule != tt.expected {
  128. t.Errorf("Expected '%s' Got '%s'", tt.expected, rule)
  129. }
  130. }
  131. }
  132. func TestOrdinalPlurals(t *testing.T) {
  133. trans := New()
  134. tests := []struct {
  135. num float64
  136. v uint64
  137. expected locales.PluralRule
  138. }{
  139. {
  140. num: 1,
  141. v: 0,
  142. expected: locales.PluralRuleOne,
  143. },
  144. {
  145. num: 2,
  146. v: 0,
  147. expected: locales.PluralRuleTwo,
  148. },
  149. {
  150. num: 3,
  151. v: 0,
  152. expected: locales.PluralRuleFew,
  153. },
  154. {
  155. num: 4,
  156. v: 0,
  157. expected: locales.PluralRuleOther,
  158. },
  159. }
  160. for _, tt := range tests {
  161. rule := trans.OrdinalPluralRule(tt.num, tt.v)
  162. if rule != tt.expected {
  163. t.Errorf("Expected '%s' Got '%s'", tt.expected, rule)
  164. }
  165. }
  166. }
  167. func TestCardinalPlurals(t *testing.T) {
  168. trans := New()
  169. tests := []struct {
  170. num float64
  171. v uint64
  172. expected locales.PluralRule
  173. }{
  174. {
  175. num: 1,
  176. v: 0,
  177. expected: locales.PluralRuleOne,
  178. },
  179. {
  180. num: 4,
  181. v: 0,
  182. expected: locales.PluralRuleOther,
  183. },
  184. }
  185. for _, tt := range tests {
  186. rule := trans.CardinalPluralRule(tt.num, tt.v)
  187. if rule != tt.expected {
  188. t.Errorf("Expected '%s' Got '%s'", tt.expected, rule)
  189. }
  190. }
  191. }
  192. func TestDaysAbbreviated(t *testing.T) {
  193. trans := New()
  194. days := trans.WeekdaysAbbreviated()
  195. for i, day := range days {
  196. s := trans.WeekdayAbbreviated(time.Weekday(i))
  197. if s != day {
  198. t.Errorf("Expected '%s' Got '%s'", day, s)
  199. }
  200. }
  201. tests := []struct {
  202. idx int
  203. expected string
  204. }{
  205. {
  206. idx: 0,
  207. expected: "Sun",
  208. },
  209. {
  210. idx: 1,
  211. expected: "Mon",
  212. },
  213. {
  214. idx: 2,
  215. expected: "Tue",
  216. },
  217. {
  218. idx: 3,
  219. expected: "Wed",
  220. },
  221. {
  222. idx: 4,
  223. expected: "Thu",
  224. },
  225. {
  226. idx: 5,
  227. expected: "Fri",
  228. },
  229. {
  230. idx: 6,
  231. expected: "Sat",
  232. },
  233. }
  234. for _, tt := range tests {
  235. s := trans.WeekdayAbbreviated(time.Weekday(tt.idx))
  236. if s != tt.expected {
  237. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  238. }
  239. }
  240. }
  241. func TestDaysNarrow(t *testing.T) {
  242. trans := New()
  243. days := trans.WeekdaysNarrow()
  244. for i, day := range days {
  245. s := trans.WeekdayNarrow(time.Weekday(i))
  246. if s != day {
  247. t.Errorf("Expected '%s' Got '%s'", string(day), s)
  248. }
  249. }
  250. tests := []struct {
  251. idx int
  252. expected string
  253. }{
  254. {
  255. idx: 0,
  256. expected: "S",
  257. },
  258. {
  259. idx: 1,
  260. expected: "M",
  261. },
  262. {
  263. idx: 2,
  264. expected: "T",
  265. },
  266. {
  267. idx: 3,
  268. expected: "W",
  269. },
  270. {
  271. idx: 4,
  272. expected: "T",
  273. },
  274. {
  275. idx: 5,
  276. expected: "F",
  277. },
  278. {
  279. idx: 6,
  280. expected: "S",
  281. },
  282. }
  283. for _, tt := range tests {
  284. s := trans.WeekdayNarrow(time.Weekday(tt.idx))
  285. if s != tt.expected {
  286. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  287. }
  288. }
  289. }
  290. func TestDaysShort(t *testing.T) {
  291. trans := New()
  292. days := trans.WeekdaysShort()
  293. for i, day := range days {
  294. s := trans.WeekdayShort(time.Weekday(i))
  295. if s != day {
  296. t.Errorf("Expected '%s' Got '%s'", day, s)
  297. }
  298. }
  299. tests := []struct {
  300. idx int
  301. expected string
  302. }{
  303. {
  304. idx: 0,
  305. expected: "Su",
  306. },
  307. {
  308. idx: 1,
  309. expected: "Mo",
  310. },
  311. {
  312. idx: 2,
  313. expected: "Tu",
  314. },
  315. {
  316. idx: 3,
  317. expected: "We",
  318. },
  319. {
  320. idx: 4,
  321. expected: "Th",
  322. },
  323. {
  324. idx: 5,
  325. expected: "Fr",
  326. },
  327. {
  328. idx: 6,
  329. expected: "Sa",
  330. },
  331. }
  332. for _, tt := range tests {
  333. s := trans.WeekdayShort(time.Weekday(tt.idx))
  334. if s != tt.expected {
  335. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  336. }
  337. }
  338. }
  339. func TestDaysWide(t *testing.T) {
  340. trans := New()
  341. days := trans.WeekdaysWide()
  342. for i, day := range days {
  343. s := trans.WeekdayWide(time.Weekday(i))
  344. if s != day {
  345. t.Errorf("Expected '%s' Got '%s'", day, s)
  346. }
  347. }
  348. tests := []struct {
  349. idx int
  350. expected string
  351. }{
  352. {
  353. idx: 0,
  354. expected: "Sunday",
  355. },
  356. {
  357. idx: 1,
  358. expected: "Monday",
  359. },
  360. {
  361. idx: 2,
  362. expected: "Tuesday",
  363. },
  364. {
  365. idx: 3,
  366. expected: "Wednesday",
  367. },
  368. {
  369. idx: 4,
  370. expected: "Thursday",
  371. },
  372. {
  373. idx: 5,
  374. expected: "Friday",
  375. },
  376. {
  377. idx: 6,
  378. expected: "Saturday",
  379. },
  380. }
  381. for _, tt := range tests {
  382. s := trans.WeekdayWide(time.Weekday(tt.idx))
  383. if s != tt.expected {
  384. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  385. }
  386. }
  387. }
  388. func TestMonthsAbbreviated(t *testing.T) {
  389. trans := New()
  390. months := trans.MonthsAbbreviated()
  391. for i, month := range months {
  392. s := trans.MonthAbbreviated(time.Month(i + 1))
  393. if s != month {
  394. t.Errorf("Expected '%s' Got '%s'", month, s)
  395. }
  396. }
  397. tests := []struct {
  398. idx int
  399. expected string
  400. }{
  401. {
  402. idx: 1,
  403. expected: "Jan",
  404. },
  405. {
  406. idx: 2,
  407. expected: "Feb",
  408. },
  409. {
  410. idx: 3,
  411. expected: "Mar",
  412. },
  413. {
  414. idx: 4,
  415. expected: "Apr",
  416. },
  417. {
  418. idx: 5,
  419. expected: "May",
  420. },
  421. {
  422. idx: 6,
  423. expected: "Jun",
  424. },
  425. {
  426. idx: 7,
  427. expected: "Jul",
  428. },
  429. {
  430. idx: 8,
  431. expected: "Aug",
  432. },
  433. {
  434. idx: 9,
  435. expected: "Sep",
  436. },
  437. {
  438. idx: 10,
  439. expected: "Oct",
  440. },
  441. {
  442. idx: 11,
  443. expected: "Nov",
  444. },
  445. {
  446. idx: 12,
  447. expected: "Dec",
  448. },
  449. }
  450. for _, tt := range tests {
  451. s := trans.MonthAbbreviated(time.Month(tt.idx))
  452. if s != tt.expected {
  453. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  454. }
  455. }
  456. }
  457. func TestMonthsNarrow(t *testing.T) {
  458. trans := New()
  459. months := trans.MonthsNarrow()
  460. for i, month := range months {
  461. s := trans.MonthNarrow(time.Month(i + 1))
  462. if s != month {
  463. t.Errorf("Expected '%s' Got '%s'", month, s)
  464. }
  465. }
  466. tests := []struct {
  467. idx int
  468. expected string
  469. }{
  470. {
  471. idx: 1,
  472. expected: "J",
  473. },
  474. {
  475. idx: 2,
  476. expected: "F",
  477. },
  478. {
  479. idx: 3,
  480. expected: "M",
  481. },
  482. {
  483. idx: 4,
  484. expected: "A",
  485. },
  486. {
  487. idx: 5,
  488. expected: "M",
  489. },
  490. {
  491. idx: 6,
  492. expected: "J",
  493. },
  494. {
  495. idx: 7,
  496. expected: "J",
  497. },
  498. {
  499. idx: 8,
  500. expected: "A",
  501. },
  502. {
  503. idx: 9,
  504. expected: "S",
  505. },
  506. {
  507. idx: 10,
  508. expected: "O",
  509. },
  510. {
  511. idx: 11,
  512. expected: "N",
  513. },
  514. {
  515. idx: 12,
  516. expected: "D",
  517. },
  518. }
  519. for _, tt := range tests {
  520. s := trans.MonthNarrow(time.Month(tt.idx))
  521. if s != tt.expected {
  522. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  523. }
  524. }
  525. }
  526. func TestMonthsWide(t *testing.T) {
  527. trans := New()
  528. months := trans.MonthsWide()
  529. for i, month := range months {
  530. s := trans.MonthWide(time.Month(i + 1))
  531. if s != month {
  532. t.Errorf("Expected '%s' Got '%s'", month, s)
  533. }
  534. }
  535. tests := []struct {
  536. idx int
  537. expected string
  538. }{
  539. {
  540. idx: 1,
  541. expected: "January",
  542. },
  543. {
  544. idx: 2,
  545. expected: "February",
  546. },
  547. {
  548. idx: 3,
  549. expected: "March",
  550. },
  551. {
  552. idx: 4,
  553. expected: "April",
  554. },
  555. {
  556. idx: 5,
  557. expected: "May",
  558. },
  559. {
  560. idx: 6,
  561. expected: "June",
  562. },
  563. {
  564. idx: 7,
  565. expected: "July",
  566. },
  567. {
  568. idx: 8,
  569. expected: "August",
  570. },
  571. {
  572. idx: 9,
  573. expected: "September",
  574. },
  575. {
  576. idx: 10,
  577. expected: "October",
  578. },
  579. {
  580. idx: 11,
  581. expected: "November",
  582. },
  583. {
  584. idx: 12,
  585. expected: "December",
  586. },
  587. }
  588. for _, tt := range tests {
  589. s := string(trans.MonthWide(time.Month(tt.idx)))
  590. if s != tt.expected {
  591. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  592. }
  593. }
  594. }
  595. func TestFmtTimeFull(t *testing.T) {
  596. loc, err := time.LoadLocation("America/Toronto")
  597. if err != nil {
  598. t.Errorf("Expected '<nil>' Got '%s'", err)
  599. }
  600. fixed := time.FixedZone("OTHER", -4)
  601. tests := []struct {
  602. t time.Time
  603. expected string
  604. }{
  605. {
  606. t: time.Date(2016, 02, 03, 9, 5, 1, 0, loc),
  607. expected: "9:05:01 am Eastern Standard Time",
  608. },
  609. {
  610. t: time.Date(2016, 02, 03, 20, 5, 1, 0, fixed),
  611. expected: "8:05:01 pm OTHER",
  612. },
  613. }
  614. trans := New()
  615. for _, tt := range tests {
  616. s := trans.FmtTimeFull(tt.t)
  617. if s != tt.expected {
  618. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  619. }
  620. }
  621. }
  622. func TestFmtTimeLong(t *testing.T) {
  623. loc, err := time.LoadLocation("America/Toronto")
  624. if err != nil {
  625. t.Errorf("Expected '<nil>' Got '%s'", err)
  626. }
  627. tests := []struct {
  628. t time.Time
  629. expected string
  630. }{
  631. {
  632. t: time.Date(2016, 02, 03, 9, 5, 1, 0, loc),
  633. expected: "9:05:01 am EST",
  634. },
  635. {
  636. t: time.Date(2016, 02, 03, 20, 5, 1, 0, loc),
  637. expected: "8:05:01 pm EST",
  638. },
  639. }
  640. trans := New()
  641. for _, tt := range tests {
  642. s := trans.FmtTimeLong(tt.t)
  643. if s != tt.expected {
  644. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  645. }
  646. }
  647. }
  648. func TestFmtTimeMedium(t *testing.T) {
  649. tests := []struct {
  650. t time.Time
  651. expected string
  652. }{
  653. {
  654. t: time.Date(2016, 02, 03, 9, 5, 1, 0, time.UTC),
  655. expected: "9:05:01 am",
  656. },
  657. {
  658. t: time.Date(2016, 02, 03, 20, 5, 1, 0, time.UTC),
  659. expected: "8:05:01 pm",
  660. },
  661. }
  662. trans := New()
  663. for _, tt := range tests {
  664. s := trans.FmtTimeMedium(tt.t)
  665. if s != tt.expected {
  666. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  667. }
  668. }
  669. }
  670. func TestFmtTimeShort(t *testing.T) {
  671. tests := []struct {
  672. t time.Time
  673. expected string
  674. }{
  675. {
  676. t: time.Date(2016, 02, 03, 9, 5, 1, 0, time.UTC),
  677. expected: "9:05 am",
  678. },
  679. {
  680. t: time.Date(2016, 02, 03, 20, 5, 1, 0, time.UTC),
  681. expected: "8:05 pm",
  682. },
  683. }
  684. trans := New()
  685. for _, tt := range tests {
  686. s := trans.FmtTimeShort(tt.t)
  687. if s != tt.expected {
  688. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  689. }
  690. }
  691. }
  692. func TestFmtDateFull(t *testing.T) {
  693. tests := []struct {
  694. t time.Time
  695. expected string
  696. }{
  697. {
  698. t: time.Date(2016, 02, 03, 9, 0, 1, 0, time.UTC),
  699. expected: "Wednesday, February 3, 2016",
  700. },
  701. }
  702. trans := New()
  703. for _, tt := range tests {
  704. s := trans.FmtDateFull(tt.t)
  705. if s != tt.expected {
  706. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  707. }
  708. }
  709. }
  710. func TestFmtDateLong(t *testing.T) {
  711. tests := []struct {
  712. t time.Time
  713. expected string
  714. }{
  715. {
  716. t: time.Date(2016, 02, 03, 9, 0, 1, 0, time.UTC),
  717. expected: "February 3, 2016",
  718. },
  719. }
  720. trans := New()
  721. for _, tt := range tests {
  722. s := trans.FmtDateLong(tt.t)
  723. if s != tt.expected {
  724. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  725. }
  726. }
  727. }
  728. func TestFmtDateMedium(t *testing.T) {
  729. tests := []struct {
  730. t time.Time
  731. expected string
  732. }{
  733. {
  734. t: time.Date(2016, 02, 03, 9, 0, 1, 0, time.UTC),
  735. expected: "Feb 3, 2016",
  736. },
  737. }
  738. trans := New()
  739. for _, tt := range tests {
  740. s := trans.FmtDateMedium(tt.t)
  741. if s != tt.expected {
  742. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  743. }
  744. }
  745. }
  746. func TestFmtDateShort(t *testing.T) {
  747. tests := []struct {
  748. t time.Time
  749. expected string
  750. }{
  751. {
  752. t: time.Date(2016, 02, 03, 9, 0, 1, 0, time.UTC),
  753. expected: "2/3/16",
  754. },
  755. {
  756. t: time.Date(-500, 02, 03, 9, 0, 1, 0, time.UTC),
  757. expected: "2/3/500",
  758. },
  759. }
  760. trans := New()
  761. for _, tt := range tests {
  762. s := trans.FmtDateShort(tt.t)
  763. if s != tt.expected {
  764. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  765. }
  766. }
  767. }
  768. func TestFmtNumber(t *testing.T) {
  769. tests := []struct {
  770. num float64
  771. v uint64
  772. expected string
  773. }{
  774. {
  775. num: 1123456.5643,
  776. v: 2,
  777. expected: "1,123,456.56",
  778. },
  779. {
  780. num: 1123456.5643,
  781. v: 1,
  782. expected: "1,123,456.6",
  783. },
  784. {
  785. num: 221123456.5643,
  786. v: 3,
  787. expected: "221,123,456.564",
  788. },
  789. {
  790. num: -221123456.5643,
  791. v: 3,
  792. expected: "-221,123,456.564",
  793. },
  794. {
  795. num: -221123456.5643,
  796. v: 3,
  797. expected: "-221,123,456.564",
  798. },
  799. {
  800. num: 0,
  801. v: 2,
  802. expected: "0.00",
  803. },
  804. {
  805. num: -0,
  806. v: 2,
  807. expected: "0.00",
  808. },
  809. {
  810. num: -0,
  811. v: 2,
  812. expected: "0.00",
  813. },
  814. }
  815. trans := New()
  816. for _, tt := range tests {
  817. s := trans.FmtNumber(tt.num, tt.v)
  818. if s != tt.expected {
  819. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  820. }
  821. }
  822. }
  823. func TestFmtCurrency(t *testing.T) {
  824. tests := []struct {
  825. num float64
  826. v uint64
  827. currency currency.Type
  828. expected string
  829. }{
  830. {
  831. num: 1123456.5643,
  832. v: 2,
  833. currency: currency.USD,
  834. expected: "$1,123,456.56",
  835. },
  836. {
  837. num: 1123456.5643,
  838. v: 1,
  839. currency: currency.USD,
  840. expected: "$1,123,456.60",
  841. },
  842. {
  843. num: 221123456.5643,
  844. v: 3,
  845. currency: currency.USD,
  846. expected: "$221,123,456.564",
  847. },
  848. {
  849. num: -221123456.5643,
  850. v: 3,
  851. currency: currency.USD,
  852. expected: "-$221,123,456.564",
  853. },
  854. {
  855. num: -221123456.5643,
  856. v: 3,
  857. currency: currency.CAD,
  858. expected: "-CAD221,123,456.564",
  859. },
  860. {
  861. num: 0,
  862. v: 2,
  863. currency: currency.USD,
  864. expected: "$0.00",
  865. },
  866. {
  867. num: -0,
  868. v: 2,
  869. currency: currency.USD,
  870. expected: "$0.00",
  871. },
  872. {
  873. num: -0,
  874. v: 2,
  875. currency: currency.CAD,
  876. expected: "CAD0.00",
  877. },
  878. {
  879. num: 1.23,
  880. v: 0,
  881. currency: currency.USD,
  882. expected: "$1.00",
  883. },
  884. }
  885. trans := New()
  886. for _, tt := range tests {
  887. s := trans.FmtCurrency(tt.num, tt.v, tt.currency)
  888. if s != tt.expected {
  889. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  890. }
  891. }
  892. }
  893. func TestFmtAccounting(t *testing.T) {
  894. tests := []struct {
  895. num float64
  896. v uint64
  897. currency currency.Type
  898. expected string
  899. }{
  900. {
  901. num: 1123456.5643,
  902. v: 2,
  903. currency: currency.USD,
  904. expected: "$1,123,456.56",
  905. },
  906. {
  907. num: 1123456.5643,
  908. v: 1,
  909. currency: currency.USD,
  910. expected: "$1,123,456.60",
  911. },
  912. {
  913. num: 221123456.5643,
  914. v: 3,
  915. currency: currency.USD,
  916. expected: "$221,123,456.564",
  917. },
  918. {
  919. num: -221123456.5643,
  920. v: 3,
  921. currency: currency.USD,
  922. expected: "($221,123,456.564)",
  923. },
  924. {
  925. num: -221123456.5643,
  926. v: 3,
  927. currency: currency.CAD,
  928. expected: "(CAD221,123,456.564)",
  929. },
  930. {
  931. num: -0,
  932. v: 2,
  933. currency: currency.USD,
  934. expected: "$0.00",
  935. },
  936. {
  937. num: -0,
  938. v: 2,
  939. currency: currency.CAD,
  940. expected: "CAD0.00",
  941. },
  942. {
  943. num: 1.23,
  944. v: 0,
  945. currency: currency.USD,
  946. expected: "$1.00",
  947. },
  948. }
  949. trans := New()
  950. for _, tt := range tests {
  951. s := trans.FmtAccounting(tt.num, tt.v, tt.currency)
  952. if s != tt.expected {
  953. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  954. }
  955. }
  956. }
  957. func TestFmtPercent(t *testing.T) {
  958. tests := []struct {
  959. num float64
  960. v uint64
  961. expected string
  962. }{
  963. {
  964. num: 15,
  965. v: 0,
  966. expected: "15%",
  967. },
  968. {
  969. num: 15,
  970. v: 2,
  971. expected: "15.00%",
  972. },
  973. {
  974. num: 434.45,
  975. v: 0,
  976. expected: "434%",
  977. },
  978. {
  979. num: 34.4,
  980. v: 2,
  981. expected: "34.40%",
  982. },
  983. {
  984. num: -34,
  985. v: 0,
  986. expected: "-34%",
  987. },
  988. }
  989. trans := New()
  990. for _, tt := range tests {
  991. s := trans.FmtPercent(tt.num, tt.v)
  992. if s != tt.expected {
  993. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  994. }
  995. }
  996. }