aboutsummaryrefslogtreecommitdiffstats
path: root/searchsummary/src/tests/juniper/testenv.cpp
blob: f65becb1ce1f6761559b9b243aa70faf307adf97 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/* Setup and parameter parsing for static Juniper environment to reuse
 * within test framework
 */

#include "testenv.h"
#include <vespa/juniper/propreader.h>
#include <unistd.h>

namespace juniper
{

bool color_highlight = false;
// Easy access in tests..
Config* TestConfig;
Juniper * _Juniper;


TestEnv::TestEnv(int argc, char **argv, const char* propfile) :
    _props(), _config(), _juniper(), _wordFolder()
{
    int c;

    while ((c = getopt(argc, argv, "d:hcm:")) != EOF)
    {
        switch (c)
        {
	case 'd':
            fprintf(stderr, "This version of Juniper compiled without debug\n");
            break;
	case 'c':
            color_highlight = true;
            break;
	case 'm':
            // option handled by test framework
            break;
	case 'h':
	default:
            Usage(argv[0]);
            return;
        }
    }

    int expected_args = 0;

    if (argc - optind < expected_args)
    {
        Usage(argv[0]);
        return;
    }

    _props.reset(new PropReader(propfile));

    if (color_highlight)
    {
        _props->UpdateProperty("juniper.dynsum.highlight_on", "\\1b[1;31m");
        _props->UpdateProperty("juniper.dynsum.highlight_off", "\\1b[0m");
    }

    _juniper.reset(new Juniper(_props.get(), &_wordFolder));
    _Juniper = _juniper.get();
    _config = _juniper->CreateConfig();
    TestConfig = _config.get();
}

TestEnv::~TestEnv()
{
}

void TestEnv::Usage(char* s)
{
    fprintf(stderr, "Usage: %s [options]\n", s);
    fprintf(stderr, "Available options:\n");
    fprintf(stderr, "  -d<debugmask>: Turn on debugging\n");
    fprintf(stderr, "  -h: This help\n");
}


TestQuery::TestQuery(const char* qexp, const char* options) :
    _qparser(qexp),
    _qhandle(_qparser, options, _Juniper->getModifier())
{ }


PropertyMap::PropertyMap()
    : _map()
{
}


PropertyMap::~PropertyMap()
{
}


PropertyMap &
PropertyMap::set(const char *name, const char *value)
{
    _map[std::string(name)] = std::string(value);
    return *this;
}


const char *
PropertyMap::GetProperty(const char* name, const char* def) const
{
    auto res = _map.find(std::string(name));
    if (res != _map.end()) {
        return res->second.c_str();
    }
    return def;
}


} // end namespace juniper