@Test @SuppressWarnings("unchecked") public void testCanCompareStrings() throws Exception { String s = "This is a TEST"; context.put("property", s); Eq instance = new Eq("property", "this is a test"); assertTrue(instance.isTruthValue(context)); }
@Test @SuppressWarnings("unchecked") public void testCanCompareNumbers() throws Exception { double number = 123; context.put("property", number); Eq instance = new Eq("property", "123.00"); assertTrue(instance.isTruthValue(context)); instance = new Eq("property", "123.456"); assertFalse(instance.isTruthValue(context)); }
@Test @SuppressWarnings("unchecked") public void testCanCompareCalendars() throws ParseException, Exception { Calendar calendar = Calendar.getInstance(); calendar.set(2009, 0, 15); // month value is 0-based context.put("property", calendar); Eq instance = new Eq("property", "15/01/2009"); assertTrue(instance.isTruthValue(context)); instance = new Eq("property", "01/01/2020"); assertFalse(instance.isTruthValue(context)); }
@Test @SuppressWarnings("unchecked") public void testCanCompareDates() throws ParseException, Exception { DateFormat df = new SimpleDateFormat("dd/MM/yyyy'T'HH:mm:ssZ"); Date date = df.parse("15/01/2009T00:00:00-0000"); context.put("property", date); Eq instance = new Eq("property", "15/01/2009"); assertTrue(instance.isTruthValue(context)); instance = new Eq("property", "01/01/2020"); assertFalse(instance.isTruthValue(context)); }
@Override public Boolean visit(Eq ast) { boolean checkLhs = ast.getLhs().accept(this); boolean checkRhs = ast.getRhs().accept(this); if (!(checkLhs && checkRhs)) return false; Type lhsType = ast.getLhs().typeOf(env); Type rhsType = ast.getRhs().typeOf(env); if (!lhsType.isCompatibleTo(rhsType)) { addToErrorList( ast, "the operator == can not be applied to instances of type " + lhsType.getClass() + " and type " + rhsType.getClass()); return false; } return true; }
@Test @SuppressWarnings("unchecked") public void testCanCompareBooleans() throws Exception { boolean b = true; context.put("property", b); Eq instance = new Eq("property", "true"); assertTrue(instance.isTruthValue(context)); instance = new Eq("property", "false"); assertFalse(instance.isTruthValue(context)); b = false; context.put("property", b); instance = new Eq("property", "false"); assertTrue(instance.isTruthValue(context)); instance = new Eq("property", "true"); assertFalse(instance.isTruthValue(context)); }
@Override public boolean is(T obj) { return eq.holds(sample, obj); }