tests.tmpl 18 KB

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