aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/expressions/ExpressionAssert.java
blob: 2608b270cce8bf38076800855e7b2b67f1519144 (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
// Copyright Yahoo. 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 java.util.regex.Pattern;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

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

    public static void assertVerifyCtx(VerificationContext ctx, Expression exp, DataType expectedValueAfter) {
        assertEquals(expectedValueAfter, exp.verify(ctx));
    }

    public static void assertVerify(DataType valueBefore, Expression exp, DataType expectedValueAfter) {
        assertVerifyCtx(new VerificationContext().setValueType(valueBefore), exp, expectedValueAfter);
    }

    public static void assertVerifyThrows(DataType valueBefore, Expression exp, String expectedException) {
        assertVerifyCtxThrows(new VerificationContext().setValueType(valueBefore), exp, expectedException);
    }

    interface CreateExpression {
        Expression create();
    }
    public static void assertVerifyThrows(DataType valueBefore, CreateExpression createExp, String expectedException) {
        assertVerifyCtxThrows(new VerificationContext().setValueType(valueBefore), createExp, expectedException);
    }

    public static void assertVerifyCtxThrows(VerificationContext ctx, CreateExpression createExp, String expectedException) {
        try {
            Expression exp = createExp.create();
            exp.verify(ctx);
            fail();
        } catch (VerificationException e) {
            if (!Pattern.matches(expectedException, e.getMessage())) {
                assertEquals(expectedException, e.getMessage());
            }
        }
    }
    public static void assertVerifyCtxThrows(VerificationContext ctx, Expression exp, String expectedException) {
        try {
            exp.verify(ctx);
            fail();
        } catch (VerificationException e) {
            if (!Pattern.matches(expectedException, e.getMessage())) {
                assertEquals(expectedException, e.getMessage());
            }
        }
    }
}