aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/expressions/LiteralBoolExpressionTestCase.java
blob: 514f79d88f229766ca546d17b241dda80fc18bfe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage.expressions;

import com.yahoo.document.DataType;
import com.yahoo.document.datatypes.BoolFieldValue;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.vespa.indexinglanguage.SimpleTestAdapter;
import org.junit.Test;

import static com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

/**
 * @author bratseth
 */
public class LiteralBoolExpressionTestCase {

    @Test
    public void requireThatHashCodeAndEqualsAreImplemented() {
        assertFalse(new LiteralBoolExpression(true).equals(new Object()));
        assertEquals(new LiteralBoolExpression(true), new LiteralBoolExpression(true));
        assertEquals(new LiteralBoolExpression(false), new LiteralBoolExpression(false));
        assertNotEquals(new LiteralBoolExpression(true), new LiteralBoolExpression(false));
        assertEquals(new LiteralBoolExpression(false).hashCode(), new LiteralBoolExpression(false).hashCode());
        assertEquals(new LiteralBoolExpression(true).hashCode(), new LiteralBoolExpression(true).hashCode());
        assertNotEquals(new LiteralBoolExpression(true).hashCode(), new LiteralBoolExpression(false).hashCode());
    }

    @Test
    public void requireThatExpressionCanBeVerified() {
        Expression exp = new LiteralBoolExpression(true);
        assertVerify(DataType.INT, exp, DataType.BOOL);
        assertVerify(DataType.STRING, exp, DataType.BOOL);
    }

    @Test
    public void testToString() {
        assertEquals("false", new LiteralBoolExpression(false).toString());
        assertEquals("true", new LiteralBoolExpression(true).toString());
    }

    @Test
    public void requireThatTrueBecomesTrue() {
        ExecutionContext context = new ExecutionContext(new SimpleTestAdapter());
        context.execute(new LiteralBoolExpression(true));
        FieldValue value = context.getValue();
        assertTrue(value instanceof BoolFieldValue);
        assertTrue(((BoolFieldValue)value).getBoolean());
    }

    @Test
    public void requireThatFalseBecomesFalse() {
        ExecutionContext context = new ExecutionContext(new SimpleTestAdapter());
        context.execute(new LiteralBoolExpression(false));
        FieldValue value = context.getValue();
        assertTrue(value instanceof BoolFieldValue);
        assertFalse(((BoolFieldValue)value).getBoolean());
    }

}