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.yaml.snakeyaml.parser;
    18
    19  import java.util.LinkedList;
    20
    21  import junit.framework.TestCase;
    22
    23  import org.yaml.snakeyaml.error.Mark;
    24  import org.yaml.snakeyaml.events.DocumentEndEvent;
    25  import org.yaml.snakeyaml.events.DocumentStartEvent;
    26  import org.yaml.snakeyaml.events.Event;
    27  import org.yaml.snakeyaml.events.ImplicitTuple;
    28  import org.yaml.snakeyaml.events.MappingEndEvent;
    29  import org.yaml.snakeyaml.events.MappingStartEvent;
    30  import org.yaml.snakeyaml.events.ScalarEvent;
    31  import org.yaml.snakeyaml.events.SequenceEndEvent;
    32  import org.yaml.snakeyaml.events.SequenceStartEvent;
    33  import org.yaml.snakeyaml.events.StreamEndEvent;
    34  import org.yaml.snakeyaml.events.StreamStartEvent;
    35  import org.yaml.snakeyaml.reader.StreamReader;
    36
    37  public class ParserImplTest extends TestCase {
    38
    39      public void testGetEvent() {
    40          String data = "string: abcd";
    41          StreamReader reader = new StreamReader(data);
    42          Parser parser = new ParserImpl(reader);
    43          Mark dummyMark = new Mark("dummy", 0, 0, 0, "", 0);
    44          LinkedList<Event> etalonEvents = new LinkedList<Event>();
    45          etalonEvents.add(new StreamStartEvent(dummyMark, dummyMark));
    46          etalonEvents.add(new DocumentStartEvent(dummyMark, dummyMark, false, null, null));
    47          etalonEvents.add(new MappingStartEvent(null, null, true, dummyMark, dummyMark,
    48                  Boolean.FALSE));
    49          etalonEvents.add(new ScalarEvent(null, null, new ImplicitTuple(true, false), "string",
    50                  dummyMark, dummyMark, (char) 0));
    51          etalonEvents.add(new ScalarEvent(null, null, new ImplicitTuple(true, false), "abcd",
    52                  dummyMark, dummyMark, (char) 0));
    53          etalonEvents.add(new MappingEndEvent(dummyMark, dummyMark));
    54          etalonEvents.add(new DocumentEndEvent(dummyMark, dummyMark, false));
    55          etalonEvents.add(new StreamEndEvent(dummyMark, dummyMark));
    56          while (parser.checkEvent(null)) {
    57              Event event = parser.getEvent();
    58              if (etalonEvents.isEmpty()) {
    59                  fail("unexpected event: " + event);
    60              }
    61              assertEquals(etalonEvents.removeFirst(), event);
    62          }
    63          assertFalse("Must contain no more events: " + parser.getEvent(), parser.checkEvent(null));
    64      }
    65
    66      public void testGetEvent2() {
    67          String data = "american:\n  - Boston Red Sox";
    68          StreamReader reader = new StreamReader(data);
    69          Parser parser = new ParserImpl(reader);
    70          Mark dummyMark = new Mark("dummy", 0, 0, 0, "", 0);
    71          LinkedList<Event> etalonEvents = new LinkedList<Event>();
    72          etalonEvents.add(new StreamStartEvent(dummyMark, dummyMark));
    73          etalonEvents.add(new DocumentStartEvent(dummyMark, dummyMark, false, null, null));
    74          etalonEvents
    75                  .add(new MappingStartEvent(null, null, true, dummyMark, dummyMark, Boolean.TRUE));
    76          etalonEvents.add(new ScalarEvent(null, null, new ImplicitTuple(true, false), "american",
    77                  dummyMark, dummyMark, (char) 0));
    78          etalonEvents.add(new SequenceStartEvent(null, null, true, dummyMark, dummyMark,
    79                  Boolean.FALSE));
    80          etalonEvents.add(new ScalarEvent(null, null, new ImplicitTuple(true, false),
    81                  "Boston Red Sox", dummyMark, dummyMark, (char) 0));
    82          etalonEvents.add(new SequenceEndEvent(dummyMark, dummyMark));
    83          etalonEvents.add(new MappingEndEvent(dummyMark, dummyMark));
    84          etalonEvents.add(new DocumentEndEvent(dummyMark, dummyMark, false));
    85          etalonEvents.add(new StreamEndEvent(dummyMark, dummyMark));
    86          while (parser.checkEvent(null)) {
    87              Event event = parser.getEvent();
    88              if (etalonEvents.isEmpty()) {
    89                  fail("unexpected event: " + event);
    90              }
    91              assertEquals(etalonEvents.removeFirst(), event);
    92          }
    93          assertFalse("Must contain no more events: " + parser.getEvent(), parser.checkEvent(null));
    94      }
    95  }