View Javadoc

    1   /**
    2    * Copyright (c) 2008-2011, 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
    17  package org.yaml.snakeyaml.types;
    18
    19  import java.io.IOException;
    20  import java.math.BigInteger;
    21  import java.util.HashMap;
    22  import java.util.Map;
    23
    24  /**
    25   * @see http://yaml.org/type/int.html
    26   */
    27  public class IntTagTest extends AbstractTest {
    28
    29      public void testInt() throws IOException {
    30          assertEquals(new Integer(685230), getMapValue("canonical: 685230", "canonical"));
    31          assertEquals(new Integer(685230), getMapValue("number: 685_230", "number"));
    32          assertEquals(new Integer(685230), getMapValue("decimal: +685230", "decimal"));
    33          assertEquals(new Integer(-685230), getMapValue("number: -685230", "number"));
    34          assertEquals(new Integer(685230), getMapValue("octal: 02472256", "octal"));
    35          assertEquals(new Integer(685230), getMapValue("hexadecimal: 0x_0A_74_AE", "hexadecimal"));
    36          assertEquals(new Integer(685230),
    37                  getMapValue("binary: 0b1010_0111_0100_1010_1110", "binary"));
    38          assertEquals(new Integer(685230), getMapValue("sexagesimal: 190:20:30", "sexagesimal"));
    39          assertEquals(new Integer(0), load("0"));
    40          assertEquals(new Integer(0), load("-0"));
    41          assertEquals(new Integer(0), load("+0"));
    42          assertEquals(Integer.MIN_VALUE, load(dump(Integer.MIN_VALUE)));
    43          assertEquals(Integer.MAX_VALUE, load(dump(Integer.MAX_VALUE)));
    44      }
    45
    46      public void testBigInt() throws IOException {
    47          assertEquals(new Long(922337203685477580L), load("922337203685477580"));
    48          assertEquals(new BigInteger("9223372036854775809999999999"),
    49                  load("9223372036854775809999999999"));
    50          assertEquals(Long.MIN_VALUE, load("-9223372036854775808"));
    51      }
    52
    53      public void testIntShorthand() throws IOException {
    54          assertEquals(new Integer(1), getMapValue("number: !!int 1", "number"));
    55      }
    56
    57      public void testIntTag() throws IOException {
    58          assertEquals(new Integer(1), getMapValue("number: !<tag:yaml.org,2002:int> 1", "number"));
    59      }
    60
    61      public void testIntOut() throws IOException {
    62          Map<String, Integer> map = new HashMap<String, Integer>();
    63          map.put("number", new Integer(1));
    64          String output = dump(map);
    65          assertTrue(output.contains("number: 1"));
    66      }
    67
    68  }