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

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

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 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 {{foo}},{{bar}} %}{% endfor %}";
    assertThat(interpreter.render(template)).isEqualTo(template);
  }
}

@@ -101,16 +101,68 @@
 tThat;%0A%0A
+import com.hubspot.jinjava.ExpectedNodeInterpreter;%0A
 import c
@@ -489,16 +489,142 @@
 onMode;%0A
+import com.hubspot.jinjava.tree.parse.TagToken;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.Optional;%0A
 import o
@@ -1602,16 +1602,22 @@
 mplate =
+%0A     
  %22%7B%25 for
@@ -1649,23 +1649,25 @@
 cle 
-%7B%7Bfoo%7D%7D,%7B%7Bbar%7D%7D
+'item-1','item-2'
  %25%7D%7B
@@ -1746,14 +1746,375 @@
 plate);%0A
+    Optional%3CDeferredToken%3E maybeDeferredToken = context%0A      .getDeferredTokens()%0A      .stream()%0A      .filter(e -%3E ((TagToken) e.getToken()).getTagName().equals(tag.getName()))%0A      .findAny();%0A    assertThat(maybeDeferredToken.isPresent());%0A    assertThat(maybeDeferredToken.get().getToken().getImage())%0A      .isEqualTo(%22%7B%25 cycle 'item-1','item-2' %25%7D%22);%0A
   %7D%0A%7D%0A

modify test to look for deferred token in the context