package org.pageseeder.diffx.sequence;

import junit.framework.AssertionFailedError;
import org.junit.Test;
import org.pageseeder.diffx.load.LoadingException;
import org.pageseeder.diffx.load.SAXRecorder;
import org.pageseeder.diffx.sequence.EventSequence;
import org.pageseeder.diffx.sequence.SequenceFolding;
import org.xml.sax.InputSource;

import java.io.IOException;
import java.io.StringReader;
import junit.framework.TestCase;

public class SequenceFoldingTest extends TestCase {

  @Test public void testEmpty() throws IOException {
    EventSequence sequence = new EventSequence();
    EventSequence out = SequenceFolding.forAllElements().fold(sequence);
    assertEquals(0, out.size());
  }

  @Test public void testSimple() throws IOException {
    EventSequence sequence = getSequence("<a/>");
    EventSequence out = SequenceFolding.forAllElements().fold(sequence);
    assertEquals(1, out.size());
  }

  @Test public void testSimple1a() throws IOException {
    EventSequence sequence = getSequence("<a><b/></a>");
    EventSequence out = SequenceFolding.forAllElements().fold(sequence);
    assertEquals(1, out.size());
  }

  @Test public void testSimple1b() throws IOException {
    EventSequence sequence = getSequence("<a><b/></a>");
    EventSequence out = SequenceFolding.forElements(new String[]{"a"}).fold(sequence);
    assertEquals(1, out.size());
  }

  @Test public void testSimple1c() throws IOException {
    EventSequence sequence = getSequence("<a><b/></a>");
    EventSequence out = SequenceFolding.forElements(new String[]{"b"}).fold(sequence);
    assertEquals(3, out.size());
  }

  private static EventSequence getSequence(String xml) {
    try {
      SAXRecorder recorder = new SAXRecorder();
      return recorder.process(new InputSource(new StringReader(xml)));
    } catch (LoadingException | IOException ex) {
      throw new AssertionFailedError("Unable record specified XML");
    }
  }

}

@@ -1610,16 +1610,1032 @@
 );%0A  %7D%0A%0A
+  @Test public void testDeep1a() throws IOException %7B%0A    EventSequence sequence = getSequence(%22%3Ca%3E%3Cb%3E%3Cc%3E%3Cd%3Ex%3C/d%3E%3C/c%3E%3C/b%3E%3C/a%3E%22);%0A    EventSequence out = SequenceFolding.forElements(new String%5B%5D%7B%22a%22%7D).fold(sequence);%0A    assertEquals(1, out.size());%0A  %7D%0A%0A  @Test public void testDeep1b() throws IOException %7B%0A    EventSequence sequence = getSequence(%22%3Ca%3E%3Cb%3E%3Cc%3E%3Cd%3Ex%3C/d%3E%3C/c%3E%3C/b%3E%3C/a%3E%22);%0A    EventSequence out = SequenceFolding.forElements(new String%5B%5D%7B%22b%22%7D).fold(sequence);%0A    assertEquals(3, out.size());%0A  %7D%0A%0A  @Test public void testDeep1c() throws IOException %7B%0A    EventSequence sequence = getSequence(%22%3Ca%3E%3Cb%3E%3Cc%3E%3Cd%3Ex%3C/d%3E%3C/c%3E%3C/b%3E%3C/a%3E%22);%0A    EventSequence out = SequenceFolding.forElements(new String%5B%5D%7B%22c%22%7D).fold(sequence);%0A    assertEquals(5, out.size());%0A  %7D%0A%0A  @Test public void testDeep1d() throws IOException %7B%0A    EventSequence sequence = getSequence(%22%3Ca%3E%3Cb%3E%3Cc%3E%3Cd%3Ex%3C/d%3E%3C/c%3E%3C/b%3E%3C/a%3E%22);%0A    EventSequence out = SequenceFolding.forElements(new String%5B%5D%7B%22d%22%7D).fold(sequence);%0A    assertEquals(7, out.size());%0A  %7D%0A%0A
   privat

Add unit test in sequence folding