1 /** 2 * Copyright (c) 2008, SnakeYAML 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package org.pyyaml; 15 16 import java.io.File; 17 import java.io.FileInputStream; 18 import java.io.IOException; 19 import java.io.InputStream; 20 import java.util.ArrayList; 21 import java.util.List; 22 import org.yaml.snakeyaml.events.Event; 23 import org.yaml.snakeyaml.tokens.Token; 24 25 /** 26 * imported from PyYAML 27 */ 28 public class PyCanonicalTest extends PyImportTest { 29 testCanonicalScanner()30 public void testCanonicalScanner() throws IOException { 31 File[] files = getStreamsByExtension(".canonical"); 32 assertTrue("No test files found.", files.length > 0); 33 for (int i = 0; i < files.length; i++) { 34 InputStream input = new FileInputStream(files[i]); 35 List<Token> tokens = canonicalScan(input); 36 input.close(); 37 assertFalse(tokens.isEmpty()); 38 } 39 } 40 canonicalScan(InputStream input)41 private List<Token> canonicalScan(InputStream input) throws IOException { 42 int ch = input.read(); 43 StringBuilder buffer = new StringBuilder(); 44 while (ch != -1) { 45 buffer.append((char) ch); 46 ch = input.read(); 47 } 48 CanonicalScanner scanner = 49 new CanonicalScanner(buffer.toString().replace(System.lineSeparator(), "\n")); 50 List<Token> result = new ArrayList<Token>(); 51 while (scanner.peekToken() != null) { 52 result.add(scanner.getToken()); 53 } 54 return result; 55 } 56 testCanonicalParser()57 public void testCanonicalParser() throws IOException { 58 File[] files = getStreamsByExtension(".canonical"); 59 assertTrue("No test files found.", files.length > 0); 60 for (int i = 0; i < files.length; i++) { 61 InputStream input = new FileInputStream(files[i]); 62 List<Event> tokens = canonicalParse(input); 63 input.close(); 64 assertFalse(tokens.isEmpty()); 65 } 66 } 67 } 68