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