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.scanner;
    18
    19  import java.io.IOException;
    20  import java.util.LinkedList;
    21
    22  import junit.framework.TestCase;
    23
    24  import org.yaml.snakeyaml.Yaml;
    25  import org.yaml.snakeyaml.error.Mark;
    26  import org.yaml.snakeyaml.reader.StreamReader;
    27  import org.yaml.snakeyaml.tokens.BlockEndToken;
    28  import org.yaml.snakeyaml.tokens.BlockMappingStartToken;
    29  import org.yaml.snakeyaml.tokens.KeyToken;
    30  import org.yaml.snakeyaml.tokens.ScalarToken;
    31  import org.yaml.snakeyaml.tokens.StreamEndToken;
    32  import org.yaml.snakeyaml.tokens.StreamStartToken;
    33  import org.yaml.snakeyaml.tokens.Token;
    34  import org.yaml.snakeyaml.tokens.ValueToken;
    35
    36  public class ScannerImplTest extends TestCase {
    37
    38      public void testGetToken() {
    39          String data = "string: abcd";
    40          StreamReader reader = new StreamReader(data);
    41          Scanner scanner = new ScannerImpl(reader);
    42          Mark dummy = new Mark("dummy", 0, 0, 0, "", 0);
    43          LinkedList<Token> etalonTokens = new LinkedList<Token>();
    44          etalonTokens.add(new StreamStartToken(dummy, dummy));
    45          etalonTokens.add(new BlockMappingStartToken(dummy, dummy));
    46          etalonTokens.add(new KeyToken(dummy, dummy));
    47          etalonTokens.add(new ScalarToken("string", true, dummy, dummy, (char) 0));
    48          etalonTokens.add(new ValueToken(dummy, dummy));
    49          etalonTokens.add(new ScalarToken("abcd", true, dummy, dummy, (char) 0));
    50          etalonTokens.add(new BlockEndToken(dummy, dummy));
    51          etalonTokens.add(new StreamEndToken(dummy, dummy));
    52          while (!etalonTokens.isEmpty() && scanner.checkToken(etalonTokens.get(0).getTokenId())) {
    53              assertEquals(etalonTokens.removeFirst(), scanner.getToken());
    54          }
    55          assertFalse("Must contain no more tokens: " + scanner.getToken(),
    56                  scanner.checkToken(new Token.ID[0]));
    57      }
    58
    59      public void testWrongTab() throws IOException {
    60          Yaml yaml = new Yaml();
    61          try {
    62              yaml.load("\t  data: 1");
    63              fail("TAB cannot start a token.");
    64          } catch (Exception e) {
    65              assertEquals(
    66                      "while scanning for the next token; found character \t'\\t' that cannot start any token",
    67                      e.getMessage());
    68          }
    69      }
    70  }