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