aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/expressions/FlattenTestCase.java
blob: b875abb923b42b663f936f35805bcb7ab465b913 (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
// 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 com.yahoo.document.annotation.*;
import com.yahoo.document.datatypes.StringFieldValue;
import org.junit.Test;

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

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

    @Test
    public void requireThatHashCodeAndEqualsAreImplemented() {
        Expression exp = new FlattenExpression();
        assertFalse(exp.equals(new Object()));
        assertEquals(exp, new FlattenExpression());
        assertEquals(exp.hashCode(), new FlattenExpression().hashCode());
    }

    @Test
    public void requireThatAnnotationsAreFlattened() {
        SpanTree tree = new SpanTree(SpanTrees.LINGUISTICS);
        tree.annotate(new Span(0, 3), new Annotation(AnnotationTypes.TERM, new StringFieldValue("oof")));
        tree.annotate(new Span(4, 3), new Annotation(AnnotationTypes.TERM, new StringFieldValue("rab")));
        tree.annotate(new Span(8, 3), new Annotation(AnnotationTypes.TERM, new StringFieldValue("zab")));

        StringFieldValue val = new StringFieldValue("foo bar baz");
        val.setSpanTree(tree);

        assertEquals(new StringFieldValue("foo[oof] bar[rab] baz[zab]"), new FlattenExpression().execute(val));
    }

    @Test
    public void requireThatNonTermAnnotationsAreIgnored() {
        SpanTree tree = new SpanTree(SpanTrees.LINGUISTICS);
        tree.annotate(new Span(0, 3), new Annotation(AnnotationTypes.STEM, new StringFieldValue("oof")));

        StringFieldValue val = new StringFieldValue("foo");
        val.setSpanTree(tree);

        assertEquals(new StringFieldValue("foo"), new FlattenExpression().execute(val));
    }

    @Test
    public void requireThatNonSpanAnnotationsAreIgnored() {
        SpanTree tree = new SpanTree(SpanTrees.LINGUISTICS);
        tree.annotate(new Annotation(AnnotationTypes.TERM, new StringFieldValue("oof")));

        StringFieldValue val = new StringFieldValue("foo");
        val.setSpanTree(tree);

        assertEquals(new StringFieldValue("foo"), new FlattenExpression().execute(val));
    }

    @Test
    public void requireThatAnnotationsAreSorted() {
        SpanTree tree = new SpanTree(SpanTrees.LINGUISTICS);
        tree.annotate(new Span(0, 3), new Annotation(AnnotationTypes.TERM, new StringFieldValue("cox")));
        tree.annotate(new Span(0, 3), new Annotation(AnnotationTypes.TERM, new StringFieldValue("baz")));
        tree.annotate(new Span(0, 3), new Annotation(AnnotationTypes.TERM, new StringFieldValue("bar")));

        StringFieldValue val = new StringFieldValue("foo");
        val.setSpanTree(tree);

        assertEquals(new StringFieldValue("foo[bar, baz, cox]"), new FlattenExpression().execute(val));
    }

    @Test
    public void requireThatAnnotationsWithoutFieldValueUseOriginalSpan() {
        SpanTree tree = new SpanTree(SpanTrees.LINGUISTICS);
        tree.annotate(new Span(0, 3), new Annotation(AnnotationTypes.TERM));

        StringFieldValue val = new StringFieldValue("foo");
        val.setSpanTree(tree);

        assertEquals(new StringFieldValue("foo[foo]"), new FlattenExpression().execute(val));
    }

    @Test
    public void requireThatExpressionCanBeVerified() {
        Expression exp = new FlattenExpression();
        assertVerify(DataType.STRING, exp, DataType.STRING);
        assertVerifyThrows(null, exp, "Expected string input, but no input is specified");
        assertVerifyThrows(DataType.INT, exp, "Expected string input, got int");
    }
}