File: C4ValueTest.cpp

package info (click to toggle)
openclonk 8.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 169,656 kB
  • sloc: cpp: 180,484; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 148; sh: 101; javascript: 34
file content (92 lines) | stat: -rw-r--r-- 2,917 bytes parent folder | download | duplicates (5)
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
/*
 * OpenClonk, http://www.openclonk.org
 *
 * Copyright (c) 2013, The OpenClonk Team and contributors
 *
 * Distributed under the terms of the ISC license; see accompanying file
 * "COPYING" for details.
 *
 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
 * See accompanying file "TRADEMARK" for details.
 *
 * To redistribute this file separately, substitute the full license texts
 * for the above references.
 */

#include <C4Include.h>
#include "script/C4Value.h"

#include <gtest/gtest.h>

TEST(C4ValueTest, SanityTests)
{
	srand(time(nullptr));
	int rnd = rand();
	// two C4Values constructed with same parameter are equal
	EXPECT_TRUE(C4Value(rnd) == C4Value(rnd));
	EXPECT_EQ(C4Value(rnd), C4Value(rnd));
	EXPECT_FALSE(C4Value(rnd) != C4Value(rnd));
	int rnd2 = rand();
	while(rnd2 == rnd) rnd2 = rand();
	EXPECT_FALSE(C4Value(rnd) == C4Value(rnd2));
	EXPECT_TRUE(C4Value(42));
	EXPECT_FALSE(C4Value(0));
	EXPECT_TRUE(C4Value(true));
	EXPECT_FALSE(C4Value(false));
}

static void PrintTo(const StdStrBuf &v, std::ostream *stream)
{
	*stream << testing::PrintToString(v.getData());
}

TEST(C4ValueTest, ToJSON)
{
	// can't use raw strings that contains \" because MSVC 2015's preprocessor fails on those

	// simple values
	EXPECT_EQ(C4Value(42).ToJSON(), "42");
	EXPECT_EQ(C4Value(-42).ToJSON(), "-42");
	EXPECT_EQ(C4Value("foobar").ToJSON(), R"#("foobar")#");
	EXPECT_EQ(C4Value("es\"caping").ToJSON(), "\"es\\\"caping\"");
	EXPECT_EQ(C4Value("es\\caping").ToJSON(), R"#("es\\caping")#");
	EXPECT_EQ(C4Value("new\nline").ToJSON(), R"#("new\nline")#");
	EXPECT_EQ(C4Value(true).ToJSON(), R"#(true)#");
	EXPECT_EQ(C4Value(false).ToJSON(), R"#(false)#");
	EXPECT_EQ(C4Value().ToJSON(), R"#(null)#");

	// proplists
	{
		auto proplist = C4PropList::NewStatic(nullptr, nullptr, nullptr);
		proplist->SetProperty(P_Options, C4Value("options"));
		proplist->SetProperty(P_Min, C4Value(13));
		auto nested = C4PropList::NewStatic(nullptr, nullptr, nullptr);
		nested->SetProperty(P_Description, C4Value(true));
		proplist->SetProperty(P_Storage, C4Value(nested));
		EXPECT_EQ(C4Value(proplist).ToJSON(), R"#({"Min":13,"Options":"options","Storage":{"Description":true}})#");
	}

	{
		auto crazy_key = C4PropList::NewStatic(nullptr, nullptr, nullptr);
		auto key = Strings.RegString("foo\"bar");
		crazy_key->SetPropertyByS(key, C4Value(42));
		EXPECT_EQ(C4Value(crazy_key).ToJSON(), "{\"foo\\\"bar\":42}");
	}

	// arrays
	{
		auto array = new C4ValueArray(3);
		array->SetItem(0, C4Value(1));
		array->SetItem(1, C4Value(2));
		array->SetItem(2, C4Value(3));
		EXPECT_EQ(C4Value(array).ToJSON(), R"#([1,2,3])#");
	}

	{
		auto proplist = C4PropList::NewStatic(nullptr, nullptr, nullptr);
		proplist->SetProperty(P_Options, C4Value(123));
		auto array = new C4ValueArray(1);
		array->SetItem(0, C4Value(proplist));
		EXPECT_EQ(C4Value(array).ToJSON(), R"#([{"Options":123}])#");
	}
}