ja_test.go 16 KB

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