package com.hubspot.jinjava.interpret;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import java.util.List;
import org.junit.Test;

public class LazyExpressionTest {

  @Test
  public void itSerializesUnderlyingValue() throws JsonProcessingException {
    LazyExpression expression = LazyExpression.of(
      () -> ImmutableMap.of("test", "hello", "test2", "hello2"),
      "{}"
    );
    Object evaluated = expression.get();
    assertThat(evaluated).isNotNull();
    assertThat(new ObjectMapper().writeValueAsString(expression))
      .isEqualTo("{\"test\":\"hello\",\"test2\":\"hello2\"}");
  }

  @Test
  public void itSerializesNonEvaluatedValueToEmpty() throws JsonProcessingException {
    LazyExpression expression = LazyExpression.of(
      () -> ImmutableMap.of("test", "hello", "test2", "hello2"),
      "{}"
    );
    assertThat(new ObjectMapper().writeValueAsString(expression)).isEqualTo("\"\"");
  }

  @Test
  public void itMemoizesByDefault() {
    List mock = mock(List.class);
    LazyExpression expression = LazyExpression.of(mock::isEmpty, "");
    expression.get();
    expression.get();
    verify(mock, times(1)).isEmpty();
  }

  @Test
  public void itAllowsMemoizationToBeDisabled() {
    List mock = mock(List.class);
    LazyExpression expression = LazyExpression.of(mock::isEmpty, "", false);
    expression.get();
    expression.get();
    verify(mock, times(2)).isEmpty();
  }
}

@@ -287,16 +287,81 @@
 bleMap;%0A
+import com.hubspot.jinjava.interpret.LazyExpression.MEMOIZATION;%0A
 import j
@@ -1587,13 +1587,287 @@
 %22%22, 
-false
+MEMOIZATION.OFF);%0A    expression.get();%0A    expression.get();%0A    verify(mock, times(2)).isEmpty();%0A  %7D%0A%0A  @Test%0A  public void itAllowsMemoizationToBeEnabled() %7B%0A    List mock = mock(List.class);%0A    LazyExpression expression = LazyExpression.of(mock::isEmpty, %22%22, MEMOIZATION.ON
 );%0A 
@@ -1924,33 +1924,33 @@
 ify(mock, times(
-2
+1
 )).isEmpty();%0A  

Fix tests, add test for memoization explicitly enabled