File: test_json_metadata.cc

package info (click to toggle)
eckit 1.32.4-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 600,644 kB
  • sloc: cpp: 111,654; ansic: 2,826; yacc: 590; lex: 361; python: 237; sh: 202; makefile: 41
file content (165 lines) | stat: -rw-r--r-- 4,949 bytes parent folder | download | duplicates (6)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * (C) Copyright 1996- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */

#include <algorithm>

#include "eckit/io/Buffer.h"
#include "eckit/parser/JSONMetadata.h"
#include "eckit/types/FloatCompare.h"

#include "eckit/testing/Test.h"

using namespace std;
using namespace eckit;
using namespace eckit::types;
using namespace eckit::testing;

namespace eckit {
namespace test {

//----------------------------------------------------------------------------------------------------------------------

CASE("test_eckit_json_metadata_params") {
    std::string json_str = "{\"key1\": 1, \"key2\": 99.5, \"key3\": []}";
    Buffer buf(json_str.c_str(), json_str.length());

    JSONMetadata md(buf);

    std::vector<std::string> params(md.keywords());

    EXPECT(params.size() == 3);
    EXPECT(std::find(params.begin(), params.end(), "key1") != params.end());
    EXPECT(std::find(params.begin(), params.end(), "key2") != params.end());
    EXPECT(std::find(params.begin(), params.end(), "key3") != params.end());
}

CASE("test_eckit_json_metadata_params_nondict") {
    // If a valid json is supplied that is not an object/dict, there should be no
    // internal parameters to find

    std::string json_str = "[1, 2, 44.5]";
    Buffer buf(json_str.c_str(), json_str.length());

    JSONMetadata md(buf);

    std::vector<std::string> params(md.keywords());

    EXPECT(params.size() == 0);
}

CASE("test_eckit_json_metadata_has") {
    std::string json_str = "{\"key1\": 1, \"key2\": 99.5, \"key3\": []}";
    Buffer buf(json_str.c_str(), json_str.length());

    JSONMetadata md(buf);

    EXPECT(md.has("key1"));
    EXPECT(md.has("key2"));
    EXPECT(md.has("key3"));
    EXPECT(!md.has("keyunused"));
}

CASE("test_eckit_json_metadata_has_nondict") {
    // If a valid json is supplied that is not an object/dict, there should be no
    // internal parameters to find

    std::string json_str = "[1, 2, 44.5]";
    Buffer buf(json_str.c_str(), json_str.length());

    JSONMetadata md(buf);

    EXPECT(!md.has("key1"));
}

CASE("test_eckit_json_metadata_get_string") {
    std::string json_str = "{\"key1\": 1, \"key2\": \"testing string\", \"key3\": []}";
    Buffer buf(json_str.c_str(), json_str.length());

    JSONMetadata md(buf);

    std::string str_tmp;
    md.get("key2", str_tmp);

    // Gets value correctly
    EXPECT(str_tmp == "testing string");

    // Throws on incorrect type
    EXPECT_THROWS_AS(md.get("key3", str_tmp), BadCast);

    // Throws on missing key
    EXPECT_THROWS_AS(md.get("keyunused", str_tmp), OutOfRange);

    // Throws on non-dictionary root-value
    std::string json_str2 = "[1234]";
    Buffer buf2(json_str2.c_str(), json_str2.length());
    JSONMetadata md2(buf2);

    EXPECT_THROWS_AS(md2.get("key1", str_tmp), AssertionFailed);
}

CASE("test_eckit_json_metadata_get_double") {
    std::string json_str = "{\"key1\": 123.45, \"key2\": \"testing string\", \"key3\": [], \"key4\": 123}";
    Buffer buf(json_str.c_str(), json_str.length());

    JSONMetadata md(buf);

    double dbl_tmp;
    md.get("key1", dbl_tmp);
    EXPECT(is_approximately_equal(dbl_tmp, 123.45, 1.0e-6));

    md.get("key4", dbl_tmp);
    EXPECT(is_approximately_equal(dbl_tmp, static_cast<double>(123), 1.0e-6));

    // Throws on incorrect type
    EXPECT_THROWS_AS(md.get("key3", dbl_tmp), BadCast);

    // Throws on missing key
    EXPECT_THROWS_AS(md.get("keyunused", dbl_tmp), OutOfRange);

    // Throws on non-dictionary root-value
    std::string json_str2 = "[1234]";
    Buffer buf2(json_str2.c_str(), json_str2.length());
    JSONMetadata md2(buf2);

    EXPECT_THROWS_AS(md2.get("key1", dbl_tmp), AssertionFailed);
}

CASE("test_eckit_json_metadata_get_long") {
    std::string json_str = "{\"key1\": 123.45, \"key2\": \"testing string\", \"key3\": [], \"key4\": 123}";
    Buffer buf(json_str.c_str(), json_str.length());

    JSONMetadata md(buf);

    long long_tmp;
    md.get("key4", long_tmp);
    EXPECT(long_tmp == 123);

    // Throws on incorrect type
    EXPECT_THROWS_AS(md.get("key3", long_tmp), BadCast);

    // Throws on missing key
    EXPECT_THROWS_AS(md.get("keyunused", long_tmp), OutOfRange);

    // Throws on non-dictionary root-value
    std::string json_str2 = "[1234]";
    Buffer buf2(json_str2.c_str(), json_str2.length());
    JSONMetadata md2(buf2);

    EXPECT_THROWS_AS(md2.get("key1", long_tmp), AssertionFailed);
}

//----------------------------------------------------------------------------------------------------------------------

}  // namespace test
}  // namespace eckit

int main(int argc, char** argv) {
    return run_tests(argc, argv);
}