forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
JsonTest.h
64 lines (56 loc) · 2.65 KB
/
JsonTest.h
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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2021 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include "MantidJson/Json.h"
#include <cxxtest/TestSuite.h>
class JsonTest : public CxxTest::TestSuite {
public:
void testJsonToString() {
std::stringstream initialString("{\"bar\":2,\"baz\":3.1400000000000001,\"foo\":1,\"hello world\":\"HelloWorld\"}");
Json::Value json;
initialString >> json;
TS_ASSERT_EQUALS(initialString.str(), Mantid::JsonHelpers::jsonToString(json))
}
void testJsonToStringWithEscapedQuotes() {
// Check that escaped quotes inside json string values are preserved.
std::stringstream initialString("{\"string with quotes\":\"filename = \\\"file.txt\\\" \"}");
Json::Value json;
initialString >> json;
TS_ASSERT_EQUALS(initialString.str(), Mantid::JsonHelpers::jsonToString(json))
}
void testStringToJson() {
const std::string initialString = "{\"bar\":2,\"baz\":3.1400000000000001,\"foo\":1,\"hello world\":\"HelloWorld\"}";
const Json::Value json = Mantid::JsonHelpers::stringToJson(initialString);
TS_ASSERT_EQUALS(1, json["foo"].asInt())
TS_ASSERT_EQUALS(2, json["bar"].asInt())
TS_ASSERT_DELTA(3.14, json["baz"].asFloat(), 1e-5)
TS_ASSERT_EQUALS("HelloWorld", json["hello world"].asString())
}
void testJsonToStringToJsonToString() {
std::stringstream initialString("{\"bar\":2,\"baz\":3,\"foo\":1,\"hello world\":\"HelloWorld\"}");
Json::Value json;
initialString >> json;
const std::string endString = Mantid::JsonHelpers::jsonToString(json);
TS_ASSERT_EQUALS(initialString.str(), endString)
TS_ASSERT_EQUALS(Mantid::JsonHelpers::jsonToString(json),
Mantid::JsonHelpers::jsonToString(Mantid::JsonHelpers::stringToJson(endString)))
}
void testStringToJsonToString() {
const std::string initialString =
"{\"bar\":2,\"baz\":3,\"foo\":1,\"hello world\":\"HelloWorld\",\"string_number\":\"0\"}";
const Json::Value json = Mantid::JsonHelpers::stringToJson(initialString);
TS_ASSERT_EQUALS(initialString, Mantid::JsonHelpers::jsonToString(json))
}
void testParse() {
const std::string initialString =
"{\"bar\":2,\"baz\":3,\"foo\":1,\"hello world\":\"HelloWorld\",\"string_number\":\"0\"}";
Json::Value json;
bool result = Mantid::JsonHelpers::parse(initialString, &json, NULL);
TS_ASSERT(result)
TS_ASSERT_EQUALS(initialString, Mantid::JsonHelpers::jsonToString(json))
}
};