<commit_before>package com.hubspot.jinjava.el;

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

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;

import com.google.common.collect.ForwardingList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.interpret.Context;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateError;
import com.hubspot.jinjava.interpret.TemplateError.ErrorReason;
import com.hubspot.jinjava.objects.PyWrapper;
import com.hubspot.jinjava.objects.date.PyishDate;

@SuppressWarnings("unchecked")
public class ExpressionResolverTest {

  private JinjavaInterpreter interpreter;
  private Context context;

  @Before
  public void setup() {
    interpreter = new Jinjava().newInterpreter();
    context = interpreter.getContext();
  }

  @Test
  public void itResolvesListLiterals() throws Exception {
    Object val = interpreter.resolveELExpression("['0.5','50']", -1);
    List<Object> list = (List<Object>) val;
    assertThat(list).containsExactly("0.5", "50");
  }

  @Test
  public void itResolvesImmutableListLiterals() throws Exception {
    Object val = interpreter.resolveELExpression("('0.5','50')", -1);
    List<Object> list = (List<Object>) val;
    assertThat(list).containsExactly("0.5", "50");
  }

  @Test(expected = UnsupportedOperationException.class)
  public void testTuplesAreImmutable() throws Exception {
    Object val = interpreter.resolveELExpression("('0.5','50')", -1);
    List<Object> list = (List<Object>) val;
    list.add("foo");
  }

  @Test
  public void itCanCompareStrings() throws Exception {
    context.put("foo", "white");
    assertThat(interpreter.resolveELExpression("'2013-12-08 16:00:00+00:00' > '2013-12-08 13:00:00+00:00'", -1)).isEqualTo(Boolean.TRUE);
    assertThat(interpreter.resolveELExpression("foo == \"white\"", -1)).isEqualTo(Boolean.TRUE);
  }

  @Test
  public void itResolvesUntrimmedExprs() throws Exception {
    context.put("foo", "bar");
    Object val = interpreter.resolveELExpression("  foo ", -1);
    assertThat(val).isEqualTo("bar");
    assertThat(interpreter.getContext().wasExpressionResolved("foo")).isTrue();
  }

  @Test
  public void itResolvesMathVals() throws Exception {
    context.put("i_am_seven", 7L);
    Object val = interpreter.resolveELExpression("(i_am_seven * 2 + 1)/3", -1);
    assertThat(val).isEqualTo(5.0);
    assertThat(interpreter.getContext().wasValueResolved("i_am_seven")).isTrue();
  }

  @Test
  public void itResolvesListVal() throws Exception {
    context.put("thelist", Lists.newArrayList(1L, 2L, 3L));
    Object val = interpreter.resolveELExpression("thelist[1]", -1);
    assertThat(val).isEqualTo(2L);
  }

  @Test
  public void itResolvesDictValWithBracket() throws Exception {
    Map<String, Object> dict = Maps.newHashMap();
    dict.put("foo", "bar");
    context.put("thedict", dict);

    Object val = interpreter.resolveELExpression("thedict['foo']", -1);
    assertThat(val).isEqualTo("bar");
    assertThat(interpreter.getContext().wasExpressionResolved("thedict['foo']")).isTrue();

  }

  @Test
  public void itResolvesDictValWithDotParam() throws Exception {
    Map<String, Object> dict = Maps.newHashMap();
    dict.put("foo", "bar");
    context.put("thedict", dict);

    Object val = interpreter.resolveELExpression("thedict.foo", -1);
    assertThat(val).isEqualTo("bar");
    assertThat(interpreter.getContext().wasExpressionResolved("thedict.foo")).isTrue();
  }

  @Test
  public void itResolvesMapValOnCustomObject() throws Exception {

    MyCustomMap dict = new MyCustomMap();
    context.put("thedict", dict);

    Object val = interpreter.resolveELExpression("thedict['foo']", -1);
    assertThat(val).isEqualTo("bar");
    assertThat(interpreter.getContext().wasExpressionResolved("thedict['foo']")).isTrue();

    Object val2 = interpreter.resolveELExpression("thedict.two", -1);
    assertThat(val2).isEqualTo("2");
    assertThat(interpreter.getContext().wasExpressionResolved("thedict.two")).isTrue();
  }

  public static final class MyCustomMap implements Map<String, String> {

    Map<String, String> data = ImmutableMap.of("foo", "bar", "two", "2");

