Skip to content

Commit

Permalink
better organization and testability of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eteran committed May 12, 2015
1 parent 24cf48f commit 3a967d1
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 108 deletions.
38 changes: 12 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,43 +1,29 @@

SHELL=/bin/sh

LD := $(CXX)
MKDIR := mkdir -p
OBJ_DIR := .obj

.SUFFIXES:

$(OBJ_DIR)/%.o: %.cc
$(CXX) $(CXXFLAGS) -MMD -c $< -o $@

TARGET = cpp-json

CXXFLAGS := -std=c++11 -pedantic -Wextra -Wall -Iinclude -O2 -g3
LDFLAGS :=
H_FILES :=
CXX_FILES := main.cc

O_FILES := $(patsubst %.cc, $(OBJ_DIR)/%.o, $(CXX_FILES))
D_FILES := $(O_FILES:.o=.d)

SOURCEFILES := $(H_FILES) $(CXX_FILES)
.PRECIOUS: $(SOURCEFILES)
.PHONY: all clean

all: $(TARGET)
TARGETS := example1 example2 example3 example4

$(O_FILES): | $(OBJ_DIR)
all: $(TARGETS)

$(D_FILES): | $(OBJ_DIR)
example1: example1.cc
$(CXX) $(CXXFLAGS) $^ -o $@

example2: example2.cc
$(CXX) $(CXXFLAGS) $^ -o $@

$(OBJ_DIR) :
@$(MKDIR) $@
example3: example3.cc
$(CXX) $(CXXFLAGS) $^ -o $@

$(TARGET): $(O_FILES)
$(LD) $(LDFLAGS) $^ -o $@
example4: example4.cc
$(CXX) $(CXXFLAGS) $^ -o $@

clean:
$(RM) $(O_FILES) $(D_FILES) $(TARGET)
$(RM) $(TARGETS)

-include $(D_FILES)

17 changes: 17 additions & 0 deletions example1.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#include "cpp-json/json.h"
#include <iostream>
#include <fstream>

//------------------------------------------------------------------------------
// Name: main
//------------------------------------------------------------------------------
int main() {

// construct from a file
std::ifstream file("example1.json");
if(file) {
json::value v1 = json::parse(file);
std::cout << stringify(v1, json::PRETTY_PRINT | json::ESCAPE_UNICODE) << std::endl;
}
}
File renamed without changes.
24 changes: 24 additions & 0 deletions example2.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

#include "cpp-json/json.h"
#include <iostream>

//------------------------------------------------------------------------------
// Name: main
//------------------------------------------------------------------------------
int main() {

// construct programatically (legacy C++03 API)
json::object obj1;
obj1.insert("test1", "hello world")
.insert("test2", 10)
.insert("test3", json::object().insert("x", json::null))

// if we are using C++11, we have a MUCH more elegant array initializer option
#if __cplusplus >= 201103L
.insert("test4", json::array().append(1, 2, 3, 4, "hello", true, 1.3));
#else
.insert("test4", json::array().append(1).append(2).append(3).append(4).append("hello").append(true).append(1.3));
#endif

std::cout << stringify(obj1, json::PRETTY_PRINT) << std::endl;
}
40 changes: 40 additions & 0 deletions example3.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

#include "cpp-json/json.h"
#include <iostream>

//------------------------------------------------------------------------------
// Name: main
//------------------------------------------------------------------------------
int main() {

// construct from string (C++11 raw string literals work nicely here!)
#if __cplusplus >= 201103L
auto v = json::parse(R"(
{
"test3" : {
"x" : 123.456
},
"test4" : [
1,
2,
3,
{
"z" : 12345
}
],
"test1" : "hello world",
"test2" : "BLAH\uD840\uDC8ABLAH"
}
)");
#else
json::value v = json::parse("{\"test3\" : {\"x\" : 123.456},\"test4\" : [1,2,3,{\"z\":12345}],\"test1\" : \"hello world\",\"test2\" : \"BLAH\\uD840\\uDC8ABLAH\"}");
#endif
std::cout << stringify(v, json::PRETTY_PRINT) << std::endl;
std::cout << "----------" << std::endl;

// get a specific value
json::value z = v["test4"][3]["z"];
std::cout << to_number(z) << std::endl;

std::cout << stringify(v, json::ESCAPE_UNICODE) << std::endl;
}
24 changes: 24 additions & 0 deletions example4.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

#include "cpp-json/json.h"
#include <iostream>

//------------------------------------------------------------------------------
// Name: main
//------------------------------------------------------------------------------
int main() {

// construct programmatically using object literal syntax in C++11
#if __cplusplus >= 201103L
json::array arr = {
1,
2,
3,
4,
"Testing 1 2 3", json::object{
{ "hello", 1234 },
{ "world", 5678 }
}
};
std::cout << stringify(arr) << std::endl;
#endif
}
23 changes: 23 additions & 0 deletions example_output/example1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"n2" : -1234,
"b2" : false,
"n1" : null,
"b1" : true,
"u" : "\u1234",
"test\nreturn" : "hello\tworld",
"object1" : {
"object2" : {
"object3" : {
"test4" : [
1,
2,
3,
4
],
"test3" : 3
},
"test2" : 2
}
},
"test1" : 1
}
16 changes: 16 additions & 0 deletions example_output/example2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"test4" : [
1,
2,
3,
4,
"hello",
true,
1.300000
],
"test3" : {
"x" : null
},
"test2" : 10,
"test1" : "hello world"
}
18 changes: 18 additions & 0 deletions example_output/example3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"test2" : "BLAH𠂊BLAH",
"test1" : "hello world",
"test4" : [
1,
2,
3,
{
"z" : 12345
}
],
"test3" : {
"x" : 123.456
}
}
----------
12345
{"test2":"BLAH\uD840\uDC8ABLAH","test1":"hello world","test4":[1,2,3,{"z":12345}],"test3":{"x":123.456}}
1 change: 1 addition & 0 deletions example_output/example4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1,2,3,4,"Testing 1 2 3",{"world":5678,"hello":1234}]
82 changes: 0 additions & 82 deletions main.cc

This file was deleted.

0 comments on commit 3a967d1

Please sign in to comment.