View Javadoc

1   /**
2    * Copyright (c) 2008-2012, http://www.snakeyaml.org
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.yaml.snakeyaml.types;
17  
18  import java.math.BigInteger;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  /**
23   * @see http://yaml.org/type/int.html
24   */
25  public class IntTagTest extends AbstractTest {
26  
27      public void testInt() {
28          assertEquals(new Integer(685230), getMapValue("canonical: 685230", "canonical"));
29          assertEquals(new Integer(685230), getMapValue("number: 685_230", "number"));
30          assertEquals(new Integer(685230), getMapValue("decimal: +685230", "decimal"));
31          assertEquals(new Integer(-685230), getMapValue("number: -685230", "number"));
32          assertEquals(new Integer(685230), getMapValue("octal: 02472256", "octal"));
33          assertEquals(new Integer(685230), getMapValue("hexadecimal: 0x_0A_74_AE", "hexadecimal"));
34          assertEquals(new Integer(685230),
35                  getMapValue("binary: 0b1010_0111_0100_1010_1110", "binary"));
36          assertEquals(new Integer(685230), getMapValue("sexagesimal: 190:20:30", "sexagesimal"));
37          assertEquals(new Integer(0), load("0"));
38          assertEquals(new Integer(0), load("-0"));
39          assertEquals(new Integer(0), load("+0"));
40          assertEquals(Integer.MIN_VALUE, load(dump(Integer.MIN_VALUE)));
41          assertEquals(Integer.MAX_VALUE, load(dump(Integer.MAX_VALUE)));
42      }
43  
44      public void testBigInt() {
45          assertEquals(new Long(922337203685477580L), load("922337203685477580"));
46          assertEquals(new BigInteger("9223372036854775809999999999"),
47                  load("9223372036854775809999999999"));
48          assertEquals(Long.MIN_VALUE, load("-9223372036854775808"));
49      }
50  
51      public void testIntShorthand() {
52          assertEquals(new Integer(1), getMapValue("number: !!int 1", "number"));
53      }
54  
55      public void testIntTag() {
56          assertEquals(new Integer(1), getMapValue("number: !<tag:yaml.org,2002:int> 1", "number"));
57      }
58  
59      public void testIntOut() {
60          Map<String, Integer> map = new HashMap<String, Integer>();
61          map.put("number", new Integer(1));
62          String output = dump(map);
63          assertTrue(output.contains("number: 1"));
64      }
65  }