1/**
2 * Copyright (c) 2008, 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 */
16package org.yaml.snakeyaml.scanner;
17
18import java.util.LinkedList;
19
20import junit.framework.TestCase;
21
22import org.yaml.snakeyaml.Yaml;
23import org.yaml.snakeyaml.error.Mark;
24import org.yaml.snakeyaml.reader.StreamReader;
25import org.yaml.snakeyaml.tokens.BlockEndToken;
26import org.yaml.snakeyaml.tokens.BlockMappingStartToken;
27import org.yaml.snakeyaml.tokens.KeyToken;
28import org.yaml.snakeyaml.tokens.ScalarToken;
29import org.yaml.snakeyaml.tokens.StreamEndToken;
30import org.yaml.snakeyaml.tokens.StreamStartToken;
31import org.yaml.snakeyaml.tokens.Token;
32import org.yaml.snakeyaml.tokens.ValueToken;
33
34public 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\n"
65                            + "found character '\\t(TAB)' that cannot start any token. (Do not use \\t(TAB) for indentation)\n"
66                            + " in 'string', line 1, column 1:\n" + "    \t  data: 1\n" + "    ^\n",
67                    e.getMessage());
68        }
69    }
70}
71