aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/tests/configgen/value_converter.cpp
blob: a815b6dc6eda95a969961fe381760567ab4e62b8 (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
// Copyright Vespa.ai. 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/configgen/value_converter.h>
#include <vespa/config/common/exceptions.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <climits>

using namespace config;
using namespace config::internal;
using namespace vespalib;
using namespace vespalib::slime;

struct MyType{
    MyType(const ConfigPayload & payload)
    {
        foo = payload.get()["foo"].asLong();
        bar = payload.get()["bar"].asLong();
    }
    int foo;
    int bar;
};

TEST("that int32_ts are converted") {
    Slime slime;
    Cursor & root = slime.setArray();
    root.addLong(3);
    root.addLong(-2);
    root.addLong(INT_MAX);
    root.addLong(INT_MIN);
    root.addDouble(3.14);
    ValueConverter<int32_t> conv;
    EXPECT_EQUAL(3, conv(root[0]));
    EXPECT_EQUAL(-2, conv(root[1]));
    EXPECT_EQUAL(INT_MAX, conv(root[2]));
    EXPECT_EQUAL(INT_MIN, conv(root[3]));
    EXPECT_EQUAL(3, conv(root[4]));
}

TEST("that int64_ts are converted") {
    Slime slime;
    Cursor & root = slime.setArray();
    root.addLong(3);
    root.addLong(-2);
    root.addLong(LONG_MAX);
    root.addLong(LONG_MIN);
    root.addLong(std::numeric_limits<int64_t>::max());
    root.addLong(std::numeric_limits<int64_t>::min());
    root.addDouble(3.14);
    std::string ref = "{\"val\":9223372036854775807}";
    Slime slime2;
    JsonFormat::decode(ref, slime2);
    EXPECT_EQUAL(std::numeric_limits<int64_t>::max(), slime2.get()["val"].asLong());
    ValueConverter<int64_t> conv;
    EXPECT_EQUAL(3, conv(root[0]));
    EXPECT_EQUAL(-2, conv(root[1]));
    EXPECT_EQUAL(LONG_MAX, conv(root[2]));
    EXPECT_EQUAL(LONG_MIN, conv(root[3]));
    EXPECT_EQUAL(std::numeric_limits<int64_t>::max(), conv(root[4]));
    EXPECT_EQUAL(std::numeric_limits<int64_t>::min(), conv(root[5]));
    EXPECT_EQUAL(3, conv(root[6]));
}

TEST("that values can be parsed as strings") {
    Slime slime;
    Cursor & root = slime.setObject();
    root.setString("intval", "1234");
    root.setString("longval", "42949672969");
    root.setString("boolval", "true");
    root.setString("doubleval", "3.14");
    ValueConverter<int32_t> intConv;
    ValueConverter<int64_t> longConv;
    ValueConverter<bool> boolConv;
    ValueConverter<double> doubleConv;
    EXPECT_EQUAL(1234, intConv(root["intval"]));
    EXPECT_EQUAL(42949672969, longConv(root["longval"]));
    EXPECT_EQUAL(true, boolConv(root["boolval"]));
    EXPECT_APPROX(3.14, doubleConv(root["doubleval"]), 0.0001);
}

TEST("that incompatible types throws exceptions") {
    Slime slime;
    Cursor & root = slime.setObject();
    root.setBool("intval", true);
    root.setBool("longval", true);
    root.setBool("doubleval", true);
    root.setLong("boolval", 3);
    ValueConverter<int32_t> intConv;
    ValueConverter<int64_t> longConv;
    ValueConverter<bool> boolConv;
    ValueConverter<double> doubleConv;
    EXPECT_EXCEPTION(intConv(root["intval"]), InvalidConfigException, "");
    EXPECT_EXCEPTION(longConv(root["longval"]), InvalidConfigException, "");
    EXPECT_EXCEPTION(doubleConv(root["doubleval"]), InvalidConfigException, "");
    EXPECT_EXCEPTION(boolConv(root["boolval"]), InvalidConfigException, "");
}

TEST("that non-valid fields throws exception") {
    Slime slime;
    Cursor & root = slime.setObject();
    ValueConverter<int64_t> conv;
    EXPECT_EXCEPTION(conv("longval", root["longval"]), InvalidConfigException, "Value for 'longval' required but not found");
}

TEST_MAIN() { TEST_RUN_ALL(); }