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 org.yaml.snakeyaml.error.Mark;
    20
    21  /**
    22   * @see imported from PyYAML
    23   */
    24  public class PyMarkTest extends PyImportTest {
    25
    26      public void testMarks() {
    27          String content = getResource("test_mark.marks");
    28          String[] inputs = content.split("---\n");
    29          for (int i = 1; i < inputs.length; i++) {
    30              String input = inputs[i];
    31              int index = 0;
    32              int line = 0;
    33              int column = 0;
    34              while (input.charAt(index) != '*') {
    35                  if (input.charAt(index) != '\n') {
    36                      line += 1;
    37                      column = 0;
    38                  } else {
    39                      column += 1;
    40                  }
    41                  index += 1;
    42              }
    43              Mark mark = new Mark("testMarks", index, line, column, input, index);
    44              String snippet = mark.get_snippet(2, 79);
    45              assertTrue("Must only have one '\n'.", snippet.indexOf("\n") > -1);
    46              assertEquals("Must only have only one '\n'.", snippet.indexOf("\n"),
    47                      snippet.lastIndexOf("\n"));
    48              String[] lines = snippet.split("\n");
    49              String data = lines[0];
    50              String pointer = lines[1];
    51              assertTrue("Mark must be restricted: " + data, data.length() < 82);
    52              int dataPosition = data.indexOf("*");
    53              int pointerPosition = pointer.indexOf("^");
    54              assertEquals("Pointer should coincide with '*':\n " + snippet, dataPosition,
    55                      pointerPosition);
    56          }
    57      }
    58  }