aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/expressions/OutputAssert.java
blob: 2f41ffcfc6fc08cf1391a798412d5a321f2629af (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
// 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.Field;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.vespa.indexinglanguage.SimpleTestAdapter;

import static org.junit.Assert.*;

/**
 * @author Simon Thoresen Hult
 */
class OutputAssert {

    public static void assertExecute(OutputExpression exp) {
        ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter(new Field(exp.getFieldName(), DataType.STRING)));
        ctx.setValue(new StringFieldValue("69"));
        ctx.execute(exp);

        FieldValue out = ctx.getInputValue(exp.getFieldName());
        assertTrue(out instanceof StringFieldValue);
        assertEquals("69", ((StringFieldValue)out).getString());
    }

    public static void assertVerify(OutputExpression exp) {
        assertVerify(new MyAdapter(null), DataType.INT, exp);
        assertVerify(new MyAdapter(null), DataType.STRING, exp);
        assertVerifyThrows(new MyAdapter(null), null, exp, "Expected any input, but no input is specified");
        assertVerifyThrows(new MyAdapter(new VerificationException((Expression) null, "foo")), DataType.INT, exp, "foo");
    }

    public static void assertVerify(FieldTypeAdapter adapter, DataType value, Expression exp) {
        assertEquals(value, new VerificationContext(adapter).setValueType(value).execute(exp).getValueType());
    }

    public static void assertVerifyThrows(FieldTypeAdapter adapter, DataType value, Expression exp,
                                          String expectedException)
    {
        try {
            new VerificationContext(adapter).setValueType(value).execute(exp);
            fail();
        } catch (VerificationException e) {
            assertEquals(expectedException, e.getMessage());
        }
    }

    private static class MyAdapter implements FieldTypeAdapter {

        final RuntimeException e;

        MyAdapter(RuntimeException e) {
            this.e = e;
        }

        @Override
        public DataType getInputType(Expression exp, String fieldName) {
            throw new AssertionError();
        }

        @Override
        public void tryOutputType(Expression exp, String fieldName, DataType valueType) {
            if (e != null) {
                throw e;
            }
        }
    }
}