|
1 | | -import DummyClass from "../src/jp-numerals" |
| 1 | +import { jpNumerals, Numeral, NumeralZero } from '../src/jp-numerals' |
| 2 | +import { JpNumeralUnit } from '../src/type' |
2 | 3 |
|
3 | | -/** |
4 | | - * Dummy test |
5 | | - */ |
6 | | -describe("Dummy test", () => { |
7 | | - it("works if true is truthy", () => { |
8 | | - expect(true).toBeTruthy() |
| 4 | +describe('Numeral Class', () => { |
| 5 | + it('can handle integer', () => { |
| 6 | + const n = new Numeral(JpNumeralUnit.万, 123_4567) |
| 7 | + expect(n.unit).toEqual(JpNumeralUnit.万) |
| 8 | + expect(n.raw).toEqual(123_4567) |
| 9 | + |
| 10 | + expect(n.rank).toEqual(1) |
| 11 | + expect(n.digits).toEqual(123) |
| 12 | + expect(n.character).toEqual('万') |
| 13 | + |
| 14 | + expect(n.toString()).toEqual('123万') |
| 15 | + expect(n.toNumeralObj()).toEqual({ |
| 16 | + unit: JpNumeralUnit.万, |
| 17 | + character: '万', |
| 18 | + rank: 1, |
| 19 | + digits: 123 |
| 20 | + }) |
| 21 | + }) |
| 22 | + |
| 23 | + it('can handle float', () => { |
| 24 | + const n = new Numeral(JpNumeralUnit.万, 123_4567.789) |
| 25 | + expect(n.raw).toEqual(123_4567.789) |
| 26 | + |
| 27 | + expect(n.digits).toEqual(123) |
| 28 | + expect(n.character).toEqual('万') |
| 29 | + |
| 30 | + expect(n.toNumeralObj()).toEqual({ |
| 31 | + unit: JpNumeralUnit.万, |
| 32 | + character: '万', |
| 33 | + rank: 1, |
| 34 | + digits: 123 |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + it('raise error if number is negative', () => { |
| 39 | + expect(() => new Numeral(JpNumeralUnit.零, -123_4567.789)).toThrow() |
| 40 | + }) |
| 41 | +}) |
| 42 | + |
| 43 | +describe('class NumeralZero', () => { |
| 44 | + it('exclude "零" for stringification', () => { |
| 45 | + const n = new NumeralZero(JpNumeralUnit.零, 123_4567) |
| 46 | + expect(n.toString()).toEqual('4567') |
| 47 | + }) |
| 48 | + |
| 49 | + it('can display decimal number', () => { |
| 50 | + const n = new NumeralZero(JpNumeralUnit.零, 123_4567.89) |
| 51 | + expect(n.toString()).toEqual('4567.89') |
9 | 52 | }) |
| 53 | +}) |
| 54 | + |
| 55 | +describe('jpNumerals', () => { |
| 56 | + it('can handle integer', () => { |
| 57 | + const n = jpNumerals(12_3456_7890) |
| 58 | + expect(n.toTuples()).toEqual([[12, '億'], [3456, '万'], [7890, '']]) |
| 59 | + |
| 60 | + expect(n.toArray()).toEqual([ |
| 61 | + new Numeral(JpNumeralUnit.億, 12_3456_7890), |
| 62 | + new Numeral(JpNumeralUnit.万, 12_3456_7890), |
| 63 | + new Numeral(JpNumeralUnit.零, 12_3456_7890) |
| 64 | + ]) |
| 65 | + |
| 66 | + expect(n.toNumeralObjs()).toEqual([ |
| 67 | + { |
| 68 | + unit: JpNumeralUnit.億, |
| 69 | + character: '億', |
| 70 | + rank: 2, |
| 71 | + digits: 12 |
| 72 | + }, |
| 73 | + { |
| 74 | + unit: JpNumeralUnit.万, |
| 75 | + character: '万', |
| 76 | + rank: 1, |
| 77 | + digits: 3456 |
| 78 | + }, |
| 79 | + { |
| 80 | + unit: JpNumeralUnit.零, |
| 81 | + character: '', |
| 82 | + rank: 0, |
| 83 | + digits: 7890 |
| 84 | + } |
| 85 | + ]) |
| 86 | + |
| 87 | + expect(n.toString()).toEqual('12億3456万7890') |
| 88 | + expect(n.toNumber()).toEqual(1234567890) |
| 89 | + }) |
| 90 | + |
| 91 | + it('can handle float', () => { |
| 92 | + const n = jpNumerals(12_3456_7890.123) |
| 93 | + expect(n.toTuples()).toEqual([[12, '億'], [3456, '万'], [7890.123, '']]) |
| 94 | + }) |
| 95 | + |
| 96 | + it('throw error if number is negative', () => { |
| 97 | + expect(() => jpNumerals(-12_3456_7890.123)).toThrow() |
| 98 | + }) |
| 99 | + |
| 100 | + it('can use base as option', () => { |
| 101 | + const n_base = jpNumerals(12_3456, JpNumeralUnit.万) |
| 102 | + const n = jpNumerals(12_3456_0000) |
| 103 | + |
| 104 | + expect(n_base.toTuples()).toEqual(n.toTuples()) |
| 105 | + expect(n_base.toArray()).toEqual(n.toArray()) |
| 106 | + expect(n_base.toNumeralObjs()).toEqual(n.toNumeralObjs()) |
10 | 107 |
|
11 | | - it("DummyClass is instantiable", () => { |
12 | | - expect(new DummyClass()).toBeInstanceOf(DummyClass) |
| 108 | + expect(n_base.toString()).toEqual(n.toString()) |
| 109 | + expect(n_base.toNumber()).toEqual(1234560000) |
13 | 110 | }) |
14 | 111 | }) |
0 commit comments