    @Override
    public int size() {<commit_msg>test method resolution on map objects<commit_after>package com.hubspot.jinjava.el;

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

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;

import com.google.common.collect.ForwardingList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.interpret.Context;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateError;
import com.hubspot.jinjava.interpret.TemplateError.ErrorReason;
import com.hubspot.jinjava.objects.PyWrapper;
import com.hubspot.jinjava.objects.date.PyishDate;

@SuppressWarnings("unchecked")
public class ExpressionResolverTest {

  private JinjavaInterpreter interpreter;
  private Context context;

  @Before
  public void setup() {
    interpreter = new Jinjava().newInterpreter();
    context = interpreter.getContext();
  }

  @Test
  public void itResolvesListLiterals() throws Exception {
    Object val = interpreter.resolveELExpression("['0.5','50']", -1);
    List<Object> list = (List<Object>) val;
    assertThat(list).containsExactly("0.5", "50");
  }

  @Test
  public void itResolvesImmutableListLiterals() throws Exception {
    Object val = interpreter.resolveELExpression("('0.5','50')", -1);
    List<Object> list = (List<Object>) val;
    assertThat(list).containsExactly("0.5", "50");
  }

  @Test(expected = UnsupportedOperationException.class)
  public void testTuplesAreImmutable() throws Exception {
    Object val = interpreter.resolveELExpression("('0.5','50')", -1);
    List<Object> list = (List<Object>) val;
    list.add("foo");
  }

  @Test
  public void itCanCompareStrings() throws Exception {
    context.put("foo", "white");
    assertThat(interpreter.resolveELExpression("'2013-12-08 16:00:00+00:00' > '2013-12-08 13:00:00+00:00'", -1)).isEqualTo(Boolean.TRUE);
    assertThat(interpreter.resolveELExpression("foo == \"white\"", -1)).isEqualTo(Boolean.TRUE);
  }

  @Test
  public void itResolvesUntrimmedExprs() throws Exception {
    context.put("foo", "bar");
    Object val = interpreter.resolveELExpression("  foo ", -1);
    assertThat(val).isEqualTo("bar");
    assertThat(interpreter.getContext().wasExpressionResolved("foo")).isTrue();
  }

  @Test
  public void itResolvesMathVals() throws Exception {
    context.put("i_am_seven", 7L);
    Object val = interpreter.resolveELExpression("(i_am_seven * 2 + 1)/3", -1);
    assertThat(val).isEqualTo(5.0);
    assertThat(interpreter.getContext().wasValueResolved("i_am_seven")).isTrue();
  }

  @Test
  public void itResolvesListVal() throws Exception {
    context.put("thelist", Lists.newArrayList(1L, 2L, 3L));
    Object val = interpreter.resolveELExpression("thelist[1]", -1);
    assertThat(val).isEqualTo(2L);
  }

  @Test
  public void itResolvesDictValWithBracket() throws Exception {
    Map<String, Object> dict = Maps.newHashMap();
    dict.put("foo", "bar");
    context.put("thedict", dict);

    Object val = interpreter.resolveELExpression("thedict['foo']", -1);
    assertThat(val).isEqualTo("bar");
    assertThat(interpreter.getContext().wasExpressionResolved("thedict['foo']")).isTrue();
  }

  @Test
  public void itResolvesDictValWithDotParam() throws Exception {
    Map<String, Object> dict = Maps.newHashMap();
    dict.put("foo", "bar");
    context.put("thedict", dict);

    Object val = interpreter.resolveELExpression("thedict.foo", -1);
    assertThat(val).isEqualTo("bar");
    assertThat(interpreter.getContext().wasExpressionResolved("thedict.foo")).isTrue();
  }

  @Test
  public void itResolvesMapValOnCustomObject() throws Exception {

    MyCustomMap dict = new MyCustomMap();
    context.put("thedict", dict);

    Object val = interpreter.resolveELExpression("thedict['foo']", -1);
    assertThat(val).isEqualTo("bar");
    assertThat(interpreter.getContext().wasExpressionResolved("thedict['foo']")).isTrue();

    Object val2 = interpreter.resolveELExpression("thedict.two", -1);
    assertThat(val2).isEqualTo("2");
    assertThat(interpreter.getContext().wasExpressionResolved("thedict.two")).isTrue();
  }

  @Test
  public void itResolvesOtherMethodsOnCustomMapObject() throws Exception {

    MyCustomMap dict = new MyCustomMap();
    context.put("thedict", dict);

    Object val = interpreter.resolveELExpression("thedict.size", -1);
    assertThat(val).isEqualTo("777");

    Object val1 = interpreter.resolveELExpression("thedict.size()", -1);
    assertThat(val1).isEqualTo(3);

    Object val2 = interpreter.resolveELExpression("thedict.items()", -1);
    assertThat(val2.toString()).isEqualTo("[foo=bar, two=2, size=777]");
 }

  public static final class MyCustomMap implements Map<String, String> {

    Map<String, String> data = ImmutableMap.of("foo", "bar", "two", "2", "size", "777");

    @Override
    public int size() {
{'fraction_non_alphanumeric': 0.14994850669412976, 'fraction_numerical': 0.018949536560247168, 'mean_word_length': 4.9944444444444445, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6540750', 'n_tokens_mistral': 3140, 'n_tokens_neox': 3071, 'n_words': 554}