Home | History | Annotate | Download | only in snakeyaml
      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  */
     16 package org.yaml.snakeyaml;
     17 
     18 import java.io.StringReader;
     19 import java.util.HashMap;
     20 import java.util.Iterator;
     21 import java.util.Map;
     22 
     23 import junit.framework.TestCase;
     24 
     25 import org.yaml.snakeyaml.events.*;
     26 import org.yaml.snakeyaml.nodes.Tag;
     27 
     28 public class YamlParseTest extends TestCase {
     29 
     30     public void testParse() {
     31         Yaml yaml = new Yaml();
     32         Event e = null;
     33         int counter = 0;
     34         for (Event event : yaml.parse(new StringReader("abc: 56"))) {
     35             if (e == null) {
     36                 assertTrue(event instanceof StreamStartEvent);
     37             }
     38             e = event;
     39             counter++;
     40         }
     41         assertTrue(e instanceof StreamEndEvent);
     42         assertEquals(8, counter);
     43     }
     44 
     45     public void testParseEvents() {
     46         Yaml yaml = new Yaml();
     47         Iterator<Event> events = yaml.parse(new StringReader("%YAML 1.1\n---\na")).iterator();
     48         assertTrue(events.next() instanceof StreamStartEvent);
     49         DocumentStartEvent documentStartEvent = (DocumentStartEvent) events.next();
     50         assertTrue(documentStartEvent.getExplicit());
     51         assertEquals(DumperOptions.Version.V1_1, documentStartEvent.getVersion());
     52         Map<String, String> DEFAULT_TAGS = new HashMap<String, String>();
     53         DEFAULT_TAGS.put("!", "!");
     54         DEFAULT_TAGS.put("!!", Tag.PREFIX);
     55         assertEquals(DEFAULT_TAGS, documentStartEvent.getTags());
     56         ScalarEvent scalarEvent = (ScalarEvent) events.next();
     57         assertNull(scalarEvent.getAnchor());
     58         assertNull(scalarEvent.getTag());
     59         assertEquals(new ImplicitTuple(true, false).toString(), scalarEvent.getImplicit().toString());
     60         DocumentEndEvent documentEndEvent = (DocumentEndEvent) events.next();
     61         assertFalse(documentEndEvent.getExplicit());
     62         assertTrue("Unexpected event.", events.next() instanceof StreamEndEvent);
     63         assertFalse(events.hasNext());
     64     }
     65 
     66     public void testParseManyDocuments() {
     67         Yaml yaml = new Yaml();
     68         Event e = null;
     69         int counter = 0;
     70         for (Event event : yaml.parse(new StringReader("abc: 56\n---\n4\n---\nqwe\n"))) {
     71             if (e == null) {
     72                 assertTrue(event instanceof StreamStartEvent);
     73             }
     74             e = event;
     75             counter++;
     76         }
     77         assertTrue(e instanceof StreamEndEvent);
     78         assertEquals(14, counter);
     79     }
     80 }
     81