1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.ancientprogramming.fixedformat4j.format.impl;
17
18 import com.ancientprogramming.fixedformat4j.annotation.Align;
19 import com.ancientprogramming.fixedformat4j.annotation.Sign;
20 import com.ancientprogramming.fixedformat4j.annotation.FixedFormatNumber;
21 import static com.ancientprogramming.fixedformat4j.annotation.FixedFormatNumber.*;
22 import com.ancientprogramming.fixedformat4j.format.FixedFormatter;
23 import com.ancientprogramming.fixedformat4j.format.FormatInstructions;
24 import com.ancientprogramming.fixedformat4j.format.data.FixedFormatDecimalData;
25 import com.ancientprogramming.fixedformat4j.format.data.FixedFormatNumberData;
26 import junit.framework.TestCase;
27
28
29
30
31
32 public class TestIntegerFormatter extends TestCase {
33
34 private FixedFormatter formatter = new IntegerFormatter();
35
36 public void testParse() {
37 assertEquals(100, formatter.parse("0000000100", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null)));
38 assertEquals(100, formatter.parse("100", new FormatInstructions(3, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null)));
39 assertEquals(1, formatter.parse("01", new FormatInstructions(2, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null)));
40 assertEquals(1234, formatter.parse("0000001234", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null)));
41 assertEquals(0, formatter.parse("+000000000", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), null)));
42 assertEquals(0, formatter.parse("-000000000", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), null)));
43 assertEquals(-1234, formatter.parse("-000001234", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), null)));
44 }
45
46 public void testFormat() {
47 assertEquals("+000000100", formatter.format(100, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.'))));
48 assertEquals("+000000101", formatter.format(101, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(1, false, '.'))));
49 assertEquals("+000001234", formatter.format(1234, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.'))));
50 assertEquals("-000001234", formatter.format(-1234, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.'))));
51 assertEquals("+000000000", formatter.format(0, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.'))));
52 assertEquals("+000000000", formatter.format(null, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.'))));
53 }
54 }