Skip to content

Commit

Permalink
Merge pull request #211 from HubSpot/fix-equalto-exp-test
Browse files Browse the repository at this point in the history
Deepen equalto expression test comparison.
  • Loading branch information
mattcoley authored Jul 18, 2018
2 parents 97e4796 + 1057e7a commit 73ef789
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.hubspot.jinjava.lib.exptest;

import java.util.Objects;

import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.el.TruthyTypeConverter;
import com.hubspot.jinjava.interpret.InterpretException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;

import de.odysseus.el.misc.BooleanOperations;
import de.odysseus.el.misc.TypeConverter;

@JinjavaDoc(
value = "Check if an object has the same value as another object",
params = {
Expand All @@ -24,6 +26,8 @@
})
public class IsEqualToExpTest implements ExpTest {

private static final TypeConverter TYPE_CONVERTER = new TruthyTypeConverter();

@Override
public String getName() {
return "equalto";
Expand All @@ -35,7 +39,7 @@ public boolean evaluate(Object var, JinjavaInterpreter interpreter, Object... ar
throw new InterpretException(getName() + " test requires 1 argument");
}

return Objects.equals(var, args[0]);
return BooleanOperations.eq(TYPE_CONVERTER, var, args[0]);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.hubspot.jinjava.lib.exptest;

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

import java.util.HashMap;

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

import com.hubspot.jinjava.Jinjava;

public class IsEqualToExpTestTest {

private static final String EQUAL_TEMPLATE = "{{ %s is equalto %s }}";

private Jinjava jinjava;

@Before
public void setup() {
jinjava = new Jinjava();
}

@Test
public void itEquatesNumbers() {
assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "4", "4"), new HashMap<>())).isEqualTo("true");
assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "4", "5"), new HashMap<>())).isEqualTo("false");
}

@Test
public void itEquatesStrings() {
assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "\"jinjava\"", "\"jinjava\""), new HashMap<>())).isEqualTo("true");
assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "\"jinjava\"", "\"not jinjava\""), new HashMap<>())).isEqualTo("false");
}

@Test
public void itEquatesBooleans() {
assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "true", "true"), new HashMap<>())).isEqualTo("true");
assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "true", "false"), new HashMap<>())).isEqualTo("false");
}

@Test
public void itEquatesDifferentTypes() {
assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "4", "\"4\""), new HashMap<>())).isEqualTo("true");
assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "4", "\"5\""), new HashMap<>())).isEqualTo("false");
assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "'c'", "\"c\""), new HashMap<>())).isEqualTo("true");
assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "'c'", "\"b\""), new HashMap<>())).isEqualTo("false");
}
}

0 comments on commit 73ef789

Please sign in to comment.