Skip to content

Commit 46585bf

Browse files
committed
Add initializer_list constructor to json_arrayt
This new constructor facilitates the construction of instances of `json_arrayt` which are const, because it gives a tidy way to construct an entire `json_arrayt`.
1 parent d5953f8 commit 46585bf

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/util/json.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ class json_arrayt:public jsont
165165
{
166166
}
167167

168+
explicit json_arrayt(std::initializer_list<jsont> initializer_list)
169+
: json_arrayt(arrayt{initializer_list})
170+
{
171+
}
172+
168173
void resize(std::size_t size)
169174
{
170175
array.resize(size);

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ SRC += analyses/ai/ai.cpp \
5151
util/graph.cpp \
5252
util/irep.cpp \
5353
util/irep_sharing.cpp \
54+
util/json_array.cpp \
5455
util/json_object.cpp \
5556
util/memory_info.cpp \
5657
util/message.cpp \

unit/util/json_array.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*******************************************************************\
2+
3+
Module: Catch tests for json_arrayt
4+
5+
Author: Diffblue Ltd.
6+
7+
\*******************************************************************/
8+
9+
#include <testing-utils/catch.hpp>
10+
#include <util/json.h>
11+
12+
SCENARIO(
13+
"Test that json_arrayt can be constructed from an initializer list.",
14+
"[core][util][json]")
15+
{
16+
GIVEN("A json_arrayt constructed from an initializer list.")
17+
{
18+
const json_arrayt array{
19+
json_stringt{"one"}, json_numbert{"2"}, json_stringt{"three"}};
20+
THEN("The elements of the `json_arrayt` match the initialiser list.")
21+
{
22+
auto it = array.begin();
23+
REQUIRE(it->kind == jsont::kindt::J_STRING);
24+
REQUIRE(it->value == "one");
25+
++it;
26+
REQUIRE(it->kind == jsont::kindt::J_NUMBER);
27+
REQUIRE(it->value == "2");
28+
++it;
29+
REQUIRE(it->kind == jsont::kindt::J_STRING);
30+
REQUIRE(it->value == "three");
31+
++it;
32+
REQUIRE(it == array.end());
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)