aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/query/parser/test/ParsingTester.java
blob: e37c5c038070cab4062ef9fc976fe7e4c9aa915d (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.query.parser.test;

import com.yahoo.config.subscription.ConfigGetter;
import com.yahoo.container.QrSearchersConfig;
import com.yahoo.language.Language;
import com.yahoo.language.Linguistics;
import com.yahoo.language.simple.SimpleDetector;
import com.yahoo.language.simple.SimpleLinguistics;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.IndexModel;
import com.yahoo.prelude.query.Item;
import com.yahoo.prelude.query.NullItem;
import com.yahoo.language.process.SpecialTokenRegistry;
import com.yahoo.language.process.SpecialTokens;
import com.yahoo.search.Query;
import com.yahoo.search.config.IndexInfoConfig;
import com.yahoo.search.query.parser.Parsable;
import com.yahoo.search.query.parser.Parser;
import com.yahoo.search.query.parser.ParserEnvironment;
import com.yahoo.search.query.parser.ParserFactory;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

/**
 * A utility for writing parser tests
 *
 * @author bratseth
 */
public class ParsingTester {

    private static final Linguistics linguistics = new SimpleLinguistics();
    private final IndexFacts indexFacts;
    private SpecialTokenRegistry tokenRegistry;

    public ParsingTester() {
        this(createIndexFacts(), createSpecialTokens());
    }

    public ParsingTester(SpecialTokens specialTokens) {
        this(createIndexFacts(), specialTokens);
    }

    public ParsingTester(IndexFacts indexFacts) {
        this(indexFacts, createSpecialTokens());
    }

    public ParsingTester(IndexFacts indexFacts, SpecialTokens specialTokens) {
        indexFacts.freeze();

        this.indexFacts = indexFacts;
        tokenRegistry = new SpecialTokenRegistry();
        tokenRegistry = new SpecialTokenRegistry(List.of(specialTokens));
    }

    /**
     * Returns an unfrozen version of the IndexFacts this will use.
     * This can be used to add new indexes and passing the resulting IndexFacts to the constructor of this.
     */
    @SuppressWarnings("deprecation")
    public static IndexFacts createIndexFacts() {
        String indexInfoConfigID = "file:src/test/java/com/yahoo/prelude/query/parser/test/parseindexinfo.cfg";
        ConfigGetter<IndexInfoConfig> getter = new ConfigGetter<>(IndexInfoConfig.class);
        IndexInfoConfig config = getter.getConfig(indexInfoConfigID);
        return new IndexFacts(new IndexModel(config, (QrSearchersConfig)null));
    }

    /**
     * Returns an unfrozen version of the special tokens this will use.
     * This can be used to add new tokens and passing the resulting special tokens to the constructor of this.
     */
    public static SpecialTokens createSpecialTokens() {
        List<SpecialTokens.Token> tokens = new ArrayList<>();
        tokens.add(new SpecialTokens.Token("c++"));
        tokens.add(new SpecialTokens.Token(".net", "dotnet"));
        tokens.add(new SpecialTokens.Token("tcp/ip"));
        tokens.add(new SpecialTokens.Token("c#"));
        tokens.add(new SpecialTokens.Token("special-token-fs","firstsecond"));
        return new SpecialTokens("default", tokens);
    }

    /**
     * Asserts that the canonical representation of the second string when parsed
     * is the first string
     *
     * @return the produced root
     */
    public Item assertParsed(String parsed, String toParse, Query.Type mode) {
        return assertParsed(parsed, toParse, null, mode, new SimpleDetector().detect(toParse, null).getLanguage(),
                            new SimpleLinguistics());
    }

    /**
     * Asserts that the canonical representation of the second string when parsed
     * is the first string
     *
     * @return the produced root
     */
    public Item assertParsed(String parsed, String toParse, String filter, Query.Type mode) {
        return assertParsed(parsed, toParse, filter, mode, new SimpleDetector().detect(toParse,null).getLanguage());
    }

    public Item assertParsed(String parsed, String toParse, String filter, Query.Type mode, Language language) {
        return assertParsed(parsed, toParse, filter, mode, language, linguistics);
    }

    /**
     * Asserts that the canonical representation of the second string when parsed
     * is the first string
     *
     * @return the produced root
     */
    public Item assertParsed(String parsed, String toParse, String filter, Query.Type mode,
                              Language language, Linguistics linguistics) {
        Item root = parseQuery(toParse, filter, language, mode, linguistics);
        if (parsed == null) {
            assertTrue(root instanceof NullItem, "root is " + root);
        } else {
            assertNotNull(root, "Got null from parsing " + toParse);
            assertEquals(parsed, root.toString(), "Parse of '" + toParse + "'");
        }
        return root;
    }

    public Item parseQuery(String query, Language language, Query.Type type) {
        return parseQuery(query, null, language, type, new SimpleLinguistics());
    }

    public Item parseQuery(String query, String filter, Language language, Query.Type type, Linguistics linguistics) {
        Parser parser = ParserFactory.newInstance(type, new ParserEnvironment()
                .setIndexFacts(indexFacts)
                .setLinguistics(linguistics)
                .setSpecialTokens(tokenRegistry.getSpecialTokens("default")));
        Item root = parser.parse(new Parsable().setQuery(query).setFilter(filter).setLanguage(language)).getRoot();
        if (root == null) throw new NullPointerException(); // Should be NullItem
        return root;
    }

}