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.pyyaml;
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.yaml.snakeyaml.events.Event;
27  import org.yaml.snakeyaml.tokens.Token;
28  
29  /**
30   * @see imported from PyYAML
31   */
32  public class PyCanonicalTest extends PyImportTest {
33  
34      public void testCanonicalScanner() throws IOException {
35          File[] files = getStreamsByExtension(".canonical");
36          assertTrue("No test files found.", files.length > 0);
37          for (int i = 0; i < files.length; i++) {
38              InputStream input = new FileInputStream(files[i]);
39              List<Token> tokens = canonicalScan(input);
40              input.close();
41              assertFalse(tokens.isEmpty());
42          }
43      }
44  
45      private List<Token> canonicalScan(InputStream input) throws IOException {
46          int ch = input.read();
47          StringBuilder buffer = new StringBuilder();
48          while (ch != -1) {
49              buffer.append((char) ch);
50              ch = input.read();
51          }
52          CanonicalScanner scanner = new CanonicalScanner(buffer.toString());
53          List<Token> result = new ArrayList<Token>();
54          while (scanner.peekToken() != null) {
55              result.add(scanner.getToken());
56          }
57          return result;
58      }
59  
60      public void testCanonicalParser() throws IOException {
61          File[] files = getStreamsByExtension(".canonical");
62          assertTrue("No test files found.", files.length > 0);
63          for (int i = 0; i < files.length; i++) {
64              InputStream input = new FileInputStream(files[i]);
65              List<Event> tokens = canonicalParse(input);
66              input.close();
67              assertFalse(tokens.isEmpty());
68          }
69      }
70  }