en_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. package en
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/go-playground/locales/currency"
  6. )
  7. func TestDaysAbbreviated(t *testing.T) {
  8. trans := New()
  9. days := trans.WeekdaysAbbreviated()
  10. for i, day := range days {
  11. s := trans.WeekdayAbbreviated(time.Weekday(i))
  12. if s != day {
  13. t.Errorf("Expected '%s' Got '%s'", day, s)
  14. }
  15. }
  16. tests := []struct {
  17. idx int
  18. expected string
  19. }{
  20. {
  21. idx: 0,
  22. expected: "Sun",
  23. },
  24. {
  25. idx: 1,
  26. expected: "Mon",
  27. },
  28. {
  29. idx: 2,
  30. expected: "Tue",
  31. },
  32. {
  33. idx: 3,
  34. expected: "Wed",
  35. },
  36. {
  37. idx: 4,
  38. expected: "Thu",
  39. },
  40. {
  41. idx: 5,
  42. expected: "Fri",
  43. },
  44. {
  45. idx: 6,
  46. expected: "Sat",
  47. },
  48. }
  49. for _, tt := range tests {
  50. s := trans.WeekdayAbbreviated(time.Weekday(tt.idx))
  51. if s != tt.expected {
  52. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  53. }
  54. }
  55. }
  56. func TestDaysNarrow(t *testing.T) {
  57. trans := New()
  58. days := trans.WeekdaysNarrow()
  59. for i, day := range days {
  60. s := trans.WeekdayNarrow(time.Weekday(i))
  61. if s != day {
  62. t.Errorf("Expected '%s' Got '%s'", string(day), s)
  63. }
  64. }
  65. tests := []struct {
  66. idx int
  67. expected string
  68. }{
  69. {
  70. idx: 0,
  71. expected: "S",
  72. },
  73. {
  74. idx: 1,
  75. expected: "M",
  76. },
  77. {
  78. idx: 2,
  79. expected: "T",
  80. },
  81. {
  82. idx: 3,
  83. expected: "W",
  84. },
  85. {
  86. idx: 4,
  87. expected: "T",
  88. },
  89. {
  90. idx: 5,
  91. expected: "F",
  92. },
  93. {
  94. idx: 6,
  95. expected: "S",
  96. },
  97. }
  98. for _, tt := range tests {
  99. s := trans.WeekdayNarrow(time.Weekday(tt.idx))
  100. if s != tt.expected {
  101. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  102. }
  103. }
  104. }
  105. func TestDaysShort(t *testing.T) {
  106. trans := New()
  107. days := trans.WeekdaysShort()
  108. for i, day := range days {
  109. s := trans.WeekdayShort(time.Weekday(i))
  110. if s != day {
  111. t.Errorf("Expected '%s' Got '%s'", day, s)
  112. }
  113. }
  114. tests := []struct {
  115. idx int
  116. expected string
  117. }{
  118. {
  119. idx: 0,
  120. expected: "Su",
  121. },
  122. {
  123. idx: 1,
  124. expected: "Mo",
  125. },
  126. {
  127. idx: 2,
  128. expected: "Tu",
  129. },
  130. {
  131. idx: 3,
  132. expected: "We",
  133. },
  134. {
  135. idx: 4,
  136. expected: "Th",
  137. },
  138. {
  139. idx: 5,
  140. expected: "Fr",
  141. },
  142. {
  143. idx: 6,
  144. expected: "Sa",
  145. },
  146. }
  147. for _, tt := range tests {
  148. s := trans.WeekdayShort(time.Weekday(tt.idx))
  149. if s != tt.expected {
  150. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  151. }
  152. }
  153. }
  154. func TestDaysWide(t *testing.T) {
  155. trans := New()
  156. days := trans.WeekdaysWide()
  157. for i, day := range days {
  158. s := trans.WeekdayWide(time.Weekday(i))
  159. if s != day {
  160. t.Errorf("Expected '%s' Got '%s'", day, s)
  161. }
  162. }
  163. tests := []struct {
  164. idx int
  165. expected string
  166. }{
  167. {
  168. idx: 0,
  169. expected: "Sunday",
  170. },
  171. {
  172. idx: 1,
  173. expected: "Monday",
  174. },
  175. {
  176. idx: 2,
  177. expected: "Tuesday",
  178. },
  179. {
  180. idx: 3,
  181. expected: "Wednesday",
  182. },
  183. {
  184. idx: 4,
  185. expected: "Thursday",
  186. },
  187. {
  188. idx: 5,
  189. expected: "Friday",
  190. },
  191. {
  192. idx: 6,
  193. expected: "Saturday",
  194. },
  195. }
  196. for _, tt := range tests {
  197. s := trans.WeekdayWide(time.Weekday(tt.idx))
  198. if s != tt.expected {
  199. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  200. }
  201. }
  202. }
  203. func TestMonthsAbbreviated(t *testing.T) {
  204. trans := New()
  205. months := trans.MonthsAbbreviated()
  206. for i, month := range months {
  207. s := trans.MonthAbbreviated(time.Month(i + 1))
  208. if s != month {
  209. t.Errorf("Expected '%s' Got '%s'", month, s)
  210. }
  211. }
  212. tests := []struct {
  213. idx int
  214. expected string
  215. }{
  216. {
  217. idx: 1,
  218. expected: "Jan",
  219. },
  220. {
  221. idx: 2,
  222. expected: "Feb",
  223. },
  224. {
  225. idx: 3,
  226. expected: "Mar",
  227. },
  228. {
  229. idx: 4,
  230. expected: "Apr",
  231. },
  232. {
  233. idx: 5,
  234. expected: "May",
  235. },
  236. {
  237. idx: 6,
  238. expected: "Jun",
  239. },
  240. {
  241. idx: 7,
  242. expected: "Jul",
  243. },
  244. {
  245. idx: 8,
  246. expected: "Aug",
  247. },
  248. {
  249. idx: 9,
  250. expected: "Sep",
  251. },
  252. {
  253. idx: 10,
  254. expected: "Oct",
  255. },
  256. {
  257. idx: 11,
  258. expected: "Nov",
  259. },
  260. {
  261. idx: 12,
  262. expected: "Dec",
  263. },
  264. }
  265. for _, tt := range tests {
  266. s := trans.MonthAbbreviated(time.Month(tt.idx))
  267. if s != tt.expected {
  268. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  269. }
  270. }
  271. }
  272. func TestMonthsNarrow(t *testing.T) {
  273. trans := New()
  274. months := trans.MonthsNarrow()
  275. for i, month := range months {
  276. s := trans.MonthNarrow(time.Month(i + 1))
  277. if s != month {
  278. t.Errorf("Expected '%s' Got '%s'", month, s)
  279. }
  280. }
  281. tests := []struct {
  282. idx int
  283. expected string
  284. }{
  285. {
  286. idx: 1,
  287. expected: "J",
  288. },
  289. {
  290. idx: 2,
  291. expected: "F",
  292. },
  293. {
  294. idx: 3,
  295. expected: "M",
  296. },
  297. {
  298. idx: 4,
  299. expected: "A",
  300. },
  301. {
  302. idx: 5,
  303. expected: "M",
  304. },
  305. {
  306. idx: 6,
  307. expected: "J",
  308. },
  309. {
  310. idx: 7,
  311. expected: "J",
  312. },
  313. {
  314. idx: 8,
  315. expected: "A",
  316. },
  317. {
  318. idx: 9,
  319. expected: "S",
  320. },
  321. {
  322. idx: 10,
  323. expected: "O",
  324. },
  325. {
  326. idx: 11,
  327. expected: "N",
  328. },
  329. {
  330. idx: 12,
  331. expected: "D",
  332. },
  333. }
  334. for _, tt := range tests {
  335. s := trans.MonthNarrow(time.Month(tt.idx))
  336. if s != tt.expected {
  337. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  338. }
  339. }
  340. }
  341. func TestMonthsWide(t *testing.T) {
  342. trans := New()
  343. months := trans.MonthsWide()
  344. for i, month := range months {
  345. s := trans.MonthWide(time.Month(i + 1))
  346. if s != month {
  347. t.Errorf("Expected '%s' Got '%s'", month, s)
  348. }
  349. }
  350. tests := []struct {
  351. idx int
  352. expected string
  353. }{
  354. {
  355. idx: 1,
  356. expected: "January",
  357. },
  358. {
  359. idx: 2,
  360. expected: "February",
  361. },
  362. {
  363. idx: 3,
  364. expected: "March",
  365. },
  366. {
  367. idx: 4,
  368. expected: "April",
  369. },
  370. {
  371. idx: 5,
  372. expected: "May",
  373. },
  374. {
  375. idx: 6,
  376. expected: "June",
  377. },
  378. {
  379. idx: 7,
  380. expected: "July",
  381. },
  382. {
  383. idx: 8,
  384. expected: "August",
  385. },
  386. {
  387. idx: 9,
  388. expected: "September",
  389. },
  390. {
  391. idx: 10,
  392. expected: "October",
  393. },
  394. {
  395. idx: 11,
  396. expected: "November",
  397. },
  398. {
  399. idx: 12,
  400. expected: "December",
  401. },
  402. }
  403. for _, tt := range tests {
  404. s := string(trans.MonthWide(time.Month(tt.idx)))
  405. if s != tt.expected {
  406. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  407. }
  408. }
  409. }
  410. func TestFullTime(t *testing.T) {
  411. loc, err := time.LoadLocation("America/Toronto")
  412. if err != nil {
  413. t.Errorf("Expected '<nil>' Got '%s'", err)
  414. }
  415. tests := []struct {
  416. t time.Time
  417. expected string
  418. }{
  419. {
  420. t: time.Date(2016, 02, 03, 9, 5, 1, 0, loc),
  421. expected: "9:05:01 am Eastern Standard Time",
  422. },
  423. }
  424. trans := New()
  425. for _, tt := range tests {
  426. s := trans.FmtTimeFull(tt.t)
  427. if s != tt.expected {
  428. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  429. }
  430. }
  431. }
  432. func TestLongTime(t *testing.T) {
  433. loc, err := time.LoadLocation("America/Toronto")
  434. if err != nil {
  435. t.Errorf("Expected '<nil>' Got '%s'", err)
  436. }
  437. tests := []struct {
  438. t time.Time
  439. expected string
  440. }{
  441. {
  442. t: time.Date(2016, 02, 03, 9, 5, 1, 0, loc),
  443. expected: "9:05:01 am EST",
  444. },
  445. }
  446. trans := New()
  447. for _, tt := range tests {
  448. s := trans.FmtTimeLong(tt.t)
  449. if s != tt.expected {
  450. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  451. }
  452. }
  453. }
  454. func TestMediumTime(t *testing.T) {
  455. tests := []struct {
  456. t time.Time
  457. expected string
  458. }{
  459. {
  460. t: time.Date(2016, 02, 03, 9, 5, 1, 0, time.UTC),
  461. expected: "9:05:01 am",
  462. },
  463. }
  464. trans := New()
  465. for _, tt := range tests {
  466. s := trans.FmtTimeMedium(tt.t)
  467. if s != tt.expected {
  468. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  469. }
  470. }
  471. }
  472. func TestShortTime(t *testing.T) {
  473. tests := []struct {
  474. t time.Time
  475. expected string
  476. }{
  477. {
  478. t: time.Date(2016, 02, 03, 9, 5, 1, 0, time.UTC),
  479. expected: "9:05 am",
  480. },
  481. }
  482. trans := New()
  483. for _, tt := range tests {
  484. s := trans.FmtTimeShort(tt.t)
  485. if s != tt.expected {
  486. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  487. }
  488. }
  489. }
  490. func TestFullDate(t *testing.T) {
  491. tests := []struct {
  492. t time.Time
  493. expected string
  494. }{
  495. {
  496. t: time.Date(2016, 02, 03, 9, 0, 1, 0, time.UTC),
  497. expected: "Wednesday, February 3, 2016",
  498. },
  499. }
  500. trans := New()
  501. for _, tt := range tests {
  502. s := trans.FmtDateFull(tt.t)
  503. if s != tt.expected {
  504. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  505. }
  506. }
  507. }
  508. func TestLongDate(t *testing.T) {
  509. tests := []struct {
  510. t time.Time
  511. expected string
  512. }{
  513. {
  514. t: time.Date(2016, 02, 03, 9, 0, 1, 0, time.UTC),
  515. expected: "February 3, 2016",
  516. },
  517. }
  518. trans := New()
  519. for _, tt := range tests {
  520. s := trans.FmtDateLong(tt.t)
  521. if s != tt.expected {
  522. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  523. }
  524. }
  525. }
  526. func TestMediumDate(t *testing.T) {
  527. tests := []struct {
  528. t time.Time
  529. expected string
  530. }{
  531. {
  532. t: time.Date(2016, 02, 03, 9, 0, 1, 0, time.UTC),
  533. expected: "Feb 3, 2016",
  534. },
  535. }
  536. trans := New()
  537. for _, tt := range tests {
  538. s := trans.FmtDateMedium(tt.t)
  539. if s != tt.expected {
  540. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  541. }
  542. }
  543. }
  544. func TestShortDate(t *testing.T) {
  545. tests := []struct {
  546. t time.Time
  547. expected string
  548. }{
  549. {
  550. t: time.Date(2016, 02, 03, 9, 0, 1, 0, time.UTC),
  551. expected: "2/3/16",
  552. },
  553. }
  554. trans := New()
  555. for _, tt := range tests {
  556. s := trans.FmtDateShort(tt.t)
  557. if s != tt.expected {
  558. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  559. }
  560. }
  561. }
  562. func TestCurrency(t *testing.T) {
  563. tests := []struct {
  564. num float64
  565. v uint64
  566. currency currency.Type
  567. expected string
  568. }{
  569. {
  570. num: 1123456.5643,
  571. v: 2,
  572. currency: currency.USD,
  573. expected: "$1,123,456.56",
  574. },
  575. {
  576. num: 1123456.5643,
  577. v: 1,
  578. currency: currency.USD,
  579. expected: "$1,123,456.60",
  580. },
  581. {
  582. num: 221123456.5643,
  583. v: 3,
  584. currency: currency.USD,
  585. expected: "$221,123,456.564",
  586. },
  587. {
  588. num: -221123456.5643,
  589. v: 3,
  590. currency: currency.USD,
  591. expected: "-$221,123,456.564",
  592. },
  593. {
  594. num: -221123456.5643,
  595. v: 3,
  596. currency: currency.CAD,
  597. expected: "-CAD 221,123,456.564",
  598. },
  599. {
  600. num: 0,
  601. v: 2,
  602. currency: currency.USD,
  603. expected: "$0.00",
  604. },
  605. {
  606. num: -0,
  607. v: 2,
  608. currency: currency.USD,
  609. expected: "$0.00",
  610. },
  611. {
  612. num: -0,
  613. v: 2,
  614. currency: currency.CAD,
  615. expected: "CAD 0.00",
  616. },
  617. }
  618. trans := New()
  619. for _, tt := range tests {
  620. s := trans.FmtCurrency(tt.num, tt.v, tt.currency)
  621. if s != tt.expected {
  622. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  623. }
  624. }
  625. }
  626. func TestAccounting(t *testing.T) {
  627. tests := []struct {
  628. num float64
  629. v uint64
  630. currency currency.Type
  631. expected string
  632. }{
  633. {
  634. num: 1123456.5643,
  635. v: 2,
  636. currency: currency.USD,
  637. expected: "$1,123,456.56",
  638. },
  639. {
  640. num: 1123456.5643,
  641. v: 1,
  642. currency: currency.USD,
  643. expected: "$1,123,456.60",
  644. },
  645. {
  646. num: 221123456.5643,
  647. v: 3,
  648. currency: currency.USD,
  649. expected: "$221,123,456.564",
  650. },
  651. {
  652. num: -221123456.5643,
  653. v: 3,
  654. currency: currency.USD,
  655. expected: "($221,123,456.564)",
  656. },
  657. {
  658. num: -221123456.5643,
  659. v: 3,
  660. currency: currency.CAD,
  661. expected: "(CAD 221,123,456.564)",
  662. },
  663. {
  664. num: -0,
  665. v: 2,
  666. currency: currency.USD,
  667. expected: "$0.00",
  668. },
  669. {
  670. num: -0,
  671. v: 2,
  672. currency: currency.CAD,
  673. expected: "CAD 0.00",
  674. },
  675. }
  676. trans := New()
  677. for _, tt := range tests {
  678. s := trans.FmtAccounting(tt.num, tt.v, tt.currency)
  679. if s != tt.expected {
  680. t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
  681. }
  682. }
  683. }