package fitnesse.wikitext.parser;

import org.junit.Test;

import fitnesse.wiki.WikiPageDummy;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class FrontMatterTest {

  @Test
  public void parsesEmptyFrontMatter() {
    assertParses(
        "---\n" +
        "---\n",
      "SymbolList[FrontMatter]");
  }

  @Test
  public void parsesSecurityProperty() {
    assertParses(
      "---\n" +
        "LastModified: 20160706000400\n" +
        "secure-read\n" +
        "---\n",
      "SymbolList[FrontMatter[KeyValue[Text, Text], KeyValue[Text, Text]]]");
  }

  @Test
  public void notAValidFrontMatter() {
    assertParses(
        "---\n" +
        "test" +
        "---\n",
      "SymbolList[Text]");
  }

  @Test
  public void parseFrontMatterWithText() {
    assertParses(
        "---\n" +
        "test\n" +
        "---\n" +
        "WikiText",
      "SymbolList[FrontMatter[KeyValue[Text, Text]], Text]");
  }

  @Test
  public void parseFrontMatterWithKeyValueText() {
    assertParses(
        "---\n" +
        "test: value with whitespace\n" +
        "---\n",
      "SymbolList[FrontMatter[KeyValue[Text, Text]]]");
  }

  @Test
  public void parseFrontMatterWithSymbolicLinkText() {
    assertParses(
        "---\n" +
        "symbolic-links:\n" +
        "  pageName: .FrontPage\n" +
        "  pageName: .FrontPage\n" +
        "---\n",
      "SymbolList[FrontMatter[KeyValue[Text, Text, KeyValue[Text, Text], KeyValue[Text, Text]]]]");
  }

  @Test
  public void readFrontMatter() {
    final Symbol symbols = parse(
        "---\n" +
        "symbolic-links:\n" +
        "  pageName: .FrontPage\n" +
        "---\n");
    Symbol frontMatter = symbols.getChildren().get(0);
    Symbol symlinks = frontMatter.getChildren().get(0);
    Symbol firstSymlink = symlinks.getChildren().get(2);

    assertTrue(frontMatter.isType(FrontMatter.symbolType));
    assertEquals("symbolic-links", symlinks.getChildren().get(0).getContent());
    assertEquals("pageName", firstSymlink.getChildren().get(0).getContent());
    assertEquals(".FrontPage", firstSymlink.getChildren().get(1).getContent());
  }

  @Test
  public void readUrlInFrontMatter() {
    final Symbol symbols = parse(
      "---\n" +
        "symbolic-links:\n" +
        "  pageName: file://test\n" +
        "---\n");
    Symbol frontMatter = symbols.getChildren().get(0);
    Symbol symlinks = frontMatter.getChildren().get(0);
    Symbol firstSymlink = symlinks.getChildren().get(2);

    assertTrue(frontMatter.isType(FrontMatter.symbolType));
    assertEquals("symbolic-links", symlinks.getChildren().get(0).getContent());
    assertEquals("pageName", firstSymlink.getChildren().get(0).getContent());
    assertEquals("file://test", firstSymlink.getChildren().get(1).getContent());
  }

  public void assertParses(String input, String expected) {
    Symbol result = parse(input);
    assertEquals(expected, ParserTestHelper.serialize(result));
  }

  private Symbol parse(final String input) {
    return Parser.make(new ParsingPage(new WikiSourcePage(new WikiPageDummy())), input,
      new SymbolProvider(new SymbolType[] { FrontMatter.symbolType, SymbolType.Text })).parse();
  }
}

@@ -2146,32 +2146,722 @@
 ontent());%0A  %7D%0A%0A
+  @Test%0A  public void readFrontMatterWithManySpacesBeforeChildEntry() %7B%0A    final Symbol symbols = parse(%0A      %22---%5Cn%22 +%0A        %22symbolic-links:%5Cn%22 +%0A        %22             pageName: .FrontPage%5Cn%22 +%0A        %22---%5Cn%22);%0A    Symbol frontMatter = symbols.getChildren().get(0);%0A    Symbol symlinks = frontMatter.getChildren().get(0);%0A    Symbol firstSymlink = symlinks.getChildren().get(2);%0A%0A    assertTrue(frontMatter.isType(FrontMatter.symbolType));%0A    assertEquals(%22symbolic-links%22, symlinks.getChildren().get(0).getContent());%0A    assertEquals(%22pageName%22, firstSymlink.getChildren().get(0).getContent());%0A    assertEquals(%22.FrontPage%22, firstSymlink.getChildren().get(1).getContent());%0A  %7D%0A%0A
   @Test%0A  public

Add test for yaml sub field with lots of space