package com.hubspot.jinjava.lib.tag.eager;

import static org.assertj.core.api.Java6Assertions.assertThat;

import com.hubspot.jinjava.ExpectedNodeInterpreter;
import com.hubspot.jinjava.JinjavaConfig;
import com.hubspot.jinjava.LegacyOverrides;
import com.hubspot.jinjava.interpret.DeferredValue;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.lib.tag.CycleTagTest;
import com.hubspot.jinjava.lib.tag.Tag;
import com.hubspot.jinjava.mode.EagerExecutionMode;
import com.hubspot.jinjava.tree.parse.TagToken;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class EagerCycleTagTest extends CycleTagTest {
  private static final long MAX_OUTPUT_SIZE = 500L;
  private Tag tag;

  @Before
  public void eagerSetup() {
    interpreter =
      new JinjavaInterpreter(
        jinjava,
        context,
        JinjavaConfig
          .newBuilder()
          .withMaxOutputSize(MAX_OUTPUT_SIZE)
          .withExecutionMode(EagerExecutionMode.instance())
          .withLegacyOverrides(
            LegacyOverrides.newBuilder().withUsePyishObjectMapper(true).build()
          )
          .build()
      );

    tag = new EagerCycleTag();
    context.registerTag(tag);
    context.put("deferred", DeferredValue.instance());
    context.registerTag(new EagerForTag());
    JinjavaInterpreter.pushCurrent(interpreter);
  }

  @After
  public void teardown() {
    JinjavaInterpreter.popCurrent();
  }

  @Test
  public void itHandlesDeferredCycle() {
    String template =
      "{% for item in deferred %}{% cycle 'item-1','item-2' %}{% endfor %}";
    assertThat(interpreter.render(template)).isEqualTo(template);
    Optional<DeferredToken> maybeDeferredToken = context
      .getDeferredTokens()
      .stream()
      .filter(e -> ((TagToken) e.getToken()).getTagName().equals(tag.getName()))
      .findAny();
    assertThat(maybeDeferredToken.isPresent());
    assertThat(maybeDeferredToken.get().getToken().getImage())
      .isEqualTo("{% cycle 'item-1','item-2' %}");
  }
}

@@ -1560,23 +1560,30 @@
 d it
-Handles
+AddCycleTagAsA
 Deferred
 Cycl
@@ -1578,21 +1578,21 @@
 Deferred
-Cycle
+Token
 () %7B%0A   
@@ -2117,11 +2117,328 @@
 %7D%22);%0A  %7D
+%0A%0A  @Test%0A  public void itHandlesDeferredCycle() %7B%0A    interpreter.getContext().put(%22deferred%22, DeferredValue.instance());%0A    String template =%0A      %22%7B%25 set l = %5B%5D %25%7D%7B%25 for item in deferred %25%7D%7B%25 cycle l.append(deferred),5 %25%7D%7B%25 endfor %25%7D%7B%7B l %7D%7D%22;%0A    assertThat(interpreter.render(template)).isEqualTo(template);%0A  %7D
 %0A%7D%0A

update test