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.resolver;
    18
    19  import junit.framework.TestCase;
    20
    21  public class RagelMachineTest extends TestCase {
    22      private RagelMachine machine = new RagelMachine();
    23
    24      public void testScan() {
    25          assertNull(machine.scan("abc"));
    26      }
    27
    28      public void testNullPointerException() {
    29          try {
    30              machine.scan(null);
    31              fail("null must not be accepted.");
    32          } catch (NullPointerException e) {
    33              assertEquals("Scalar must be provided.", e.getMessage());
    34          }
    35      }
    36
    37      public void testScanBoolean() {
    38          assertEquals("tag:yaml.org,2002:bool", machine.scan("true"));
    39          assertEquals("tag:yaml.org,2002:bool", machine.scan("True"));
    40          assertEquals("tag:yaml.org,2002:bool", machine.scan("TRUE"));
    41          assertEquals("tag:yaml.org,2002:bool", machine.scan("false"));
    42          assertEquals("tag:yaml.org,2002:bool", machine.scan("False"));
    43          assertEquals("tag:yaml.org,2002:bool", machine.scan("FALSE"));
    44          assertEquals("tag:yaml.org,2002:bool", machine.scan("on"));
    45          assertEquals("tag:yaml.org,2002:bool", machine.scan("ON"));
    46          assertEquals("tag:yaml.org,2002:bool", machine.scan("On"));
    47          assertEquals("tag:yaml.org,2002:bool", machine.scan("off"));
    48          assertEquals("tag:yaml.org,2002:bool", machine.scan("Off"));
    49          assertEquals("tag:yaml.org,2002:bool", machine.scan("OFF"));
    50          assertEquals("tag:yaml.org,2002:bool", machine.scan("on"));
    51          assertEquals("tag:yaml.org,2002:bool", machine.scan("ON"));
    52          assertEquals("tag:yaml.org,2002:bool", machine.scan("On"));
    53          assertEquals("tag:yaml.org,2002:bool", machine.scan("off"));
    54          assertEquals("tag:yaml.org,2002:bool", machine.scan("Off"));
    55          assertEquals("tag:yaml.org,2002:bool", machine.scan("OFF"));
    56      }
    57
    58      public void testScanNull() {
    59          assertEquals("tag:yaml.org,2002:null", machine.scan("null"));
    60          assertEquals("tag:yaml.org,2002:null", machine.scan("Null"));
    61          assertEquals("tag:yaml.org,2002:null", machine.scan("NULL"));
    62          assertEquals("tag:yaml.org,2002:null", machine.scan("~"));
    63          assertEquals("tag:yaml.org,2002:null", machine.scan(" "));
    64          assertEquals("tag:yaml.org,2002:null", machine.scan(""));
    65      }
    66
    67      public void testScanMerge() {
    68          assertEquals("tag:yaml.org,2002:merge", machine.scan("<<"));
    69      }
    70
    71      public void testScanValue() {
    72          assertEquals("tag:yaml.org,2002:value", machine.scan("="));
    73      }
    74
    75      public void testScanInt() {
    76          assertEquals("tag:yaml.org,2002:int", machine.scan("0"));
    77          assertEquals("tag:yaml.org,2002:int", machine.scan("1"));
    78          assertEquals("tag:yaml.org,2002:int", machine.scan("-0"));
    79          assertEquals("tag:yaml.org,2002:int", machine.scan("-9"));
    80          assertEquals("tag:yaml.org,2002:int", machine.scan("0b0011"));
    81          assertEquals("tag:yaml.org,2002:int", machine.scan("0x12ef"));
    82          assertEquals("tag:yaml.org,2002:int", machine.scan("0123"));
    83          assertEquals("tag:yaml.org,2002:int", machine.scan("1_000"));
    84          assertEquals("tag:yaml.org,2002:int", machine.scan("1_000_000"));
    85          assertEquals("tag:yaml.org,2002:int", machine.scan("+0"));
    86          assertEquals("tag:yaml.org,2002:int", machine.scan("+10"));
    87          assertEquals("tag:yaml.org,2002:int", machine.scan("1__000"));
    88          assertEquals("tag:yaml.org,2002:int", machine.scan("24:12:34"));
    89          assertEquals("tag:yaml.org,2002:int", machine.scan("240:12:34"));
    90      }
    91
    92      public void testScanFloat() {
    93          assertEquals("tag:yaml.org,2002:float", machine.scan("1.0"));
    94          assertEquals("tag:yaml.org,2002:float", machine.scan("-0.0"));
    95          assertEquals("tag:yaml.org,2002:float", machine.scan("+2.2310"));
    96          assertEquals("tag:yaml.org,2002:float", machine.scan("1.0e+12"));
    97          assertEquals("tag:yaml.org,2002:float", machine.scan("1.345e-3"));
    98          assertEquals("tag:yaml.org,2002:float", machine.scan("190:20:30.15"));
    99          assertEquals("tag:yaml.org,2002:float", machine.scan("-.inf"));
    100         assertEquals("tag:yaml.org,2002:float", machine.scan("+.INF"));
    101         assertEquals("tag:yaml.org,2002:float", machine.scan(".Inf"));
    102         assertEquals("tag:yaml.org,2002:float", machine.scan(".nan"));
    103         assertEquals("tag:yaml.org,2002:float", machine.scan(".NaN"));
    104         assertEquals("tag:yaml.org,2002:float", machine.scan(".NAN"));
    105         assertEquals("tag:yaml.org,2002:float", machine.scan("1_000.5"));
    106         assertEquals("tag:yaml.org,2002:float", machine.scan("1.023_456"));
    107         assertEquals("tag:yaml.org,2002:float", machine.scan("-1_123.45"));
    108         assertEquals("tag:yaml.org,2002:float", machine.scan(".5"));
    109         assertEquals("tag:yaml.org,2002:float", machine.scan("1.E+1"));
    110         assertNull(machine.scan("0x1,1"), machine.scan("0x1,1"));
    111     }
    112
    113     public void testScanTimestamp() {
    114         assertEquals("tag:yaml.org,2002:timestamp", machine.scan("2009-02-28"));
    115         assertEquals("tag:yaml.org,2002:timestamp", machine.scan("2001-12-15T02:59:43.1Z"));
    116         assertEquals("tag:yaml.org,2002:timestamp", machine.scan("2001-12-14t21:59:43.10-05:00"));
    117         assertEquals("tag:yaml.org,2002:timestamp", machine.scan("2001-12-14 21:59:43.10 -5"));
    118         assertEquals("tag:yaml.org,2002:timestamp", machine.scan("2001-12-15 2:59:43.10"));
    119     }
    120 }