aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/expressions/ExpressionTestCase.java
blob: 92f8fbbfd80dd50a8fcc977db0c970762f3e8780 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// 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.FieldValue;
import com.yahoo.document.datatypes.IntegerFieldValue;
import com.yahoo.document.datatypes.StringFieldValue;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 * @author Simon Thoresen Hult
 */
public class ExpressionTestCase {

    @Test
    public void requireThatInputTypeIsCheckedBeforeExecute() {
        assertExecute(newRequiredInput(DataType.INT), null);
        assertExecute(newRequiredInput(DataType.INT), new IntegerFieldValue(69));
        assertExecuteThrows(newRequiredInput(DataType.INT), new StringFieldValue("foo"),
                            new IllegalArgumentException("expected int input, got string"));
    }

    @Test
    public void requireThatOutputTypeIsCheckedAfterExecute() {
        assertExecute(newCreatedOutput(DataType.INT, (FieldValue)null), null);
        assertExecute(newCreatedOutput(DataType.INT, new IntegerFieldValue(69)), null);
        assertExecuteThrows(newCreatedOutput(DataType.INT, new StringFieldValue("foo")), null,
                            new IllegalStateException("expected int output, got string"));
    }

    @Test
    public void requireThatInputTypeIsCheckedBeforeVerify() {
        assertVerify(newRequiredInput(DataType.INT), DataType.INT);
        assertVerifyThrows(newRequiredInput(DataType.INT), null,
                           "Expected int input, but no input is specified");
        assertVerifyThrows(newRequiredInput(DataType.INT), UnresolvedDataType.INSTANCE,
                           "Failed to resolve input type");
        assertVerifyThrows(newRequiredInput(DataType.INT), DataType.STRING,
                           "Expected int input, got string");
    }

    @Test
    public void requireThatOutputTypeIsCheckedAfterVerify() {
        assertVerify(newCreatedOutput(DataType.INT, DataType.INT), null);
        assertVerifyThrows(newCreatedOutput(DataType.INT, (DataType)null), null,
                           "Expected int output, but no output is specified");
        assertVerifyThrows(newCreatedOutput(DataType.INT, UnresolvedDataType.INSTANCE), null,
                           "Failed to resolve output type");
        assertVerifyThrows(newCreatedOutput(DataType.INT, DataType.STRING), null,
                           "Expected int output, got string");
    }

    @Test
    public void requireThatEqualsMethodWorks() {
        assertTrue(Expression.equals(null, null));
        assertTrue(Expression.equals(1, 1));
        assertFalse(Expression.equals(1, 2));
        assertFalse(Expression.equals(1, null));
        assertFalse(Expression.equals(null, 2));
    }

    private static Expression newRequiredInput(DataType requiredInput) {
        return new SimpleExpression(requiredInput);
    }

    private static Expression newCreatedOutput(DataType createdOutput, FieldValue actualOutput) {
        return new SimpleExpression().setCreatedOutput(createdOutput).setExecuteValue(actualOutput);
    }

    private static Expression newCreatedOutput(DataType createdOutput, DataType actualOutput) {
        return new SimpleExpression().setCreatedOutput(createdOutput).setVerifyValue(actualOutput);
    }

    private static void assertExecute(Expression exp, FieldValue val) {
        exp.execute(val);
    }

    private static void assertExecuteThrows(Expression exp, FieldValue val, Exception expectedException) {
        try {
            exp.execute(val);
            fail();
        } catch (RuntimeException e) {
            assertEquals(expectedException.getClass(), e.getClass());
            assertTrue(e.getMessage().contains(expectedException.getMessage()));
        }
    }

    private static void assertVerify(Expression exp, DataType val) {
        exp.verify(val);
    }

    private static void assertVerifyThrows(Expression exp, DataType val, String expectedException) {
        try {
            exp.verify(val);
            fail();
        } catch (VerificationException e) {
            assertEquals(expectedException, e.getMessage());
        }
    }
}