aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/tests/configparser/configparser.cpp
blob: 32043ae79dc6b12c1a2ec91e4deb1ad98470797e (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
143
144
145
146
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/config/common/configparser.h>
#include <vespa/config/common/exceptions.h>
#include <vespa/config/common/configvalue.h>
#include <vespa/config/common/misc.h>
#include "config-foo.h"
#include <fstream>
#include <vespa/vespalib/stllike/asciistream.h>

using namespace config;
using vespalib::asciistream;

namespace {

    void writeFile(const vespalib::string & fileName, const vespalib::string & data)
    {
        std::ofstream of;
        of.open(fileName.c_str());
        of << data;
        of.close();
    }

    ConfigValue readConfig(const vespalib::string & fileName)
    {
        asciistream is(asciistream::createFromFile(fileName));
        return ConfigValue(getlines(is), "");
    }
}

TEST("require that default value exception provides error message")
{
    writeFile("foo.cfg", "blabla foo\n");
    try {
        FooConfig config(readConfig("foo.cfg"));
        ASSERT_TRUE(false);
    } catch (InvalidConfigException & ice) {
        ASSERT_EQUAL("Error parsing config 'foo' in namespace 'config': Config parameter fooValue has no default value and is not specified in config", ice.getMessage());
    }
}

TEST("require that unknown fields can exist in config payload")
{
    writeFile("foo.cfg", "blablabla foo\nfooValue \"hello\"\n");
    try {
        FooConfig config(readConfig("foo.cfg"));
        ASSERT_EQUAL("hello", config.fooValue);
    } catch (InvalidConfigException & ice) {
        ASSERT_FALSE(true);
    }
}

TEST("require that required fields will throw error with unknown fields")
{
    writeFile("foo.cfg", "blablabla foo\nfooValu \"hello\"\n");
    try {
        FooConfig config(readConfig("foo.cfg"));
        ASSERT_TRUE(false);
    } catch (InvalidConfigException & ice) {
        ASSERT_TRUE(true);
    }
}

TEST("require that array lengths does not have to be specified")
{
    writeFile("foo.cfg", "\nfooValue \"hello\"\nfooArray[0] 3\nfooArray[1] 9\nfooArray[2] 33\nfooStruct[0].innerStruct[0].bar 2\nfooStruct[0].innerStruct[1].bar 3\nfooStruct[1].innerStruct[0].bar 4");
    try {
        FooConfig config(readConfig("foo.cfg"));
        ASSERT_EQUAL("hello", config.fooValue);
        ASSERT_EQUAL(3u, config.fooArray.size());
        ASSERT_EQUAL(3, config.fooArray[0]);
        ASSERT_EQUAL(9, config.fooArray[1]);
        ASSERT_EQUAL(33, config.fooArray[2]);
        ASSERT_EQUAL(2u, config.fooStruct.size());
        ASSERT_EQUAL(2u, config.fooStruct[0].innerStruct.size());
        ASSERT_EQUAL(1u, config.fooStruct[1].innerStruct.size());
        ASSERT_EQUAL(2, config.fooStruct[0].innerStruct[0].bar);
        ASSERT_EQUAL(3, config.fooStruct[0].innerStruct[1].bar);
        ASSERT_EQUAL(4, config.fooStruct[1].innerStruct[0].bar);
    } catch (InvalidConfigException & ice) {
        ASSERT_TRUE(false);
    }
}

TEST("require that array lengths may be specified")
{
    writeFile("foo.cfg", "\nfooValue \"hello\"\nfooArray[3]\nfooArray[0] 3\nfooArray[1] 9\nfooArray[2] 33\nfooStruct[2]\nfooStruct[0].innerStruct[2]\nfooStruct[0].innerStruct[0].bar 2\nfooStruct[0].innerStruct[1].bar 3\nfooStruct[1].innerStruct[1]\nfooStruct[1].innerStruct[0].bar 4");
    try {
        FooConfig config(readConfig("foo.cfg"));
        ASSERT_EQUAL("hello", config.fooValue);
        ASSERT_EQUAL(3u, config.fooArray.size());
        ASSERT_EQUAL(3, config.fooArray[0]);
        ASSERT_EQUAL(9, config.fooArray[1]);
        ASSERT_EQUAL(33, config.fooArray[2]);
        ASSERT_EQUAL(2u, config.fooStruct[0].innerStruct.size());
        ASSERT_EQUAL(1u, config.fooStruct[1].innerStruct.size());
        ASSERT_EQUAL(2, config.fooStruct[0].innerStruct[0].bar);
        ASSERT_EQUAL(3, config.fooStruct[0].innerStruct[1].bar);
        ASSERT_EQUAL(4, config.fooStruct[1].innerStruct[0].bar);
    } catch (InvalidConfigException & ice) {
        ASSERT_TRUE(false);
    }
}

TEST("require that escaped values are properly unescaped") {
    StringVector payload;
    payload.push_back("foo \"a\\nb\\rc\\\\d\\\"e\x42g\"");
    vespalib::string value(ConfigParser::parse<vespalib::string>("foo", payload));
    ASSERT_EQUAL("a\nb\rc\\d\"eBg", value);
}

TEST("verify that locale does not affect double parsing") {
    StringVector payload;
    setlocale(LC_NUMERIC, "nb_NO.UTF-8");
    payload.push_back("foo 3,14");
    ASSERT_EXCEPTION(ConfigParser::parse<double>("foo", payload), InvalidConfigException, "Value 3,14 is not a legal double");
    setlocale(LC_NUMERIC, "C");
}

TEST("require that maps can be parsed")
{
    writeFile("foo.cfg", "\nfooValue \"a\"\nfooMap{\"foo\"} 1336\nfooMap{\"bar\"} 1337\n");
    FooConfig config(readConfig("foo.cfg"));
    ASSERT_EQUAL("a", config.fooValue);
    ASSERT_EQUAL(2u, config.fooMap.size());
    ASSERT_EQUAL(1336, config.fooMap.at("foo"));
    ASSERT_EQUAL(1337, config.fooMap.at("bar"));
}

TEST("handles quotes for bool values") {
    StringVector payload;
    payload.push_back("foo \"true\"");
    payload.push_back("bar \"123\"");
    payload.push_back("baz \"1234\"");
    payload.push_back("quux \"3.2\"");
    bool b(ConfigParser::parse<bool>("foo", payload));
    int32_t i(ConfigParser::parse<int32_t>("bar", payload));
    int64_t l(ConfigParser::parse<int64_t>("baz", payload));
    double d(ConfigParser::parse<double>("quux", payload));
    EXPECT_EQUAL(true, b);
    EXPECT_EQUAL(123, i);
    EXPECT_EQUAL(1234, l);
    EXPECT_APPROX(3.2, d, 0.001);
}

TEST_MAIN() { TEST_RUN_ALL(); }