Skip to content

Commit 3a967d1

Browse files
committed
better organization and testability of tests
1 parent 24cf48f commit 3a967d1

File tree

11 files changed

+175
-108
lines changed

11 files changed

+175
-108
lines changed

Makefile

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,29 @@
11

22
SHELL=/bin/sh
33

4-
LD := $(CXX)
5-
MKDIR := mkdir -p
6-
OBJ_DIR := .obj
7-
84
.SUFFIXES:
95

10-
$(OBJ_DIR)/%.o: %.cc
11-
$(CXX) $(CXXFLAGS) -MMD -c $< -o $@
12-
13-
TARGET = cpp-json
14-
156
CXXFLAGS := -std=c++11 -pedantic -Wextra -Wall -Iinclude -O2 -g3
16-
LDFLAGS :=
17-
H_FILES :=
18-
CXX_FILES := main.cc
19-
20-
O_FILES := $(patsubst %.cc, $(OBJ_DIR)/%.o, $(CXX_FILES))
21-
D_FILES := $(O_FILES:.o=.d)
227

23-
SOURCEFILES := $(H_FILES) $(CXX_FILES)
24-
.PRECIOUS: $(SOURCEFILES)
258
.PHONY: all clean
269

27-
all: $(TARGET)
10+
TARGETS := example1 example2 example3 example4
2811

29-
$(O_FILES): | $(OBJ_DIR)
12+
all: $(TARGETS)
3013

31-
$(D_FILES): | $(OBJ_DIR)
14+
example1: example1.cc
15+
$(CXX) $(CXXFLAGS) $^ -o $@
16+
17+
example2: example2.cc
18+
$(CXX) $(CXXFLAGS) $^ -o $@
3219

33-
$(OBJ_DIR) :
34-
@$(MKDIR) $@
20+
example3: example3.cc
21+
$(CXX) $(CXXFLAGS) $^ -o $@
3522

36-
$(TARGET): $(O_FILES)
37-
$(LD) $(LDFLAGS) $^ -o $@
23+
example4: example4.cc
24+
$(CXX) $(CXXFLAGS) $^ -o $@
3825

3926
clean:
40-
$(RM) $(O_FILES) $(D_FILES) $(TARGET)
27+
$(RM) $(TARGETS)
4128

42-
-include $(D_FILES)
4329

example1.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
#include "cpp-json/json.h"
3+
#include <iostream>
4+
#include <fstream>
5+
6+
//------------------------------------------------------------------------------
7+
// Name: main
8+
//------------------------------------------------------------------------------
9+
int main() {
10+
11+
// construct from a file
12+
std::ifstream file("example1.json");
13+
if(file) {
14+
json::value v1 = json::parse(file);
15+
std::cout << stringify(v1, json::PRETTY_PRINT | json::ESCAPE_UNICODE) << std::endl;
16+
}
17+
}
File renamed without changes.

example2.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
#include "cpp-json/json.h"
3+
#include <iostream>
4+
5+
//------------------------------------------------------------------------------
6+
// Name: main
7+
//------------------------------------------------------------------------------
8+
int main() {
9+
10+
// construct programatically (legacy C++03 API)
11+
json::object obj1;
12+
obj1.insert("test1", "hello world")
13+
.insert("test2", 10)
14+
.insert("test3", json::object().insert("x", json::null))
15+
16+
// if we are using C++11, we have a MUCH more elegant array initializer option
17+
#if __cplusplus >= 201103L
18+
.insert("test4", json::array().append(1, 2, 3, 4, "hello", true, 1.3));
19+
#else
20+
.insert("test4", json::array().append(1).append(2).append(3).append(4).append("hello").append(true).append(1.3));
21+
#endif
22+
23+
std::cout << stringify(obj1, json::PRETTY_PRINT) << std::endl;
24+
}

example3.cc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
#include "cpp-json/json.h"
3+
#include <iostream>
4+
5+
//------------------------------------------------------------------------------
6+
// Name: main
7+
//------------------------------------------------------------------------------
8+
int main() {
9+
10+
// construct from string (C++11 raw string literals work nicely here!)
11+
#if __cplusplus >= 201103L
12+
auto v = json::parse(R"(
13+
{
14+
"test3" : {
15+
"x" : 123.456
16+
},
17+
"test4" : [
18+
1,
19+
2,
20+
3,
21+
{
22+
"z" : 12345
23+
}
24+
],
25+
"test1" : "hello world",
26+
"test2" : "BLAH\uD840\uDC8ABLAH"
27+
}
28+
)");
29+
#else
30+
json::value v = json::parse("{\"test3\" : {\"x\" : 123.456},\"test4\" : [1,2,3,{\"z\":12345}],\"test1\" : \"hello world\",\"test2\" : \"BLAH\\uD840\\uDC8ABLAH\"}");
31+
#endif
32+
std::cout << stringify(v, json::PRETTY_PRINT) << std::endl;
33+
std::cout << "----------" << std::endl;
34+
35+
// get a specific value
36+
json::value z = v["test4"][3]["z"];
37+
std::cout << to_number(z) << std::endl;
38+
39+
std::cout << stringify(v, json::ESCAPE_UNICODE) << std::endl;
40+
}

example4.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
#include "cpp-json/json.h"
3+
#include <iostream>
4+
5+
//------------------------------------------------------------------------------
6+
// Name: main
7+
//------------------------------------------------------------------------------
8+
int main() {
9+
10+
// construct programmatically using object literal syntax in C++11
11+
#if __cplusplus >= 201103L
12+
json::array arr = {
13+
1,
14+
2,
15+
3,
16+
4,
17+
"Testing 1 2 3", json::object{
18+
{ "hello", 1234 },
19+
{ "world", 5678 }
20+
}
21+
};
22+
std::cout << stringify(arr) << std::endl;
23+
#endif
24+
}

example_output/example1.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"n2" : -1234,
3+
"b2" : false,
4+
"n1" : null,
5+
"b1" : true,
6+
"u" : "\u1234",
7+
"test\nreturn" : "hello\tworld",
8+
"object1" : {
9+
"object2" : {
10+
"object3" : {
11+
"test4" : [
12+
1,
13+
2,
14+
3,
15+
4
16+
],
17+
"test3" : 3
18+
},
19+
"test2" : 2
20+
}
21+
},
22+
"test1" : 1
23+
}

example_output/example2.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"test4" : [
3+
1,
4+
2,
5+
3,
6+
4,
7+
"hello",
8+
true,
9+
1.300000
10+
],
11+
"test3" : {
12+
"x" : null
13+
},
14+
"test2" : 10,
15+
"test1" : "hello world"
16+
}

example_output/example3.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"test2" : "BLAH𠂊BLAH",
3+
"test1" : "hello world",
4+
"test4" : [
5+
1,
6+
2,
7+
3,
8+
{
9+
"z" : 12345
10+
}
11+
],
12+
"test3" : {
13+
"x" : 123.456
14+
}
15+
}
16+
----------
17+
12345
18+
{"test2":"BLAH\uD840\uDC8ABLAH","test1":"hello world","test4":[1,2,3,{"z":12345}],"test3":{"x":123.456}}

example_output/example4.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[1,2,3,4,"Testing 1 2 3",{"world":5678,"hello":1234}]

0 commit comments

Comments
 (0)