Changed unit testing framework from Google Test to Catch

This commit is contained in:
Benoit Blanchon
2017-04-18 18:22:24 +02:00
parent f2ef338cb8
commit df541a2a22
266 changed files with 15955 additions and 146149 deletions

View File

@ -14,4 +14,5 @@ if(${COVERAGE})
set(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage") set(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
endif() endif()
add_subdirectory(third-party/catch)
add_subdirectory(test) add_subdirectory(test)

View File

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <string.h> // for strcmp
#include "./ctype.hpp" #include "./ctype.hpp"
namespace ArduinoJson { namespace ArduinoJson {

View File

@ -8,6 +8,7 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> // for size_t
#include "../Polyfills/math.hpp" #include "../Polyfills/math.hpp"
namespace ArduinoJson { namespace ArduinoJson {

View File

@ -5,11 +5,8 @@
# https://bblanchon.github.io/ArduinoJson/ # https://bblanchon.github.io/ArduinoJson/
# If you like this project, please add a star! # If you like this project, please add a star!
include(gtest.cmake)
if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)") if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
add_compile_options( add_compile_options(
-fno-exceptions
-pedantic -pedantic
-Wall -Wall
-Wcast-align -Wcast-align

View File

@ -6,12 +6,13 @@
# If you like this project, please add a star! # If you like this project, please add a star!
add_executable(DynamicJsonBufferTests add_executable(DynamicJsonBufferTests
array.cpp alloc.cpp
basics.cpp createArray.cpp
noMemory.cpp no_memory.cpp
object.cpp createObject.cpp
string.cpp strdup.cpp
startString.cpp
) )
target_link_libraries(DynamicJsonBufferTests gtest) target_link_libraries(DynamicJsonBufferTests catch)
add_test(DynamicJsonBuffer DynamicJsonBufferTests) add_test(DynamicJsonBuffer DynamicJsonBufferTests)

View File

@ -0,0 +1,45 @@
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <catch.hpp>
static bool isAligned(void* ptr) {
const size_t mask = sizeof(void*) - 1;
size_t addr = reinterpret_cast<size_t>(ptr);
return (addr & mask) == 0;
}
TEST_CASE("DynamicJsonBuffer::alloc()") {
DynamicJsonBuffer buffer;
SECTION("InitialSizeIsZero") {
REQUIRE(0 == buffer.size());
}
SECTION("SizeIncreasesAfterAlloc") {
buffer.alloc(1);
REQUIRE(1U <= buffer.size());
buffer.alloc(1);
REQUIRE(2U <= buffer.size());
}
SECTION("ReturnDifferentPointer") {
void* p1 = buffer.alloc(1);
void* p2 = buffer.alloc(2);
REQUIRE(p1 != p2);
}
SECTION("Alignment") {
// make room for two but not three
buffer = DynamicJsonBuffer(2 * sizeof(void*) + 1);
REQUIRE(isAligned(buffer.alloc(1))); // this on is aligned by design
REQUIRE(isAligned(buffer.alloc(1))); // this one fits in the first block
REQUIRE(isAligned(buffer.alloc(1))); // this one requires a new block
}
}

View File

@ -1,33 +0,0 @@
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
TEST(DynamicJsonBuffer_Array_Tests, GrowsWithArray) {
DynamicJsonBuffer jsonBuffer;
JsonArray &array = jsonBuffer.createArray();
ASSERT_EQ(JSON_ARRAY_SIZE(0), jsonBuffer.size());
array.add("hello");
ASSERT_EQ(JSON_ARRAY_SIZE(1), jsonBuffer.size());
array.add("world");
ASSERT_EQ(JSON_ARRAY_SIZE(2), jsonBuffer.size());
}
TEST(DynamicJsonBuffer_Array_Tests, CanAdd1000Values) {
DynamicJsonBuffer jsonBuffer;
JsonArray &array = jsonBuffer.createArray();
for (int i = 1; i <= 1000; i++) {
array.add("hello");
ASSERT_EQ(array.size(), i);
}
}

View File

@ -1,59 +0,0 @@
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
class DynamicJsonBuffer_Basic_Tests : public testing::Test {
protected:
DynamicJsonBuffer buffer;
};
TEST_F(DynamicJsonBuffer_Basic_Tests, InitialSizeIsZero) {
ASSERT_EQ(0, buffer.size());
}
TEST_F(DynamicJsonBuffer_Basic_Tests, SizeIncreasesAfterAlloc) {
buffer.alloc(1);
ASSERT_LE(1U, buffer.size());
buffer.alloc(1);
ASSERT_LE(2U, buffer.size());
}
TEST_F(DynamicJsonBuffer_Basic_Tests, ReturnDifferentPointer) {
void* p1 = buffer.alloc(1);
void* p2 = buffer.alloc(2);
ASSERT_NE(p1, p2);
}
static bool isAligned(void* ptr) {
const size_t mask = sizeof(void*) - 1;
size_t addr = reinterpret_cast<size_t>(ptr);
return (addr & mask) == 0;
}
TEST_F(DynamicJsonBuffer_Basic_Tests, Alignment) {
// make room for tow but not three
buffer = DynamicJsonBuffer(2 * sizeof(void*) + 1);
ASSERT_TRUE(isAligned(buffer.alloc(1))); // this on is aligned by design
ASSERT_TRUE(isAligned(buffer.alloc(1))); // this one fits in the first block
ASSERT_TRUE(isAligned(buffer.alloc(1))); // this one requires a new block
}
TEST_F(DynamicJsonBuffer_Basic_Tests, strdup) {
char original[] = "hello";
char* copy = buffer.strdup(original);
strcpy(original, "world");
ASSERT_STREQ("hello", copy);
}
TEST_F(DynamicJsonBuffer_Basic_Tests, strdup_givenNull) {
const char* original = NULL;
char* copy = buffer.strdup(original);
ASSERT_EQ(NULL, copy);
}

View File

@ -0,0 +1,31 @@
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("DynamicJsonBuffer::createArray()") {
DynamicJsonBuffer jsonBuffer;
JsonArray &array = jsonBuffer.createArray();
SECTION("GrowsWithArray") {
REQUIRE(JSON_ARRAY_SIZE(0) == jsonBuffer.size());
array.add("hello");
REQUIRE(JSON_ARRAY_SIZE(1) == jsonBuffer.size());
array.add("world");
REQUIRE(JSON_ARRAY_SIZE(2) == jsonBuffer.size());
}
SECTION("CanAdd1000Values") {
for (size_t i = 1; i <= 1000; i++) {
array.add("hello");
REQUIRE(array.size() == i);
}
}
}

View File

@ -6,20 +6,20 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
TEST(DynamicJsonBuffer_Object_Tests, GrowsWithObject) { TEST_CASE("DynamicJsonBuffer::createObject()") {
DynamicJsonBuffer json; DynamicJsonBuffer json;
JsonObject &obj = json.createObject(); JsonObject &obj = json.createObject();
ASSERT_EQ(JSON_OBJECT_SIZE(0), json.size()); REQUIRE(JSON_OBJECT_SIZE(0) == json.size());
obj["hello"] = 1; obj["hello"] = 1;
ASSERT_EQ(JSON_OBJECT_SIZE(1), json.size()); REQUIRE(JSON_OBJECT_SIZE(1) == json.size());
obj["world"] = 2; obj["world"] = 2;
ASSERT_EQ(JSON_OBJECT_SIZE(2), json.size()); REQUIRE(JSON_OBJECT_SIZE(2) == json.size());
obj["world"] = 3; // <- same key, should not grow obj["world"] = 3; // <- same key, should not grow
ASSERT_EQ(JSON_OBJECT_SIZE(2), json.size()); REQUIRE(JSON_OBJECT_SIZE(2) == json.size());
} }

View File

@ -1,52 +0,0 @@
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
class NoMemoryAllocator {
public:
void* allocate(size_t) {
return NULL;
}
void deallocate(void*) {}
};
class DynamicJsonBuffer_NoMemory_Tests : public ::testing::Test {
protected:
DynamicJsonBufferBase<NoMemoryAllocator> _jsonBuffer;
};
TEST_F(DynamicJsonBuffer_NoMemory_Tests, FixCodeCoverage) {
// call this function to fix code coverage
NoMemoryAllocator().deallocate(NULL);
}
TEST_F(DynamicJsonBuffer_NoMemory_Tests, CreateArray) {
ASSERT_FALSE(_jsonBuffer.createArray().success());
}
TEST_F(DynamicJsonBuffer_NoMemory_Tests, CreateObject) {
ASSERT_FALSE(_jsonBuffer.createObject().success());
}
TEST_F(DynamicJsonBuffer_NoMemory_Tests, ParseArray) {
char json[] = "[]";
ASSERT_FALSE(_jsonBuffer.parseArray(json).success());
}
TEST_F(DynamicJsonBuffer_NoMemory_Tests, ParseObject) {
char json[] = "{}";
ASSERT_FALSE(_jsonBuffer.parseObject(json).success());
}
TEST_F(DynamicJsonBuffer_NoMemory_Tests, String) {
DynamicJsonBufferBase<NoMemoryAllocator>::String str =
_jsonBuffer.startString();
str.append('!');
ASSERT_EQ(NULL, str.c_str());
}

View File

@ -0,0 +1,50 @@
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <catch.hpp>
struct NoMemoryAllocator {
void* allocate(size_t) {
return NULL;
}
void deallocate(void*) {}
};
TEST_CASE("DynamicJsonBuffer no memory") {
DynamicJsonBufferBase<NoMemoryAllocator> _jsonBuffer;
SECTION("FixCodeCoverage") {
// call this function to fix code coverage
NoMemoryAllocator().deallocate(NULL);
}
SECTION("createArray()") {
REQUIRE_FALSE(_jsonBuffer.createArray().success());
}
SECTION("createObject()") {
REQUIRE_FALSE(_jsonBuffer.createObject().success());
}
SECTION("parseArray()") {
char json[] = "[]";
REQUIRE_FALSE(_jsonBuffer.parseArray(json).success());
}
SECTION("parseObject()") {
char json[] = "{}";
REQUIRE_FALSE(_jsonBuffer.parseObject(json).success());
}
SECTION("startString()") {
DynamicJsonBufferBase<NoMemoryAllocator>::String str =
_jsonBuffer.startString();
str.append('!');
REQUIRE(0 == str.c_str());
}
}

View File

@ -0,0 +1,50 @@
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("DynamicJsonBuffer::startString()") {
SECTION("WorksWhenBufferIsBigEnough") {
DynamicJsonBuffer jsonBuffer(6);
DynamicJsonBuffer::String str = jsonBuffer.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
REQUIRE(std::string("hello") == str.c_str());
}
SECTION("GrowsWhenBufferIsTooSmall") {
DynamicJsonBuffer jsonBuffer(5);
DynamicJsonBuffer::String str = jsonBuffer.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
REQUIRE(std::string("hello") == str.c_str());
}
SECTION("SizeIncreases") {
DynamicJsonBuffer jsonBuffer(5);
DynamicJsonBuffer::String str = jsonBuffer.startString();
REQUIRE(0 == jsonBuffer.size());
str.append('h');
REQUIRE(1 == jsonBuffer.size());
str.c_str();
REQUIRE(2 == jsonBuffer.size());
}
}

View File

@ -0,0 +1,26 @@
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("DynamicJsonBuffer::strdup()") {
DynamicJsonBuffer buffer;
SECTION("Should return a copy") {
char original[] = "hello";
char* copy = buffer.strdup(original);
strcpy(original, "world");
REQUIRE(std::string("hello") == copy);
}
SECTION("Given NULL, return NULL") {
const char* original = NULL;
char* copy = buffer.strdup(original);
REQUIRE(0 == copy);
}
}

View File

@ -1,48 +0,0 @@
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
TEST(DynamicJsonBuffer_String_Tests, WorksWhenBufferIsBigEnough) {
DynamicJsonBuffer jsonBuffer(6);
DynamicJsonBuffer::String str = jsonBuffer.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
ASSERT_STREQ("hello", str.c_str());
}
TEST(DynamicJsonBuffer_String_Tests, GrowsWhenBufferIsTooSmall) {
DynamicJsonBuffer jsonBuffer(5);
DynamicJsonBuffer::String str = jsonBuffer.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
ASSERT_STREQ("hello", str.c_str());
}
TEST(DynamicJsonBuffer_String_Tests, SizeIncreases) {
DynamicJsonBuffer jsonBuffer(5);
DynamicJsonBuffer::String str = jsonBuffer.startString();
ASSERT_EQ(0, jsonBuffer.size());
str.append('h');
ASSERT_EQ(1, jsonBuffer.size());
str.c_str();
ASSERT_EQ(2, jsonBuffer.size());
}

View File

@ -7,8 +7,8 @@
add_executable(IntegrationTests add_executable(IntegrationTests
gbathree.cpp gbathree.cpp
parse_print.cpp round_trip.cpp
) )
target_link_libraries(IntegrationTests gtest) target_link_libraries(IntegrationTests catch)
add_test(IntegrationTests IntegrationTests) add_test(IntegrationTests IntegrationTests)

View File

@ -6,20 +6,12 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class GbathreeBug : public testing::Test { TEST_CASE("Gbathree") {
public:
GbathreeBug() : _object(_buffer.parseObject(getJson())) {}
protected:
char _json[1024];
DynamicJsonBuffer _buffer; DynamicJsonBuffer _buffer;
const JsonObject& _object;
private: const JsonObject& _object = _buffer.parseObject(
char* getJson() {
strcpy(_json,
"{\"protocol_name\":\"fluorescence\",\"repeats\":1,\"wait\":0," "{\"protocol_name\":\"fluorescence\",\"repeats\":1,\"wait\":0,"
"\"averages\":1,\"measurements\":3,\"meas2_light\":15,\"meas1_" "\"averages\":1,\"measurements\":3,\"meas2_light\":15,\"meas1_"
"baseline\":0,\"act_light\":20,\"pulsesize\":25,\"pulsedistance\":" "baseline\":0,\"act_light\":20,\"pulsesize\":25,\"pulsedistance\":"
@ -30,186 +22,190 @@ class GbathreeBug : public testing::Test {
"\"measlights\":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15," "\"measlights\":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,"
"15,15]],\"measlights2\":[[15,15,15,15],[15,15,15,15],[15,15,15,15]," "15,15]],\"measlights2\":[[15,15,15,15],[15,15,15,15],[15,15,15,15],"
"[15,15,15,15]],\"altc\":[2,2,2,2],\"altd\":[2,2,2,2]}"); "[15,15,15,15]],\"altc\":[2,2,2,2],\"altd\":[2,2,2,2]}");
return _json;
}
};
TEST_F(GbathreeBug, Success) { SECTION("Success") {
EXPECT_TRUE(_object.success()); REQUIRE(_object.success());
} }
TEST_F(GbathreeBug, ProtocolName) { SECTION("ProtocolName") {
EXPECT_STREQ("fluorescence", _object["protocol_name"]); REQUIRE("fluorescence" == _object["protocol_name"]);
} }
TEST_F(GbathreeBug, Repeats) { SECTION("Repeats") {
EXPECT_EQ(1, _object["repeats"]); REQUIRE(1 == _object["repeats"]);
} }
TEST_F(GbathreeBug, Wait) { SECTION("Wait") {
EXPECT_EQ(0, _object["wait"]); REQUIRE(0 == _object["wait"]);
} }
TEST_F(GbathreeBug, Measurements) { SECTION("Measurements") {
EXPECT_EQ(3, _object["measurements"]); REQUIRE(3 == _object["measurements"]);
} }
TEST_F(GbathreeBug, Meas2_Light) { SECTION("Meas2_Light") {
EXPECT_EQ(15, _object["meas2_light"]); REQUIRE(15 == _object["meas2_light"]);
} }
TEST_F(GbathreeBug, Meas1_Baseline) { SECTION("Meas1_Baseline") {
EXPECT_EQ(0, _object["meas1_baseline"]); REQUIRE(0 == _object["meas1_baseline"]);
} }
TEST_F(GbathreeBug, Act_Light) { SECTION("Act_Light") {
EXPECT_EQ(20, _object["act_light"]); REQUIRE(20 == _object["act_light"]);
} }
TEST_F(GbathreeBug, Pulsesize) { SECTION("Pulsesize") {
EXPECT_EQ(25, _object["pulsesize"]); REQUIRE(25 == _object["pulsesize"]);
} }
TEST_F(GbathreeBug, Pulsedistance) { SECTION("Pulsedistance") {
EXPECT_EQ(10000, _object["pulsedistance"]); REQUIRE(10000 == _object["pulsedistance"]);
} }
TEST_F(GbathreeBug, Actintensity1) { SECTION("Actintensity1") {
EXPECT_EQ(50, _object["actintensity1"]); REQUIRE(50 == _object["actintensity1"]);
} }
TEST_F(GbathreeBug, Actintensity2) { SECTION("Actintensity2") {
EXPECT_EQ(255, _object["actintensity2"]); REQUIRE(255 == _object["actintensity2"]);
} }
TEST_F(GbathreeBug, Measintensity) { SECTION("Measintensity") {
EXPECT_EQ(255, _object["measintensity"]); REQUIRE(255 == _object["measintensity"]);
} }
TEST_F(GbathreeBug, Calintensity) { SECTION("Calintensity") {
EXPECT_EQ(255, _object["calintensity"]); REQUIRE(255 == _object["calintensity"]);
} }
TEST_F(GbathreeBug, Pulses) { SECTION("Pulses") {
// "pulses":[50,50,50] // "pulses":[50,50,50]
JsonArray& array = _object["pulses"]; JsonArray& array = _object["pulses"];
EXPECT_TRUE(array.success()); REQUIRE(array.success());
EXPECT_EQ(3, array.size()); REQUIRE(3 == array.size());
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
EXPECT_EQ(50, array[i]); REQUIRE(50 == array[i]);
} }
} }
TEST_F(GbathreeBug, Act) { SECTION("Act") {
// "act":[2,1,2,2] // "act":[2,1,2,2]
JsonArray& array = _object["act"]; JsonArray& array = _object["act"];
EXPECT_TRUE(array.success()); REQUIRE(array.success());
EXPECT_EQ(4, array.size()); REQUIRE(4 == array.size());
EXPECT_EQ(2, array[0]); REQUIRE(2 == array[0]);
EXPECT_EQ(1, array[1]); REQUIRE(1 == array[1]);
EXPECT_EQ(2, array[2]); REQUIRE(2 == array[2]);
EXPECT_EQ(2, array[3]); REQUIRE(2 == array[3]);
} }
TEST_F(GbathreeBug, Detectors) { SECTION("Detectors") {
// "detectors":[[34,34,34,34],[34,34,34,34],[34,34,34,34],[34,34,34,34]] // "detectors":[[34,34,34,34],[34,34,34,34],[34,34,34,34],[34,34,34,34]]
JsonArray& array = _object["detectors"]; JsonArray& array = _object["detectors"];
EXPECT_TRUE(array.success()); REQUIRE(array.success());
EXPECT_EQ(4, array.size()); REQUIRE(4 == array.size());
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
JsonArray& nestedArray = array[i]; JsonArray& nestedArray = array[i];
EXPECT_EQ(4, nestedArray.size()); REQUIRE(4 == nestedArray.size());
for (int j = 0; j < 4; j++) EXPECT_EQ(34, nestedArray[j]); for (int j = 0; j < 4; j++) {
REQUIRE(34 == nestedArray[j]);
}
} }
} }
TEST_F(GbathreeBug, Alta) { SECTION("Alta") {
// alta:[2,2,2,2] // alta:[2,2,2,2]
JsonArray& array = _object["alta"]; JsonArray& array = _object["alta"];
EXPECT_TRUE(array.success()); REQUIRE(array.success());
EXPECT_EQ(4, array.size()); REQUIRE(4 == array.size());
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
EXPECT_EQ(2, array[i]); REQUIRE(2 == array[i]);
} }
} }
TEST_F(GbathreeBug, Altb) { SECTION("Altb") {
// altb:[2,2,2,2] // altb:[2,2,2,2]
JsonArray& array = _object["altb"]; JsonArray& array = _object["altb"];
EXPECT_TRUE(array.success()); REQUIRE(array.success());
EXPECT_EQ(4, array.size()); REQUIRE(4 == array.size());
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
EXPECT_EQ(2, array[i]); REQUIRE(2 == array[i]);
} }
} }
TEST_F(GbathreeBug, Measlights) { SECTION("Measlights") {
// "measlights":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]] // "measlights":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]]
JsonArray& array = _object["measlights"]; JsonArray& array = _object["measlights"];
EXPECT_TRUE(array.success()); REQUIRE(array.success());
EXPECT_EQ(4, array.size()); REQUIRE(4 == array.size());
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
JsonArray& nestedArray = array[i]; JsonArray& nestedArray = array[i];
EXPECT_EQ(4, nestedArray.size()); REQUIRE(4 == nestedArray.size());
for (int j = 0; j < 4; j++) EXPECT_EQ(15, nestedArray[j]); for (int j = 0; j < 4; j++) {
REQUIRE(15 == nestedArray[j]);
}
} }
} }
TEST_F(GbathreeBug, Measlights2) { SECTION("Measlights2") {
// "measlights2":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]] // "measlights2":[[15,15,15,15],[15,15,15,15],[15,15,15,15],[15,15,15,15]]
JsonArray& array = _object["measlights2"]; JsonArray& array = _object["measlights2"];
EXPECT_TRUE(array.success()); REQUIRE(array.success());
EXPECT_EQ(4, array.size()); REQUIRE(4 == array.size());
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
JsonArray& nestedArray = array[i]; JsonArray& nestedArray = array[i];
EXPECT_EQ(4, nestedArray.size()); REQUIRE(4 == nestedArray.size());
for (int j = 0; j < 4; j++) EXPECT_EQ(15, nestedArray[j]); for (int j = 0; j < 4; j++) {
REQUIRE(15 == nestedArray[j]);
}
} }
} }
TEST_F(GbathreeBug, Altc) { SECTION("Altc") {
// altc:[2,2,2,2] // altc:[2,2,2,2]
JsonArray& array = _object["altc"]; JsonArray& array = _object["altc"];
EXPECT_TRUE(array.success()); REQUIRE(array.success());
EXPECT_EQ(4, array.size()); REQUIRE(4 == array.size());
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
EXPECT_EQ(2, array[i]); REQUIRE(2 == array[i]);
} }
} }
TEST_F(GbathreeBug, Altd) { SECTION("Altd") {
// altd:[2,2,2,2] // altd:[2,2,2,2]
JsonArray& array = _object["altd"]; JsonArray& array = _object["altd"];
EXPECT_TRUE(array.success()); REQUIRE(array.success());
EXPECT_EQ(4, array.size()); REQUIRE(4 == array.size());
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
EXPECT_EQ(2, array[i]); REQUIRE(2 == array[i]);
}
} }
} }

View File

@ -6,47 +6,23 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class IntegrationTests : public testing::TestWithParam<const char*> { void check(std::string originalJson) {
static const size_t MAX_JSON_SIZE = 10000; DynamicJsonBuffer jb;
protected: std::string prettyJson;
virtual void SetUp() { jb.parseObject(originalJson).prettyPrintTo(prettyJson);
_input = GetParam();
strcpy(_inputBuffer, _input); std::string finalJson;
jb.parseObject(prettyJson).printTo(finalJson);
REQUIRE(originalJson == finalJson);
} }
void parseThenPrint(char* input, char* output) { TEST_CASE("Round Trip: parse -> prettyPrint -> parse -> print") {
DynamicJsonBuffer buffer; SECTION("OpenWeatherMap") {
buffer.parseObject(input).printTo(output, MAX_JSON_SIZE); check(
}
void parseThenPrettyPrint(char* input, char* output) {
DynamicJsonBuffer buffer;
buffer.parseObject(input).prettyPrintTo(output, MAX_JSON_SIZE);
}
const char* _input;
char _inputBuffer[MAX_JSON_SIZE];
char _outputBuffer[MAX_JSON_SIZE];
char _intermediateBuffer[MAX_JSON_SIZE];
};
TEST_P(IntegrationTests, ParseThenPrint) {
parseThenPrint(_inputBuffer, _outputBuffer);
ASSERT_STREQ(_input, _outputBuffer);
}
TEST_P(IntegrationTests, ParseThenPrettyPrintThenParseThenPrint) {
parseThenPrettyPrint(_inputBuffer, _intermediateBuffer);
parseThenPrint(_intermediateBuffer, _outputBuffer);
ASSERT_STREQ(_input, _outputBuffer);
}
INSTANTIATE_TEST_CASE_P(
OpenWeatherMap, IntegrationTests,
testing::Values(
"{\"coord\":{\"lon\":145.77,\"lat\":-16.92},\"sys\":{\"type\":1,\"id\":" "{\"coord\":{\"lon\":145.77,\"lat\":-16.92},\"sys\":{\"type\":1,\"id\":"
"8166,\"message\":0.1222,\"country\":\"AU\",\"sunrise\":1414784325," "8166,\"message\":0.1222,\"country\":\"AU\",\"sunrise\":1414784325,"
"\"sunset\":1414830137},\"weather\":[{\"id\":801,\"main\":\"Clouds\"," "\"sunset\":1414830137},\"weather\":[{\"id\":801,\"main\":\"Clouds\","
@ -54,11 +30,11 @@ INSTANTIATE_TEST_CASE_P(
"stations\",\"main\":{\"temp\":296.15,\"pressure\":1014,\"humidity\":" "stations\",\"main\":{\"temp\":296.15,\"pressure\":1014,\"humidity\":"
"83,\"temp_min\":296.15,\"temp_max\":296.15},\"wind\":{\"speed\":2.22," "83,\"temp_min\":296.15,\"temp_max\":296.15},\"wind\":{\"speed\":2.22,"
"\"deg\":114.501},\"clouds\":{\"all\":20},\"dt\":1414846800,\"id\":" "\"deg\":114.501},\"clouds\":{\"all\":20},\"dt\":1414846800,\"id\":"
"2172797,\"name\":\"Cairns\",\"cod\":200}")); "2172797,\"name\":\"Cairns\",\"cod\":200}");
}
INSTANTIATE_TEST_CASE_P( SECTION("YahooQueryLanguage") {
YahooQueryLanguage, IntegrationTests, check(
testing::Values(
"{\"query\":{\"count\":40,\"created\":\"2014-11-01T14:16:49Z\"," "{\"query\":{\"count\":40,\"created\":\"2014-11-01T14:16:49Z\","
"\"lang\":\"fr-FR\",\"results\":{\"item\":[{\"title\":\"Burkina army " "\"lang\":\"fr-FR\",\"results\":{\"item\":[{\"title\":\"Burkina army "
"backs Zida as interim leader\"},{\"title\":\"British jets intercept " "backs Zida as interim leader\"},{\"title\":\"British jets intercept "
@ -102,4 +78,6 @@ INSTANTIATE_TEST_CASE_P(
"release of Marine veteran\"},{\"title\":\"As election closes in, " "release of Marine veteran\"},{\"title\":\"As election closes in, "
"Obama on center stage\"},{\"title\":\"Body of Zambian president " "Obama on center stage\"},{\"title\":\"Body of Zambian president "
"arrives home\"},{\"title\":\"South Africa arrests 2 Vietnamese for " "arrives home\"},{\"title\":\"South Africa arrests 2 Vietnamese for "
"poaching\"}]}}}")); "poaching\"}]}}}");
}
}

View File

@ -19,5 +19,5 @@ add_executable(JsonArrayTests
subscript.cpp subscript.cpp
) )
target_link_libraries(JsonArrayTests gtest) target_link_libraries(JsonArrayTests catch)
add_test(JsonArray JsonArrayTests) add_test(JsonArray JsonArrayTests)

View File

@ -6,85 +6,83 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class JsonArray_Add_Tests : public ::testing::Test {
protected:
JsonArray_Add_Tests() : _array(_jsonBuffer.createArray()) {}
TEST_CASE("JsonArray::add()") {
DynamicJsonBuffer _jsonBuffer; DynamicJsonBuffer _jsonBuffer;
JsonArray& _array; JsonArray& _array = _jsonBuffer.createArray();
};
#define TEST_(name) TEST_F(JsonArray_Add_Tests, name) SECTION("SizeIncreased_WhenValuesAreAdded") {
TEST_(SizeIncreased_WhenValuesAreAdded) {
_array.add("hello"); _array.add("hello");
EXPECT_EQ(1U, _array.size()); REQUIRE(1U == _array.size());
} }
TEST_(StoreInteger) { SECTION("StoreInteger") {
_array.add(123); _array.add(123);
EXPECT_EQ(123, _array[0].as<int>()); REQUIRE(123 == _array[0].as<int>());
EXPECT_TRUE(_array[0].is<int>()); REQUIRE(_array[0].is<int>());
EXPECT_FALSE(_array[0].is<double>()); REQUIRE_FALSE(_array[0].is<double>());
} }
TEST_(StoreDouble) { SECTION("StoreDouble") {
_array.add(123.45); _array.add(123.45);
EXPECT_EQ(123.45, _array[0].as<double>()); REQUIRE(123.45 == _array[0].as<double>());
EXPECT_TRUE(_array[0].is<double>()); REQUIRE(_array[0].is<double>());
EXPECT_FALSE(_array[0].is<int>()); REQUIRE_FALSE(_array[0].is<int>());
} }
TEST_(StoreBoolean) { SECTION("StoreBoolean") {
_array.add(true); _array.add(true);
EXPECT_EQ(true, _array[0].as<bool>()); REQUIRE(true == _array[0].as<bool>());
EXPECT_TRUE(_array[0].is<bool>()); REQUIRE(_array[0].is<bool>());
EXPECT_FALSE(_array[0].is<int>()); REQUIRE_FALSE(_array[0].is<int>());
} }
TEST_(StoreString) { SECTION("StoreString") {
_array.add("hello"); const char* str = "hello";
EXPECT_STREQ("hello", _array[0].as<const char*>()); _array.add(str);
EXPECT_TRUE(_array[0].is<const char*>()); REQUIRE(str == _array[0].as<const char*>());
EXPECT_FALSE(_array[0].is<int>()); REQUIRE(_array[0].is<const char*>());
REQUIRE_FALSE(_array[0].is<int>());
} }
TEST_(StoreNestedArray) { SECTION("StoreNestedArray") {
JsonArray& arr = _jsonBuffer.createArray(); JsonArray& arr = _jsonBuffer.createArray();
_array.add(arr); _array.add(arr);
EXPECT_EQ(&arr, &_array[0].as<JsonArray&>()); REQUIRE(&arr == &_array[0].as<JsonArray&>());
EXPECT_TRUE(_array[0].is<JsonArray&>()); REQUIRE(_array[0].is<JsonArray&>());
EXPECT_FALSE(_array[0].is<int>()); REQUIRE_FALSE(_array[0].is<int>());
} }
TEST_(StoreNestedObject) { SECTION("StoreNestedObject") {
JsonObject& obj = _jsonBuffer.createObject(); JsonObject& obj = _jsonBuffer.createObject();
_array.add(obj); _array.add(obj);
EXPECT_EQ(&obj, &_array[0].as<JsonObject&>()); REQUIRE(&obj == &_array[0].as<JsonObject&>());
EXPECT_TRUE(_array[0].is<JsonObject&>()); REQUIRE(_array[0].is<JsonObject&>());
EXPECT_FALSE(_array[0].is<int>()); REQUIRE_FALSE(_array[0].is<int>());
} }
TEST_(StoreArraySubscript) { SECTION("StoreArraySubscript") {
const char* str = "hello";
JsonArray& arr = _jsonBuffer.createArray(); JsonArray& arr = _jsonBuffer.createArray();
arr.add("hello"); arr.add(str);
_array.add(arr[0]); _array.add(arr[0]);
EXPECT_STREQ("hello", _array[0]); REQUIRE(str == _array[0]);
} }
TEST_(StoreObjectSubscript) { SECTION("StoreObjectSubscript") {
const char* str = "hello";
JsonObject& obj = _jsonBuffer.createObject(); JsonObject& obj = _jsonBuffer.createObject();
obj["x"] = "hello"; obj["x"] = str;
_array.add(obj["x"]); _array.add(obj["x"]);
EXPECT_STREQ("hello", _array[0]); REQUIRE(str == _array[0]);
}
} }

View File

@ -6,36 +6,27 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
#define TEST_(name) TEST(JsonArray_Basic_Tests, name) TEST_CASE("JsonArray basics") {
DynamicJsonBuffer jb;
JsonArray& array = jb.createArray();
TEST_(SuccessIsTrue) { SECTION("SuccessIsTrue") {
DynamicJsonBuffer _jsonBuffer; REQUIRE(array.success());
JsonArray& array = _jsonBuffer.createArray();
EXPECT_TRUE(array.success());
} }
TEST_(InitialSizeIsZero) { SECTION("InitialSizeIsZero") {
DynamicJsonBuffer _jsonBuffer; REQUIRE(0U == array.size());
JsonArray& array = _jsonBuffer.createArray();
EXPECT_EQ(0U, array.size());
} }
TEST_(CreateNestedArray) { SECTION("CreateNestedArray") {
DynamicJsonBuffer _jsonBuffer;
JsonArray& array = _jsonBuffer.createArray();
JsonArray& arr = array.createNestedArray(); JsonArray& arr = array.createNestedArray();
EXPECT_EQ(&arr, &array[0].as<JsonArray&>()); REQUIRE(&arr == &array[0].as<JsonArray&>());
} }
TEST_(CreateNestedObject) { SECTION("CreateNestedObject") {
DynamicJsonBuffer _jsonBuffer;
JsonArray& array = _jsonBuffer.createArray();
JsonObject& obj = array.createNestedObject(); JsonObject& obj = array.createNestedObject();
EXPECT_EQ(&obj, &array[0].as<JsonObject&>()); REQUIRE(&obj == &array[0].as<JsonObject&>());
}
} }

View File

@ -6,22 +6,23 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
TEST(JsonArray_CopyFrom_Tests, OneDimension) { TEST_CASE("JsonArray::copyFrom()") {
SECTION("OneDimension") {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonArray& array = jsonBuffer.createArray(); JsonArray& array = jsonBuffer.createArray();
char json[32]; char json[32];
int source[] = {1, 2, 3}; int source[] = {1, 2, 3};
bool ok = array.copyFrom(source); bool ok = array.copyFrom(source);
ASSERT_TRUE(ok); REQUIRE(ok);
array.printTo(json, sizeof(json)); array.printTo(json, sizeof(json));
ASSERT_STREQ("[1,2,3]", json); REQUIRE(std::string("[1,2,3]") == json);
} }
TEST(JsonArray_CopyFrom_Tests, OneDimension_JsonBufferTooSmall) { SECTION("OneDimension_JsonBufferTooSmall") {
const size_t SIZE = JSON_ARRAY_SIZE(2); const size_t SIZE = JSON_ARRAY_SIZE(2);
StaticJsonBuffer<SIZE> jsonBuffer; StaticJsonBuffer<SIZE> jsonBuffer;
JsonArray& array = jsonBuffer.createArray(); JsonArray& array = jsonBuffer.createArray();
@ -29,26 +30,26 @@ TEST(JsonArray_CopyFrom_Tests, OneDimension_JsonBufferTooSmall) {
int source[] = {1, 2, 3}; int source[] = {1, 2, 3};
bool ok = array.copyFrom(source); bool ok = array.copyFrom(source);
ASSERT_FALSE(ok); REQUIRE_FALSE(ok);
array.printTo(json, sizeof(json)); array.printTo(json, sizeof(json));
ASSERT_STREQ("[1,2]", json); REQUIRE(std::string("[1,2]") == json);
} }
TEST(JsonArray_CopyFrom_Tests, TwoDimensions) { SECTION("TwoDimensions") {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonArray& array = jsonBuffer.createArray(); JsonArray& array = jsonBuffer.createArray();
char json[32]; char json[32];
int source[][3] = {{1, 2, 3}, {4, 5, 6}}; int source[][3] = {{1, 2, 3}, {4, 5, 6}};
bool ok = array.copyFrom(source); bool ok = array.copyFrom(source);
ASSERT_TRUE(ok); REQUIRE(ok);
array.printTo(json, sizeof(json)); array.printTo(json, sizeof(json));
ASSERT_STREQ("[[1,2,3],[4,5,6]]", json); REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json);
} }
TEST(JsonArray_CopyFrom_Tests, TwoDimensions_JsonBufferTooSmall) { SECTION("TwoDimensions_JsonBufferTooSmall") {
const size_t SIZE = const size_t SIZE =
JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2); JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2);
StaticJsonBuffer<SIZE> jsonBuffer; StaticJsonBuffer<SIZE> jsonBuffer;
@ -57,8 +58,9 @@ TEST(JsonArray_CopyFrom_Tests, TwoDimensions_JsonBufferTooSmall) {
int source[][3] = {{1, 2, 3}, {4, 5, 6}}; int source[][3] = {{1, 2, 3}, {4, 5, 6}};
bool ok = array.copyFrom(source); bool ok = array.copyFrom(source);
ASSERT_FALSE(ok); REQUIRE_FALSE(ok);
array.printTo(json, sizeof(json)); array.printTo(json, sizeof(json));
ASSERT_STREQ("[[1,2,3],[4,5]]", json); REQUIRE(std::string("[[1,2,3],[4,5]]") == json);
}
} }

View File

@ -6,51 +6,50 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
TEST(JsonArray_CopyTo_Tests, BiggerOneDimensionIntegerArray) {
char json[] = "[1,2,3]";
TEST_CASE("JsonArray::copyTo()") {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
SECTION("BiggerOneDimensionIntegerArray") {
char json[] = "[1,2,3]";
JsonArray& array = jsonBuffer.parseArray(json); JsonArray& array = jsonBuffer.parseArray(json);
int destination[4] = {0}; int destination[4] = {0};
size_t result = array.copyTo(destination); size_t result = array.copyTo(destination);
ASSERT_EQ(3, result); REQUIRE(3 == result);
ASSERT_EQ(1, destination[0]); REQUIRE(1 == destination[0]);
ASSERT_EQ(2, destination[1]); REQUIRE(2 == destination[1]);
ASSERT_EQ(3, destination[2]); REQUIRE(3 == destination[2]);
ASSERT_EQ(0, destination[3]); REQUIRE(0 == destination[3]);
} }
TEST(JsonArray_CopyTo_Tests, SmallerOneDimensionIntegerArray) { SECTION("SmallerOneDimensionIntegerArray") {
char json[] = "[1,2,3]"; char json[] = "[1,2,3]";
DynamicJsonBuffer jsonBuffer;
JsonArray& array = jsonBuffer.parseArray(json); JsonArray& array = jsonBuffer.parseArray(json);
int destination[2] = {0}; int destination[2] = {0};
size_t result = array.copyTo(destination); size_t result = array.copyTo(destination);
ASSERT_EQ(2, result); REQUIRE(2 == result);
ASSERT_EQ(1, destination[0]); REQUIRE(1 == destination[0]);
ASSERT_EQ(2, destination[1]); REQUIRE(2 == destination[1]);
} }
TEST(JsonArray_CopyTo_Tests, TwoOneDimensionIntegerArray) { SECTION("TwoOneDimensionIntegerArray") {
char json[] = "[[1,2],[3],[4]]"; char json[] = "[[1,2],[3],[4]]";
DynamicJsonBuffer jsonBuffer;
JsonArray& array = jsonBuffer.parseArray(json); JsonArray& array = jsonBuffer.parseArray(json);
int destination[3][2] = {{0}}; int destination[3][2] = {{0}};
array.copyTo(destination); array.copyTo(destination);
ASSERT_EQ(1, destination[0][0]); REQUIRE(1 == destination[0][0]);
ASSERT_EQ(2, destination[0][1]); REQUIRE(2 == destination[0][1]);
ASSERT_EQ(3, destination[1][0]); REQUIRE(3 == destination[1][0]);
ASSERT_EQ(0, destination[1][1]); REQUIRE(0 == destination[1][1]);
ASSERT_EQ(4, destination[2][0]); REQUIRE(4 == destination[2][0]);
ASSERT_EQ(0, destination[2][1]); REQUIRE(0 == destination[2][1]);
}
} }

View File

@ -6,28 +6,32 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
TEST(JsonArray_Invalid_Tests, SubscriptFails) { using namespace Catch::Matchers;
ASSERT_FALSE(JsonArray::invalid()[0].success());
TEST_CASE("JsonArray::invalid()") {
SECTION("SubscriptFails") {
REQUIRE_FALSE(JsonArray::invalid()[0].success());
} }
TEST(JsonArray_Invalid_Tests, AddFails) { SECTION("AddFails") {
JsonArray& array = JsonArray::invalid(); JsonArray& array = JsonArray::invalid();
array.add(1); array.add(1);
ASSERT_EQ(0, array.size()); REQUIRE(0 == array.size());
} }
TEST(JsonArray_Invalid_Tests, CreateNestedArrayFails) { SECTION("CreateNestedArrayFails") {
ASSERT_FALSE(JsonArray::invalid().createNestedArray().success()); REQUIRE_FALSE(JsonArray::invalid().createNestedArray().success());
} }
TEST(JsonArray_Invalid_Tests, CreateNestedObjectFails) { SECTION("CreateNestedObjectFails") {
ASSERT_FALSE(JsonArray::invalid().createNestedObject().success()); REQUIRE_FALSE(JsonArray::invalid().createNestedObject().success());
} }
TEST(JsonArray_Invalid_Tests, PrintToWritesBrackets) { SECTION("PrintToWritesBrackets") {
char buffer[32]; char buffer[32];
JsonArray::invalid().printTo(buffer, sizeof(buffer)); JsonArray::invalid().printTo(buffer, sizeof(buffer));
ASSERT_STREQ("[]", buffer); REQUIRE_THAT(buffer, Equals("[]"));
}
} }

View File

@ -6,7 +6,7 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
template <typename TIterator> template <typename TIterator>
static void run_iterator_test() { static void run_iterator_test() {
@ -19,21 +19,23 @@ static void run_iterator_test() {
TIterator it = array.begin(); TIterator it = array.begin();
TIterator end = array.end(); TIterator end = array.end();
EXPECT_NE(end, it); REQUIRE(end != it);
EXPECT_EQ(12, it->template as<int>()); REQUIRE(12 == it->template as<int>());
EXPECT_EQ(12, static_cast<int>(*it)); REQUIRE(12 == static_cast<int>(*it));
++it; ++it;
EXPECT_NE(end, it); REQUIRE(end != it);
EXPECT_EQ(34, it->template as<int>()); REQUIRE(34 == it->template as<int>());
EXPECT_EQ(34, static_cast<int>(*it)); REQUIRE(34 == static_cast<int>(*it));
++it; ++it;
EXPECT_EQ(end, it); REQUIRE(end == it);
} }
TEST(JsonArray_Iterator_Test, RunItertorToEnd) { TEST_CASE("JsonArray::begin()/end()") {
SECTION("Mutable") {
run_iterator_test<JsonArray::iterator>(); run_iterator_test<JsonArray::iterator>();
} }
TEST(JsonArray_Iterator_Test, RunConstItertorToEnd) { SECTION("Const") {
run_iterator_test<JsonArray::const_iterator>(); run_iterator_test<JsonArray::const_iterator>();
} }
}

View File

@ -6,64 +6,57 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class JsonArray_PrettyPrintTo_Tests : public testing::Test {
public:
JsonArray_PrettyPrintTo_Tests() : array(jsonBuffer.createArray()) {}
protected:
DynamicJsonBuffer jsonBuffer;
JsonArray& array;
void outputMustBe(const char* expected) {
char actual[256];
static void check(JsonArray& array, std::string expected) {
std::string actual;
size_t actualLen = array.prettyPrintTo(actual); size_t actualLen = array.prettyPrintTo(actual);
size_t measuredLen = array.measurePrettyLength(); size_t measuredLen = array.measurePrettyLength();
CHECK(actualLen == expected.size());
EXPECT_STREQ(expected, actual); CHECK(measuredLen == expected.size());
EXPECT_EQ(strlen(expected), actualLen); REQUIRE(expected == actual);
EXPECT_EQ(strlen(expected), measuredLen);
}
};
TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) {
outputMustBe("[]");
} }
TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) { TEST_CASE("JsonArray::prettyPrintTo()") {
DynamicJsonBuffer jb;
JsonArray& array = jb.createArray();
SECTION("Empty") {
check(array, "[]");
}
SECTION("OneElement") {
array.add(1); array.add(1);
outputMustBe( check(array,
"[\r\n" "[\r\n"
" 1\r\n" " 1\r\n"
"]"); "]");
} }
TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements) { SECTION("TwoElements") {
array.add(1); array.add(1);
array.add(2); array.add(2);
outputMustBe( check(array,
"[\r\n" "[\r\n"
" 1,\r\n" " 1,\r\n"
" 2\r\n" " 2\r\n"
"]"); "]");
} }
TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays) { SECTION("EmptyNestedArrays") {
array.createNestedArray(); array.createNestedArray();
array.createNestedArray(); array.createNestedArray();
outputMustBe( check(array,
"[\r\n" "[\r\n"
" [],\r\n" " [],\r\n"
" []\r\n" " []\r\n"
"]"); "]");
} }
TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays) { SECTION("NestedArrays") {
JsonArray& nested1 = array.createNestedArray(); JsonArray& nested1 = array.createNestedArray();
nested1.add(1); nested1.add(1);
nested1.add(2); nested1.add(2);
@ -71,7 +64,7 @@ TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays) {
JsonObject& nested2 = array.createNestedObject(); JsonObject& nested2 = array.createNestedObject();
nested2["key"] = 3; nested2["key"] = 3;
outputMustBe( check(array,
"[\r\n" "[\r\n"
" [\r\n" " [\r\n"
" 1,\r\n" " 1,\r\n"
@ -82,3 +75,4 @@ TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays) {
" }\r\n" " }\r\n"
"]"); "]");
} }
}

View File

@ -6,147 +6,140 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class JsonArray_PrintTo_Tests : public testing::Test { static void check(JsonArray &array, std::string expected) {
public: std::string actual;
JsonArray_PrintTo_Tests() : array(json.createArray()) {} size_t actualLen = array.printTo(actual);
protected:
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json;
JsonArray &array;
void outputMustBe(const char *expected) {
size_t actualLen = array.printTo(buffer);
size_t measuredLen = array.measureLength(); size_t measuredLen = array.measureLength();
CHECK(actualLen == expected.size());
EXPECT_STREQ(expected, buffer); CHECK(measuredLen == expected.size());
EXPECT_EQ(strlen(expected), actualLen); REQUIRE(expected == actual);
EXPECT_EQ(strlen(expected), measuredLen);
} }
private: TEST_CASE("JsonArray::printTo()") {
char buffer[256]; StaticJsonBuffer<JSON_ARRAY_SIZE(2)> jb;
}; JsonArray &array = jb.createArray();
TEST_F(JsonArray_PrintTo_Tests, Empty) { SECTION("Empty") {
outputMustBe("[]"); check(array, "[]");
} }
TEST_F(JsonArray_PrintTo_Tests, Null) { SECTION("Null") {
array.add(static_cast<char *>(0)); array.add(static_cast<char *>(0));
outputMustBe("[null]"); check(array, "[null]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneString) { SECTION("OneString") {
array.add("hello"); array.add("hello");
outputMustBe("[\"hello\"]"); check(array, "[\"hello\"]");
} }
TEST_F(JsonArray_PrintTo_Tests, TwoStrings) { SECTION("TwoStrings") {
array.add("hello"); array.add("hello");
array.add("world"); array.add("world");
outputMustBe("[\"hello\",\"world\"]"); check(array, "[\"hello\",\"world\"]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneStringOverCapacity) { SECTION("OneStringOverCapacity") {
array.add("hello"); array.add("hello");
array.add("world"); array.add("world");
array.add("lost"); array.add("lost");
outputMustBe("[\"hello\",\"world\"]"); check(array, "[\"hello\",\"world\"]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneDoubleDefaultDigits) { SECTION("OneDoubleDefaultDigits") {
array.add(3.14159265358979323846); array.add(3.14159265358979323846);
outputMustBe("[3.14]"); check(array, "[3.14]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits) { SECTION("OneDoubleFourDigits") {
array.add(3.14159265358979323846, 4); array.add(3.14159265358979323846, 4);
outputMustBe("[3.1416]"); check(array, "[3.1416]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits_AlternativeSyntax) { SECTION("OneDoubleFourDigits_AlternativeSyntax") {
array.add(double_with_n_digits(3.14159265358979323846, 4)); array.add(double_with_n_digits(3.14159265358979323846, 4));
outputMustBe("[3.1416]"); check(array, "[3.1416]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneFloatDefaultDigits) { SECTION("OneFloatDefaultDigits") {
array.add(3.14159f); array.add(3.14159f);
outputMustBe("[3.14]"); check(array, "[3.14]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneFloatFourDigits) { SECTION("OneFloatFourDigits") {
array.add(3.14159f, 4); array.add(3.14159f, 4);
outputMustBe("[3.1416]"); check(array, "[3.1416]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneInteger) { SECTION("OneInteger") {
array.add(1); array.add(1);
outputMustBe("[1]"); check(array, "[1]");
} }
TEST_F(JsonArray_PrintTo_Tests, TwoIntegers) { SECTION("TwoIntegers") {
array.add(1); array.add(1);
array.add(2); array.add(2);
outputMustBe("[1,2]"); check(array, "[1,2]");
} }
TEST_F(JsonArray_PrintTo_Tests, RawJson) { SECTION("RawJson") {
array.add(RawJson("{\"key\":\"value\"}")); array.add(RawJson("{\"key\":\"value\"}"));
outputMustBe("[{\"key\":\"value\"}]"); check(array, "[{\"key\":\"value\"}]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity) { SECTION("OneIntegerOverCapacity") {
array.add(1); array.add(1);
array.add(2); array.add(2);
array.add(3); array.add(3);
outputMustBe("[1,2]"); check(array, "[1,2]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneTrue) { SECTION("OneTrue") {
array.add(true); array.add(true);
outputMustBe("[true]"); check(array, "[true]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneFalse) { SECTION("OneFalse") {
array.add(false); array.add(false);
outputMustBe("[false]"); check(array, "[false]");
} }
TEST_F(JsonArray_PrintTo_Tests, TwoBooleans) { SECTION("TwoBooleans") {
array.add(false); array.add(false);
array.add(true); array.add(true);
outputMustBe("[false,true]"); check(array, "[false,true]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity) { SECTION("OneBooleanOverCapacity") {
array.add(false); array.add(false);
array.add(true); array.add(true);
array.add(false); array.add(false);
outputMustBe("[false,true]"); check(array, "[false,true]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedArray) { SECTION("OneEmptyNestedArray") {
array.createNestedArray(); array.createNestedArray();
outputMustBe("[[]]"); check(array, "[[]]");
} }
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedHash) { SECTION("OneEmptyNestedHash") {
array.createNestedObject(); array.createNestedObject();
outputMustBe("[{}]"); check(array, "[{}]");
}
} }

View File

@ -6,72 +6,66 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class JsonArray_Remove_Tests : public ::testing::Test {
protected:
JsonArray_Remove_Tests() : _array(_jsonBuffer.createArray()) {
_array.add("one");
_array.add("two");
_array.add("three");
}
TEST_CASE("JsonArray::remove()") {
DynamicJsonBuffer _jsonBuffer; DynamicJsonBuffer _jsonBuffer;
JsonArray& _array; JsonArray& _array = _jsonBuffer.createArray();
}; _array.add(1);
_array.add(2);
_array.add(3);
#define TEST_(name) TEST_F(JsonArray_Remove_Tests, name) SECTION("RemoveFirstByIndex") {
TEST_(RemoveFirstByIndex) {
_array.remove(0); _array.remove(0);
EXPECT_EQ(2, _array.size()); REQUIRE(2 == _array.size());
EXPECT_STREQ("two", _array[0]); REQUIRE(_array[0] == 2);
EXPECT_STREQ("three", _array[1]); REQUIRE(_array[1] == 3);
} }
TEST_(RemoveMiddleByIndex) { SECTION("RemoveMiddleByIndex") {
_array.remove(1); _array.remove(1);
EXPECT_EQ(2, _array.size()); REQUIRE(2 == _array.size());
EXPECT_STREQ("one", _array[0]); REQUIRE(_array[0] == 1);
EXPECT_STREQ("three", _array[1]); REQUIRE(_array[1] == 3);
} }
TEST_(RemoveLastByIndex) { SECTION("RemoveLastByIndex") {
_array.remove(2); _array.remove(2);
EXPECT_EQ(2, _array.size()); REQUIRE(2 == _array.size());
EXPECT_STREQ("one", _array[0]); REQUIRE(_array[0] == 1);
EXPECT_STREQ("two", _array[1]); REQUIRE(_array[1] == 2);
} }
TEST_(RemoveFirstByIterator) { SECTION("RemoveFirstByIterator") {
JsonArray::iterator it = _array.begin(); JsonArray::iterator it = _array.begin();
_array.remove(it); _array.remove(it);
EXPECT_EQ(2, _array.size()); REQUIRE(2 == _array.size());
EXPECT_STREQ("two", _array[0]); REQUIRE(_array[0] == 2);
EXPECT_STREQ("three", _array[1]); REQUIRE(_array[1] == 3);
} }
TEST_(RemoveMiddleByIterator) { SECTION("RemoveMiddleByIterator") {
JsonArray::iterator it = _array.begin(); JsonArray::iterator it = _array.begin();
++it; ++it;
_array.remove(it); _array.remove(it);
EXPECT_EQ(2, _array.size()); REQUIRE(2 == _array.size());
EXPECT_STREQ("one", _array[0]); REQUIRE(_array[0] == 1);
EXPECT_STREQ("three", _array[1]); REQUIRE(_array[1] == 3);
} }
TEST_(RemoveLastByIterator) { SECTION("RemoveLastByIterator") {
JsonArray::iterator it = _array.begin(); JsonArray::iterator it = _array.begin();
++it; ++it;
++it; ++it;
_array.remove(it); _array.remove(it);
EXPECT_EQ(2, _array.size()); REQUIRE(2 == _array.size());
EXPECT_STREQ("one", _array[0]); REQUIRE(_array[0] == 1);
EXPECT_STREQ("two", _array[1]); REQUIRE(_array[1] == 2);
}
} }

View File

@ -6,87 +6,83 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class JsonArray_Set_Tests : public ::testing::Test { using namespace Catch::Matchers;
protected:
JsonArray_Set_Tests() : _array(_jsonBuffer.createArray()) {
_array.add(0);
}
TEST_CASE("JsonArray::set()") {
DynamicJsonBuffer _jsonBuffer; DynamicJsonBuffer _jsonBuffer;
JsonArray& _array; JsonArray& _array = _jsonBuffer.createArray();
}; _array.add(0);
#define TEST_(name) TEST_F(JsonArray_Set_Tests, name) SECTION("SizeIsUnchanged") {
TEST_(SizeIsUnchanged) {
_array.set(0, "hello"); _array.set(0, "hello");
EXPECT_EQ(1U, _array.size()); REQUIRE(1U == _array.size());
} }
TEST_(StoreInteger) { SECTION("StoreInteger") {
_array.set(0, 123); _array.set(0, 123);
EXPECT_EQ(123, _array[0].as<int>()); REQUIRE(123 == _array[0].as<int>());
EXPECT_TRUE(_array[0].is<int>()); REQUIRE(_array[0].is<int>());
EXPECT_FALSE(_array[0].is<double>()); REQUIRE_FALSE(_array[0].is<double>());
} }
TEST_(StoreDouble) { SECTION("StoreDouble") {
_array.set(0, 123.45); _array.set(0, 123.45);
EXPECT_EQ(123.45, _array[0].as<double>()); REQUIRE(123.45 == _array[0].as<double>());
EXPECT_TRUE(_array[0].is<double>()); REQUIRE(_array[0].is<double>());
EXPECT_FALSE(_array[0].is<int>()); REQUIRE_FALSE(_array[0].is<int>());
} }
TEST_(StoreBoolean) { SECTION("StoreBoolean") {
_array.set(0, true); _array.set(0, true);
EXPECT_EQ(true, _array[0].as<bool>()); REQUIRE(true == _array[0].as<bool>());
EXPECT_TRUE(_array[0].is<bool>()); REQUIRE(_array[0].is<bool>());
EXPECT_FALSE(_array[0].is<int>()); REQUIRE_FALSE(_array[0].is<int>());
} }
TEST_(StoreString) { SECTION("StoreString") {
_array.set(0, "hello"); _array.set(0, "hello");
EXPECT_STREQ("hello", _array[0].as<const char*>()); REQUIRE_THAT(_array[0].as<const char*>(), Equals("hello"));
EXPECT_TRUE(_array[0].is<const char*>()); REQUIRE(_array[0].is<const char*>());
EXPECT_FALSE(_array[0].is<int>()); REQUIRE_FALSE(_array[0].is<int>());
} }
TEST_(StoreNestedArray) { SECTION("StoreNestedArray") {
JsonArray& arr = _jsonBuffer.createArray(); JsonArray& arr = _jsonBuffer.createArray();
_array.set(0, arr); _array.set(0, arr);
EXPECT_EQ(&arr, &_array[0].as<JsonArray&>()); REQUIRE(&arr == &_array[0].as<JsonArray&>());
EXPECT_TRUE(_array[0].is<JsonArray&>()); REQUIRE(_array[0].is<JsonArray&>());
EXPECT_FALSE(_array[0].is<int>()); REQUIRE_FALSE(_array[0].is<int>());
} }
TEST_(StoreNestedObject) { SECTION("StoreNestedObject") {
JsonObject& obj = _jsonBuffer.createObject(); JsonObject& obj = _jsonBuffer.createObject();
_array.set(0, obj); _array.set(0, obj);
EXPECT_EQ(&obj, &_array[0].as<JsonObject&>()); REQUIRE(&obj == &_array[0].as<JsonObject&>());
EXPECT_TRUE(_array[0].is<JsonObject&>()); REQUIRE(_array[0].is<JsonObject&>());
EXPECT_FALSE(_array[0].is<int>()); REQUIRE_FALSE(_array[0].is<int>());
} }
TEST_(StoreArraySubscript) { SECTION("StoreArraySubscript") {
JsonArray& arr = _jsonBuffer.createArray(); JsonArray& arr = _jsonBuffer.createArray();
arr.add("hello"); arr.add("hello");
_array.set(0, arr[0]); _array.set(0, arr[0]);
EXPECT_STREQ("hello", _array[0]); REQUIRE_THAT(_array[0].as<char*>(), Equals("hello"));
} }
TEST_(StoreObjectSubscript) { SECTION("StoreObjectSubscript") {
JsonObject& obj = _jsonBuffer.createObject(); JsonObject& obj = _jsonBuffer.createObject();
obj["x"] = "hello"; obj["x"] = "hello";
_array.set(0, obj["x"]); _array.set(0, obj["x"]);
EXPECT_STREQ("hello", _array[0]); REQUIRE_THAT(_array[0].as<char*>(), Equals("hello"));
}
} }

View File

@ -6,111 +6,111 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h>
#include <stdint.h> #include <stdint.h>
#include <catch.hpp>
class JsonArray_Subscript_Tests : public ::testing::Test { TEST_CASE("JsonArray::operator[]") {
protected:
JsonArray_Subscript_Tests() : _array(_jsonBuffer.createArray()) {
_array.add(0);
}
DynamicJsonBuffer _jsonBuffer; DynamicJsonBuffer _jsonBuffer;
JsonArray& _array; JsonArray& _array = _jsonBuffer.createArray();
}; _array.add(0);
#define TEST_(name) TEST_F(JsonArray_Subscript_Tests, name) SECTION("SizeIsUnchanged") {
TEST_(SizeIsUnchanged) {
_array[0] = "hello"; _array[0] = "hello";
EXPECT_EQ(1U, _array.size()); REQUIRE(1U == _array.size());
} }
TEST_(StoreInteger) { SECTION("StoreInteger") {
_array[0] = 123; _array[0] = 123;
EXPECT_EQ(123, _array[0].as<int>()); REQUIRE(123 == _array[0].as<int>());
EXPECT_TRUE(_array[0].is<int>()); REQUIRE(true == _array[0].is<int>());
EXPECT_FALSE(_array[0].is<double>()); REQUIRE(false == _array[0].is<double>());
} }
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64 #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
TEST_(StoreLongLong) { SECTION("StoreLongLong") {
_array[0] = 9223372036854775807; _array[0] = 9223372036854775807;
EXPECT_EQ(9223372036854775807, _array[0].as<long long>()); REQUIRE(9223372036854775807 == _array[0].as<long long>());
EXPECT_TRUE(_array[0].is<int>()); REQUIRE(true == _array[0].is<int>());
EXPECT_FALSE(_array[0].is<double>()); REQUIRE(false == _array[0].is<double>());
} }
#endif #endif
TEST_(StoreDouble) { SECTION("StoreDouble") {
_array[0] = 123.45; _array[0] = 123.45;
EXPECT_EQ(123.45, _array[0].as<double>()); REQUIRE(123.45 == _array[0].as<double>());
EXPECT_TRUE(_array[0].is<double>()); REQUIRE(true == _array[0].is<double>());
EXPECT_FALSE(_array[0].is<int>()); REQUIRE(false == _array[0].is<int>());
} }
TEST_(StoreDoubleWithDecimals) { SECTION("StoreDoubleWithDecimals") {
_array[0].set(123.45, 2); _array[0].set(123.45, 2);
EXPECT_EQ(123.45, _array[0].as<double>()); REQUIRE(123.45 == _array[0].as<double>());
EXPECT_TRUE(_array[0].is<double>()); REQUIRE(true == _array[0].is<double>());
EXPECT_FALSE(_array[0].is<int>()); REQUIRE(false == _array[0].is<int>());
} }
TEST_(StoreBoolean) { SECTION("StoreBoolean") {
_array[0] = true; _array[0] = true;
EXPECT_EQ(true, _array[0].as<bool>()); REQUIRE(true == _array[0].as<bool>());
EXPECT_TRUE(_array[0].is<bool>()); REQUIRE(true == _array[0].is<bool>());
EXPECT_FALSE(_array[0].is<int>()); REQUIRE(false == _array[0].is<int>());
} }
TEST_(StoreString) { SECTION("StoreString") {
_array[0] = "hello"; const char* str = "hello";
EXPECT_STREQ("hello", _array[0].as<const char*>());
EXPECT_STREQ("hello", _array[0].as<char*>()); // <- short hand _array[0] = str;
EXPECT_TRUE(_array[0].is<const char*>()); REQUIRE(str == _array[0].as<const char*>());
EXPECT_FALSE(_array[0].is<int>()); REQUIRE(str == _array[0].as<char*>()); // <- short hand
REQUIRE(true == _array[0].is<const char*>());
REQUIRE(false == _array[0].is<int>());
} }
TEST_(StoreNestedArray) { SECTION("StoreNestedArray") {
JsonArray& arr = _jsonBuffer.createArray(); JsonArray& arr = _jsonBuffer.createArray();
_array[0] = arr; _array[0] = arr;
EXPECT_EQ(&arr, &_array[0].as<JsonArray&>()); REQUIRE(&arr == &_array[0].as<JsonArray&>());
EXPECT_EQ(&arr, &_array[0].as<JsonArray>()); // <- short hand REQUIRE(&arr == &_array[0].as<JsonArray>()); // <- short hand
EXPECT_EQ(&arr, &_array[0].as<const JsonArray&>()); REQUIRE(&arr == &_array[0].as<const JsonArray&>());
EXPECT_EQ(&arr, &_array[0].as<const JsonArray>()); // <- short hand REQUIRE(&arr == &_array[0].as<const JsonArray>()); // <- short hand
EXPECT_TRUE(_array[0].is<JsonArray&>()); REQUIRE(true == _array[0].is<JsonArray&>());
EXPECT_FALSE(_array[0].is<int>()); REQUIRE(false == _array[0].is<int>());
} }
TEST_(StoreNestedObject) { SECTION("StoreNestedObject") {
JsonObject& obj = _jsonBuffer.createObject(); JsonObject& obj = _jsonBuffer.createObject();
_array[0] = obj; _array[0] = obj;
EXPECT_EQ(&obj, &_array[0].as<JsonObject&>()); REQUIRE(&obj == &_array[0].as<JsonObject&>());
EXPECT_EQ(&obj, &_array[0].as<JsonObject>()); // <- short hand REQUIRE(&obj == &_array[0].as<JsonObject>()); // <- short hand
EXPECT_EQ(&obj, &_array[0].as<const JsonObject&>()); REQUIRE(&obj == &_array[0].as<const JsonObject&>());
EXPECT_EQ(&obj, &_array[0].as<const JsonObject>()); // <- short hand REQUIRE(&obj == &_array[0].as<const JsonObject>()); // <- short hand
EXPECT_TRUE(_array[0].is<JsonObject&>()); REQUIRE(true == _array[0].is<JsonObject&>());
EXPECT_FALSE(_array[0].is<int>()); REQUIRE(false == _array[0].is<int>());
} }
TEST_(StoreArraySubscript) { SECTION("StoreArraySubscript") {
JsonArray& arr = _jsonBuffer.createArray(); JsonArray& arr = _jsonBuffer.createArray();
arr.add("hello"); const char* str = "hello";
arr.add(str);
_array[0] = arr[0]; _array[0] = arr[0];
EXPECT_STREQ("hello", _array[0]); REQUIRE(str == _array[0]);
} }
TEST_(StoreObjectSubscript) { SECTION("StoreObjectSubscript") {
JsonObject& obj = _jsonBuffer.createObject(); JsonObject& obj = _jsonBuffer.createObject();
obj["x"] = "hello"; const char* str = "hello";
obj["x"] = str;
_array[0] = obj["x"]; _array[0] = obj["x"];
EXPECT_STREQ("hello", _array[0]); REQUIRE(str == _array[0]);
}
} }

View File

@ -13,5 +13,5 @@ add_executable(JsonBufferTests
parseObject.cpp parseObject.cpp
) )
target_link_libraries(JsonBufferTests gtest) target_link_libraries(JsonBufferTests catch)
add_test(JsonBuffer JsonBufferTests) add_test(JsonBuffer JsonBufferTests)

View File

@ -6,9 +6,10 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
TEST(JsonParser_Nested_Tests, ArrayNestedInObject) { TEST_CASE("JsonBuffer nested objects") {
SECTION("ArrayNestedInObject") {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } "; char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
@ -17,26 +18,26 @@ TEST(JsonParser_Nested_Tests, ArrayNestedInObject) {
const JsonArray &array2 = object["cd"]; const JsonArray &array2 = object["cd"];
JsonArray &array3 = object["ef"]; JsonArray &array3 = object["ef"];
ASSERT_TRUE(object.success()); REQUIRE(true == object.success());
ASSERT_TRUE(array1.success()); REQUIRE(true == array1.success());
ASSERT_TRUE(array2.success()); REQUIRE(true == array2.success());
ASSERT_FALSE(array3.success()); REQUIRE(false == array3.success());
ASSERT_EQ(2, array1.size()); REQUIRE(2 == array1.size());
ASSERT_EQ(2, array2.size()); REQUIRE(2 == array2.size());
ASSERT_EQ(0, array3.size()); REQUIRE(0 == array3.size());
EXPECT_EQ(1, array1[0].as<int>()); REQUIRE(1 == array1[0].as<int>());
EXPECT_EQ(2, array1[1].as<int>()); REQUIRE(2 == array1[1].as<int>());
EXPECT_EQ(3, array2[0].as<int>()); REQUIRE(3 == array2[0].as<int>());
EXPECT_EQ(4, array2[1].as<int>()); REQUIRE(4 == array2[1].as<int>());
EXPECT_EQ(0, array3[0].as<int>()); REQUIRE(0 == array3[0].as<int>());
} }
TEST(JsonParser_Nested_Tests, ObjectNestedInArray) { SECTION("ObjectNestedInArray") {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
char jsonString[] = char jsonString[] =
" [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] "; " [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
@ -46,19 +47,20 @@ TEST(JsonParser_Nested_Tests, ObjectNestedInArray) {
const JsonObject &object2 = array[1]; const JsonObject &object2 = array[1];
JsonObject &object3 = array[2]; JsonObject &object3 = array[2];
ASSERT_TRUE(array.success()); REQUIRE(true == array.success());
ASSERT_TRUE(object1.success()); REQUIRE(true == object1.success());
ASSERT_TRUE(object2.success()); REQUIRE(true == object2.success());
ASSERT_FALSE(object3.success()); REQUIRE(false == object3.success());
ASSERT_EQ(2, object1.size()); REQUIRE(2 == object1.size());
ASSERT_EQ(2, object2.size()); REQUIRE(2 == object2.size());
ASSERT_EQ(0, object3.size()); REQUIRE(0 == object3.size());
EXPECT_EQ(1, object1["a"].as<int>()); REQUIRE(1 == object1["a"].as<int>());
EXPECT_EQ(2, object1["b"].as<int>()); REQUIRE(2 == object1["b"].as<int>());
EXPECT_EQ(3, object2["c"].as<int>()); REQUIRE(3 == object2["c"].as<int>());
EXPECT_EQ(4, object2["d"].as<int>()); REQUIRE(4 == object2["d"].as<int>());
EXPECT_EQ(0, object3["e"].as<int>()); REQUIRE(0 == object3["e"].as<int>());
}
} }

View File

@ -6,80 +6,46 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class JsonParser_NestingLimit_Tests : public testing::Test { bool tryParseArray(const char *json, uint8_t nestingLimit) {
protected:
void whenNestingLimitIs(uint8_t nestingLimit) {
_nestingLimit = nestingLimit;
}
void parseArrayMustFail(const char *json) {
ASSERT_FALSE(tryParseArray(json));
}
void parseArrayMustSucceed(const char *json) {
ASSERT_TRUE(tryParseArray(json));
}
void parseObjectMustFail(const char *json) {
ASSERT_FALSE(tryParseObject(json));
}
void parseObjectMustSucceed(const char *json) {
ASSERT_TRUE(tryParseObject(json));
}
private:
bool tryParseArray(const char *json) {
DynamicJsonBuffer buffer; DynamicJsonBuffer buffer;
char s[256]; return buffer.parseArray(json, nestingLimit).success();
strcpy(s, json);
return buffer.parseArray(s, _nestingLimit).success();
} }
bool tryParseObject(const char *json) { bool tryParseObject(const char *json, uint8_t nestingLimit) {
DynamicJsonBuffer buffer; DynamicJsonBuffer buffer;
char s[256]; return buffer.parseObject(json, nestingLimit).success();
strcpy(s, json);
return buffer.parseObject(s, _nestingLimit).success();
} }
uint8_t _nestingLimit; TEST_CASE("JsonParser nestingLimit") {
}; SECTION("ParseArrayWithNestingLimit0") {
REQUIRE(true == tryParseArray("[]", 0));
TEST_F(JsonParser_NestingLimit_Tests, ParseArrayWithNestingLimit0) { REQUIRE(false == tryParseArray("[[]]", 0));
whenNestingLimitIs(0);
parseArrayMustSucceed("[]");
parseArrayMustFail("[[]]");
} }
TEST_F(JsonParser_NestingLimit_Tests, ParseArrayWithNestingLimit1) { SECTION("ParseArrayWithNestingLimit1") {
whenNestingLimitIs(1); REQUIRE(true == tryParseArray("[[]]", 1));
parseArrayMustSucceed("[[]]"); REQUIRE(false == tryParseArray("[[[]]]", 1));
parseArrayMustFail("[[[]]]");
} }
TEST_F(JsonParser_NestingLimit_Tests, ParseArrayWithNestingLimit2) { SECTION("ParseArrayWithNestingLimit2") {
whenNestingLimitIs(2); REQUIRE(true == tryParseArray("[[[]]]", 2));
parseArrayMustSucceed("[[[]]]"); REQUIRE(false == tryParseArray("[[[[]]]]", 2));
parseArrayMustFail("[[[[]]]]");
} }
TEST_F(JsonParser_NestingLimit_Tests, ParseObjectWithNestingLimit0) { SECTION("ParseObjectWithNestingLimit0") {
whenNestingLimitIs(0); REQUIRE(true == tryParseObject("{}", 0));
parseObjectMustSucceed("{}"); REQUIRE(false == tryParseObject("{\"key\":{}}", 0));
parseObjectMustFail("{\"key\":{}}");
} }
TEST_F(JsonParser_NestingLimit_Tests, ParseObjectWithNestingLimit1) { SECTION("ParseObjectWithNestingLimit1") {
whenNestingLimitIs(1); REQUIRE(true == tryParseObject("{\"key\":{}}", 1));
parseObjectMustSucceed("{\"key\":{}}"); REQUIRE(false == tryParseObject("{\"key\":{\"key\":{}}}", 1));
parseObjectMustFail("{\"key\":{\"key\":{}}}");
} }
TEST_F(JsonParser_NestingLimit_Tests, ParseObjectWithNestingLimit2) { SECTION("ParseObjectWithNestingLimit2") {
whenNestingLimitIs(2); REQUIRE(true == tryParseObject("{\"key\":{\"key\":{}}}", 2));
parseObjectMustSucceed("{\"key\":{\"key\":{}}}"); REQUIRE(false == tryParseObject("{\"key\":{\"key\":{\"key\":{}}}}", 2));
parseObjectMustFail("{\"key\":{\"key\":{\"key\":{}}}}"); }
} }

View File

@ -6,100 +6,78 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class JsonParser_Variant_Test : public testing::Test { using namespace Catch::Matchers;
protected:
void whenInputIs(const char* jsonString) { TEST_CASE("JsonBuffer::parse()") {
strcpy(_jsonString, jsonString); DynamicJsonBuffer jb;
_result = _jsonBuffer.parse(_jsonString);
SECTION("EmptyObject") {
JsonVariant variant = jb.parse("{}");
REQUIRE(variant.success());
REQUIRE(variant.is<JsonObject>());
} }
template <typename T> SECTION("EmptyArray") {
void resultMustEqual(T expected) { JsonVariant variant = jb.parse("[]");
EXPECT_EQ(expected, _result.as<T>()); REQUIRE(variant.success());
REQUIRE(variant.is<JsonArray>());
} }
void resultMustEqual(const char* expected) { SECTION("Integer") {
EXPECT_STREQ(expected, _result.as<char*>()); JsonVariant variant = jb.parse("-42");
REQUIRE(variant.success());
REQUIRE(variant.is<int>());
REQUIRE_FALSE(variant.is<double>());
REQUIRE(variant == -42);
} }
void resultMustEqual(double expected) { SECTION("Double") {
EXPECT_DOUBLE_EQ(expected, _result.as<double>()); JsonVariant variant = jb.parse("-1.23e+4");
REQUIRE(variant.success());
REQUIRE_FALSE(variant.is<int>());
REQUIRE(variant.is<double>());
REQUIRE(variant.as<double>() == Approx(-1.23e+4));
} }
template <typename T> SECTION("Double quoted string") {
void resultTypeMustBe() { JsonVariant variant = jb.parse("\"hello world\"");
EXPECT_TRUE(_result.is<T>()); REQUIRE(variant.success());
REQUIRE(variant.is<char*>());
REQUIRE_THAT(variant.as<char*>(), Equals("hello world"));
} }
void resultMustBeInvalid() { SECTION("Single quoted string") {
EXPECT_FALSE(_result.success()); JsonVariant variant = jb.parse("\'hello world\'");
} REQUIRE(variant.success());
void resultMustBeValid() { REQUIRE(variant.is<char*>());
EXPECT_TRUE(_result.success()); REQUIRE_THAT(variant.as<char*>(), Equals("hello world"));
} }
template <typename T> SECTION("True") {
void verify(const char* input, T expected) { JsonVariant variant = jb.parse("true");
whenInputIs(input); REQUIRE(variant.success());
resultMustBeValid(); REQUIRE(variant.is<bool>());
resultTypeMustBe<T>(); REQUIRE(variant == true);
resultMustEqual(expected);
} }
private: SECTION("False") {
DynamicJsonBuffer _jsonBuffer; JsonVariant variant = jb.parse("false");
JsonVariant _result; REQUIRE(variant.success());
char _jsonString[256]; REQUIRE(variant.is<bool>());
}; REQUIRE(variant == false);
TEST_F(JsonParser_Variant_Test, EmptyObject) {
whenInputIs("{}");
resultMustBeValid();
resultTypeMustBe<JsonObject>();
} }
TEST_F(JsonParser_Variant_Test, EmptyArray) { SECTION("OpenBrace") {
whenInputIs("[]"); JsonVariant variant = jb.parse("{");
resultMustBeValid(); REQUIRE_FALSE(variant.success());
resultTypeMustBe<JsonArray>();
} }
TEST_F(JsonParser_Variant_Test, Integer) { SECTION("Incomplete string") {
verify("42", 42); JsonVariant variant = jb.parse("\"hello");
verify("-42", -42); REQUIRE(variant.success());
REQUIRE(variant.is<char*>());
REQUIRE_THAT(variant.as<char*>(), Equals("hello"));
} }
TEST_F(JsonParser_Variant_Test, Double) {
verify("3.14", 3.14);
verify("3.14", 3.14);
verify("1E+10", 1E+10);
verify("-1E+10", -1E+10);
verify("1.234E+10", 1.234E+10);
verify("1.79769e+308", 1.79769e+308);
verify("-1.79769e+308", -1.79769e+308);
verify("1.7976931348623157e+308", 1.7976931348623157e+308);
verify("0.017976931348623157e+310", 0.017976931348623157e+310);
}
TEST_F(JsonParser_Variant_Test, String) {
verify("\"hello world\"", "hello world");
}
TEST_F(JsonParser_Variant_Test, True) {
verify("true", true);
verify("false", false);
}
TEST_F(JsonParser_Variant_Test, OpenBrace) {
whenInputIs("{");
resultMustBeInvalid();
}
TEST_F(JsonParser_Variant_Test, IncompleteStrings) {
verify("\"", "");
verify("\"hello", "hello");
verify("\'", "");
verify("\'world", "world");
} }

View File

@ -6,362 +6,316 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class JsonParser_Array_Tests : public testing::Test { TEST_CASE("JsonBuffer::parseArray()") {
protected: DynamicJsonBuffer jb;
void whenInputIs(const char *json) {
strcpy(_jsonString, json); SECTION("EmptyArray") {
JsonArray& arr = jb.parseArray("[]");
REQUIRE(arr.success());
REQUIRE(0 == arr.size());
} }
void whenInputIs(const char *json, size_t len) { SECTION("MissingOpeningBracket") {
memcpy(_jsonString, json, len); JsonArray& arr = jb.parseArray("]");
REQUIRE_FALSE(arr.success());
} }
void parseMustSucceed() { SECTION("ArrayWithNoEnd") {
_array = &_jsonBuffer.parseArray(_jsonString); JsonArray& arr = jb.parseArray("[");
EXPECT_TRUE(_array->success()); REQUIRE_FALSE(arr.success());
} }
void parseMustFail() { SECTION("EmptyArrayWithLeadingSpaces") {
_array = &_jsonBuffer.parseArray(_jsonString); JsonArray& arr = jb.parseArray(" []");
EXPECT_FALSE(_array->success());
EXPECT_EQ(0, _array->size()); REQUIRE(arr.success());
REQUIRE(0 == arr.size());
} }
void sizeMustBe(int expected) { SECTION("Garbage") {
ASSERT_EQ(expected, _array->size()); JsonArray& arr = jb.parseArray("%*$£¤");
REQUIRE_FALSE(arr.success());
} }
template <typename T> SECTION("OneInteger") {
void firstElementMustBe(T expected) { JsonArray& arr = jb.parseArray("[42]");
elementAtIndexMustBe(0, expected);
REQUIRE(arr.success());
REQUIRE(1 == arr.size());
REQUIRE(arr[0] == 42);
} }
template <typename T> SECTION("OneIntegerWithSpacesBefore") {
void secondElementMustBe(T expected) { JsonArray& arr = jb.parseArray("[ \t\r\n42]");
elementAtIndexMustBe(1, expected);
REQUIRE(arr.success());
REQUIRE(1 == arr.size());
REQUIRE(arr[0] == 42);
} }
template <typename T> SECTION("OneIntegerWithSpaceAfter") {
void elementAtIndexMustBe(int index, T expected) { JsonArray& arr = jb.parseArray("[42 \t\r\n]");
EXPECT_EQ(expected, (*_array)[index].as<T>());
REQUIRE(arr.success());
REQUIRE(1 == arr.size());
REQUIRE(arr[0] == 42);
} }
void elementAtIndexMustBe(int index, const char *expected) { SECTION("TwoIntegers") {
EXPECT_STREQ(expected, (*_array)[index].as<const char *>()); JsonArray& arr = jb.parseArray("[42,84]");
REQUIRE(arr.success());
REQUIRE(2 == arr.size());
REQUIRE(arr[0] == 42);
REQUIRE(arr[1] == 84);
} }
DynamicJsonBuffer _jsonBuffer; SECTION("TwoDoubles") {
JsonArray *_array; JsonArray& arr = jb.parseArray("[4.2,1e2]");
char _jsonString[256];
};
TEST_F(JsonParser_Array_Tests, EmptyArray) { REQUIRE(arr.success());
whenInputIs("[]"); REQUIRE(2 == arr.size());
REQUIRE(arr[0] == 4.2);
parseMustSucceed(); REQUIRE(arr[1] == 1e2);
sizeMustBe(0);
} }
TEST_F(JsonParser_Array_Tests, MissingOpeningBracket) { SECTION("UnsignedLong") {
whenInputIs("]"); JsonArray& arr = jb.parseArray("[4294967295]");
parseMustFail();
REQUIRE(arr.success());
REQUIRE(1 == arr.size());
REQUIRE(arr[0] == 4294967295UL);
} }
TEST_F(JsonParser_Array_Tests, ArrayWithNoEnd) { SECTION("TwoBooleans") {
whenInputIs("["); JsonArray& arr = jb.parseArray("[true,false]");
parseMustFail();
REQUIRE(arr.success());
REQUIRE(2 == arr.size());
REQUIRE(arr[0] == true);
REQUIRE(arr[1] == false);
} }
TEST_F(JsonParser_Array_Tests, EmptyArrayWithLeadingSpaces) { SECTION("TwoNulls") {
whenInputIs(" []"); JsonArray& arr = jb.parseArray("[null,null]");
parseMustSucceed(); REQUIRE(arr.success());
sizeMustBe(0); REQUIRE(2 == arr.size());
REQUIRE(arr[0].as<char*>() == 0);
REQUIRE(arr[1].as<char*>() == 0);
} }
TEST_F(JsonParser_Array_Tests, Garbage) { SECTION("TwoStringsDoubleQuotes") {
whenInputIs("%*$£¤"); JsonArray& arr = jb.parseArray("[ \"hello\" , \"world\" ]");
parseMustFail(); REQUIRE(arr.success());
REQUIRE(2 == arr.size());
REQUIRE(arr[0] == "hello");
REQUIRE(arr[1] == "world");
} }
TEST_F(JsonParser_Array_Tests, OneInteger) { SECTION("TwoStringsSingleQuotes") {
whenInputIs("[42]"); JsonArray& arr = jb.parseArray("[ 'hello' , 'world' ]");
parseMustSucceed(); REQUIRE(arr.success());
sizeMustBe(1); REQUIRE(2 == arr.size());
firstElementMustBe(42); REQUIRE(arr[0] == "hello");
REQUIRE(arr[1] == "world");
} }
TEST_F(JsonParser_Array_Tests, OneIntegerWithSpacesBefore) { SECTION("TwoStringsNoQuotes") {
whenInputIs("[ \t\r\n42]"); JsonArray& arr = jb.parseArray("[ hello , world ]");
parseMustSucceed(); REQUIRE(arr.success());
sizeMustBe(1); REQUIRE(2 == arr.size());
firstElementMustBe(42); REQUIRE(arr[0] == "hello");
REQUIRE(arr[1] == "world");
} }
TEST_F(JsonParser_Array_Tests, OneIntegerWithSpaceAfter) { SECTION("EmptyStringsDoubleQuotes") {
whenInputIs("[42 \t\r\n]"); JsonArray& arr = jb.parseArray("[\"\",\"\"]");
parseMustSucceed(); REQUIRE(arr.success());
sizeMustBe(1); REQUIRE(2 == arr.size());
firstElementMustBe(42); REQUIRE(arr[0] == "");
REQUIRE(arr[1] == "");
} }
TEST_F(JsonParser_Array_Tests, TwoIntegers) { SECTION("EmptyStringSingleQuotes") {
whenInputIs("[42,84]"); JsonArray& arr = jb.parseArray("[\'\',\'\']");
parseMustSucceed(); REQUIRE(arr.success());
sizeMustBe(2); REQUIRE(2 == arr.size());
firstElementMustBe(42); REQUIRE(arr[0] == "");
secondElementMustBe(84); REQUIRE(arr[1] == "");
} }
TEST_F(JsonParser_Array_Tests, TwoDoubles) { SECTION("EmptyStringNoQuotes") {
whenInputIs("[4.2,1e2]"); JsonArray& arr = jb.parseArray("[,]");
parseMustSucceed(); REQUIRE(arr.success());
sizeMustBe(2); REQUIRE(2 == arr.size());
firstElementMustBe(4.2); REQUIRE(arr[0] == "");
secondElementMustBe(1e2); REQUIRE(arr[1] == "");
} }
TEST_F(JsonParser_Array_Tests, UnsignedLong) { SECTION("ClosingDoubleQuoteMissing") {
whenInputIs("[4294967295]"); JsonArray& arr = jb.parseArray("[\"]");
parseMustSucceed(); REQUIRE_FALSE(arr.success());
sizeMustBe(1);
firstElementMustBe(4294967295UL);
} }
TEST_F(JsonParser_Array_Tests, TwoBooleans) { SECTION("ClosingSignleQuoteMissing") {
whenInputIs("[true,false]"); JsonArray& arr = jb.parseArray("[\']");
parseMustSucceed(); REQUIRE_FALSE(arr.success());
sizeMustBe(2);
firstElementMustBe(true);
secondElementMustBe(false);
} }
TEST_F(JsonParser_Array_Tests, TwoNulls) { SECTION("StringWithEscapedChars") {
const char *const nullCharPtr = 0; JsonArray& arr = jb.parseArray("[\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"]");
whenInputIs("[null,null]"); REQUIRE(arr.success());
REQUIRE(1 == arr.size());
parseMustSucceed(); REQUIRE(arr[0] == "1\"2\\3/4\b5\f6\n7\r8\t9");
sizeMustBe(2);
firstElementMustBe(nullCharPtr);
secondElementMustBe(nullCharPtr);
} }
TEST_F(JsonParser_Array_Tests, TwoStringsDoubleQuotes) { SECTION("StringWithUnterminatedEscapeSequence") {
whenInputIs("[ \"hello\" , \"world\" ]"); JsonArray& arr = jb.parseArray("\"\\\0\"", 4);
REQUIRE_FALSE(arr.success());
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe("hello");
secondElementMustBe("world");
} }
TEST_F(JsonParser_Array_Tests, TwoStringsSingleQuotes) { SECTION("CCommentBeforeOpeningBracket") {
whenInputIs("[ 'hello' , 'world' ]"); JsonArray& arr = jb.parseArray("/*COMMENT*/ [\"hello\"]");
parseMustSucceed(); REQUIRE(arr.success());
sizeMustBe(2); REQUIRE(1 == arr.size());
firstElementMustBe("hello"); REQUIRE(arr[0] == "hello");
secondElementMustBe("world");
} }
TEST_F(JsonParser_Array_Tests, TwoStringsNoQuotes) { SECTION("CCommentAfterOpeningBracket") {
whenInputIs("[ hello , world ]"); JsonArray& arr = jb.parseArray("[/*COMMENT*/ \"hello\"]");
parseMustSucceed(); REQUIRE(arr.success());
sizeMustBe(2); REQUIRE(1 == arr.size());
firstElementMustBe("hello"); REQUIRE(arr[0] == "hello");
secondElementMustBe("world");
} }
TEST_F(JsonParser_Array_Tests, EmptyStringsDoubleQuotes) { SECTION("CCommentBeforeClosingBracket") {
whenInputIs("[\"\",\"\"]"); JsonArray& arr = jb.parseArray("[\"hello\"/*COMMENT*/]");
parseMustSucceed(); REQUIRE(arr.success());
sizeMustBe(2); REQUIRE(1 == arr.size());
firstElementMustBe(""); REQUIRE(arr[0] == "hello");
secondElementMustBe("");
} }
TEST_F(JsonParser_Array_Tests, EmptyStringSingleQuotes) { SECTION("CCommentAfterClosingBracket") {
whenInputIs("[\'\',\'\']"); JsonArray& arr = jb.parseArray("[\"hello\"]/*COMMENT*/");
parseMustSucceed(); REQUIRE(arr.success());
sizeMustBe(2); REQUIRE(1 == arr.size());
firstElementMustBe(""); REQUIRE(arr[0] == "hello");
secondElementMustBe("");
} }
TEST_F(JsonParser_Array_Tests, EmptyStringNoQuotes) { SECTION("CCommentBeforeComma") {
whenInputIs("[,]"); JsonArray& arr = jb.parseArray("[\"hello\"/*COMMENT*/,\"world\"]");
parseMustSucceed(); REQUIRE(arr.success());
sizeMustBe(2); REQUIRE(2 == arr.size());
firstElementMustBe(""); REQUIRE(arr[0] == "hello");
secondElementMustBe(""); REQUIRE(arr[1] == "world");
} }
TEST_F(JsonParser_Array_Tests, ClosingDoubleQuoteMissing) { SECTION("CCommentAfterComma") {
whenInputIs("[\"]"); JsonArray& arr = jb.parseArray("[\"hello\",/*COMMENT*/ \"world\"]");
parseMustFail(); REQUIRE(arr.success());
REQUIRE(2 == arr.size());
REQUIRE(arr[0] == "hello");
REQUIRE(arr[1] == "world");
} }
TEST_F(JsonParser_Array_Tests, ClosingSignleQuoteMissing) { SECTION("CppCommentBeforeOpeningBracket") {
whenInputIs("[\']"); JsonArray& arr = jb.parseArray("//COMMENT\n\t[\"hello\"]");
parseMustFail(); REQUIRE(arr.success());
REQUIRE(1 == arr.size());
REQUIRE(arr[0] == "hello");
} }
TEST_F(JsonParser_Array_Tests, StringWithEscapedChars) { SECTION("CppCommentAfterOpeningBracket") {
whenInputIs("[\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"]"); JsonArray& arr = jb.parseArray("[//COMMENT\n\"hello\"]");
parseMustSucceed(); REQUIRE(arr.success());
sizeMustBe(1); REQUIRE(1 == arr.size());
firstElementMustBe("1\"2\\3/4\b5\f6\n7\r8\t9"); REQUIRE(arr[0] == "hello");
} }
TEST_F(JsonParser_Array_Tests, StringWithUnterminatedEscapeSequence) { SECTION("CppCommentBeforeClosingBracket") {
whenInputIs("\"\\\0\"", 4); JsonArray& arr = jb.parseArray("[\"hello\"//COMMENT\r\n]");
parseMustFail();
REQUIRE(arr.success());
REQUIRE(1 == arr.size());
REQUIRE(arr[0] == "hello");
} }
TEST_F(JsonParser_Array_Tests, CCommentBeforeOpeningBracket) { SECTION("CppCommentAfterClosingBracket") {
whenInputIs("/*COMMENT*/ [\"hello\"]"); JsonArray& arr = jb.parseArray("[\"hello\"]//COMMENT\n");
parseMustSucceed(); REQUIRE(arr.success());
sizeMustBe(1); REQUIRE(1 == arr.size());
firstElementMustBe("hello"); REQUIRE(arr[0] == "hello");
} }
TEST_F(JsonParser_Array_Tests, CCommentAfterOpeningBracket) { SECTION("CppCommentBeforeComma") {
whenInputIs("[/*COMMENT*/ \"hello\"]"); JsonArray& arr = jb.parseArray("[\"hello\"//COMMENT\n,\"world\"]");
parseMustSucceed(); REQUIRE(arr.success());
sizeMustBe(1); REQUIRE(2 == arr.size());
firstElementMustBe("hello"); REQUIRE(arr[0] == "hello");
REQUIRE(arr[1] == "world");
} }
TEST_F(JsonParser_Array_Tests, CCommentBeforeClosingBracket) { SECTION("CppCommentAfterComma") {
whenInputIs("[\"hello\"/*COMMENT*/]"); JsonArray& arr = jb.parseArray("[\"hello\",//COMMENT\n\"world\"]");
parseMustSucceed(); REQUIRE(arr.success());
sizeMustBe(1); REQUIRE(2 == arr.size());
firstElementMustBe("hello"); REQUIRE(arr[0] == "hello");
REQUIRE(arr[1] == "world");
} }
TEST_F(JsonParser_Array_Tests, CCommentAfterClosingBracket) { SECTION("InvalidCppComment") {
whenInputIs("[\"hello\"]/*COMMENT*/"); JsonArray& arr = jb.parseArray("[/COMMENT\n]");
REQUIRE_FALSE(arr.success());
parseMustSucceed();
sizeMustBe(1);
firstElementMustBe("hello");
} }
TEST_F(JsonParser_Array_Tests, CCommentBeforeComma) { SECTION("InvalidComment") {
whenInputIs("[\"hello\"/*COMMENT*/,\"world\"]"); JsonArray& arr = jb.parseArray("[/*/\n]");
REQUIRE_FALSE(arr.success());
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe("hello");
secondElementMustBe("world");
} }
TEST_F(JsonParser_Array_Tests, CCommentAfterComma) { SECTION("UnfinishedCComment") {
whenInputIs("[\"hello\",/*COMMENT*/ \"world\"]"); JsonArray& arr = jb.parseArray("[/*COMMENT]");
REQUIRE_FALSE(arr.success());
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe("hello");
secondElementMustBe("world");
} }
TEST_F(JsonParser_Array_Tests, CppCommentBeforeOpeningBracket) { SECTION("EndsInCppComment") {
whenInputIs("//COMMENT\n\t[\"hello\"]"); JsonArray& arr = jb.parseArray("[//COMMENT");
REQUIRE_FALSE(arr.success());
parseMustSucceed();
sizeMustBe(1);
firstElementMustBe("hello");
} }
TEST_F(JsonParser_Array_Tests, CppCommentAfterOpeningBracket) { SECTION("AfterClosingStar") {
whenInputIs("[//COMMENT\n\"hello\"]"); JsonArray& arr = jb.parseArray("[/*COMMENT*");
REQUIRE_FALSE(arr.success());
parseMustSucceed();
sizeMustBe(1);
firstElementMustBe("hello");
} }
TEST_F(JsonParser_Array_Tests, CppCommentBeforeClosingBracket) { SECTION("DeeplyNested") {
whenInputIs("[\"hello\"//COMMENT\r\n]"); JsonArray& arr =
jb.parseArray("[[[[[[[[[[[[[[[[[[[\"Not too deep\"]]]]]]]]]]]]]]]]]]]");
parseMustSucceed(); REQUIRE(arr.success());
sizeMustBe(1);
firstElementMustBe("hello");
} }
TEST_F(JsonParser_Array_Tests, CppCommentAfterClosingBracket) {
whenInputIs("[\"hello\"]//COMMENT\n");
parseMustSucceed();
sizeMustBe(1);
firstElementMustBe("hello");
}
TEST_F(JsonParser_Array_Tests, CppCommentBeforeComma) {
whenInputIs("[\"hello\"//COMMENT\n,\"world\"]");
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe("hello");
secondElementMustBe("world");
}
TEST_F(JsonParser_Array_Tests, CppCommentAfterComma) {
whenInputIs("[\"hello\",//COMMENT\n\"world\"]");
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe("hello");
secondElementMustBe("world");
}
TEST_F(JsonParser_Array_Tests, InvalidCppComment) {
whenInputIs("[/COMMENT\n]");
parseMustFail();
}
TEST_F(JsonParser_Array_Tests, InvalidComment) {
whenInputIs("[/*/\n]");
parseMustFail();
}
TEST_F(JsonParser_Array_Tests, UnfinishedCComment) {
whenInputIs("[/*COMMENT]");
parseMustFail();
}
TEST_F(JsonParser_Array_Tests, EndsInCppComment) {
whenInputIs("[//COMMENT");
parseMustFail();
}
TEST_F(JsonParser_Array_Tests, AfterClosingStar) {
whenInputIs("[/*COMMENT*");
parseMustFail();
}
TEST_F(JsonParser_Array_Tests, DeeplyNested) {
whenInputIs("[[[[[[[[[[[[[[[[[[[\"Not too deep\"]]]]]]]]]]]]]]]]]]]");
parseMustSucceed();
} }

View File

@ -6,182 +6,153 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class JsonParser_Object_Test : public testing::Test { TEST_CASE("JsonBuffer::parseObject()") {
protected: DynamicJsonBuffer jb;
void whenInputIs(const char *jsonString) {
strcpy(_jsonString, jsonString); SECTION("EmptyObject") {
_object = &_jsonBuffer.parseObject(_jsonString); JsonObject& obj = jb.parseObject("{}");
REQUIRE(obj.success());
REQUIRE(obj.size() == 0);
} }
void parseMustSucceed() { SECTION("MissingOpeningBrace") {
EXPECT_TRUE(_object->success()); JsonObject& obj = jb.parseObject("}");
REQUIRE_FALSE(obj.success());
} }
void parseMustFail() { SECTION("MissingClosingBrace") {
EXPECT_FALSE(_object->success()); JsonObject& obj = jb.parseObject("{");
REQUIRE_FALSE(obj.success());
} }
void sizeMustBe(int expected) { SECTION("MissingColonAndValue") {
EXPECT_EQ(expected, _object->size()); JsonObject& obj = jb.parseObject("{\"key\"}");
REQUIRE_FALSE(obj.success());
} }
void keyMustHaveValue(const char *key, const char *expected) { SECTION("MissingQuotesAndColonAndValue") {
EXPECT_STREQ(expected, (*_object)[key]); JsonObject& obj = jb.parseObject("{key}");
REQUIRE_FALSE(obj.success());
} }
template <typename T> SECTION("OneString") {
void keyMustHaveValue(const char *key, T expected) { JsonObject& obj = jb.parseObject("{\"key\":\"value\"}");
EXPECT_EQ(expected, (*_object)[key].as<T>()); REQUIRE(obj.success());
REQUIRE(obj.size() == 1);
REQUIRE(obj["key"] == "value");
} }
private: SECTION("OneStringSingleQuotes") {
DynamicJsonBuffer _jsonBuffer; JsonObject& obj = jb.parseObject("{'key':'value'}");
JsonObject *_object; REQUIRE(obj.success());
char _jsonString[256]; REQUIRE(obj.size() == 1);
}; REQUIRE(obj["key"] == "value");
TEST_F(JsonParser_Object_Test, EmptyObject) {
whenInputIs("{}");
parseMustSucceed();
sizeMustBe(0);
} }
TEST_F(JsonParser_Object_Test, MissingOpeningBrace) { SECTION("OneStringNoQuotes") {
whenInputIs("}"); JsonObject& obj = jb.parseObject("{key:value}");
parseMustFail(); REQUIRE(obj.success());
REQUIRE(obj.size() == 1);
REQUIRE(obj["key"] == "value");
} }
TEST_F(JsonParser_Object_Test, MissingClosingBrace) { SECTION("OneStringSpaceBeforeKey") {
whenInputIs("{"); JsonObject& obj = jb.parseObject("{ \"key\":\"value\"}");
parseMustFail(); REQUIRE(obj.success());
REQUIRE(obj.size() == 1);
REQUIRE(obj["key"] == "value");
} }
TEST_F(JsonParser_Object_Test, MissingColonAndValue) { SECTION("OneStringSpaceAfterKey") {
whenInputIs("{\"key\"}"); JsonObject& obj = jb.parseObject("{\"key\" :\"value\"}");
parseMustFail(); REQUIRE(obj.success());
REQUIRE(obj.size() == 1);
REQUIRE(obj["key"] == "value");
} }
TEST_F(JsonParser_Object_Test, MissingQuotesAndColonAndValue) { SECTION("OneStringSpaceBeforeValue") {
whenInputIs("{key}"); JsonObject& obj = jb.parseObject("{\"key\": \"value\"}");
parseMustFail(); REQUIRE(obj.success());
REQUIRE(obj.size() == 1);
REQUIRE(obj["key"] == "value");
} }
TEST_F(JsonParser_Object_Test, OneString) { SECTION("OneStringSpaceAfterValue") {
whenInputIs("{\"key\":\"value\"}"); JsonObject& obj = jb.parseObject("{\"key\":\"value\" }");
parseMustSucceed(); REQUIRE(obj.success());
sizeMustBe(1); REQUIRE(obj.size() == 1);
keyMustHaveValue("key", "value"); REQUIRE(obj["key"] == "value");
} }
TEST_F(JsonParser_Object_Test, OneStringSingleQuotes) { SECTION("TwoStrings") {
whenInputIs("{'key':'value'}"); JsonObject& obj =
parseMustSucceed(); jb.parseObject("{\"key1\":\"value1\",\"key2\":\"value2\"}");
sizeMustBe(1); REQUIRE(obj.success());
keyMustHaveValue("key", "value"); REQUIRE(obj.size() == 2);
REQUIRE(obj["key1"] == "value1");
REQUIRE(obj["key2"] == "value2");
} }
TEST_F(JsonParser_Object_Test, OneStringNoQuotes) { SECTION("TwoStringsSpaceBeforeComma") {
whenInputIs("{key:value}"); JsonObject& obj =
parseMustSucceed(); jb.parseObject("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
sizeMustBe(1); REQUIRE(obj.success());
keyMustHaveValue("key", "value"); REQUIRE(obj.size() == 2);
REQUIRE(obj["key1"] == "value1");
REQUIRE(obj["key2"] == "value2");
} }
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeKey) { SECTION("TwoStringsSpaceAfterComma") {
whenInputIs("{ \"key\":\"value\"}"); JsonObject& obj =
parseMustSucceed(); jb.parseObject("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
sizeMustBe(1); REQUIRE(obj.success());
keyMustHaveValue("key", "value"); REQUIRE(obj.size() == 2);
REQUIRE(obj["key1"] == "value1");
REQUIRE(obj["key2"] == "value2");
} }
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterKey) { SECTION("EndingWithAComma") {
whenInputIs("{\"key\" :\"value\"}"); JsonObject& obj = jb.parseObject("{\"key1\":\"value1\",}");
parseMustSucceed(); REQUIRE_FALSE(obj.success());
sizeMustBe(1); REQUIRE(obj.size() == 0);
keyMustHaveValue("key", "value");
} }
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeValue) { SECTION("TwoIntergers") {
whenInputIs("{\"key\": \"value\"}"); JsonObject& obj = jb.parseObject("{\"key1\":42,\"key2\":-42}");
parseMustSucceed(); REQUIRE(obj.success());
sizeMustBe(1); REQUIRE(obj.size() == 2);
keyMustHaveValue("key", "value"); REQUIRE(obj["key1"] == 42);
REQUIRE(obj["key2"] == -42);
} }
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterValue) { SECTION("TwoDoubles") {
whenInputIs("{\"key\":\"value\" }"); JsonObject& obj = jb.parseObject("{\"key1\":12.345,\"key2\":-7E89}");
parseMustSucceed(); REQUIRE(obj.success());
sizeMustBe(1); REQUIRE(obj.size() == 2);
keyMustHaveValue("key", "value"); REQUIRE(obj["key1"] == 12.345);
REQUIRE(obj["key2"] == -7E89);
} }
TEST_F(JsonParser_Object_Test, TwoStrings) { SECTION("TwoBooleans") {
whenInputIs("{\"key1\":\"value1\",\"key2\":\"value2\"}"); JsonObject& obj = jb.parseObject("{\"key1\":true,\"key2\":false}");
parseMustSucceed(); REQUIRE(obj.success());
sizeMustBe(2); REQUIRE(obj.size() == 2);
keyMustHaveValue("key1", "value1"); REQUIRE(obj["key1"] == true);
keyMustHaveValue("key2", "value2"); REQUIRE(obj["key2"] == false);
} }
TEST_F(JsonParser_Object_Test, TwoStringsSpaceBeforeComma) { SECTION("TwoNulls") {
whenInputIs("{\"key1\":\"value1\" ,\"key2\":\"value2\"}"); JsonObject& obj = jb.parseObject("{\"key1\":null,\"key2\":null}");
parseMustSucceed(); REQUIRE(obj.success());
sizeMustBe(2); REQUIRE(obj.size() == 2);
keyMustHaveValue("key1", "value1"); REQUIRE(obj["key1"].as<char*>() == 0);
keyMustHaveValue("key2", "value2"); REQUIRE(obj["key2"].as<char*>() == 0);
} }
TEST_F(JsonParser_Object_Test, TwoStringsSpaceAfterComma) { SECTION("NullForKey") {
whenInputIs("{\"key1\":\"value1\" ,\"key2\":\"value2\"}"); JsonObject& obj = jb.parseObject("null:\"value\"}");
parseMustSucceed(); REQUIRE_FALSE(obj.success());
sizeMustBe(2);
keyMustHaveValue("key1", "value1");
keyMustHaveValue("key2", "value2");
} }
TEST_F(JsonParser_Object_Test, EndingWithAComma) {
whenInputIs("{\"key1\":\"value1\",}");
parseMustFail();
sizeMustBe(0);
}
TEST_F(JsonParser_Object_Test, TwoIntergers) {
whenInputIs("{\"key1\":42,\"key2\":-42}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", 42);
keyMustHaveValue("key2", -42);
}
TEST_F(JsonParser_Object_Test, TwoDoubles) {
whenInputIs("{\"key1\":12.345,\"key2\":-7E89}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", 12.345);
keyMustHaveValue("key2", -7E89);
}
TEST_F(JsonParser_Object_Test, TwoBooleans) {
whenInputIs("{\"key1\":true,\"key2\":false}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", true);
keyMustHaveValue("key2", false);
}
TEST_F(JsonParser_Object_Test, TwoNulls) {
const char *const nullstr = 0;
whenInputIs("{\"key1\":null,\"key2\":null}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", nullstr);
keyMustHaveValue("key2", nullstr);
}
TEST_F(JsonParser_Object_Test, NullForKey) {
whenInputIs("null:\"value\"}");
parseMustFail();
} }

View File

@ -18,5 +18,5 @@ add_executable(JsonObjectTests
subscript.cpp subscript.cpp
) )
target_link_libraries(JsonObjectTests gtest) target_link_libraries(JsonObjectTests catch)
add_test(JsonObject JsonObjectTests) add_test(JsonObject JsonObjectTests)

View File

@ -6,20 +6,17 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
#define TEST_(name) TEST(JsonObject_Basic_Tests, name) TEST_CASE("JsonObject basics") {
TEST_(InitialSizeIsZero) {
DynamicJsonBuffer _jsonBuffer; DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject(); JsonObject& _object = _jsonBuffer.createObject();
EXPECT_EQ(0, _object.size()); SECTION("InitialSizeIsZero") {
REQUIRE(0 == _object.size());
} }
TEST_(SuccessIsTrue) { SECTION("SuccessIsTrue") {
DynamicJsonBuffer _jsonBuffer; REQUIRE(_object.success());
JsonObject& _object = _jsonBuffer.createObject(); }
EXPECT_TRUE(_object.success());
} }

View File

@ -6,34 +6,28 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
#define TEST_(name) TEST(JsonObject_Basic_Tests, name) TEST_CASE("JsonObject::containsKey()") {
TEST_(ContainsKeyReturnsFalseForNonExistingKey) {
DynamicJsonBuffer _jsonBuffer; DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject(); JsonObject& _object = _jsonBuffer.createObject();
SECTION("ContainsKeyReturnsFalseForNonExistingKey") {
_object.set("hello", 42); _object.set("hello", 42);
EXPECT_FALSE(_object.containsKey("world")); REQUIRE(false == _object.containsKey("world"));
} }
TEST_(ContainsKeyReturnsTrueForDefinedValue) { SECTION("ContainsKeyReturnsTrueForDefinedValue") {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
_object.set("hello", 42); _object.set("hello", 42);
EXPECT_TRUE(_object.containsKey("hello")); REQUIRE(true == _object.containsKey("hello"));
} }
TEST_(ContainsKeyReturnsFalseAfterRemove) { SECTION("ContainsKeyReturnsFalseAfterRemove") {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
_object.set("hello", 42); _object.set("hello", 42);
_object.remove("hello"); _object.remove("hello");
EXPECT_FALSE(_object.containsKey("hello")); REQUIRE(false == _object.containsKey("hello"));
}
} }

View File

@ -6,21 +6,17 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class JsonObject_Get_Tests : public ::testing::Test { using namespace Catch::Matchers;
public:
JsonObject_Get_Tests() : _object(_jsonBuffer.createObject()) {}
protected: TEST_CASE("JsonObject::get()") {
DynamicJsonBuffer _jsonBuffer; DynamicJsonBuffer jb;
JsonObject& _object; JsonObject& obj = jb.createObject();
};
#define TEST_(name) TEST_F(JsonObject_Get_Tests, name) SECTION("GetConstCharPointer_GivenStringLiteral") {
obj.set("hello", "world");
TEST_(GetConstCharPointer_GivenStringLiteral) { const char* value = obj.get<const char*>("hello");
_object.set("hello", "world"); REQUIRE_THAT(value, Equals("world"));
const char* value = _object.get<const char*>("hello"); }
EXPECT_STREQ("world", value);
} }

View File

@ -6,28 +6,33 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
TEST(JsonObject_Invalid_Tests, SubscriptFails) { using namespace Catch::Matchers;
ASSERT_FALSE(JsonObject::invalid()["key"].success());
TEST_CASE("JsonObject::invalid()") {
JsonObject& obj = JsonObject::invalid();
SECTION("SubscriptFails") {
REQUIRE_FALSE(obj["key"].success());
} }
TEST(JsonObject_Invalid_Tests, AddFails) { SECTION("AddFails") {
JsonObject& object = JsonObject::invalid(); obj.set("hello", "world");
object.set("hello", "world"); REQUIRE(0 == obj.size());
ASSERT_EQ(0, object.size());
} }
TEST(JsonObject_Invalid_Tests, CreateNestedArrayFails) { SECTION("CreateNestedArrayFails") {
ASSERT_FALSE(JsonObject::invalid().createNestedArray("hello").success()); REQUIRE_FALSE(obj.createNestedArray("hello").success());
} }
TEST(JsonObject_Invalid_Tests, CreateNestedObjectFails) { SECTION("CreateNestedObjectFails") {
ASSERT_FALSE(JsonObject::invalid().createNestedObject("world").success()); REQUIRE_FALSE(obj.createNestedObject("world").success());
} }
TEST(JsonObject_Invalid_Tests, PrintToWritesBraces) { SECTION("PrintToWritesBraces") {
char buffer[32]; char buffer[32];
JsonObject::invalid().printTo(buffer, sizeof(buffer)); obj.printTo(buffer, sizeof(buffer));
ASSERT_STREQ("{}", buffer); REQUIRE_THAT(buffer, Equals("{}"));
}
} }

View File

@ -6,52 +6,49 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class JsonObject_Iterator_Test : public testing::Test { using namespace Catch::Matchers;
public:
JsonObject_Iterator_Test() : _object(_buffer.createObject()) {
_object["ab"] = 12;
_object["cd"] = 34;
}
protected: TEST_CASE("JsonObject::begin()/end()") {
StaticJsonBuffer<JSON_OBJECT_SIZE(2)> _buffer; StaticJsonBuffer<JSON_OBJECT_SIZE(2)> jb;
JsonObject& _object; JsonObject& obj = jb.createObject();
}; obj["ab"] = 12;
obj["cd"] = 34;
TEST_F(JsonObject_Iterator_Test, NonConstIterator) { SECTION("NonConstIterator") {
JsonObject::iterator it = _object.begin(); JsonObject::iterator it = obj.begin();
ASSERT_NE(_object.end(), it); REQUIRE(obj.end() != it);
EXPECT_STREQ("ab", it->key); REQUIRE_THAT(it->key, Equals("ab"));
EXPECT_EQ(12, it->value); REQUIRE(12 == it->value);
it->key = "a.b"; it->key = "a.b";
it->value = 1.2; it->value = 1.2;
++it; ++it;
ASSERT_NE(_object.end(), it); REQUIRE(obj.end() != it);
EXPECT_STREQ("cd", it->key); REQUIRE_THAT(it->key, Equals("cd"));
EXPECT_EQ(34, it->value); REQUIRE(34 == it->value);
it->key = "c.d"; it->key = "c.d";
it->value = 3.4; it->value = 3.4;
++it; ++it;
ASSERT_EQ(_object.end(), it); REQUIRE(obj.end() == it);
ASSERT_EQ(2, _object.size()); REQUIRE(2 == obj.size());
EXPECT_EQ(1.2, _object["a.b"]); REQUIRE(1.2 == obj["a.b"]);
EXPECT_EQ(3.4, _object["c.d"]); REQUIRE(3.4 == obj["c.d"]);
} }
TEST_F(JsonObject_Iterator_Test, ConstIterator) { SECTION("ConstIterator") {
const JsonObject& const_object = _object; const JsonObject& const_object = obj;
JsonObject::const_iterator it = const_object.begin(); JsonObject::const_iterator it = const_object.begin();
ASSERT_NE(const_object.end(), it); REQUIRE(const_object.end() != it);
EXPECT_STREQ("ab", it->key); REQUIRE_THAT(it->key, Equals("ab"));
EXPECT_EQ(12, it->value); REQUIRE(12 == it->value);
++it; ++it;
ASSERT_NE(const_object.end(), it); REQUIRE(const_object.end() != it);
EXPECT_STREQ("cd", it->key); REQUIRE_THAT(it->key, Equals("cd"));
EXPECT_EQ(34, it->value); REQUIRE(34 == it->value);
++it; ++it;
ASSERT_EQ(const_object.end(), it); REQUIRE(const_object.end() == it);
}
} }

View File

@ -6,71 +6,67 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
#include <string>
class JsonObject_PrettyPrintTo_Tests : public testing::Test { void check(const JsonObject &obj, const std::string expected) {
public: char json[256];
JsonObject_PrettyPrintTo_Tests() : _object(_jsonBuffer.createObject()) {}
protected: size_t actualLen = obj.prettyPrintTo(json);
DynamicJsonBuffer _jsonBuffer; size_t measuredLen = obj.measurePrettyLength();
JsonObject &_object;
void outputMustBe(const char *expected) { REQUIRE(json == expected);
char buffer[256]; REQUIRE(expected.size() == actualLen);
REQUIRE(expected.size() == measuredLen);
size_t actualLen = _object.prettyPrintTo(buffer);
size_t measuredLen = _object.measurePrettyLength();
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), actualLen);
EXPECT_EQ(strlen(expected), measuredLen);
}
};
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) {
outputMustBe("{}");
} }
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) { TEST_CASE("JsonObject::prettyPrintTo()") {
_object["key"] = "value"; DynamicJsonBuffer jb;
JsonObject &obj = jb.createObject();
outputMustBe( SECTION("EmptyObject") {
check(obj, "{}");
}
SECTION("OneMember") {
obj["key"] = "value";
check(obj,
"{\r\n" "{\r\n"
" \"key\": \"value\"\r\n" " \"key\": \"value\"\r\n"
"}"); "}");
} }
TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers) { SECTION("TwoMembers") {
_object["key1"] = "value1"; obj["key1"] = "value1";
_object["key2"] = "value2"; obj["key2"] = "value2";
outputMustBe( check(obj,
"{\r\n" "{\r\n"
" \"key1\": \"value1\",\r\n" " \"key1\": \"value1\",\r\n"
" \"key2\": \"value2\"\r\n" " \"key2\": \"value2\"\r\n"
"}"); "}");
} }
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) { SECTION("EmptyNestedContainers") {
_object.createNestedObject("key1"); obj.createNestedObject("key1");
_object.createNestedArray("key2"); obj.createNestedArray("key2");
outputMustBe( check(obj,
"{\r\n" "{\r\n"
" \"key1\": {},\r\n" " \"key1\": {},\r\n"
" \"key2\": []\r\n" " \"key2\": []\r\n"
"}"); "}");
} }
TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers) { SECTION("NestedContainers") {
JsonObject &nested1 = _object.createNestedObject("key1"); JsonObject &nested1 = obj.createNestedObject("key1");
nested1["a"] = 1; nested1["a"] = 1;
JsonArray &nested2 = _object.createNestedArray("key2"); JsonArray &nested2 = obj.createNestedArray("key2");
nested2.add(2); nested2.add(2);
outputMustBe( check(obj,
"{\r\n" "{\r\n"
" \"key1\": {\r\n" " \"key1\": {\r\n"
" \"a\": 1\r\n" " \"a\": 1\r\n"
@ -80,3 +76,4 @@ TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers) {
" ]\r\n" " ]\r\n"
"}"); "}");
} }
}

View File

@ -6,118 +6,114 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
#include <string>
class JsonObject_PrintTo_Tests : public testing::Test { void check(const JsonObject &obj, const std::string &expected) {
public:
JsonObject_PrintTo_Tests() : _object(_jsonBuffer.createObject()) {}
protected:
void outputMustBe(const char *expected) {
char actual[256]; char actual[256];
size_t actualLen = _object.printTo(actual); size_t actualLen = obj.printTo(actual);
size_t measuredLen = _object.measureLength(); size_t measuredLen = obj.measureLength();
EXPECT_STREQ(expected, actual); REQUIRE(expected == actual);
EXPECT_EQ(strlen(expected), actualLen); REQUIRE(expected.size() == actualLen);
EXPECT_EQ(strlen(expected), measuredLen); REQUIRE(expected.size() == measuredLen);
} }
TEST_CASE("JsonObject::printTo()") {
DynamicJsonBuffer _jsonBuffer; DynamicJsonBuffer _jsonBuffer;
JsonObject &_object; JsonObject &obj = _jsonBuffer.createObject();
};
TEST_F(JsonObject_PrintTo_Tests, EmptyObject) { SECTION("EmptyObject") {
outputMustBe("{}"); check(obj, "{}");
} }
TEST_F(JsonObject_PrintTo_Tests, TwoStrings) { SECTION("TwoStrings") {
_object["key1"] = "value1"; obj["key1"] = "value1";
_object.set("key2", "value2"); obj.set("key2", "value2");
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}"); check(obj, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
} }
TEST_F(JsonObject_PrintTo_Tests, RemoveFirst) { SECTION("RemoveFirst") {
_object["key1"] = "value1"; obj["key1"] = "value1";
_object["key2"] = "value2"; obj["key2"] = "value2";
_object.remove("key1"); obj.remove("key1");
outputMustBe("{\"key2\":\"value2\"}"); check(obj, "{\"key2\":\"value2\"}");
} }
TEST_F(JsonObject_PrintTo_Tests, RemoveLast) { SECTION("RemoveLast") {
_object["key1"] = "value1"; obj["key1"] = "value1";
_object["key2"] = "value2"; obj["key2"] = "value2";
_object.remove("key2"); obj.remove("key2");
outputMustBe("{\"key1\":\"value1\"}"); check(obj, "{\"key1\":\"value1\"}");
} }
TEST_F(JsonObject_PrintTo_Tests, RemoveUnexistingKey) { SECTION("RemoveUnexistingKey") {
_object["key1"] = "value1"; obj["key1"] = "value1";
_object["key2"] = "value2"; obj["key2"] = "value2";
_object.remove("key3"); obj.remove("key3");
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}"); check(obj, "{\"key1\":\"value1\",\"key2\":\"value2\"}");
} }
TEST_F(JsonObject_PrintTo_Tests, ReplaceExistingKey) { SECTION("ReplaceExistingKey") {
_object["key"] = "value1"; obj["key"] = "value1";
_object["key"] = "value2"; obj["key"] = "value2";
outputMustBe("{\"key\":\"value2\"}"); check(obj, "{\"key\":\"value2\"}");
} }
TEST_F(JsonObject_PrintTo_Tests, TwoIntegers) { SECTION("TwoIntegers") {
_object["a"] = 1; obj["a"] = 1;
_object.set("b", 2); obj.set("b", 2);
outputMustBe("{\"a\":1,\"b\":2}"); check(obj, "{\"a\":1,\"b\":2}");
} }
TEST_F(JsonObject_PrintTo_Tests, RawJson) { SECTION("RawJson") {
_object["a"] = RawJson("[1,2]"); obj["a"] = RawJson("[1,2]");
_object.set("b", RawJson("[4,5]")); obj.set("b", RawJson("[4,5]"));
outputMustBe("{\"a\":[1,2],\"b\":[4,5]}"); check(obj, "{\"a\":[1,2],\"b\":[4,5]}");
} }
TEST_F(JsonObject_PrintTo_Tests, TwoDoublesFourDigits) { SECTION("TwoDoublesFourDigits") {
_object["a"] = double_with_n_digits(3.14159265358979323846, 4); obj["a"] = double_with_n_digits(3.14159265358979323846, 4);
_object.set("b", 2.71828182845904523536, 4); obj.set("b", 2.71828182845904523536, 4);
_object.set("c", double_with_n_digits(3.14159265358979323846, 3)); obj.set("c", double_with_n_digits(3.14159265358979323846, 3));
outputMustBe("{\"a\":3.1416,\"b\":2.7183,\"c\":3.142}"); check(obj, "{\"a\":3.1416,\"b\":2.7183,\"c\":3.142}");
} }
TEST_F(JsonObject_PrintTo_Tests, TwoDoubleDefaultDigits) { SECTION("TwoDoubleDefaultDigits") {
_object["a"] = 3.14159265358979323846; obj["a"] = 3.14159265358979323846;
_object.set("b", 2.71828182845904523536); obj.set("b", 2.71828182845904523536);
outputMustBe("{\"a\":3.14,\"b\":2.72}"); check(obj, "{\"a\":3.14,\"b\":2.72}");
} }
TEST_F(JsonObject_PrintTo_Tests, TwoNull) { SECTION("TwoNull") {
_object["a"] = static_cast<char *>(0); obj["a"] = static_cast<char *>(0);
_object.set("b", static_cast<char *>(0)); obj.set("b", static_cast<char *>(0));
outputMustBe("{\"a\":null,\"b\":null}"); check(obj, "{\"a\":null,\"b\":null}");
} }
TEST_F(JsonObject_PrintTo_Tests, TwoBooleans) { SECTION("TwoBooleans") {
_object["a"] = true; obj["a"] = true;
_object.set("b", false); obj.set("b", false);
outputMustBe("{\"a\":true,\"b\":false}"); check(obj, "{\"a\":true,\"b\":false}");
} }
TEST_F(JsonObject_PrintTo_Tests, ThreeNestedArrays) { SECTION("ThreeNestedArrays") {
_object.createNestedArray("a"); obj.createNestedArray("a");
_object["b"] = _jsonBuffer.createArray(); obj["b"] = _jsonBuffer.createArray();
_object.set("c", _jsonBuffer.createArray()); obj.set("c", _jsonBuffer.createArray());
outputMustBe("{\"a\":[],\"b\":[],\"c\":[]}"); check(obj, "{\"a\":[],\"b\":[],\"c\":[]}");
} }
TEST_F(JsonObject_PrintTo_Tests, ThreeNestedObjects) { SECTION("ThreeNestedObjects") {
_object.createNestedObject("a"); obj.createNestedObject("a");
_object["b"] = _jsonBuffer.createObject(); obj["b"] = _jsonBuffer.createObject();
_object.set("c", _jsonBuffer.createObject()); obj.set("c", _jsonBuffer.createObject());
outputMustBe("{\"a\":{},\"b\":{},\"c\":{}}"); check(obj, "{\"a\":{},\"b\":{},\"c\":{}}");
}
} }

View File

@ -6,39 +6,39 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
#include <string>
#define TEST_(name) TEST(JsonObject_Remove_Tests, name) TEST_CASE("JsonObject::remove()") {
DynamicJsonBuffer jb;
TEST_(SizeDecreased_WhenValuesAreRemoved) { SECTION("SizeDecreased_WhenValuesAreRemoved") {
DynamicJsonBuffer _jsonBuffer; JsonObject& obj = jb.createObject();
JsonObject& _object = _jsonBuffer.createObject(); obj["hello"] = 1;
_object["hello"] = 1;
_object.remove("hello"); obj.remove("hello");
EXPECT_EQ(0, _object.size()); REQUIRE(0 == obj.size());
} }
TEST_(SizeUntouched_WhenRemoveIsCalledWithAWrongKey) { SECTION("SizeUntouched_WhenRemoveIsCalledWithAWrongKey") {
DynamicJsonBuffer _jsonBuffer; JsonObject& obj = jb.createObject();
JsonObject& _object = _jsonBuffer.createObject(); obj["hello"] = 1;
_object["hello"] = 1;
_object.remove("world"); obj.remove("world");
EXPECT_EQ(1, _object.size()); REQUIRE(1 == obj.size());
} }
TEST_(RemoveByIterator) { SECTION("RemoveByIterator") {
DynamicJsonBuffer _jsonBuffer; JsonObject& obj = jb.parseObject("{\"a\":0,\"b\":1,\"c\":2}");
JsonObject& _object = _jsonBuffer.parseObject("{\"a\":0,\"b\":1,\"c\":2}");
for (JsonObject::iterator it = _object.begin(); it != _object.end(); ++it) { for (JsonObject::iterator it = obj.begin(); it != obj.end(); ++it) {
if (it->value == 1) _object.remove(it); if (it->value == 1) obj.remove(it);
} }
char result[64]; std::string result;
_object.printTo(result); obj.printTo(result);
EXPECT_STREQ("{\"a\":0,\"c\":2}", result); REQUIRE("{\"a\":0,\"c\":2}" == result);
}
} }

View File

@ -6,122 +6,113 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
#include <string>
class JsonObject_Set_Tests : public ::testing::Test { TEST_CASE("JsonObject::set()") {
public: DynamicJsonBuffer jb;
JsonObject_Set_Tests() : _object(_jsonBuffer.createObject()) {} JsonObject& _object = jb.createObject();
protected: SECTION("SizeIncreased_WhenValuesAreAdded") {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object;
};
#define TEST_(name) TEST_F(JsonObject_Set_Tests, name)
TEST_(SizeIncreased_WhenValuesAreAdded) {
_object.set("hello", 42); _object.set("hello", 42);
EXPECT_EQ(1, _object.size()); REQUIRE(1 == _object.size());
} }
TEST_(SizeUntouched_WhenSameValueIsAdded) { SECTION("SizeUntouched_WhenSameValueIsAdded") {
_object["hello"] = 1; _object["hello"] = 1;
_object["hello"] = 2; _object["hello"] = 2;
EXPECT_EQ(1, _object.size()); REQUIRE(1 == _object.size());
} }
TEST_(StoreInteger) { SECTION("StoreInteger") {
_object.set("hello", 123); _object.set("hello", 123);
EXPECT_EQ(123, _object["hello"].as<int>()); REQUIRE(123 == _object["hello"].as<int>());
EXPECT_TRUE(_object["hello"].is<int>()); REQUIRE(_object["hello"].is<int>());
EXPECT_FALSE(_object["hello"].is<double>()); REQUIRE_FALSE(_object["hello"].is<double>());
} }
TEST_(StoreDouble) { SECTION("StoreDouble") {
_object.set("hello", 123.45); _object.set("hello", 123.45);
EXPECT_EQ(123.45, _object["hello"].as<double>()); REQUIRE(123.45 == _object["hello"].as<double>());
EXPECT_TRUE(_object["hello"].is<double>()); REQUIRE(_object["hello"].is<double>());
EXPECT_FALSE(_object["hello"].is<long>()); REQUIRE_FALSE(_object["hello"].is<long>());
} }
TEST_(StoreDoubleWithDigits) { SECTION("StoreDoubleWithDigits") {
_object.set("hello", 123.45, 2); _object.set("hello", 123.45, 2);
EXPECT_EQ(123.45, _object["hello"].as<double>()); REQUIRE(123.45 == _object["hello"].as<double>());
EXPECT_TRUE(_object["hello"].is<double>()); REQUIRE(_object["hello"].is<double>());
EXPECT_FALSE(_object["hello"].is<long>()); REQUIRE_FALSE(_object["hello"].is<long>());
} }
TEST_(StoreBoolean) { SECTION("StoreBoolean") {
_object.set("hello", true); _object.set("hello", true);
EXPECT_TRUE(_object["hello"].as<bool>()); REQUIRE(_object["hello"].as<bool>());
EXPECT_TRUE(_object["hello"].is<bool>()); REQUIRE(_object["hello"].is<bool>());
EXPECT_FALSE(_object["hello"].is<long>()); REQUIRE_FALSE(_object["hello"].is<long>());
} }
TEST_(StoreString) { SECTION("StoreString") {
_object.set("hello", "h3110"); _object.set("hello", "h3110");
EXPECT_STREQ("h3110", _object["hello"].as<const char*>()); REQUIRE(std::string("h3110") == _object["hello"].as<const char*>());
EXPECT_TRUE(_object["hello"].is<const char*>()); REQUIRE(_object["hello"].is<const char*>());
EXPECT_FALSE(_object["hello"].is<long>()); REQUIRE_FALSE(_object["hello"].is<long>());
} }
TEST_(StoreArray) { SECTION("StoreArray") {
JsonArray& arr = _jsonBuffer.createArray(); JsonArray& arr = jb.createArray();
_object.set("hello", arr); _object.set("hello", arr);
EXPECT_EQ(&arr, &_object["hello"].as<JsonArray>()); REQUIRE(&arr == &_object["hello"].as<JsonArray>());
EXPECT_TRUE(_object["hello"].is<JsonArray&>()); REQUIRE(_object["hello"].is<JsonArray&>());
EXPECT_FALSE(_object["hello"].is<JsonObject&>()); REQUIRE_FALSE(_object["hello"].is<JsonObject&>());
} }
TEST_(StoreObject) { SECTION("StoreObject") {
JsonObject& obj = _jsonBuffer.createObject(); JsonObject& obj = jb.createObject();
_object.set("hello", obj); _object.set("hello", obj);
EXPECT_EQ(&obj, &_object["hello"].as<JsonObject>()); REQUIRE(&obj == &_object["hello"].as<JsonObject>());
EXPECT_TRUE(_object["hello"].is<JsonObject&>()); REQUIRE(_object["hello"].is<JsonObject&>());
EXPECT_FALSE(_object["hello"].is<JsonArray&>()); REQUIRE_FALSE(_object["hello"].is<JsonArray&>());
} }
TEST_(StoreArraySubscript) { SECTION("StoreArraySubscript") {
JsonArray& arr = _jsonBuffer.createArray(); JsonArray& arr = jb.createArray();
arr.add(42); arr.add(42);
_object.set("a", arr[0]); _object.set("a", arr[0]);
EXPECT_EQ(42, _object["a"]); REQUIRE(42 == _object["a"]);
} }
TEST_(StoreObjectSubscript) { SECTION("StoreObjectSubscript") {
JsonObject& obj = _jsonBuffer.createObject(); JsonObject& obj = jb.createObject();
obj.set("x", 42); obj.set("x", 42);
_object.set("a", obj["x"]); _object.set("a", obj["x"]);
EXPECT_EQ(42, _object["a"]); REQUIRE(42 == _object["a"]);
} }
TEST_(ShouldReturnTrue_WhenAllocationSucceeds) { SECTION("ShouldReturnTrue_WhenAllocationSucceeds") {
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 15> jsonBuffer; StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 15> jsonBuffer;
JsonObject& obj = jsonBuffer.createObject(); JsonObject& obj = jsonBuffer.createObject();
bool result = obj.set(std::string("hello"), std::string("world")); REQUIRE(true == obj.set(std::string("hello"), std::string("world")));
ASSERT_TRUE(result);
} }
TEST_(ShouldReturnFalse_WhenAllocationFails) { SECTION("ShouldReturnFalse_WhenAllocationFails") {
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 10> jsonBuffer; StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 10> jsonBuffer;
JsonObject& obj = jsonBuffer.createObject(); JsonObject& obj = jsonBuffer.createObject();
bool result = obj.set(std::string("hello"), std::string("world")); REQUIRE(false == obj.set(std::string("hello"), std::string("world")));
}
ASSERT_FALSE(result);
} }

View File

@ -6,132 +6,127 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class JsonObject_Subscript_Tests : public ::testing::Test { TEST_CASE("JsonObject::operator[]") {
public:
JsonObject_Subscript_Tests() : _object(_jsonBuffer.createObject()) {}
protected:
DynamicJsonBuffer _jsonBuffer; DynamicJsonBuffer _jsonBuffer;
JsonObject& _object; JsonObject& _object = _jsonBuffer.createObject();
};
#define TEST_(name) TEST_F(JsonObject_Subscript_Tests, name) SECTION("SizeIncreased_WhenValuesAreAdded") {
TEST_(SizeIncreased_WhenValuesAreAdded) {
_object["hello"] = 1; _object["hello"] = 1;
EXPECT_EQ(1, _object.size()); REQUIRE(1 == _object.size());
} }
TEST_(SizeUntouched_WhenSameValueIsAdded) { SECTION("SizeUntouched_WhenSameValueIsAdded") {
_object["hello"] = 1; _object["hello"] = 1;
_object["hello"] = 2; _object["hello"] = 2;
EXPECT_EQ(1, _object.size()); REQUIRE(1 == _object.size());
} }
TEST_(StoreInteger) { SECTION("StoreInteger") {
_object["hello"] = 123; _object["hello"] = 123;
EXPECT_EQ(123, _object["hello"].as<int>()); REQUIRE(123 == _object["hello"].as<int>());
EXPECT_TRUE(_object["hello"].is<int>()); REQUIRE(true == _object["hello"].is<int>());
EXPECT_FALSE(_object["hello"].is<double>()); REQUIRE(false == _object["hello"].is<double>());
} }
TEST_(StoreVolatileInteger) { // issue #415 SECTION("StoreVolatileInteger") { // issue #415
volatile int i = 123; volatile int i = 123;
_object["hello"] = i; _object["hello"] = i;
EXPECT_EQ(123, _object["hello"].as<int>()); REQUIRE(123 == _object["hello"].as<int>());
EXPECT_TRUE(_object["hello"].is<int>()); REQUIRE(true == _object["hello"].is<int>());
EXPECT_FALSE(_object["hello"].is<double>()); REQUIRE(false == _object["hello"].is<double>());
} }
TEST_(StoreDouble) { SECTION("StoreDouble") {
_object["hello"] = 123.45; _object["hello"] = 123.45;
EXPECT_TRUE(_object["hello"].is<double>()); REQUIRE(true == _object["hello"].is<double>());
EXPECT_FALSE(_object["hello"].is<long>()); REQUIRE(false == _object["hello"].is<long>());
EXPECT_EQ(123.45, _object["hello"].as<double>()); REQUIRE(123.45 == _object["hello"].as<double>());
} }
TEST_(StoreDoubleWithDigits) { SECTION("StoreDoubleWithDigits") {
_object["hello"].set(123.45, 2); _object["hello"].set(123.45, 2);
EXPECT_TRUE(_object["hello"].is<double>()); REQUIRE(true == _object["hello"].is<double>());
EXPECT_FALSE(_object["hello"].is<long>()); REQUIRE(false == _object["hello"].is<long>());
EXPECT_EQ(123.45, _object["hello"].as<double>()); REQUIRE(123.45 == _object["hello"].as<double>());
} }
TEST_(StoreBoolean) { SECTION("StoreBoolean") {
_object["hello"] = true; _object["hello"] = true;
EXPECT_TRUE(_object["hello"].is<bool>()); REQUIRE(true == _object["hello"].is<bool>());
EXPECT_FALSE(_object["hello"].is<long>()); REQUIRE(false == _object["hello"].is<long>());
EXPECT_TRUE(_object["hello"].as<bool>()); REQUIRE(true == _object["hello"].as<bool>());
} }
TEST_(StoreString) { SECTION("StoreString") {
_object["hello"] = "h3110"; _object["hello"] = "h3110";
EXPECT_TRUE(_object["hello"].is<const char*>()); REQUIRE(true == _object["hello"].is<const char*>());
EXPECT_FALSE(_object["hello"].is<long>()); REQUIRE(false == _object["hello"].is<long>());
EXPECT_STREQ("h3110", _object["hello"].as<const char*>()); REQUIRE(std::string("h3110") == _object["hello"].as<const char*>());
EXPECT_STREQ("h3110", _object["hello"].as<char*>()); // <- short hand REQUIRE(std::string("h3110") ==
_object["hello"].as<char*>()); // <- short hand
} }
TEST_(StoreArray) { SECTION("StoreArray") {
JsonArray& arr = _jsonBuffer.createArray(); JsonArray& arr = _jsonBuffer.createArray();
_object["hello"] = arr; _object["hello"] = arr;
EXPECT_EQ(&arr, &_object["hello"].as<JsonArray&>()); REQUIRE(&arr == &_object["hello"].as<JsonArray&>());
EXPECT_EQ(&arr, &_object["hello"].as<JsonArray>()); // <- short hand REQUIRE(&arr == &_object["hello"].as<JsonArray>()); // <- short hand
EXPECT_EQ(&arr, &_object["hello"].as<const JsonArray&>()); REQUIRE(&arr == &_object["hello"].as<const JsonArray&>());
EXPECT_EQ(&arr, &_object["hello"].as<const JsonArray>()); // <- short hand REQUIRE(&arr == &_object["hello"].as<const JsonArray>()); // <- short hand
EXPECT_TRUE(_object["hello"].is<JsonArray&>()); REQUIRE(true == _object["hello"].is<JsonArray&>());
EXPECT_TRUE(_object["hello"].is<JsonArray>()); REQUIRE(true == _object["hello"].is<JsonArray>());
EXPECT_TRUE(_object["hello"].is<const JsonArray&>()); REQUIRE(true == _object["hello"].is<const JsonArray&>());
EXPECT_TRUE(_object["hello"].is<const JsonArray>()); REQUIRE(true == _object["hello"].is<const JsonArray>());
EXPECT_FALSE(_object["hello"].is<JsonObject&>()); REQUIRE(false == _object["hello"].is<JsonObject&>());
} }
TEST_(StoreObject) { SECTION("StoreObject") {
JsonObject& obj = _jsonBuffer.createObject(); JsonObject& obj = _jsonBuffer.createObject();
_object["hello"] = obj; _object["hello"] = obj;
EXPECT_EQ(&obj, &_object["hello"].as<JsonObject&>()); REQUIRE(&obj == &_object["hello"].as<JsonObject&>());
EXPECT_EQ(&obj, &_object["hello"].as<JsonObject>()); // <- short hand REQUIRE(&obj == &_object["hello"].as<JsonObject>()); // <- short hand
EXPECT_EQ(&obj, &_object["hello"].as<const JsonObject&>()); REQUIRE(&obj == &_object["hello"].as<const JsonObject&>());
EXPECT_EQ(&obj, &_object["hello"].as<const JsonObject>()); // <- short hand REQUIRE(&obj == &_object["hello"].as<const JsonObject>()); // <- short hand
EXPECT_TRUE(_object["hello"].is<JsonObject&>()); REQUIRE(true == _object["hello"].is<JsonObject&>());
EXPECT_TRUE(_object["hello"].is<JsonObject>()); REQUIRE(true == _object["hello"].is<JsonObject>());
EXPECT_TRUE(_object["hello"].is<const JsonObject&>()); REQUIRE(true == _object["hello"].is<const JsonObject&>());
EXPECT_TRUE(_object["hello"].is<const JsonObject>()); REQUIRE(true == _object["hello"].is<const JsonObject>());
EXPECT_FALSE(_object["hello"].is<JsonArray&>()); REQUIRE(false == _object["hello"].is<JsonArray&>());
} }
TEST_(StoreArraySubscript) { SECTION("StoreArraySubscript") {
JsonArray& arr = _jsonBuffer.createArray(); JsonArray& arr = _jsonBuffer.createArray();
arr.add(42); arr.add(42);
_object["a"] = arr[0]; _object["a"] = arr[0];
EXPECT_EQ(42, _object["a"]); REQUIRE(42 == _object["a"]);
} }
TEST_(StoreObjectSubscript) { SECTION("StoreObjectSubscript") {
JsonObject& obj = _jsonBuffer.createObject(); JsonObject& obj = _jsonBuffer.createObject();
obj.set("x", 42); obj.set("x", 42);
_object["a"] = obj["x"]; _object["a"] = obj["x"];
EXPECT_EQ(42, _object["a"]); REQUIRE(42 == _object["a"]);
} }
TEST_(KeyAsCharArray) { // issue #423 SECTION("KeyAsCharArray") { // issue #423
char key[] = "hello"; char key[] = "hello";
_object[key] = 42; _object[key] = 42;
EXPECT_EQ(42, _object[key]); REQUIRE(42 == _object[key]);
}
} }

View File

@ -17,5 +17,5 @@ add_executable(JsonVariantTests
undefined.cpp undefined.cpp
) )
target_link_libraries(JsonVariantTests gtest) target_link_libraries(JsonVariantTests catch)
add_test(JsonVariant JsonVariantTests) add_test(JsonVariant JsonVariantTests)

View File

@ -6,205 +6,205 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h>
#include <stdint.h> #include <stdint.h>
#include <catch.hpp>
static const char* null = 0; static const char* null = 0;
TEST(JsonVariant_As_Tests, DoubleAsBool) { TEST_CASE("JsonVariant::as()") {
SECTION("DoubleAsBool") {
JsonVariant variant = 4.2; JsonVariant variant = 4.2;
ASSERT_TRUE(variant.as<bool>()); REQUIRE(variant.as<bool>());
} }
TEST(JsonVariant_As_Tests, DoubleAsCstr) { SECTION("DoubleAsCstr") {
JsonVariant variant = 4.2; JsonVariant variant = 4.2;
ASSERT_FALSE(variant.as<const char*>()); REQUIRE_FALSE(variant.as<const char*>());
} }
TEST(JsonVariant_As_Tests, DoubleAsString) { SECTION("DoubleAsString") {
JsonVariant variant = 4.2; JsonVariant variant = 4.2;
ASSERT_EQ(std::string("4.20"), variant.as<std::string>()); REQUIRE(std::string("4.20") == variant.as<std::string>());
} }
TEST(JsonVariant_As_Tests, DoubleAsLong) { SECTION("DoubleAsLong") {
JsonVariant variant = 4.2; JsonVariant variant = 4.2;
ASSERT_EQ(4L, variant.as<long>()); REQUIRE(4L == variant.as<long>());
} }
TEST(JsonVariant_As_Tests, DoubleAsUnsigned) { SECTION("DoubleAsUnsigned") {
JsonVariant variant = 4.2; JsonVariant variant = 4.2;
ASSERT_EQ(4U, variant.as<unsigned>()); REQUIRE(4U == variant.as<unsigned>());
} }
TEST(JsonVariant_As_Tests, DoubleZeroAsBool) { SECTION("DoubleZeroAsBool") {
JsonVariant variant = 0.0; JsonVariant variant = 0.0;
ASSERT_FALSE(variant.as<bool>()); REQUIRE_FALSE(variant.as<bool>());
} }
TEST(JsonVariant_As_Tests, DoubleZeroAsLong) { SECTION("DoubleZeroAsLong") {
JsonVariant variant = 0.0; JsonVariant variant = 0.0;
ASSERT_EQ(0L, variant.as<long>()); REQUIRE(0L == variant.as<long>());
} }
TEST(JsonVariant_As_Tests, FalseAsBool) { SECTION("FalseAsBool") {
JsonVariant variant = false; JsonVariant variant = false;
ASSERT_FALSE(variant.as<bool>()); REQUIRE_FALSE(variant.as<bool>());
} }
TEST(JsonVariant_As_Tests, FalseAsDouble) { SECTION("FalseAsDouble") {
JsonVariant variant = false; JsonVariant variant = false;
ASSERT_EQ(0.0, variant.as<double>()); REQUIRE(0.0 == variant.as<double>());
} }
TEST(JsonVariant_As_Tests, FalseAsLong) { SECTION("FalseAsLong") {
JsonVariant variant = false; JsonVariant variant = false;
ASSERT_EQ(0L, variant.as<long>()); REQUIRE(0L == variant.as<long>());
} }
TEST(JsonVariant_As_Tests, FalseAsString) { SECTION("FalseAsString") {
JsonVariant variant = false; JsonVariant variant = false;
ASSERT_EQ(std::string("false"), variant.as<std::string>()); REQUIRE(std::string("false") == variant.as<std::string>());
} }
TEST(JsonVariant_As_Tests, TrueAsBool) { SECTION("TrueAsBool") {
JsonVariant variant = true; JsonVariant variant = true;
ASSERT_TRUE(variant.as<bool>()); REQUIRE(variant.as<bool>());
} }
TEST(JsonVariant_As_Tests, TrueAsDouble) { SECTION("TrueAsDouble") {
JsonVariant variant = true; JsonVariant variant = true;
ASSERT_EQ(1.0, variant.as<double>()); REQUIRE(1.0 == variant.as<double>());
} }
TEST(JsonVariant_As_Tests, TrueAsLong) { SECTION("TrueAsLong") {
JsonVariant variant = true; JsonVariant variant = true;
ASSERT_EQ(1L, variant.as<long>()); REQUIRE(1L == variant.as<long>());
} }
TEST(JsonVariant_As_Tests, TrueAsString) { SECTION("TrueAsString") {
JsonVariant variant = true; JsonVariant variant = true;
ASSERT_EQ(std::string("true"), variant.as<std::string>()); REQUIRE(std::string("true") == variant.as<std::string>());
} }
TEST(JsonVariant_As_Tests, LongAsBool) { SECTION("LongAsBool") {
JsonVariant variant = 42L; JsonVariant variant = 42L;
ASSERT_TRUE(variant.as<bool>()); REQUIRE(variant.as<bool>());
} }
TEST(JsonVariant_As_Tests, LongZeroAsBool) { SECTION("LongZeroAsBool") {
JsonVariant variant = 0L; JsonVariant variant = 0L;
ASSERT_FALSE(variant.as<bool>()); REQUIRE_FALSE(variant.as<bool>());
} }
TEST(JsonVariant_As_Tests, PositiveLongAsDouble) { SECTION("PositiveLongAsDouble") {
JsonVariant variant = 42L; JsonVariant variant = 42L;
ASSERT_EQ(42.0, variant.as<double>()); REQUIRE(42.0 == variant.as<double>());
} }
TEST(JsonVariant_As_Tests, NegativeLongAsDouble) { SECTION("NegativeLongAsDouble") {
JsonVariant variant = -42L; JsonVariant variant = -42L;
ASSERT_EQ(-42.0, variant.as<double>()); REQUIRE(-42.0 == variant.as<double>());
} }
TEST(JsonVariant_As_Tests, LongAsString) { SECTION("LongAsString") {
JsonVariant variant = 42L; JsonVariant variant = 42L;
ASSERT_EQ(std::string("42"), variant.as<std::string>()); REQUIRE(std::string("42") == variant.as<std::string>());
} }
TEST(JsonVariant_As_Tests, LongZeroAsDouble) { SECTION("LongZeroAsDouble") {
JsonVariant variant = 0L; JsonVariant variant = 0L;
ASSERT_EQ(0.0, variant.as<double>()); REQUIRE(0.0 == variant.as<double>());
} }
TEST(JsonVariant_As_Tests, NullAsBool) { SECTION("NullAsBool") {
JsonVariant variant = null; JsonVariant variant = null;
ASSERT_FALSE(variant.as<bool>()); REQUIRE_FALSE(variant.as<bool>());
} }
TEST(JsonVariant_As_Tests, NullAsDouble) { SECTION("NullAsDouble") {
JsonVariant variant = null; JsonVariant variant = null;
ASSERT_EQ(0.0, variant.as<double>()); REQUIRE(0.0 == variant.as<double>());
} }
TEST(JsonVariant_As_Tests, NullAsLong) { SECTION("NullAsLong") {
JsonVariant variant = null; JsonVariant variant = null;
ASSERT_EQ(0L, variant.as<long>()); REQUIRE(0L == variant.as<long>());
} }
TEST(JsonVariant_As_Tests, NullAsString) { SECTION("NullAsString") {
JsonVariant variant = null; JsonVariant variant = null;
ASSERT_EQ(std::string("null"), variant.as<std::string>()); REQUIRE(std::string("null") == variant.as<std::string>());
} }
TEST(JsonVariant_As_Tests, NumberStringAsBool) { SECTION("NumberStringAsBool") {
JsonVariant variant = "42"; JsonVariant variant = "42";
ASSERT_TRUE(variant.as<bool>()); REQUIRE(variant.as<bool>());
} }
TEST(JsonVariant_As_Tests, NumberStringAsLong) { SECTION("NumberStringAsLong") {
JsonVariant variant = "42"; JsonVariant variant = "42";
ASSERT_EQ(42L, variant.as<long>()); REQUIRE(42L == variant.as<long>());
} }
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64 #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
TEST(JsonVariant_As_Tests, NumberStringAsInt64Negative) { SECTION("NumberStringAsInt64Negative") {
JsonVariant variant = "-9223372036854775808"; JsonVariant variant = "-9223372036854775808";
ASSERT_EQ(-9223372036854775807 - 1, variant.as<long long>()); REQUIRE(-9223372036854775807 - 1 == variant.as<long long>());
} }
TEST(JsonVariant_As_Tests, NumberStringAsInt64Positive) { SECTION("NumberStringAsInt64Positive") {
JsonVariant variant = "9223372036854775807"; JsonVariant variant = "9223372036854775807";
ASSERT_EQ(9223372036854775807, variant.as<long long>()); REQUIRE(9223372036854775807 == variant.as<long long>());
} }
#endif #endif
TEST(JsonVariant_As_Tests, RandomStringAsBool) { SECTION("RandomStringAsBool") {
JsonVariant variant = "hello"; JsonVariant variant = "hello";
ASSERT_FALSE(variant.as<bool>()); REQUIRE_FALSE(variant.as<bool>());
} }
TEST(JsonVariant_As_Tests, RandomStringAsLong) { SECTION("RandomStringAsLong") {
JsonVariant variant = "hello"; JsonVariant variant = "hello";
ASSERT_EQ(0L, variant.as<long>()); REQUIRE(0L == variant.as<long>());
} }
TEST(JsonVariant_As_Tests, RandomStringAsConstCharPtr) { SECTION("RandomStringAsConstCharPtr") {
JsonVariant variant = "hello"; JsonVariant variant = "hello";
ASSERT_STREQ("hello", variant.as<const char*>()); REQUIRE(std::string("hello") == variant.as<const char*>());
} }
TEST(JsonVariant_As_Tests, RandomStringAsCharPtr) { SECTION("RandomStringAsCharPtr") {
JsonVariant variant = "hello"; JsonVariant variant = "hello";
ASSERT_STREQ("hello", variant.as<char*>()); REQUIRE(std::string("hello") == variant.as<char*>());
} }
TEST(JsonVariant_As_Tests, RandomStringAsString) { SECTION("RandomStringAsString") {
JsonVariant variant = "hello"; JsonVariant variant = "hello";
ASSERT_EQ(std::string("hello"), variant.as<std::string>()); REQUIRE(std::string("hello") == variant.as<std::string>());
} }
TEST(JsonVariant_As_Tests, TrueStringAsBool) { SECTION("TrueStringAsBool") {
JsonVariant variant = "true"; JsonVariant variant = "true";
ASSERT_TRUE(variant.as<bool>()); REQUIRE(variant.as<bool>());
} }
TEST(JsonVariant_As_Tests, TrueStringAsLong) { SECTION("TrueStringAsLong") {
JsonVariant variant = "true"; JsonVariant variant = "true";
ASSERT_EQ(1L, variant.as<long>()); REQUIRE(1L == variant.as<long>());
} }
TEST(JsonVariant_As_Tests, ObjectAsString) { SECTION("ObjectAsString") {
DynamicJsonBuffer buffer; DynamicJsonBuffer buffer;
JsonObject& obj = buffer.createObject(); JsonObject& obj = buffer.createObject();
obj["key"] = "value"; obj["key"] = "value";
JsonVariant variant = obj; JsonVariant variant = obj;
ASSERT_EQ(std::string("{\"key\":\"value\"}"), variant.as<std::string>()); REQUIRE(std::string("{\"key\":\"value\"}") == variant.as<std::string>());
} }
TEST(JsonVariant_As_Tests, ArrayAsString) { SECTION("ArrayAsString") {
DynamicJsonBuffer buffer; DynamicJsonBuffer buffer;
JsonArray& arr = buffer.createArray(); JsonArray& arr = buffer.createArray();
@ -212,23 +212,24 @@ TEST(JsonVariant_As_Tests, ArrayAsString) {
arr.add(2); arr.add(2);
JsonVariant variant = arr; JsonVariant variant = arr;
ASSERT_EQ(std::string("[4,2]"), variant.as<std::string>()); REQUIRE(std::string("[4,2]") == variant.as<std::string>());
} }
TEST(JsonVariant_As_Tests, ArrayAsJsonArray) { SECTION("ArrayAsJsonArray") {
DynamicJsonBuffer buffer; DynamicJsonBuffer buffer;
JsonArray& arr = buffer.createArray(); JsonArray& arr = buffer.createArray();
JsonVariant variant = arr; JsonVariant variant = arr;
ASSERT_EQ(&arr, &variant.as<JsonArray&>()); REQUIRE(&arr == &variant.as<JsonArray&>());
ASSERT_EQ(&arr, &variant.as<JsonArray>()); // <- shorthand REQUIRE(&arr == &variant.as<JsonArray>()); // <- shorthand
} }
TEST(JsonVariant_As_Tests, ObjectAsJsonObject) { SECTION("ObjectAsJsonObject") {
DynamicJsonBuffer buffer; DynamicJsonBuffer buffer;
JsonObject& arr = buffer.createObject(); JsonObject& arr = buffer.createObject();
JsonVariant variant = arr; JsonVariant variant = arr;
ASSERT_EQ(&arr, &variant.as<JsonObject&>()); REQUIRE(&arr == &variant.as<JsonObject&>());
ASSERT_EQ(&arr, &variant.as<JsonObject>()); // <- shorthand REQUIRE(&arr == &variant.as<JsonObject>()); // <- shorthand
}
} }

View File

@ -6,176 +6,182 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class JsonVariant_Comparison_Tests : public ::testing::Test {
protected:
template <typename T> template <typename T>
void testValue(T low, T mid, T high) { void checkEquals(JsonVariant a, T b) {
setValueTo(mid); REQUIRE(b == a);
mustBeEqualTo(mid); REQUIRE(a == b);
mustBeGreaterThan(low); REQUIRE(b <= a);
mustBeLessThan(high); REQUIRE(a <= b);
} REQUIRE(b >= a);
REQUIRE(a >= b);
private: REQUIRE_FALSE(b != a);
template <typename T> REQUIRE_FALSE(a != b);
void setValueTo(T expected) { REQUIRE_FALSE(b > a);
actual = expected; REQUIRE_FALSE(a > b);
REQUIRE_FALSE(b < a);
REQUIRE_FALSE(a < b);
} }
template <typename T> template <typename T>
void mustBeEqualTo(T expected) { void checkGreater(JsonVariant a, T b) {
EXPECT_EQ(expected, actual); // operator== REQUIRE(a > b);
EXPECT_EQ(actual, expected); // operator== REQUIRE(b < a);
EXPECT_LE(expected, actual); // operator<= REQUIRE(a != b);
EXPECT_LE(actual, expected); // operator<= REQUIRE(b != a);
EXPECT_GE(expected, actual); // operator>=
EXPECT_GE(actual, expected); // operator>= REQUIRE_FALSE(a < b);
REQUIRE_FALSE(b > a);
REQUIRE_FALSE(a == b);
REQUIRE_FALSE(b == a);
} }
template <typename T> template <typename T>
void mustBeGreaterThan(T expected) { void checkLower(JsonVariant a, T b) {
EXPECT_GT(actual, expected); // operator> REQUIRE(a < b);
EXPECT_LT(expected, actual); // operator< REQUIRE(b > a);
EXPECT_NE(actual, expected); // operator!= REQUIRE(a != b);
EXPECT_NE(expected, actual); // operator!= REQUIRE(b != a);
REQUIRE_FALSE(a > b);
REQUIRE_FALSE(b < a);
REQUIRE_FALSE(a == b);
REQUIRE_FALSE(b == a);
} }
template <typename T> template <typename T>
void mustBeLessThan(T expected) { void checkComparisons(T low, T mid, T high) {
EXPECT_LT(actual, expected); // operator< checkEquals(mid, mid);
EXPECT_GT(expected, actual); // operator< checkGreater(mid, low);
EXPECT_NE(actual, expected); // operator!= checkLower(mid, high);
EXPECT_NE(expected, actual); // operator!=
} }
JsonVariant actual; TEST_CASE("JsonVariant comparisons") {
}; SECTION("Double") {
checkComparisons<double>(123.44, 123.45, 123.46);
TEST_F(JsonVariant_Comparison_Tests, Double) {
testValue<double>(123.44, 123.45, 123.46);
} }
TEST_F(JsonVariant_Comparison_Tests, Float) { SECTION("Float") {
testValue<float>(123.44f, 123.45f, 123.46f); checkComparisons<float>(123.44f, 123.45f, 123.46f);
} }
TEST_F(JsonVariant_Comparison_Tests, SChar) { SECTION("SChar") {
testValue<signed char>(122, 123, 124); checkComparisons<signed char>(122, 123, 124);
} }
TEST_F(JsonVariant_Comparison_Tests, SInt) { SECTION("SInt") {
testValue<signed int>(122, 123, 124); checkComparisons<signed int>(122, 123, 124);
} }
TEST_F(JsonVariant_Comparison_Tests, SLong) { SECTION("SLong") {
testValue<signed long>(122L, 123L, 124L); checkComparisons<signed long>(122L, 123L, 124L);
} }
TEST_F(JsonVariant_Comparison_Tests, SShort) { SECTION("SShort") {
testValue<signed short>(122, 123, 124); checkComparisons<signed short>(122, 123, 124);
} }
TEST_F(JsonVariant_Comparison_Tests, UChar) { SECTION("UChar") {
testValue<unsigned char>(122, 123, 124); checkComparisons<unsigned char>(122, 123, 124);
} }
TEST_F(JsonVariant_Comparison_Tests, UInt) { SECTION("UInt") {
testValue<unsigned int>(122, 123, 124); checkComparisons<unsigned int>(122, 123, 124);
} }
TEST_F(JsonVariant_Comparison_Tests, ULong) { SECTION("ULong") {
testValue<unsigned long>(122L, 123L, 124L); checkComparisons<unsigned long>(122L, 123L, 124L);
} }
TEST_F(JsonVariant_Comparison_Tests, UShort) { SECTION("UShort") {
testValue<unsigned short>(122, 123, 124); checkComparisons<unsigned short>(122, 123, 124);
} }
TEST_F(JsonVariant_Comparison_Tests, StringLiteral) { SECTION("StringLiteral") {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonVariant variant = jsonBuffer.parse("\"hello\""); JsonVariant variant = jsonBuffer.parse("\"hello\"");
ASSERT_TRUE(variant == "hello"); REQUIRE(variant == "hello");
ASSERT_FALSE(variant != "hello"); REQUIRE_FALSE(variant != "hello");
ASSERT_TRUE(variant != "world"); REQUIRE(variant != "world");
ASSERT_FALSE(variant == "world"); REQUIRE_FALSE(variant == "world");
ASSERT_TRUE("hello" == variant); REQUIRE("hello" == variant);
ASSERT_FALSE("hello" != variant); REQUIRE_FALSE("hello" != variant);
ASSERT_TRUE("world" != variant); REQUIRE("world" != variant);
ASSERT_FALSE("world" == variant); REQUIRE_FALSE("world" == variant);
} }
TEST_F(JsonVariant_Comparison_Tests, String) { SECTION("String") {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonVariant variant = jsonBuffer.parse("\"hello\""); JsonVariant variant = jsonBuffer.parse("\"hello\"");
ASSERT_TRUE(variant == std::string("hello")); REQUIRE(variant == std::string("hello"));
ASSERT_FALSE(variant != std::string("hello")); REQUIRE_FALSE(variant != std::string("hello"));
ASSERT_TRUE(variant != std::string("world")); REQUIRE(variant != std::string("world"));
ASSERT_FALSE(variant == std::string("world")); REQUIRE_FALSE(variant == std::string("world"));
ASSERT_TRUE(std::string("hello") == variant); REQUIRE(std::string("hello") == variant);
ASSERT_FALSE(std::string("hello") != variant); REQUIRE_FALSE(std::string("hello") != variant);
ASSERT_TRUE(std::string("world") != variant); REQUIRE(std::string("world") != variant);
ASSERT_FALSE(std::string("world") == variant); REQUIRE_FALSE(std::string("world") == variant);
} }
TEST_F(JsonVariant_Comparison_Tests, IntegerInVariant) { SECTION("IntegerInVariant") {
JsonVariant variant1 = 42; JsonVariant variant1 = 42;
JsonVariant variant2 = 42; JsonVariant variant2 = 42;
JsonVariant variant3 = 666; JsonVariant variant3 = 666;
ASSERT_TRUE(variant1 == variant2); REQUIRE(variant1 == variant2);
ASSERT_FALSE(variant1 != variant2); REQUIRE_FALSE(variant1 != variant2);
ASSERT_TRUE(variant1 != variant3); REQUIRE(variant1 != variant3);
ASSERT_FALSE(variant1 == variant3); REQUIRE_FALSE(variant1 == variant3);
} }
TEST_F(JsonVariant_Comparison_Tests, StringInVariant) { SECTION("StringInVariant") {
JsonVariant variant1 = "0hello" + 1; // make sure they have JsonVariant variant1 = "0hello" + 1; // make sure they have
JsonVariant variant2 = "1hello" + 1; // different addresses JsonVariant variant2 = "1hello" + 1; // different addresses
JsonVariant variant3 = "world"; JsonVariant variant3 = "world";
ASSERT_TRUE(variant1 == variant2); REQUIRE(variant1 == variant2);
ASSERT_FALSE(variant1 != variant2); REQUIRE_FALSE(variant1 != variant2);
ASSERT_TRUE(variant1 != variant3); REQUIRE(variant1 != variant3);
ASSERT_FALSE(variant1 == variant3); REQUIRE_FALSE(variant1 == variant3);
} }
TEST_F(JsonVariant_Comparison_Tests, DoubleInVariant) { SECTION("DoubleInVariant") {
JsonVariant variant1 = 42.0; JsonVariant variant1 = 42.0;
JsonVariant variant2 = 42.0; JsonVariant variant2 = 42.0;
JsonVariant variant3 = 666.0; JsonVariant variant3 = 666.0;
ASSERT_TRUE(variant1 == variant2); REQUIRE(variant1 == variant2);
ASSERT_FALSE(variant1 != variant2); REQUIRE_FALSE(variant1 != variant2);
ASSERT_TRUE(variant1 != variant3); REQUIRE(variant1 != variant3);
ASSERT_FALSE(variant1 == variant3); REQUIRE_FALSE(variant1 == variant3);
} }
TEST_F(JsonVariant_Comparison_Tests, BoolInVariant) { SECTION("BoolInVariant") {
JsonVariant variant1 = true; JsonVariant variant1 = true;
JsonVariant variant2 = true; JsonVariant variant2 = true;
JsonVariant variant3 = false; JsonVariant variant3 = false;
ASSERT_TRUE(variant1 == variant2); REQUIRE(variant1 == variant2);
ASSERT_FALSE(variant1 != variant2); REQUIRE_FALSE(variant1 != variant2);
ASSERT_TRUE(variant1 != variant3); REQUIRE(variant1 != variant3);
ASSERT_FALSE(variant1 == variant3); REQUIRE_FALSE(variant1 == variant3);
} }
TEST_F(JsonVariant_Comparison_Tests, ArrayInVariant) { SECTION("ArrayInVariant") {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonArray& array1 = jsonBuffer.createArray(); JsonArray& array1 = jsonBuffer.createArray();
JsonArray& array2 = jsonBuffer.createArray(); JsonArray& array2 = jsonBuffer.createArray();
@ -184,14 +190,14 @@ TEST_F(JsonVariant_Comparison_Tests, ArrayInVariant) {
JsonVariant variant2 = array1; JsonVariant variant2 = array1;
JsonVariant variant3 = array2; JsonVariant variant3 = array2;
ASSERT_TRUE(variant1 == variant2); REQUIRE(variant1 == variant2);
ASSERT_FALSE(variant1 != variant2); REQUIRE_FALSE(variant1 != variant2);
ASSERT_TRUE(variant1 != variant3); REQUIRE(variant1 != variant3);
ASSERT_FALSE(variant1 == variant3); REQUIRE_FALSE(variant1 == variant3);
} }
TEST_F(JsonVariant_Comparison_Tests, ObjectInVariant) { SECTION("ObjectInVariant") {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& obj1 = jsonBuffer.createObject(); JsonObject& obj1 = jsonBuffer.createObject();
JsonObject& obj2 = jsonBuffer.createObject(); JsonObject& obj2 = jsonBuffer.createObject();
@ -200,14 +206,14 @@ TEST_F(JsonVariant_Comparison_Tests, ObjectInVariant) {
JsonVariant variant2 = obj1; JsonVariant variant2 = obj1;
JsonVariant variant3 = obj2; JsonVariant variant3 = obj2;
ASSERT_TRUE(variant1 == variant2); REQUIRE(variant1 == variant2);
ASSERT_FALSE(variant1 != variant2); REQUIRE_FALSE(variant1 != variant2);
ASSERT_TRUE(variant1 != variant3); REQUIRE(variant1 != variant3);
ASSERT_FALSE(variant1 == variant3); REQUIRE_FALSE(variant1 == variant3);
} }
TEST_F(JsonVariant_Comparison_Tests, VariantsOfDifferentTypes) { SECTION("VariantsOfDifferentTypes") {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonVariant variants[] = { JsonVariant variants[] = {
true, true,
@ -221,8 +227,9 @@ TEST_F(JsonVariant_Comparison_Tests, VariantsOfDifferentTypes) {
for (size_t i = 0; i < n; i++) { for (size_t i = 0; i < n; i++) {
for (size_t j = i + 1; j < n; j++) { for (size_t j = i + 1; j < n; j++) {
ASSERT_TRUE(variants[i] != variants[j]); REQUIRE(variants[i] != variants[j]);
ASSERT_FALSE(variants[i] == variants[j]); REQUIRE_FALSE(variants[i] == variants[j]);
}
} }
} }
} }

View File

@ -6,63 +6,62 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class JsonVariant_Copy_Tests : public ::testing::Test { TEST_CASE("JsonVariant copy") {
protected:
DynamicJsonBuffer _jsonBuffer; DynamicJsonBuffer _jsonBuffer;
JsonVariant _variant1; JsonVariant _variant1;
JsonVariant _variant2; JsonVariant _variant2;
};
TEST_F(JsonVariant_Copy_Tests, IntegersAreCopiedByValue) { SECTION("IntegersAreCopiedByValue") {
_variant1 = 123; _variant1 = 123;
_variant2 = _variant1; _variant2 = _variant1;
_variant1 = 456; _variant1 = 456;
EXPECT_EQ(123, _variant2.as<int>()); REQUIRE(123 == _variant2.as<int>());
} }
TEST_F(JsonVariant_Copy_Tests, DoublesAreCopiedByValue) { SECTION("DoublesAreCopiedByValue") {
_variant1 = 123.45; _variant1 = 123.45;
_variant2 = _variant1; _variant2 = _variant1;
_variant1 = 456.78; _variant1 = 456.78;
EXPECT_EQ(123.45, _variant2.as<double>()); REQUIRE(123.45 == _variant2.as<double>());
} }
TEST_F(JsonVariant_Copy_Tests, BooleansAreCopiedByValue) { SECTION("BooleansAreCopiedByValue") {
_variant1 = true; _variant1 = true;
_variant2 = _variant1; _variant2 = _variant1;
_variant1 = false; _variant1 = false;
EXPECT_TRUE(_variant2.as<bool>()); REQUIRE(_variant2.as<bool>());
} }
TEST_F(JsonVariant_Copy_Tests, StringsAreCopiedByValue) { SECTION("StringsAreCopiedByValue") {
_variant1 = "hello"; _variant1 = "hello";
_variant2 = _variant1; _variant2 = _variant1;
_variant1 = "world"; _variant1 = "world";
EXPECT_STREQ("hello", _variant2.as<const char *>()); REQUIRE(std::string("hello") == _variant2.as<const char *>());
} }
TEST_F(JsonVariant_Copy_Tests, ObjectsAreCopiedByReference) { SECTION("ObjectsAreCopiedByReference") {
JsonObject &object = _jsonBuffer.createObject(); JsonObject &object = _jsonBuffer.createObject();
_variant1 = object; _variant1 = object;
object["hello"] = "world"; object["hello"] = "world";
EXPECT_EQ(1, _variant1.as<JsonObject>().size()); REQUIRE(1 == _variant1.as<JsonObject>().size());
} }
TEST_F(JsonVariant_Copy_Tests, ArraysAreCopiedByReference) { SECTION("ArraysAreCopiedByReference") {
JsonArray &array = _jsonBuffer.createArray(); JsonArray &array = _jsonBuffer.createArray();
_variant1 = array; _variant1 = array;
array.add("world"); array.add("world");
EXPECT_EQ(1, _variant1.as<JsonArray>().size()); REQUIRE(1 == _variant1.as<JsonArray>().size());
}
} }

View File

@ -6,257 +6,113 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
#define SUITE JsonVariant_Is_Tests void checkIsArray(JsonVariant var) {
REQUIRE(var.is<JsonArray>());
REQUIRE(var.is<JsonArray&>());
REQUIRE(var.is<const JsonArray>());
REQUIRE(var.is<const JsonArray&>());
template <typename TTo, typename TFrom> REQUIRE_FALSE(var.is<bool>());
void assertIsNot(TFrom value) { REQUIRE_FALSE(var.is<double>());
JsonVariant variant = value; REQUIRE_FALSE(var.is<float>());
ASSERT_FALSE(variant.is<TTo>()); REQUIRE_FALSE(var.is<int>());
REQUIRE_FALSE(var.is<long>());
REQUIRE_FALSE(var.is<const char*>());
REQUIRE_FALSE(var.is<JsonObject>());
} }
template <typename TTo> void checkIsBool(JsonVariant var) {
void assertIsNot(JsonArray& value) { REQUIRE(var.is<bool>());
JsonVariant variant = value;
ASSERT_FALSE(variant.is<TTo>()); REQUIRE_FALSE(var.is<double>());
REQUIRE_FALSE(var.is<float>());
REQUIRE_FALSE(var.is<int>());
REQUIRE_FALSE(var.is<long>());
REQUIRE_FALSE(var.is<const char*>());
REQUIRE_FALSE(var.is<JsonArray>());
REQUIRE_FALSE(var.is<JsonObject>());
} }
template <typename TTo, typename TFrom> void checkIsFloat(JsonVariant var) {
void assertIs(TFrom value) { REQUIRE(var.is<double>());
JsonVariant variant = value; REQUIRE(var.is<float>());
ASSERT_TRUE(variant.is<TTo>());
REQUIRE_FALSE(var.is<bool>());
REQUIRE_FALSE(var.is<int>());
REQUIRE_FALSE(var.is<long>());
REQUIRE_FALSE(var.is<const char*>());
REQUIRE_FALSE(var.is<JsonArray>());
REQUIRE_FALSE(var.is<JsonObject>());
} }
template <typename TTo> void checkIsInteger(JsonVariant var) {
void assertIs(JsonArray& value) { REQUIRE(var.is<long>());
JsonVariant variant = value; REQUIRE(var.is<int>());
ASSERT_TRUE(variant.is<TTo>());
REQUIRE_FALSE(var.is<bool>());
REQUIRE_FALSE(var.is<double>());
REQUIRE_FALSE(var.is<float>());
REQUIRE_FALSE(var.is<const char*>());
REQUIRE_FALSE(var.is<JsonArray>());
REQUIRE_FALSE(var.is<JsonObject>());
} }
TEST(SUITE, ArrayIsArray) { void checkIsString(JsonVariant var) {
REQUIRE(var.is<const char*>());
REQUIRE_FALSE(var.is<bool>());
REQUIRE_FALSE(var.is<int>());
REQUIRE_FALSE(var.is<double>());
REQUIRE_FALSE(var.is<float>());
REQUIRE_FALSE(var.is<long>());
REQUIRE_FALSE(var.is<JsonArray>());
REQUIRE_FALSE(var.is<JsonObject>());
}
TEST_CASE("JsonVariant::is()") {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
assertIs<JsonArray&>(jsonBuffer.createArray());
} SECTION("JsonArray") {
TEST(SUITE, ArrayIsNotBool) { checkIsArray(jsonBuffer.createArray());
DynamicJsonBuffer jsonBuffer;
assertIsNot<bool>(jsonBuffer.createArray());
}
TEST(SUITE, ArrayIsNotDouble) {
DynamicJsonBuffer jsonBuffer;
assertIsNot<double>(jsonBuffer.createArray());
}
TEST(SUITE, ArrayIsNotFloat) {
DynamicJsonBuffer jsonBuffer;
assertIsNot<float>(jsonBuffer.createArray());
}
TEST(SUITE, ArrayIsNotInt) {
DynamicJsonBuffer jsonBuffer;
assertIsNot<int>(jsonBuffer.createArray());
}
TEST(SUITE, ArrayIsNotLong) {
DynamicJsonBuffer jsonBuffer;
assertIsNot<long>(jsonBuffer.createArray());
}
TEST(SUITE, ArrayIsNotString) {
DynamicJsonBuffer jsonBuffer;
assertIsNot<const char*>(jsonBuffer.createArray());
} }
TEST(SUITE, BoolIsArray) { SECTION("bool") {
assertIsNot<JsonArray&>(true); checkIsBool(true);
} checkIsBool(false);
TEST(SUITE, BoolIsBool) {
assertIs<bool>(true);
}
TEST(SUITE, BoolIsDouble) {
assertIsNot<double>(true);
}
TEST(SUITE, BoolIsFloat) {
assertIsNot<float>(true);
}
TEST(SUITE, BoolIsInt) {
assertIsNot<int>(true);
}
TEST(SUITE, BoolIsLong) {
assertIsNot<long>(true);
}
TEST(SUITE, BoolIsString) {
assertIsNot<const char*>(true);
} }
TEST(SUITE, DoubleIsArray) { SECTION("double") {
assertIsNot<JsonArray&>(4.2); checkIsFloat(4.2);
}
TEST(SUITE, DoubleIsBool) {
assertIsNot<bool>(4.2);
}
TEST(SUITE, DoubleIsDouble) {
assertIs<double>(4.2);
}
TEST(SUITE, DoubleIsFloat) {
assertIs<float>(4.2);
}
TEST(SUITE, DoubleIsInt) {
assertIsNot<int>(4.2);
}
TEST(SUITE, DoubleIsLong) {
assertIsNot<long>(4.2);
}
TEST(SUITE, DoubleIsString) {
assertIsNot<const char*>(4.2);
} }
TEST(SUITE, LongIsArray) { SECTION("int") {
assertIsNot<JsonArray&>(42L); checkIsInteger(42);
}
TEST(SUITE, LongIsBool) {
assertIsNot<bool>(42L);
}
TEST(SUITE, LongIsDouble) {
assertIsNot<double>(42L);
}
TEST(SUITE, LongIsFloat) {
assertIsNot<float>(42L);
}
TEST(SUITE, LongIsInt) {
assertIs<int>(42L);
}
TEST(SUITE, LongIsLong) {
assertIs<long>(42L);
}
TEST(SUITE, LongIsString) {
assertIsNot<const char*>(42L);
} }
TEST(SUITE, StringIsArray) { SECTION("long") {
assertIsNot<JsonArray&>("42"); checkIsInteger(42L);
}
TEST(SUITE, StringIsBool) {
assertIsNot<bool>("42");
}
TEST(SUITE, StringIsDouble) {
assertIsNot<double>("42");
}
TEST(SUITE, StringIsFloat) {
assertIsNot<float>("42");
}
TEST(SUITE, StringIsInt) {
assertIsNot<int>("42");
}
TEST(SUITE, StringIsLong) {
assertIsNot<long>("42");
}
TEST(SUITE, StringIsString) {
assertIs<const char*>("42");
} }
TEST(SUITE, UnparsedTrueIsArray) { SECTION("string") {
assertIsNot<JsonArray&>(RawJson("true")); checkIsString("42");
}
TEST(SUITE, UnparsedTrueIsBool) {
assertIs<bool>(RawJson("true"));
}
TEST(SUITE, UnparsedTrueIsDouble) {
assertIsNot<double>(RawJson("true"));
}
TEST(SUITE, UnparsedTrueIsFloat) {
assertIsNot<float>(RawJson("true"));
}
TEST(SUITE, UnparsedTrueIsInt) {
assertIsNot<int>(RawJson("true"));
}
TEST(SUITE, UnparsedTrueIsLong) {
assertIsNot<long>(RawJson("true"));
}
TEST(SUITE, UnparsedTrueIsString) {
assertIsNot<const char*>(RawJson("true"));
} }
TEST(SUITE, UnparsedFalseIsArray) { SECTION("unparsed bool") {
assertIsNot<JsonArray&>(RawJson("false")); checkIsBool(RawJson("true"));
} checkIsBool(RawJson("false"));
TEST(SUITE, UnparsedFalseIsBool) {
assertIs<bool>(RawJson("false"));
}
TEST(SUITE, UnparsedFalseIsDouble) {
assertIsNot<double>(RawJson("false"));
}
TEST(SUITE, UnparsedFalseIsFloat) {
assertIsNot<float>(RawJson("false"));
}
TEST(SUITE, UnparsedFalseIsInt) {
assertIsNot<int>(RawJson("false"));
}
TEST(SUITE, UnparsedFalseIsLong) {
assertIsNot<long>(RawJson("false"));
}
TEST(SUITE, UnparsedFalseIsString) {
assertIsNot<const char*>(RawJson("false"));
} }
TEST(SUITE, UnparsedIntIsArray) { SECTION("unparsed int") {
assertIsNot<JsonArray&>(RawJson("42")); checkIsInteger(RawJson("42"));
}
TEST(SUITE, UnparsedIntIsBool) {
assertIsNot<bool>(RawJson("42"));
}
TEST(SUITE, UnparsedIntIsDouble) {
assertIsNot<double>(RawJson("42"));
}
TEST(SUITE, UnparsedIntIsFloat) {
assertIsNot<float>(RawJson("42"));
}
TEST(SUITE, UnparsedIntIsInt) {
assertIs<int>(RawJson("42"));
}
TEST(SUITE, UnparsedIntIsLong) {
assertIs<long>(RawJson("42"));
}
TEST(SUITE, UnparsedIntIsString) {
assertIsNot<const char*>(RawJson("42"));
} }
TEST(SUITE, UnparsedFloatIsBool) { SECTION("unparsed float") {
assertIsNot<bool>(RawJson("4.2e-10")); checkIsFloat(RawJson("4.2e-10"));
}
TEST(SUITE, UnparsedFloatIsDouble) {
assertIs<double>(RawJson("4.2e-10"));
}
TEST(SUITE, UnparsedFloatIsFloat) {
assertIs<float>(RawJson("4.2e-10"));
}
TEST(SUITE, UnparsedFloatIsInt) {
assertIsNot<int>(RawJson("4.2e-10"));
}
TEST(SUITE, UnparsedFloatIsLong) {
assertIsNot<long>(RawJson("4.2e-10"));
}
TEST(SUITE, UnparsedFloatIsStr) {
assertIsNot<const char*>(RawJson("4.2"));
} }
TEST(SUITE, UnparsedNullIsArray) { SECTION("unparsed null") {
assertIsNot<JsonArray&>(RawJson("null")); checkIsString(RawJson("null"));
} }
TEST(SUITE, UnparsedNullIsBool) {
assertIsNot<bool>(RawJson("null"));
}
TEST(SUITE, UnparsedNullIsDouble) {
assertIsNot<double>(RawJson("null"));
}
TEST(SUITE, UnparsedNullIsFloat) {
assertIsNot<float>(RawJson("null"));
}
TEST(SUITE, UnparsedNullIsInt) {
assertIsNot<int>(RawJson("null"));
}
TEST(SUITE, UnparsedNullIsLong) {
assertIsNot<long>(RawJson("null"));
}
TEST(SUITE, UnparsedNullIsConstCharPtr) {
assertIs<const char*>(RawJson("null"));
}
TEST(SUITE, UnparsedNullIsCharPtr) {
assertIs<char*>(RawJson("null"));
} }

View File

@ -6,128 +6,104 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
#include <limits> #include <limits>
class JsonVariant_PrintTo_Tests : public testing::Test { void check(JsonVariant variant, const std::string &expected) {
protected:
JsonVariant variant;
void outputMustBe(const char *expected) {
char buffer[256] = ""; char buffer[256] = "";
size_t n = variant.printTo(buffer, sizeof(buffer)); size_t returnValue = variant.printTo(buffer, sizeof(buffer));
ASSERT_STREQ(expected, buffer); REQUIRE(expected == buffer);
ASSERT_EQ(strlen(expected), n); REQUIRE(expected.size() == returnValue);
}
};
TEST_F(JsonVariant_PrintTo_Tests, Empty) {
outputMustBe("");
} }
TEST_F(JsonVariant_PrintTo_Tests, Null) { TEST_CASE("JsonVariant::printTo()") {
variant = static_cast<char *>(0); SECTION("Empty") {
outputMustBe("null"); check(JsonVariant(), "");
} }
TEST_F(JsonVariant_PrintTo_Tests, String) { SECTION("Null") {
variant = "hello"; check(static_cast<char *>(0), "null");
outputMustBe("\"hello\"");
} }
TEST_F(JsonVariant_PrintTo_Tests, DoubleZero) { SECTION("String") {
variant = 0.0; check("hello", "\"hello\"");
outputMustBe("0.00");
} }
TEST_F(JsonVariant_PrintTo_Tests, DoubleDefaultDigits) { SECTION("DoubleZero") {
variant = 3.14159265358979323846; check(0.0, "0.00");
outputMustBe("3.14");
} }
TEST_F(JsonVariant_PrintTo_Tests, DoubleFourDigits) { SECTION("DoubleDefaultDigits") {
variant = JsonVariant(3.14159265358979323846, 4); check(3.14159265358979323846, "3.14");
outputMustBe("3.1416");
} }
TEST_F(JsonVariant_PrintTo_Tests, Infinity) { SECTION("DoubleFourDigits") {
variant = std::numeric_limits<double>::infinity(); check(JsonVariant(3.14159265358979323846, 4), "3.1416");
outputMustBe("Infinity");
} }
TEST_F(JsonVariant_PrintTo_Tests, MinusInfinity) { SECTION("Infinity") {
variant = -std::numeric_limits<double>::infinity(); check(std::numeric_limits<double>::infinity(), "Infinity");
outputMustBe("-Infinity");
} }
TEST_F(JsonVariant_PrintTo_Tests, SignalingNaN) { SECTION("MinusInfinity") {
variant = std::numeric_limits<double>::signaling_NaN(); check(-std::numeric_limits<double>::infinity(), "-Infinity");
outputMustBe("NaN");
} }
TEST_F(JsonVariant_PrintTo_Tests, QuietNaN) { SECTION("SignalingNaN") {
variant = std::numeric_limits<double>::quiet_NaN(); check(std::numeric_limits<double>::signaling_NaN(), "NaN");
outputMustBe("NaN");
} }
TEST_F(JsonVariant_PrintTo_Tests, VeryBigPositiveDouble) { SECTION("QuietNaN") {
variant = JsonVariant(3.14159265358979323846e42, 4); check(std::numeric_limits<double>::quiet_NaN(), "NaN");
outputMustBe("3.1416e42");
} }
TEST_F(JsonVariant_PrintTo_Tests, VeryBigNegativeDouble) { SECTION("VeryBigPositiveDouble") {
variant = JsonVariant(-3.14159265358979323846e42, 4); check(JsonVariant(3.14159265358979323846e42, 4), "3.1416e42");
outputMustBe("-3.1416e42");
} }
TEST_F(JsonVariant_PrintTo_Tests, VerySmallPositiveDouble) { SECTION("VeryBigNegativeDouble") {
variant = JsonVariant(3.14159265358979323846e-42, 4); check(JsonVariant(-3.14159265358979323846e42, 4), "-3.1416e42");
outputMustBe("3.1416e-42");
} }
TEST_F(JsonVariant_PrintTo_Tests, VerySmallNegativeDouble) { SECTION("VerySmallPositiveDouble") {
variant = JsonVariant(-3.14159265358979323846e-42, 4); check(JsonVariant(3.14159265358979323846e-42, 4), "3.1416e-42");
outputMustBe("-3.1416e-42");
} }
TEST_F(JsonVariant_PrintTo_Tests, Integer) { SECTION("VerySmallNegativeDouble") {
variant = 42; check(JsonVariant(-3.14159265358979323846e-42, 4), "-3.1416e-42");
outputMustBe("42");
} }
TEST_F(JsonVariant_PrintTo_Tests, NegativeLong) { SECTION("Integer") {
variant = -42; check(42, "42");
outputMustBe("-42");
} }
TEST_F(JsonVariant_PrintTo_Tests, UnsignedLong) { SECTION("NegativeLong") {
variant = 4294967295UL; check(-42, "-42");
outputMustBe("4294967295");
} }
TEST_F(JsonVariant_PrintTo_Tests, True) { SECTION("UnsignedLong") {
variant = true; check(4294967295UL, "4294967295");
outputMustBe("true");
} }
TEST_F(JsonVariant_PrintTo_Tests, OneFalse) { SECTION("True") {
variant = false; check(true, "true");
outputMustBe("false"); }
SECTION("OneFalse") {
check(false, "false");
} }
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64 #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
TEST_F(JsonVariant_PrintTo_Tests, NegativeInt64) { SECTION("NegativeInt64") {
variant = -9223372036854775807 - 1; check(-9223372036854775807 - 1, "-9223372036854775808");
outputMustBe("-9223372036854775808");
} }
TEST_F(JsonVariant_PrintTo_Tests, PositiveInt64) { SECTION("PositiveInt64") {
variant = 9223372036854775807; check(9223372036854775807, "9223372036854775807");
outputMustBe("9223372036854775807");
} }
TEST_F(JsonVariant_PrintTo_Tests, UInt64) { SECTION("UInt64") {
variant = 18446744073709551615; check(18446744073709551615, "18446744073709551615");
outputMustBe("18446744073709551615");
} }
#endif #endif
}

View File

@ -6,129 +6,128 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h>
#include <stdint.h> #include <stdint.h>
#include <catch.hpp>
#include <limits> #include <limits>
class JsonVariant_Storage_Tests : public ::testing::Test {
protected:
template <typename T> template <typename T>
void testValue(T expected) { void checkValue(T expected) {
JsonVariant variant = expected; JsonVariant variant = expected;
EXPECT_EQ(expected, variant.as<T>()); REQUIRE(expected == variant.as<T>());
} }
template <typename T> template <typename T>
void testReference(T &expected) { void checkReference(T &expected) {
JsonVariant variant = expected; JsonVariant variant = expected;
EXPECT_EQ(expected, variant.as<T &>()); REQUIRE(expected == variant.as<T &>());
} }
template <typename T> template <typename T>
void testNumericType() { void checkNumericType() {
T min = std::numeric_limits<T>::min(); T min = std::numeric_limits<T>::min();
T max = std::numeric_limits<T>::max(); T max = std::numeric_limits<T>::max();
JsonVariant variantMin(min); JsonVariant variantMin(min);
JsonVariant variantMax(max); JsonVariant variantMax(max);
EXPECT_EQ(min, variantMin.as<T>()); REQUIRE(min == variantMin.as<T>());
EXPECT_EQ(max, variantMax.as<T>()); REQUIRE(max == variantMax.as<T>());
} }
};
TEST_CASE("JsonVariant set()/get()") {
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64 #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
TEST_F(JsonVariant_Storage_Tests, SizeOfJsonInteger) { SECTION("SizeOfJsonInteger") {
ASSERT_EQ(8, sizeof(Internals::JsonInteger)); REQUIRE(8 == sizeof(Internals::JsonInteger));
} }
#endif #endif
TEST_F(JsonVariant_Storage_Tests, Null) { SECTION("Null") {
testValue<const char *>(NULL); checkValue<const char *>(NULL);
} }
TEST_F(JsonVariant_Storage_Tests, String) { SECTION("String") {
testValue<const char *>("hello"); checkValue<const char *>("hello");
} }
TEST_F(JsonVariant_Storage_Tests, False) { SECTION("False") {
testValue<bool>(false); checkValue<bool>(false);
} }
TEST_F(JsonVariant_Storage_Tests, True) { SECTION("True") {
testValue<bool>(true); checkValue<bool>(true);
} }
TEST_F(JsonVariant_Storage_Tests, Double) { SECTION("Double") {
testNumericType<double>(); checkNumericType<double>();
} }
TEST_F(JsonVariant_Storage_Tests, Float) { SECTION("Float") {
testNumericType<float>(); checkNumericType<float>();
} }
TEST_F(JsonVariant_Storage_Tests, Char) { SECTION("Char") {
testNumericType<char>(); checkNumericType<char>();
} }
TEST_F(JsonVariant_Storage_Tests, SChar) { SECTION("SChar") {
testNumericType<signed char>(); checkNumericType<signed char>();
} }
TEST_F(JsonVariant_Storage_Tests, SInt) { SECTION("SInt") {
testNumericType<signed int>(); checkNumericType<signed int>();
} }
TEST_F(JsonVariant_Storage_Tests, SLong) { SECTION("SLong") {
testNumericType<signed long>(); checkNumericType<signed long>();
} }
TEST_F(JsonVariant_Storage_Tests, SShort) { SECTION("SShort") {
testNumericType<signed short>(); checkNumericType<signed short>();
} }
TEST_F(JsonVariant_Storage_Tests, UChar) { SECTION("UChar") {
testNumericType<unsigned char>(); checkNumericType<unsigned char>();
} }
TEST_F(JsonVariant_Storage_Tests, UInt) { SECTION("UInt") {
testNumericType<unsigned int>(); checkNumericType<unsigned int>();
} }
TEST_F(JsonVariant_Storage_Tests, ULong) { SECTION("ULong") {
testNumericType<unsigned long>(); checkNumericType<unsigned long>();
} }
TEST_F(JsonVariant_Storage_Tests, UShort) { SECTION("UShort") {
testNumericType<unsigned short>(); checkNumericType<unsigned short>();
} }
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64 #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
TEST_F(JsonVariant_Storage_Tests, LongLong) { SECTION("LongLong") {
testNumericType<unsigned long long>(); checkNumericType<unsigned long long>();
} }
TEST_F(JsonVariant_Storage_Tests, ULongLong) { SECTION("ULongLong") {
testNumericType<unsigned long long>(); checkNumericType<unsigned long long>();
} }
#endif #endif
TEST_F(JsonVariant_Storage_Tests, Int8) { SECTION("Int8") {
testNumericType<int8_t>(); checkNumericType<int8_t>();
} }
TEST_F(JsonVariant_Storage_Tests, Uint8) { SECTION("Uint8") {
testNumericType<uint8_t>(); checkNumericType<uint8_t>();
} }
TEST_F(JsonVariant_Storage_Tests, Int16) { SECTION("Int16") {
testNumericType<int16_t>(); checkNumericType<int16_t>();
} }
TEST_F(JsonVariant_Storage_Tests, Uint16) { SECTION("Uint16") {
testNumericType<uint16_t>(); checkNumericType<uint16_t>();
} }
TEST_F(JsonVariant_Storage_Tests, Int32) { SECTION("Int32") {
testNumericType<int32_t>(); checkNumericType<int32_t>();
} }
TEST_F(JsonVariant_Storage_Tests, Uint32) { SECTION("Uint32") {
testNumericType<uint32_t>(); checkNumericType<uint32_t>();
} }
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64 #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
TEST_F(JsonVariant_Storage_Tests, Int64) { SECTION("Int64") {
testNumericType<int64_t>(); checkNumericType<int64_t>();
} }
TEST_F(JsonVariant_Storage_Tests, Uint64) { SECTION("Uint64") {
testNumericType<uint64_t>(); checkNumericType<uint64_t>();
} }
#endif #endif
TEST_F(JsonVariant_Storage_Tests, CanStoreObject) { SECTION("CanStoreObject") {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject &object = jsonBuffer.createObject(); JsonObject &object = jsonBuffer.createObject();
testReference(object); checkReference(object);
}
} }

View File

@ -6,77 +6,75 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class JsonVariant_Subscript_Tests : public ::testing::Test { TEST_CASE("JsonVariant::operator[]") {
protected:
DynamicJsonBuffer _jsonBuffer; DynamicJsonBuffer _jsonBuffer;
JsonVariant _variant;
};
TEST_F(JsonVariant_Subscript_Tests, Array) { SECTION("Array") {
JsonArray &array = _jsonBuffer.createArray(); JsonArray &array = _jsonBuffer.createArray();
array.add("element at index 0"); array.add("element at index 0");
array.add("element at index 1"); array.add("element at index 1");
_variant = array; JsonVariant var = array;
EXPECT_EQ(2, _variant.size()); REQUIRE(2 == var.size());
EXPECT_STREQ("element at index 0", _variant[0]); REQUIRE(std::string("element at index 0") == var[0]);
EXPECT_STREQ("element at index 1", _variant[1]); REQUIRE(std::string("element at index 1") == var[1]);
EXPECT_STREQ("element at index 0", REQUIRE(std::string("element at index 0") ==
_variant[static_cast<unsigned char>(0)]); // issue #381 var[static_cast<unsigned char>(0)]); // issue #381
EXPECT_FALSE(_variant[-1].success()); REQUIRE_FALSE(var[-1].success());
EXPECT_FALSE(_variant[3].success()); REQUIRE_FALSE(var[3].success());
EXPECT_FALSE(_variant["0"].success()); REQUIRE_FALSE(var["0"].success());
} }
TEST_F(JsonVariant_Subscript_Tests, Object) { SECTION("Object") {
JsonObject &object = _jsonBuffer.createObject(); JsonObject &object = _jsonBuffer.createObject();
object["a"] = "element at key \"a\""; object["a"] = "element at key \"a\"";
object["b"] = "element at key \"b\""; object["b"] = "element at key \"b\"";
_variant = object; JsonVariant var = object;
EXPECT_EQ(2, _variant.size()); REQUIRE(2 == var.size());
EXPECT_STREQ("element at key \"a\"", _variant["a"]); REQUIRE(std::string("element at key \"a\"") == var["a"]);
EXPECT_STREQ("element at key \"b\"", _variant["b"]); REQUIRE(std::string("element at key \"b\"") == var["b"]);
EXPECT_FALSE(_variant["c"].success()); REQUIRE_FALSE(var["c"].success());
EXPECT_FALSE(_variant[0].success()); REQUIRE_FALSE(var[0].success());
} }
TEST_F(JsonVariant_Subscript_Tests, Undefined) { SECTION("Undefined") {
_variant = JsonVariant(); JsonVariant var = JsonVariant();
EXPECT_EQ(0, _variant.size()); REQUIRE(0 == var.size());
EXPECT_FALSE(_variant["0"].success()); REQUIRE_FALSE(var["0"].success());
EXPECT_FALSE(_variant[0].success()); REQUIRE_FALSE(var[0].success());
} }
TEST_F(JsonVariant_Subscript_Tests, String) { SECTION("String") {
_variant = "hello world"; JsonVariant var = "hello world";
EXPECT_EQ(0, _variant.size()); REQUIRE(0 == var.size());
EXPECT_FALSE(_variant["0"].success()); REQUIRE_FALSE(var["0"].success());
EXPECT_FALSE(_variant[0].success()); REQUIRE_FALSE(var[0].success());
} }
TEST_F(JsonVariant_Subscript_Tests, ObjectSetValue) { SECTION("ObjectSetValue") {
_variant = _jsonBuffer.createObject(); JsonVariant var = _jsonBuffer.createObject();
_variant["hello"] = "world"; var["hello"] = "world";
EXPECT_EQ(1, _variant.size()); REQUIRE(1 == var.size());
EXPECT_STREQ("world", _variant["hello"]); REQUIRE(std::string("world") == var["hello"]);
} }
TEST_F(JsonVariant_Subscript_Tests, ArraySetValue) { SECTION("ArraySetValue") {
_variant = _jsonBuffer.parseArray("[\"hello\"]"); JsonVariant var = _jsonBuffer.parseArray("[\"hello\"]");
_variant[0] = "world"; var[0] = "world";
EXPECT_EQ(1, _variant.size()); REQUIRE(1 == var.size());
EXPECT_STREQ("world", _variant[0]); REQUIRE(std::string("world") == var[0]);
} }
TEST_F(JsonVariant_Subscript_Tests, NestedObjectSetValue) { SECTION("NestedObjectSetValue") {
_variant = _jsonBuffer.parseArray("[{}]"); JsonVariant var = _jsonBuffer.parseArray("[{}]");
_variant[0]["hello"] = "world"; var[0]["hello"] = "world";
EXPECT_EQ(1, _variant.size()); REQUIRE(1 == var.size());
EXPECT_EQ(1, _variant[0].size()); REQUIRE(1 == var[0].size());
EXPECT_STREQ("world", _variant[0]["hello"]); REQUIRE(std::string("world") == var[0]["hello"]);
}
} }

View File

@ -6,38 +6,40 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
TEST(JsonVariant_Success_Tests, ReturnsFalse_WhenUndefined) { TEST_CASE("JsonVariant::success()") {
SECTION("ReturnsFalse_WhenUndefined") {
JsonVariant variant; JsonVariant variant;
EXPECT_FALSE(variant.success()); REQUIRE(false == variant.success());
} }
TEST(JsonVariant_Success_Tests, ReturnsTrue_WhenInteger) { SECTION("ReturnsTrue_WhenInteger") {
JsonVariant variant = 0; JsonVariant variant = 0;
EXPECT_TRUE(variant.success()); REQUIRE(true == variant.success());
} }
TEST(JsonVariant_Success_Tests, ReturnsTrue_WhenEmptyArray) { SECTION("ReturnsTrue_WhenEmptyArray") {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonVariant variant = jsonBuffer.createArray(); JsonVariant variant = jsonBuffer.createArray();
EXPECT_TRUE(variant.success()); REQUIRE(true == variant.success());
} }
TEST(JsonVariant_Success_Tests, ReturnsTrue_WhenEmptyObject) { SECTION("ReturnsTrue_WhenEmptyObject") {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonVariant variant = jsonBuffer.createObject(); JsonVariant variant = jsonBuffer.createObject();
EXPECT_TRUE(variant.success()); REQUIRE(true == variant.success());
} }
TEST(JsonVariant_Success_Tests, ReturnsFalse_WhenInvalidArray) { SECTION("ReturnsFalse_WhenInvalidArray") {
JsonVariant variant = JsonArray::invalid(); JsonVariant variant = JsonArray::invalid();
EXPECT_FALSE(variant.success()); REQUIRE(false == variant.success());
} }
TEST(JsonVariant_Success_Tests, ReturnsFalse_WhenInvalidObject) { SECTION("ReturnsFalse_WhenInvalidObject") {
JsonVariant variant = JsonObject::invalid(); JsonVariant variant = JsonObject::invalid();
EXPECT_FALSE(variant.success()); REQUIRE(false == variant.success());
}
} }

View File

@ -6,53 +6,52 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class JsonVariant_Undefined_Tests : public ::testing::Test { TEST_CASE("JsonVariant undefined") {
protected:
JsonVariant variant; JsonVariant variant;
};
TEST_F(JsonVariant_Undefined_Tests, AsLongReturns0) { SECTION("AsLongReturns0") {
EXPECT_EQ(0, variant.as<long>()); REQUIRE(0 == variant.as<long>());
} }
TEST_F(JsonVariant_Undefined_Tests, AsUnsignedReturns0) { SECTION("AsUnsignedReturns0") {
EXPECT_EQ(0, variant.as<unsigned>()); REQUIRE(0 == variant.as<unsigned>());
} }
TEST_F(JsonVariant_Undefined_Tests, AsStringReturnsNull) { SECTION("AsStringReturnsNull") {
EXPECT_EQ(0, variant.as<char*>()); REQUIRE(0 == variant.as<char*>());
} }
TEST_F(JsonVariant_Undefined_Tests, AsDoubleReturns0) { SECTION("AsDoubleReturns0") {
EXPECT_EQ(0, variant.as<double>()); REQUIRE(0 == variant.as<double>());
} }
TEST_F(JsonVariant_Undefined_Tests, AsBoolReturnsFalse) { SECTION("AsBoolReturnsFalse") {
EXPECT_FALSE(variant.as<bool>()); REQUIRE(false == variant.as<bool>());
} }
TEST_F(JsonVariant_Undefined_Tests, AsArrayReturnInvalid) { SECTION("AsArrayReturnInvalid") {
EXPECT_EQ(JsonArray::invalid(), variant.as<JsonArray&>()); REQUIRE(JsonArray::invalid() == variant.as<JsonArray&>());
} }
TEST_F(JsonVariant_Undefined_Tests, AsConstArrayReturnInvalid) { SECTION("AsConstArrayReturnInvalid") {
EXPECT_EQ(JsonArray::invalid(), variant.as<const JsonArray&>()); REQUIRE(JsonArray::invalid() == variant.as<const JsonArray&>());
} }
TEST_F(JsonVariant_Undefined_Tests, AsObjectReturnInvalid) { SECTION("AsObjectReturnInvalid") {
EXPECT_EQ(JsonObject::invalid(), variant.as<JsonObject&>()); REQUIRE(JsonObject::invalid() == variant.as<JsonObject&>());
} }
TEST_F(JsonVariant_Undefined_Tests, AsConstObjectReturnInvalid) { SECTION("AsConstObjectReturnInvalid") {
EXPECT_EQ(JsonObject::invalid(), variant.as<const JsonObject&>()); REQUIRE(JsonObject::invalid() == variant.as<const JsonObject&>());
} }
TEST_F(JsonVariant_Undefined_Tests, AsArrayWrapperReturnInvalid) { SECTION("AsArrayWrapperReturnInvalid") {
EXPECT_EQ(JsonArray::invalid(), variant.as<JsonArray>()); REQUIRE(JsonArray::invalid() == variant.as<JsonArray>());
} }
TEST_F(JsonVariant_Undefined_Tests, AsObjectWrapperReturnInvalid) { SECTION("AsObjectWrapperReturnInvalid") {
EXPECT_EQ(JsonObject::invalid(), variant.as<JsonObject>()); REQUIRE(JsonObject::invalid() == variant.as<JsonObject>());
}
} }

View File

@ -10,5 +10,5 @@ add_executable(JsonWriterTests
writeString.cpp writeString.cpp
) )
target_link_libraries(JsonWriterTests gtest) target_link_libraries(JsonWriterTests catch)
add_test(JsonWriter JsonWriterTests) add_test(JsonWriter JsonWriterTests)

View File

@ -5,109 +5,86 @@
// https://bblanchon.github.io/ArduinoJson/ // https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star! // If you like this project, please add a star!
#include <gtest/gtest.h> #include <catch.hpp>
#include <limits> #include <limits>
#include <string>
#include <ArduinoJson/Serialization/JsonWriter.hpp> #include <ArduinoJson/Serialization/JsonWriter.hpp>
#include <ArduinoJson/Serialization/StaticStringBuilder.hpp> #include <ArduinoJson/Serialization/StaticStringBuilder.hpp>
using namespace ArduinoJson::Internals; using namespace ArduinoJson::Internals;
class JsonWriter_WriteFloat_Tests : public testing::Test { void check(const std::string& expected, double input, uint8_t digits = 2) {
protected: char output[1024];
void whenInputIs(double input, uint8_t digits = 2) { StaticStringBuilder sb(output, sizeof(output));
StaticStringBuilder sb(buffer, sizeof(buffer));
JsonWriter writer(sb); JsonWriter writer(sb);
writer.writeFloat(input, digits); writer.writeFloat(input, digits);
returnValue = writer.bytesWritten(); REQUIRE(output == expected);
REQUIRE(writer.bytesWritten() == expected.size());
} }
void outputMustBe(const char *expected) { TEST_CASE("JsonWriter::writeFloat()") {
EXPECT_STREQ(expected, buffer); SECTION("NaN") {
EXPECT_EQ(strlen(expected), returnValue); check("NaN", std::numeric_limits<double>::signaling_NaN());
} }
private: SECTION("PositiveInfinity") {
char buffer[1024]; check("Infinity", std::numeric_limits<double>::infinity());
size_t returnValue;
};
TEST_F(JsonWriter_WriteFloat_Tests, NaN) {
whenInputIs(std::numeric_limits<double>::signaling_NaN());
outputMustBe("NaN");
} }
TEST_F(JsonWriter_WriteFloat_Tests, PositiveInfinity) { SECTION("NegativeInfinity") {
whenInputIs(std::numeric_limits<double>::infinity()); check("-Infinity", -std::numeric_limits<double>::infinity());
outputMustBe("Infinity");
} }
TEST_F(JsonWriter_WriteFloat_Tests, NegativeInfinity) { SECTION("Zero") {
whenInputIs(-std::numeric_limits<double>::infinity()); check("0.00", 0);
outputMustBe("-Infinity");
} }
TEST_F(JsonWriter_WriteFloat_Tests, Zero) { SECTION("ZeroDigits_Rounding") {
whenInputIs(0); check("10", 9.5, 0);
outputMustBe("0.00");
} }
TEST_F(JsonWriter_WriteFloat_Tests, ZeroDigits_Rounding) { SECTION("ZeroDigits_NoRounding") {
whenInputIs(9.5, 0); check("9", 9.4, 0);
outputMustBe("10");
} }
TEST_F(JsonWriter_WriteFloat_Tests, ZeroDigits_NoRounding) { SECTION("OneDigit_Rounding") {
whenInputIs(9.4, 0); check("10.0", 9.95, 1);
outputMustBe("9");
} }
TEST_F(JsonWriter_WriteFloat_Tests, OneDigit_Rounding) { SECTION("OneDigit_NoRounding") {
whenInputIs(9.95, 1); check("9.9", 9.94, 1);
outputMustBe("10.0");
} }
TEST_F(JsonWriter_WriteFloat_Tests, OneDigit_NoRounding) { SECTION("TwoDigits_Rounding") {
whenInputIs(9.94, 1); check("10.00", 9.995, 2);
outputMustBe("9.9");
} }
TEST_F(JsonWriter_WriteFloat_Tests, TwoDigits_Rounding) { SECTION("TwoDigits_NoRounding") {
whenInputIs(9.995, 2); check("9.99", 9.994, 2);
outputMustBe("10.00");
} }
TEST_F(JsonWriter_WriteFloat_Tests, TwoDigits_NoRounding) { SECTION("ThreeDigits_Rounding") {
whenInputIs(9.994, 2); check("10.000", 9.9995, 3);
outputMustBe("9.99");
} }
TEST_F(JsonWriter_WriteFloat_Tests, ThreeDigits_Rounding) { SECTION("ThreeDigits_NoRounding") {
whenInputIs(9.9995, 3); check("9.999", 9.9994, 3);
outputMustBe("10.000");
} }
TEST_F(JsonWriter_WriteFloat_Tests, ThreeDigits_NoRounding) { SECTION("FourDigits_Rounding") {
whenInputIs(9.9994, 3); check("10.0000", 9.99995, 4);
outputMustBe("9.999");
} }
TEST_F(JsonWriter_WriteFloat_Tests, FourDigits_Rounding) { SECTION("FourDigits_NoRounding") {
whenInputIs(9.99995, 4); check("9.9999", 9.99994, 4);
outputMustBe("10.0000");
} }
TEST_F(JsonWriter_WriteFloat_Tests, FourDigits_NoRounding) { SECTION("FiveDigits_Rounding") {
whenInputIs(9.99994, 4); check("10.00000", 9.999995, 5);
outputMustBe("9.9999");
} }
TEST_F(JsonWriter_WriteFloat_Tests, FiveDigits_Rounding) { SECTION("FiveDigits_NoRounding") {
whenInputIs(9.999995, 5); check("9.99999", 9.999994, 5);
outputMustBe("10.00000");
} }
TEST_F(JsonWriter_WriteFloat_Tests, FiveDigits_NoRounding) {
whenInputIs(9.999994, 5);
outputMustBe("9.99999");
} }

View File

@ -5,78 +5,60 @@
// https://bblanchon.github.io/ArduinoJson/ // https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star! // If you like this project, please add a star!
#include <gtest/gtest.h> #include <catch.hpp>
#include <ArduinoJson/Serialization/JsonWriter.hpp> #include <ArduinoJson/Serialization/JsonWriter.hpp>
#include <ArduinoJson/Serialization/StaticStringBuilder.hpp> #include <ArduinoJson/Serialization/StaticStringBuilder.hpp>
using namespace ArduinoJson::Internals; using namespace ArduinoJson::Internals;
class JsonWriter_WriteString_Tests : public testing::Test { void check(const char* input, std::string expected) {
protected: char output[1024];
void whenInputIs(const char *input) { StaticStringBuilder sb(output, sizeof(output));
StaticStringBuilder sb(buffer, sizeof(buffer));
JsonWriter writer(sb); JsonWriter writer(sb);
writer.writeString(input); writer.writeString(input);
returnValue = writer.bytesWritten(); REQUIRE(expected == output);
REQUIRE(writer.bytesWritten() == expected.size());
} }
void outputMustBe(const char *expected) { TEST_CASE("JsonWriter::writeString()") {
EXPECT_STREQ(expected, buffer); SECTION("Null") {
EXPECT_EQ(strlen(expected), returnValue); check(0, "null");
} }
private: SECTION("EmptyString") {
char buffer[1024]; check("", "\"\"");
size_t returnValue;
};
TEST_F(JsonWriter_WriteString_Tests, Null) {
whenInputIs(0);
outputMustBe("null");
} }
TEST_F(JsonWriter_WriteString_Tests, EmptyString) { SECTION("QuotationMark") {
whenInputIs(""); check("\"", "\"\\\"\"");
outputMustBe("\"\"");
} }
TEST_F(JsonWriter_WriteString_Tests, QuotationMark) { SECTION("ReverseSolidus") {
whenInputIs("\""); check("\\", "\"\\\\\"");
outputMustBe("\"\\\"\"");
} }
TEST_F(JsonWriter_WriteString_Tests, ReverseSolidus) { SECTION("Solidus") {
whenInputIs("\\"); check("/", "\"/\""); // but the JSON format allows \/
outputMustBe("\"\\\\\"");
} }
TEST_F(JsonWriter_WriteString_Tests, Solidus) { SECTION("Backspace") {
whenInputIs("/"); check("\b", "\"\\b\"");
outputMustBe("\"/\""); // but the JSON format allows \/
} }
TEST_F(JsonWriter_WriteString_Tests, Backspace) { SECTION("Formfeed") {
whenInputIs("\b"); check("\f", "\"\\f\"");
outputMustBe("\"\\b\"");
} }
TEST_F(JsonWriter_WriteString_Tests, Formfeed) { SECTION("Newline") {
whenInputIs("\f"); check("\n", "\"\\n\"");
outputMustBe("\"\\f\"");
} }
TEST_F(JsonWriter_WriteString_Tests, Newline) { SECTION("CarriageReturn") {
whenInputIs("\n"); check("\r", "\"\\r\"");
outputMustBe("\"\\n\"");
} }
TEST_F(JsonWriter_WriteString_Tests, CarriageReturn) { SECTION("HorizontalTab") {
whenInputIs("\r"); check("\t", "\"\\t\"");
outputMustBe("\"\\r\"");
} }
TEST_F(JsonWriter_WriteString_Tests, HorizontalTab) {
whenInputIs("\t");
outputMustBe("\"\\t\"");
} }

View File

@ -15,5 +15,5 @@ add_executable(MiscTests
vla.cpp vla.cpp
) )
target_link_libraries(MiscTests gtest) target_link_libraries(MiscTests catch)
add_test(Misc MiscTests) add_test(Misc MiscTests)

View File

@ -6,70 +6,37 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
using namespace ArduinoJson::Internals; using namespace ArduinoJson::Internals;
class StringBuilderTests : public testing::Test { TEST_CASE("StringBuilder") {
protected: char output[20];
virtual void SetUp() { StaticStringBuilder sb(output, sizeof(output));
_stringBuilder = new StaticStringBuilder(_buffer, sizeof(_buffer));
SECTION("InitialState") {
REQUIRE(std::string("") == output);
} }
virtual void TearDown() { SECTION("OverCapacity") {
delete _stringBuilder; REQUIRE(19 == sb.print("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
REQUIRE(0 == sb.print("ABC"));
REQUIRE(std::string("ABCDEFGHIJKLMNOPQRS") == output);
} }
void print(const char *value) { SECTION("EmptyString") {
_returnValue = _stringBuilder->print(value); REQUIRE(0 == sb.print(""));
REQUIRE(std::string("") == output);
} }
void outputMustBe(const char *expected) { SECTION("OneString") {
EXPECT_STREQ(expected, _buffer); REQUIRE(4 == sb.print("ABCD"));
REQUIRE(std::string("ABCD") == output);
} }
void resultMustBe(size_t expected) { SECTION("TwoStrings") {
EXPECT_EQ(expected, _returnValue); REQUIRE(4 == sb.print("ABCD"));
REQUIRE(4 == sb.print("EFGH"));
REQUIRE(std::string("ABCDEFGH") == output);
} }
private:
char _buffer[20];
Print *_stringBuilder;
size_t _returnValue;
};
TEST_F(StringBuilderTests, InitialState) {
outputMustBe("");
}
TEST_F(StringBuilderTests, OverCapacity) {
print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
resultMustBe(19);
print("ABC");
resultMustBe(0);
outputMustBe("ABCDEFGHIJKLMNOPQRS");
}
TEST_F(StringBuilderTests, EmptyString) {
print("");
resultMustBe(0);
outputMustBe("");
}
TEST_F(StringBuilderTests, OneString) {
print("ABCD");
resultMustBe(4);
outputMustBe("ABCD");
}
TEST_F(StringBuilderTests, TwoStrings) {
print("ABCD");
resultMustBe(4);
print("EFGH");
resultMustBe(4);
outputMustBe("ABCDEFGH");
} }

View File

@ -6,31 +6,37 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
#include <sstream> #include <sstream>
using namespace ArduinoJson::TypeTraits; using namespace ArduinoJson::TypeTraits;
TEST(TypeTraits, IsBaseOf) { TEST_CASE("TypeTraits") {
ASSERT_FALSE((IsBaseOf<std::istream, std::ostringstream>::value)); SECTION("IsBaseOf") {
ASSERT_TRUE((IsBaseOf<std::istream, std::istringstream>::value)); REQUIRE_FALSE(
ASSERT_TRUE((IsBaseOf<JsonVariantBase<JsonObjectSubscript<const char*> >, static_cast<bool>(IsBaseOf<std::istream, std::ostringstream>::value));
REQUIRE(
static_cast<bool>(IsBaseOf<std::istream, std::istringstream>::value));
REQUIRE(static_cast<bool>(
IsBaseOf<JsonVariantBase<JsonObjectSubscript<const char*> >,
JsonObjectSubscript<const char*> >::value)); JsonObjectSubscript<const char*> >::value));
} }
TEST(TypeTraits, IsArray) { SECTION("IsArray") {
ASSERT_FALSE((IsArray<const char*>::value)); REQUIRE_FALSE((IsArray<const char*>::value));
ASSERT_TRUE((IsArray<const char[]>::value)); REQUIRE((IsArray<const char[]>::value));
ASSERT_TRUE((IsArray<const char[10]>::value)); REQUIRE((IsArray<const char[10]>::value));
} }
TEST(TypeTraits, IsVariant) { SECTION("IsVariant") {
ASSERT_TRUE((IsVariant<JsonObjectSubscript<const char*> >::value)); REQUIRE(
ASSERT_TRUE((IsVariant<JsonVariant>::value)); static_cast<bool>(IsVariant<JsonObjectSubscript<const char*> >::value));
REQUIRE(static_cast<bool>(IsVariant<JsonVariant>::value));
} }
TEST(TypeTraits, IsString) { SECTION("IsString") {
ASSERT_TRUE((IsString<const char*>::value)); REQUIRE((IsString<const char*>::value));
ASSERT_TRUE((IsString<std::string>::value)); REQUIRE((IsString<std::string>::value));
ASSERT_FALSE((IsString<double>::value)); REQUIRE_FALSE((IsString<double>::value));
}
} }

View File

@ -8,7 +8,7 @@
#define ARDUINOJSON_ENABLE_DEPRECATED 1 #define ARDUINOJSON_ENABLE_DEPRECATED 1
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
#if defined(__clang__) #if defined(__clang__)
#pragma clang diagnostic ignored "-Wdeprecated-declarations" #pragma clang diagnostic ignored "-Wdeprecated-declarations"
@ -18,25 +18,27 @@
#pragma warning(disable : 4996) #pragma warning(disable : 4996)
#endif #endif
TEST(Deprecated, asArray) { TEST_CASE("Deprecated functions") {
SECTION("JsonVariant::asArray()") {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonVariant variant = jsonBuffer.createArray(); JsonVariant variant = jsonBuffer.createArray();
ASSERT_TRUE(variant.asArray().success()); REQUIRE(variant.asArray().success());
} }
TEST(Deprecated, asObject) { SECTION("JsonVariant::asObject()") {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonVariant variant = jsonBuffer.createObject(); JsonVariant variant = jsonBuffer.createObject();
ASSERT_TRUE(variant.asObject().success()); REQUIRE(variant.asObject().success());
} }
TEST(Deprecated, asString) { SECTION("JsonVariant::asString()") {
JsonVariant variant = "hello"; JsonVariant variant = "hello";
ASSERT_STREQ("hello", variant.asString()); REQUIRE(std::string("hello") == variant.asString());
} }
TEST(Deprecated, removeAt) { SECTION("JsonArray::removeAt()") {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonArray& arr = jsonBuffer.createArray(); JsonArray& arr = jsonBuffer.createArray();
arr.removeAt(0); arr.removeAt(0);
} }
}

View File

@ -6,80 +6,82 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
#include <sstream> #include <sstream>
TEST(StdStream_Tests, JsonVariantFalse) { TEST_CASE("std::stream") {
SECTION("JsonVariantFalse") {
std::ostringstream os; std::ostringstream os;
JsonVariant variant = false; JsonVariant variant = false;
os << variant; os << variant;
ASSERT_EQ("false", os.str()); REQUIRE("false" == os.str());
} }
TEST(StdStream_Tests, JsonVariantString) { SECTION("JsonVariantString") {
std::ostringstream os; std::ostringstream os;
JsonVariant variant = "coucou"; JsonVariant variant = "coucou";
os << variant; os << variant;
ASSERT_EQ("\"coucou\"", os.str()); REQUIRE("\"coucou\"" == os.str());
} }
TEST(StdStream_Tests, JsonObject) { SECTION("JsonObject") {
std::ostringstream os; std::ostringstream os;
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& object = jsonBuffer.createObject(); JsonObject& object = jsonBuffer.createObject();
object["key"] = "value"; object["key"] = "value";
os << object; os << object;
ASSERT_EQ("{\"key\":\"value\"}", os.str()); REQUIRE("{\"key\":\"value\"}" == os.str());
} }
TEST(StdStream_Tests, JsonObjectSubscript) { SECTION("JsonObjectSubscript") {
std::ostringstream os; std::ostringstream os;
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& object = jsonBuffer.createObject(); JsonObject& object = jsonBuffer.createObject();
object["key"] = "value"; object["key"] = "value";
os << object["key"]; os << object["key"];
ASSERT_EQ("\"value\"", os.str()); REQUIRE("\"value\"" == os.str());
} }
TEST(StdStream_Tests, JsonArray) { SECTION("JsonArray") {
std::ostringstream os; std::ostringstream os;
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonArray& array = jsonBuffer.createArray(); JsonArray& array = jsonBuffer.createArray();
array.add("value"); array.add("value");
os << array; os << array;
ASSERT_EQ("[\"value\"]", os.str()); REQUIRE("[\"value\"]" == os.str());
} }
TEST(StdStream_Tests, JsonArraySubscript) { SECTION("JsonArraySubscript") {
std::ostringstream os; std::ostringstream os;
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonArray& array = jsonBuffer.createArray(); JsonArray& array = jsonBuffer.createArray();
array.add("value"); array.add("value");
os << array[0]; os << array[0];
ASSERT_EQ("\"value\"", os.str()); REQUIRE("\"value\"" == os.str());
} }
TEST(StdStream_Tests, ParseArray) { SECTION("ParseArray") {
std::istringstream json(" [ 42 /* comment */ ] "); std::istringstream json(" [ 42 /* comment */ ] ");
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonArray& arr = jsonBuffer.parseArray(json); JsonArray& arr = jsonBuffer.parseArray(json);
ASSERT_TRUE(arr.success()); REQUIRE(true == arr.success());
ASSERT_EQ(1, arr.size()); REQUIRE(1 == arr.size());
ASSERT_EQ(42, arr[0]); REQUIRE(42 == arr[0]);
} }
TEST(StdStream_Tests, ParseObject) { SECTION("ParseObject") {
std::istringstream json(" { hello : world // comment\n }"); std::istringstream json(" { hello : world // comment\n }");
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& obj = jsonBuffer.parseObject(json); JsonObject& obj = jsonBuffer.parseObject(json);
ASSERT_TRUE(obj.success()); REQUIRE(true == obj.success());
ASSERT_EQ(1, obj.size()); REQUIRE(1 == obj.size());
ASSERT_STREQ("world", obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
} }
TEST(StdStream_Tests, ShouldNotReadPastTheEnd) { SECTION("ShouldNotReadPastTheEnd") {
std::istringstream json("{}123"); std::istringstream json("{}123");
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
jsonBuffer.parseObject(json); jsonBuffer.parseObject(json);
ASSERT_EQ('1', json.get()); REQUIRE('1' == json.get());
}
} }

View File

@ -6,247 +6,248 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class StringTests : public ::testing::Test {
protected:
static void eraseString(std::string &str) { static void eraseString(std::string &str) {
char *p = const_cast<char *>(str.c_str()); char *p = const_cast<char *>(str.c_str());
while (*p) *p++ = '*'; while (*p) *p++ = '*';
} }
DynamicJsonBuffer _jsonBuffer; TEST_CASE("std::string") {
}; DynamicJsonBuffer jb;
TEST_F(StringTests, JsonBuffer_ParseArray) { SECTION("JsonBuffer_ParseArray") {
std::string json("[\"hello\"]"); std::string json("[\"hello\"]");
JsonArray &array = _jsonBuffer.parseArray(json); JsonArray &array = jb.parseArray(json);
eraseString(json); eraseString(json);
ASSERT_TRUE(array.success()); REQUIRE(true == array.success());
ASSERT_STREQ("hello", array[0]); REQUIRE(std::string("hello") == array[0]);
} }
TEST_F(StringTests, JsonBuffer_ParseObject) { SECTION("JsonBuffer_ParseObject") {
std::string json("{\"hello\":\"world\"}"); std::string json("{\"hello\":\"world\"}");
JsonObject &object = _jsonBuffer.parseObject(json); JsonObject &object = jb.parseObject(json);
eraseString(json); eraseString(json);
ASSERT_TRUE(object.success()); REQUIRE(true == object.success());
ASSERT_STREQ("world", object["hello"]); REQUIRE(std::string("world") == object["hello"]);
} }
TEST_F(StringTests, JsonObject_Subscript) { SECTION("JsonObject_Subscript") {
char json[] = "{\"key\":\"value\"}"; char json[] = "{\"key\":\"value\"}";
JsonObject &object = _jsonBuffer.parseObject(json); JsonObject &object = jb.parseObject(json);
ASSERT_STREQ("value", object[std::string("key")]); REQUIRE(std::string("value") == object[std::string("key")]);
} }
TEST_F(StringTests, JsonObject_ConstSubscript) { SECTION("JsonObject_ConstSubscript") {
char json[] = "{\"key\":\"value\"}"; char json[] = "{\"key\":\"value\"}";
const JsonObject &object = _jsonBuffer.parseObject(json); const JsonObject &object = jb.parseObject(json);
ASSERT_STREQ("value", object[std::string("key")]); REQUIRE(std::string("value") == object[std::string("key")]);
} }
TEST_F(StringTests, JsonObject_SetKey) { SECTION("JsonObject_SetKey") {
JsonObject &object = _jsonBuffer.createObject(); JsonObject &object = jb.createObject();
std::string key("hello"); std::string key("hello");
object.set(key, "world"); object.set(key, "world");
eraseString(key); eraseString(key);
ASSERT_STREQ("world", object["hello"]); REQUIRE(std::string("world") == object["hello"]);
} }
TEST_F(StringTests, JsonObject_SetValue) { SECTION("JsonObject_SetValue") {
JsonObject &object = _jsonBuffer.createObject(); JsonObject &object = jb.createObject();
std::string value("world"); std::string value("world");
object.set("hello", value); object.set("hello", value);
eraseString(value); eraseString(value);
ASSERT_STREQ("world", object["hello"]); REQUIRE(std::string("world") == object["hello"]);
} }
TEST_F(StringTests, JsonObject_SetKeyValue) { SECTION("JsonObject_SetKeyValue") {
JsonObject &object = _jsonBuffer.createObject(); JsonObject &object = jb.createObject();
std::string key("hello"); std::string key("hello");
std::string value("world"); std::string value("world");
object.set(key, value); object.set(key, value);
eraseString(key); eraseString(key);
eraseString(value); eraseString(value);
ASSERT_STREQ("world", object["hello"]); REQUIRE(std::string("world") == object["hello"]);
} }
TEST_F(StringTests, JsonObject_SetToArraySubscript) { SECTION("JsonObject_SetToArraySubscript") {
JsonArray &arr = _jsonBuffer.createArray(); JsonArray &arr = jb.createArray();
arr.add("world"); arr.add("world");
JsonObject &object = _jsonBuffer.createObject(); JsonObject &object = jb.createObject();
object.set(std::string("hello"), arr[0]); object.set(std::string("hello"), arr[0]);
ASSERT_STREQ("world", object["hello"]); REQUIRE(std::string("world") == object["hello"]);
} }
TEST_F(StringTests, JsonObject_SetToObjectSubscript) { SECTION("JsonObject_SetToObjectSubscript") {
JsonObject &arr = _jsonBuffer.createObject(); JsonObject &arr = jb.createObject();
arr.set("x", "world"); arr.set("x", "world");
JsonObject &object = _jsonBuffer.createObject(); JsonObject &object = jb.createObject();
object.set(std::string("hello"), arr["x"]); object.set(std::string("hello"), arr["x"]);
ASSERT_STREQ("world", object["hello"]); REQUIRE(std::string("world") == object["hello"]);
} }
TEST_F(StringTests, JsonObject_Get) { SECTION("JsonObject_Get") {
char json[] = "{\"key\":\"value\"}"; char json[] = "{\"key\":\"value\"}";
const JsonObject &object = _jsonBuffer.parseObject(json); const JsonObject &object = jb.parseObject(json);
ASSERT_STREQ("value", object.get<const char *>(std::string("key"))); REQUIRE(std::string("value") ==
object.get<const char *>(std::string("key")));
} }
TEST_F(StringTests, JsonObject_GetT) { SECTION("JsonObject_GetT") {
char json[] = "{\"key\":\"value\"}"; char json[] = "{\"key\":\"value\"}";
const JsonObject &object = _jsonBuffer.parseObject(json); const JsonObject &object = jb.parseObject(json);
ASSERT_STREQ("value", object.get<const char *>(std::string("key"))); REQUIRE(std::string("value") ==
object.get<const char *>(std::string("key")));
} }
TEST_F(StringTests, JsonObject_IsT) { SECTION("JsonObject_IsT") {
char json[] = "{\"key\":\"value\"}"; char json[] = "{\"key\":\"value\"}";
const JsonObject &object = _jsonBuffer.parseObject(json); const JsonObject &object = jb.parseObject(json);
ASSERT_TRUE(object.is<const char *>(std::string("key"))); REQUIRE(true == object.is<const char *>(std::string("key")));
} }
TEST_F(StringTests, JsonObject_CreateNestedObject) { SECTION("JsonObject_CreateNestedObject") {
std::string key = "key"; std::string key = "key";
char json[64]; char json[64];
JsonObject &object = _jsonBuffer.createObject(); JsonObject &object = jb.createObject();
object.createNestedObject(key); object.createNestedObject(key);
eraseString(key); eraseString(key);
object.printTo(json, sizeof(json)); object.printTo(json, sizeof(json));
ASSERT_STREQ("{\"key\":{}}", json); REQUIRE(std::string("{\"key\":{}}") == json);
} }
TEST_F(StringTests, JsonObject_CreateNestedArray) { SECTION("JsonObject_CreateNestedArray") {
std::string key = "key"; std::string key = "key";
char json[64]; char json[64];
JsonObject &object = _jsonBuffer.createObject(); JsonObject &object = jb.createObject();
object.createNestedArray(key); object.createNestedArray(key);
eraseString(key); eraseString(key);
object.printTo(json, sizeof(json)); object.printTo(json, sizeof(json));
ASSERT_STREQ("{\"key\":[]}", json); REQUIRE(std::string("{\"key\":[]}") == json);
} }
TEST_F(StringTests, JsonObject_ContainsKey) { SECTION("JsonObject_ContainsKey") {
char json[] = "{\"key\":\"value\"}"; char json[] = "{\"key\":\"value\"}";
const JsonObject &object = _jsonBuffer.parseObject(json); const JsonObject &object = jb.parseObject(json);
ASSERT_TRUE(object.containsKey(std::string("key"))); REQUIRE(true == object.containsKey(std::string("key")));
} }
TEST_F(StringTests, JsonObject_Remove) { SECTION("JsonObject_Remove") {
char json[] = "{\"key\":\"value\"}"; char json[] = "{\"key\":\"value\"}";
JsonObject &object = _jsonBuffer.parseObject(json); JsonObject &object = jb.parseObject(json);
ASSERT_EQ(1, object.size()); REQUIRE(1 == object.size());
object.remove(std::string("key")); object.remove(std::string("key"));
ASSERT_EQ(0, object.size()); REQUIRE(0 == object.size());
} }
TEST_F(StringTests, JsonObjectSubscript_SetKey) { SECTION("JsonObjectSubscript_SetKey") {
JsonObject &object = _jsonBuffer.createObject(); JsonObject &object = jb.createObject();
std::string key("hello"); std::string key("hello");
object[key] = "world"; object[key] = "world";
eraseString(key); eraseString(key);
ASSERT_STREQ("world", object["hello"]); REQUIRE(std::string("world") == object["hello"]);
} }
TEST_F(StringTests, JsonObjectSubscript_SetValue) { SECTION("JsonObjectSubscript_SetValue") {
JsonObject &object = _jsonBuffer.createObject(); JsonObject &object = jb.createObject();
std::string value("world"); std::string value("world");
object["hello"] = value; object["hello"] = value;
eraseString(value); eraseString(value);
ASSERT_STREQ("world", object["hello"]); REQUIRE(std::string("world") == object["hello"]);
} }
TEST_F(StringTests, JsonArray_Add) { SECTION("JsonArray_Add") {
JsonArray &array = _jsonBuffer.createArray(); JsonArray &array = jb.createArray();
std::string value("hello"); std::string value("hello");
array.add(value); array.add(value);
eraseString(value); eraseString(value);
ASSERT_STREQ("hello", array[0]); REQUIRE(std::string("hello") == array[0]);
} }
TEST_F(StringTests, JsonArray_Set) { SECTION("JsonArray_Set") {
JsonArray &array = _jsonBuffer.createArray(); JsonArray &array = jb.createArray();
std::string value("world"); std::string value("world");
array.add("hello"); array.add("hello");
array.set(0, value); array.set(0, value);
eraseString(value); eraseString(value);
ASSERT_STREQ("world", array[0]); REQUIRE(std::string("world") == array[0]);
} }
TEST_F(StringTests, JsonArraySubscript) { SECTION("JsonArraySubscript") {
JsonArray &array = _jsonBuffer.createArray(); JsonArray &array = jb.createArray();
std::string value("world"); std::string value("world");
array.add("hello"); array.add("hello");
array[0] = value; array[0] = value;
eraseString(value); eraseString(value);
ASSERT_STREQ("world", array[0]); REQUIRE(std::string("world") == array[0]);
} }
TEST_F(StringTests, JsonArray_PrintTo) { SECTION("JsonArray_PrintTo") {
JsonArray &array = _jsonBuffer.createArray(); JsonArray &array = jb.createArray();
array.add(4); array.add(4);
array.add(2); array.add(2);
std::string json; std::string json;
array.printTo(json); array.printTo(json);
ASSERT_EQ(std::string("[4,2]"), json); REQUIRE(std::string("[4,2]") == json);
} }
TEST_F(StringTests, JsonArray_PrettyPrintTo) { SECTION("JsonArray_PrettyPrintTo") {
JsonArray &array = _jsonBuffer.createArray(); JsonArray &array = jb.createArray();
array.add(4); array.add(4);
array.add(2); array.add(2);
std::string json; std::string json;
array.prettyPrintTo(json); array.prettyPrintTo(json);
ASSERT_EQ(std::string("[\r\n 4,\r\n 2\r\n]"), json); REQUIRE(std::string("[\r\n 4,\r\n 2\r\n]") == json);
} }
TEST_F(StringTests, JsonObject_PrintTo) { SECTION("JsonObject_PrintTo") {
JsonObject &object = _jsonBuffer.createObject(); JsonObject &object = jb.createObject();
object["key"] = "value"; object["key"] = "value";
std::string json; std::string json;
object.printTo(json); object.printTo(json);
ASSERT_EQ(std::string("{\"key\":\"value\"}"), json); REQUIRE(std::string("{\"key\":\"value\"}") == json);
} }
TEST_F(StringTests, JsonObject_PrettyPrintTo) { SECTION("JsonObject_PrettyPrintTo") {
JsonObject &object = _jsonBuffer.createObject(); JsonObject &object = jb.createObject();
object["key"] = "value"; object["key"] = "value";
std::string json; std::string json;
object.prettyPrintTo(json); object.prettyPrintTo(json);
ASSERT_EQ(std::string("{\r\n \"key\": \"value\"\r\n}"), json); REQUIRE(std::string("{\r\n \"key\": \"value\"\r\n}") == json);
} }
TEST_F(StringTests, JsonBuffer_GrowWhenAddingNewKey) { SECTION("JsonBuffer_GrowWhenAddingNewKey") {
JsonObject &object = _jsonBuffer.createObject(); JsonObject &object = jb.createObject();
std::string key1("hello"), key2("world"); std::string key1("hello"), key2("world");
object[key1] = 1; object[key1] = 1;
size_t sizeBefore = _jsonBuffer.size(); size_t sizeBefore = jb.size();
object[key2] = 2; object[key2] = 2;
size_t sizeAfter = _jsonBuffer.size(); size_t sizeAfter = jb.size();
ASSERT_GT(sizeAfter - sizeBefore, key2.size()); REQUIRE(sizeAfter - sizeBefore >= key2.size());
} }
TEST_F(StringTests, JsonBuffer_DontGrowWhenReusingKey) { SECTION("JsonBuffer_DontGrowWhenReusingKey") {
JsonObject &object = _jsonBuffer.createObject(); JsonObject &object = jb.createObject();
std::string key("hello"); std::string key("hello");
object[key] = 1; object[key] = 1;
size_t sizeBefore = _jsonBuffer.size(); size_t sizeBefore = jb.size();
object[key] = 2; object[key] = 2;
size_t sizeAfter = _jsonBuffer.size(); size_t sizeAfter = jb.size();
ASSERT_EQ(sizeBefore, sizeAfter); REQUIRE(sizeBefore == sizeAfter);
} }
TEST_F(StringTests, JsonBuffer_strdup) { SECTION("JsonBuffer_strdup") {
std::string original("hello"); std::string original("hello");
char *copy = _jsonBuffer.strdup(original); char *copy = jb.strdup(original);
original[0] = 'w'; original[0] = 'w';
ASSERT_STREQ("hello", copy); REQUIRE(std::string("hello") == copy);
}
} }

View File

@ -6,214 +6,215 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
#if defined(__clang__) #if defined(__clang__)
#define CONFLICTS_WITH_BUILTIN_OPERATOR #define CONFLICTS_WITH_BUILTIN_OPERATOR
#endif #endif
TEST(UnsignedCharArray, ParseArray) { TEST_CASE("unsigned char string") {
SECTION("JsonBuffer::parseArray") {
unsigned char json[] = "[42]"; unsigned char json[] = "[42]";
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> jsonBuffer; StaticJsonBuffer<JSON_ARRAY_SIZE(1)> jsonBuffer;
JsonArray& arr = jsonBuffer.parseArray(json); JsonArray& arr = jsonBuffer.parseArray(json);
EXPECT_TRUE(arr.success()); REQUIRE(true == arr.success());
} }
TEST(UnsignedCharArray, ParseObject) { SECTION("JsonBuffer::parseObject") {
unsigned char json[] = "{\"a\":42}"; unsigned char json[] = "{\"a\":42}";
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer; StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer;
JsonObject& obj = jsonBuffer.parseObject(json); JsonObject& obj = jsonBuffer.parseObject(json);
EXPECT_TRUE(obj.success()); REQUIRE(true == obj.success());
} }
TEST(UnsignedCharArray, JsonVariant_Constructor) { SECTION("JsonVariant constructor") {
unsigned char value[] = "42"; unsigned char value[] = "42";
JsonVariant variant(value); JsonVariant variant(value);
EXPECT_EQ(42, variant.as<int>()); REQUIRE(42 == variant.as<int>());
} }
TEST(UnsignedCharArray, JsonVariant_Assign) { SECTION("JsonVariant assignment operator") {
unsigned char value[] = "42"; unsigned char value[] = "42";
JsonVariant variant(666); JsonVariant variant(666);
variant = value; variant = value;
EXPECT_EQ(42, variant.as<int>()); REQUIRE(42 == variant.as<int>());
} }
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
TEST(UnsignedCharArray, JsonVariant_Subscript) { SECTION("JsonVariant::operator[]") {
unsigned char key[] = "hello"; unsigned char key[] = "hello";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}"); JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
EXPECT_STREQ("world", variant[key]); REQUIRE(std::string("world") == variant[key]);
} }
#endif #endif
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
TEST(UnsignedCharArray, JsonVariant_Subscript_Const) { SECTION("JsonVariant::operator[] const") {
unsigned char key[] = "hello"; unsigned char key[] = "hello";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
const JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}"); const JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
EXPECT_STREQ("world", variant[key]); REQUIRE(std::string("world") == variant[key]);
} }
#endif #endif
TEST(UnsignedCharArray, JsonVariant_Equals) { SECTION("JsonVariant::operator==") {
unsigned char comparand[] = "hello"; unsigned char comparand[] = "hello";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
const JsonVariant variant = "hello"; const JsonVariant variant = "hello";
EXPECT_TRUE(comparand == variant); REQUIRE(comparand == variant);
EXPECT_TRUE(variant == comparand); REQUIRE(variant == comparand);
EXPECT_FALSE(comparand != variant); REQUIRE_FALSE(comparand != variant);
EXPECT_FALSE(variant != comparand); REQUIRE_FALSE(variant != comparand);
} }
TEST(UnsignedCharArray, JsonVariant_Differs) { SECTION("JsonVariant::operator!=") {
unsigned char comparand[] = "hello"; unsigned char comparand[] = "hello";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
const JsonVariant variant = "world"; const JsonVariant variant = "world";
EXPECT_TRUE(comparand != variant); REQUIRE(comparand != variant);
EXPECT_TRUE(variant != comparand); REQUIRE(variant != comparand);
EXPECT_FALSE(comparand == variant); REQUIRE_FALSE(comparand == variant);
EXPECT_FALSE(variant == comparand); REQUIRE_FALSE(variant == comparand);
} }
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
TEST(UnsignedCharArray, JsonObject_Subscript) { SECTION("JsonObject::operator[]") {
unsigned char key[] = "hello"; unsigned char key[] = "hello";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& obj = jsonBuffer.createObject(); JsonObject& obj = jsonBuffer.createObject();
obj[key] = "world"; obj[key] = "world";
EXPECT_STREQ("world", obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
} }
#endif #endif
TEST(UnsignedCharArray, JsonObject_Subscript_Assign) { // issue #416 SECTION("JsonObjectSubscript::operator=") { // issue #416
unsigned char value[] = "world"; unsigned char value[] = "world";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& obj = jsonBuffer.createObject(); JsonObject& obj = jsonBuffer.createObject();
obj["hello"] = value; obj["hello"] = value;
EXPECT_STREQ("world", obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
} }
TEST(UnsignedCharArray, JsonObject_Subscript_Set) { SECTION("JsonObjectSubscript::set()") {
unsigned char value[] = "world"; unsigned char value[] = "world";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& obj = jsonBuffer.createObject(); JsonObject& obj = jsonBuffer.createObject();
obj["hello"].set(value); obj["hello"].set(value);
EXPECT_STREQ("world", obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
} }
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
TEST(UnsignedCharArray, JsonObject_Subscript_Const) { SECTION("JsonObject::operator[] const") {
unsigned char key[] = "hello"; unsigned char key[] = "hello";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}"); const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
EXPECT_STREQ("world", obj[key]); REQUIRE(std::string("world") == obj[key]);
} }
#endif #endif
TEST(UnsignedCharArray, JsonObject_Get) { SECTION("JsonObject::get()") {
unsigned char key[] = "hello"; unsigned char key[] = "hello";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}"); JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
EXPECT_STREQ("world", obj.get<char*>(key)); REQUIRE(std::string("world") == obj.get<char*>(key));
} }
TEST(UnsignedCharArray, JsonObject_Set_Key) { SECTION("JsonObject::set() key") {
unsigned char key[] = "hello"; unsigned char key[] = "hello";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& obj = jsonBuffer.createObject(); JsonObject& obj = jsonBuffer.createObject();
obj.set(key, "world"); obj.set(key, "world");
EXPECT_STREQ("world", obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
} }
TEST(UnsignedCharArray, JsonObject_Set_Value) { SECTION("JsonObject::set() value") {
unsigned char value[] = "world"; unsigned char value[] = "world";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& obj = jsonBuffer.createObject(); JsonObject& obj = jsonBuffer.createObject();
obj.set("hello", value); obj.set("hello", value);
EXPECT_STREQ("world", obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
} }
TEST(UnsignedCharArray, JsonObject_Set_Key_WithDecimals) { SECTION("JsonObject::set() key with decimals") {
unsigned char key[] = "hello"; unsigned char key[] = "hello";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& obj = jsonBuffer.createObject(); JsonObject& obj = jsonBuffer.createObject();
obj.set(key, 3.14, 2); obj.set(key, 3.14, 2);
EXPECT_EQ(3.14, obj["hello"]); REQUIRE(3.14 == obj["hello"]);
} }
TEST(UnsignedCharArray, JsonObject_Set_KeyAndValue) { SECTION("JsonObject::set key&value") {
unsigned char key[] = "world"; unsigned char key[] = "world";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& obj = jsonBuffer.createObject(); JsonObject& obj = jsonBuffer.createObject();
obj.set(key, key); obj.set(key, key);
EXPECT_STREQ("world", obj["world"]); REQUIRE(std::string("world") == obj["world"]);
} }
TEST(UnsignedCharArray, JsonObject_ContainsKey) { SECTION("JsonObject::containsKey()") {
unsigned char key[] = "hello"; unsigned char key[] = "hello";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}"); const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
EXPECT_TRUE(obj.containsKey(key)); REQUIRE(true == obj.containsKey(key));
} }
TEST(UnsignedCharArray, JsonObject_Remove) { SECTION("JsonObject::remove()") {
unsigned char key[] = "hello"; unsigned char key[] = "hello";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}"); JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
obj.remove(key); obj.remove(key);
EXPECT_EQ(0, obj.size()); REQUIRE(0 == obj.size());
} }
TEST(UnsignedCharArray, JsonObject_Is) { SECTION("JsonObject::is()") {
unsigned char key[] = "hello"; unsigned char key[] = "hello";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":42}"); JsonObject& obj = jsonBuffer.parseObject("{\"hello\":42}");
EXPECT_TRUE(obj.is<int>(key)); REQUIRE(true == obj.is<int>(key));
} }
TEST(UnsignedCharArray, JsonObject_CreateNestedArray) { SECTION("JsonObject::createNestedArray()") {
unsigned char key[] = "hello"; unsigned char key[] = "hello";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
@ -221,7 +222,7 @@ TEST(UnsignedCharArray, JsonObject_CreateNestedArray) {
obj.createNestedArray(key); obj.createNestedArray(key);
} }
TEST(UnsignedCharArray, JsonObject_CreateNestedObject) { SECTION("JsonObject::createNestedObject()") {
unsigned char key[] = "hello"; unsigned char key[] = "hello";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
@ -229,17 +230,17 @@ TEST(UnsignedCharArray, JsonObject_CreateNestedObject) {
obj.createNestedObject(key); obj.createNestedObject(key);
} }
TEST(UnsignedCharArray, JsonArray_Add) { SECTION("JsonArray::add()") {
unsigned char value[] = "world"; unsigned char value[] = "world";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonArray& arr = jsonBuffer.createArray(); JsonArray& arr = jsonBuffer.createArray();
arr.add(value); arr.add(value);
EXPECT_STREQ("world", arr[0]); REQUIRE(std::string("world") == arr[0]);
} }
TEST(UnsignedCharArray, JsonArray_Set) { SECTION("JsonArray::set()") {
unsigned char value[] = "world"; unsigned char value[] = "world";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
@ -247,10 +248,10 @@ TEST(UnsignedCharArray, JsonArray_Set) {
arr.add("hello"); arr.add("hello");
arr.set(0, value); arr.set(0, value);
EXPECT_STREQ("world", arr[0]); REQUIRE(std::string("world") == arr[0]);
} }
TEST(UnsignedCharArray, JsonArraySubscript_Set) { SECTION("JsonArraySubscript::set()") {
unsigned char value[] = "world"; unsigned char value[] = "world";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
@ -258,10 +259,10 @@ TEST(UnsignedCharArray, JsonArraySubscript_Set) {
arr.add("hello"); arr.add("hello");
arr[0].set(value); arr[0].set(value);
EXPECT_STREQ("world", arr[0]); REQUIRE(std::string("world") == arr[0]);
} }
TEST(UnsignedCharArray, JsonArraySubscript_Assign) { SECTION("JsonArraySubscript::operator=") {
unsigned char value[] = "world"; unsigned char value[] = "world";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
@ -269,15 +270,16 @@ TEST(UnsignedCharArray, JsonArraySubscript_Assign) {
arr.add("hello"); arr.add("hello");
arr[0] = value; arr[0] = value;
EXPECT_STREQ("world", arr[0]); REQUIRE(std::string("world") == arr[0]);
} }
TEST(UnsignedCharArray, JsonBuffer_strdup) { SECTION("JsonBuffer::strdup()") {
unsigned char value[] = "world"; unsigned char value[] = "world";
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
const char* dup = jsonBuffer.strdup(value); const char* dup = jsonBuffer.strdup(value);
EXPECT_NE(static_cast<const void*>(value), static_cast<const void*>(dup)); REQUIRE(static_cast<const void*>(value) != static_cast<const void*>(dup));
EXPECT_STREQ("world", dup); REQUIRE(std::string("world") == dup);
}
} }

View File

@ -6,7 +6,7 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
#if defined(__clang__) #if defined(__clang__)
#pragma clang diagnostic ignored "-Wvla-extension" #pragma clang diagnostic ignored "-Wvla-extension"
@ -19,7 +19,8 @@
#ifndef VLA_NOT_SUPPORTED #ifndef VLA_NOT_SUPPORTED
TEST(VariableLengthArray, ParseArray) { TEST_CASE("Variable Length Array") {
SECTION("ParseArray") {
int i = 8; int i = 8;
char vla[i]; char vla[i];
strcpy(vla, "[42]"); strcpy(vla, "[42]");
@ -27,10 +28,10 @@ TEST(VariableLengthArray, ParseArray) {
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> jsonBuffer; StaticJsonBuffer<JSON_ARRAY_SIZE(1)> jsonBuffer;
JsonArray& arr = jsonBuffer.parseArray(vla); JsonArray& arr = jsonBuffer.parseArray(vla);
EXPECT_TRUE(arr.success()); REQUIRE(true == arr.success());
} }
TEST(VariableLengthArray, ParseObject) { SECTION("ParseObject") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "{\"a\":42}"); strcpy(vla, "{\"a\":42}");
@ -38,10 +39,10 @@ TEST(VariableLengthArray, ParseObject) {
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer; StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer;
JsonObject& obj = jsonBuffer.parseObject(vla); JsonObject& obj = jsonBuffer.parseObject(vla);
EXPECT_TRUE(obj.success()); REQUIRE(true == obj.success());
} }
TEST(VariableLengthArray, Parse) { SECTION("Parse") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "42"); strcpy(vla, "42");
@ -49,20 +50,20 @@ TEST(VariableLengthArray, Parse) {
StaticJsonBuffer<1> jsonBuffer; StaticJsonBuffer<1> jsonBuffer;
JsonVariant variant = jsonBuffer.parse(vla); JsonVariant variant = jsonBuffer.parse(vla);
EXPECT_EQ(42, variant.as<int>()); REQUIRE(42 == variant.as<int>());
} }
TEST(VariableLengthArray, JsonVariant_Constructor) { SECTION("JsonVariant_Constructor") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "42"); strcpy(vla, "42");
JsonVariant variant(vla); JsonVariant variant(vla);
EXPECT_EQ(42, variant.as<int>()); REQUIRE(42 == variant.as<int>());
} }
TEST(VariableLengthArray, JsonVariant_Assign) { SECTION("JsonVariant_Assign") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "42"); strcpy(vla, "42");
@ -70,11 +71,11 @@ TEST(VariableLengthArray, JsonVariant_Assign) {
JsonVariant variant(666); JsonVariant variant(666);
variant = vla; variant = vla;
EXPECT_EQ(42, variant.as<int>()); REQUIRE(42 == variant.as<int>());
} }
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
TEST(VariableLengthArray, JsonVariant_Subscript) { SECTION("JsonVariant_Subscript") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "hello"); strcpy(vla, "hello");
@ -82,12 +83,12 @@ TEST(VariableLengthArray, JsonVariant_Subscript) {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}"); JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
EXPECT_STREQ("world", variant[vla]); REQUIRE(std::string("world") == variant[vla]);
} }
#endif #endif
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
TEST(VariableLengthArray, JsonVariant_Subscript_Const) { SECTION("JsonVariant_Subscript_Const") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "hello"); strcpy(vla, "hello");
@ -95,11 +96,11 @@ TEST(VariableLengthArray, JsonVariant_Subscript_Const) {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
const JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}"); const JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
EXPECT_STREQ("world", variant[vla]); REQUIRE(std::string("world") == variant[vla]);
} }
#endif #endif
TEST(VariableLengthArray, JsonVariant_Equals) { SECTION("JsonVariant_Equals") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "hello"); strcpy(vla, "hello");
@ -107,13 +108,13 @@ TEST(VariableLengthArray, JsonVariant_Equals) {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
const JsonVariant variant = "hello"; const JsonVariant variant = "hello";
EXPECT_TRUE(vla == variant); REQUIRE((vla == variant));
EXPECT_TRUE(variant == vla); REQUIRE((variant == vla));
EXPECT_FALSE(vla != variant); REQUIRE_FALSE((vla != variant));
EXPECT_FALSE(variant != vla); REQUIRE_FALSE((variant != vla));
} }
TEST(VariableLengthArray, JsonVariant_Differs) { SECTION("JsonVariant_Differs") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "hello"); strcpy(vla, "hello");
@ -121,14 +122,14 @@ TEST(VariableLengthArray, JsonVariant_Differs) {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
const JsonVariant variant = "world"; const JsonVariant variant = "world";
EXPECT_TRUE(vla != variant); REQUIRE((vla != variant));
EXPECT_TRUE(variant != vla); REQUIRE((variant != vla));
EXPECT_FALSE(vla == variant); REQUIRE_FALSE((vla == variant));
EXPECT_FALSE(variant == vla); REQUIRE_FALSE((variant == vla));
} }
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
TEST(VariableLengthArray, JsonObject_Subscript) { SECTION("JsonObject_Subscript") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "hello"); strcpy(vla, "hello");
@ -137,11 +138,11 @@ TEST(VariableLengthArray, JsonObject_Subscript) {
JsonObject& obj = jsonBuffer.createObject(); JsonObject& obj = jsonBuffer.createObject();
obj[vla] = "world"; obj[vla] = "world";
EXPECT_STREQ("world", obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
} }
#endif #endif
TEST(VariableLengthArray, JsonObject_Subscript_Assign) { // issue #416 SECTION("JsonObject_Subscript_Assign") { // issue #416
int i = 32; int i = 32;
char vla[i]; char vla[i];
strcpy(vla, "world"); strcpy(vla, "world");
@ -150,10 +151,10 @@ TEST(VariableLengthArray, JsonObject_Subscript_Assign) { // issue #416
JsonObject& obj = jsonBuffer.createObject(); JsonObject& obj = jsonBuffer.createObject();
obj["hello"] = vla; obj["hello"] = vla;
EXPECT_STREQ("world", obj["hello"].as<char*>()); REQUIRE(std::string("world") == obj["hello"].as<char*>());
} }
TEST(VariableLengthArray, JsonObject_Subscript_Set) { SECTION("JsonObject_Subscript_Set") {
int i = 32; int i = 32;
char vla[i]; char vla[i];
strcpy(vla, "world"); strcpy(vla, "world");
@ -162,11 +163,11 @@ TEST(VariableLengthArray, JsonObject_Subscript_Set) {
JsonObject& obj = jsonBuffer.createObject(); JsonObject& obj = jsonBuffer.createObject();
obj["hello"].set(vla); obj["hello"].set(vla);
EXPECT_STREQ("world", obj["hello"].as<char*>()); REQUIRE(std::string("world") == obj["hello"].as<char*>());
} }
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
TEST(VariableLengthArray, JsonObject_Subscript_Const) { SECTION("JsonObject_Subscript_Const") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "hello"); strcpy(vla, "hello");
@ -174,11 +175,11 @@ TEST(VariableLengthArray, JsonObject_Subscript_Const) {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}"); const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
EXPECT_STREQ("world", obj[vla]); REQUIRE(std::string("world") == obj[vla]);
} }
#endif #endif
TEST(VariableLengthArray, JsonObject_Get) { SECTION("JsonObject_Get") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "hello"); strcpy(vla, "hello");
@ -186,10 +187,10 @@ TEST(VariableLengthArray, JsonObject_Get) {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}"); JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
EXPECT_STREQ("world", obj.get<char*>(vla)); REQUIRE(std::string("world") == obj.get<char*>(vla));
} }
TEST(VariableLengthArray, JsonObject_Set_Key) { SECTION("JsonObject_Set_Key") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "hello"); strcpy(vla, "hello");
@ -198,10 +199,10 @@ TEST(VariableLengthArray, JsonObject_Set_Key) {
JsonObject& obj = jsonBuffer.createObject(); JsonObject& obj = jsonBuffer.createObject();
obj.set(vla, "world"); obj.set(vla, "world");
EXPECT_STREQ("world", obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
} }
TEST(VariableLengthArray, JsonObject_Set_Value) { SECTION("JsonObject_Set_Value") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "world"); strcpy(vla, "world");
@ -210,10 +211,10 @@ TEST(VariableLengthArray, JsonObject_Set_Value) {
JsonObject& obj = jsonBuffer.createObject(); JsonObject& obj = jsonBuffer.createObject();
obj.set("hello", vla); obj.set("hello", vla);
EXPECT_STREQ("world", obj["hello"]); REQUIRE(std::string("world") == obj["hello"]);
} }
TEST(VariableLengthArray, JsonObject_Set_Key_WithDecimals) { SECTION("JsonObject_Set_Key_WithDecimals") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "hello"); strcpy(vla, "hello");
@ -222,10 +223,10 @@ TEST(VariableLengthArray, JsonObject_Set_Key_WithDecimals) {
JsonObject& obj = jsonBuffer.createObject(); JsonObject& obj = jsonBuffer.createObject();
obj.set(vla, 3.14, 2); obj.set(vla, 3.14, 2);
EXPECT_EQ(3.14, obj["hello"]); REQUIRE(3.14 == obj["hello"]);
} }
TEST(VariableLengthArray, JsonObject_Set_KeyAndValue) { SECTION("JsonObject_Set_KeyAndValue") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "world"); strcpy(vla, "world");
@ -234,10 +235,10 @@ TEST(VariableLengthArray, JsonObject_Set_KeyAndValue) {
JsonObject& obj = jsonBuffer.createObject(); JsonObject& obj = jsonBuffer.createObject();
obj.set(vla, vla); obj.set(vla, vla);
EXPECT_STREQ("world", obj["world"]); REQUIRE(std::string("world") == obj["world"]);
} }
TEST(VariableLengthArray, JsonObject_ContainsKey) { SECTION("JsonObject_ContainsKey") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "hello"); strcpy(vla, "hello");
@ -245,10 +246,10 @@ TEST(VariableLengthArray, JsonObject_ContainsKey) {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}"); const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
EXPECT_TRUE(obj.containsKey(vla)); REQUIRE(true == obj.containsKey(vla));
} }
TEST(VariableLengthArray, JsonObject_Remove) { SECTION("JsonObject_Remove") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "hello"); strcpy(vla, "hello");
@ -257,10 +258,10 @@ TEST(VariableLengthArray, JsonObject_Remove) {
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}"); JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
obj.remove(vla); obj.remove(vla);
EXPECT_EQ(0, obj.size()); REQUIRE(0 == obj.size());
} }
TEST(VariableLengthArray, JsonObject_Is) { SECTION("JsonObject_Is") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "hello"); strcpy(vla, "hello");
@ -268,10 +269,10 @@ TEST(VariableLengthArray, JsonObject_Is) {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":42}"); JsonObject& obj = jsonBuffer.parseObject("{\"hello\":42}");
EXPECT_TRUE(obj.is<int>(vla)); REQUIRE(true == obj.is<int>(vla));
} }
TEST(VariableLengthArray, JsonObject_CreateNestedArray) { SECTION("JsonObject_CreateNestedArray") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "hello"); strcpy(vla, "hello");
@ -281,7 +282,7 @@ TEST(VariableLengthArray, JsonObject_CreateNestedArray) {
obj.createNestedArray(vla); obj.createNestedArray(vla);
} }
TEST(VariableLengthArray, JsonObject_CreateNestedObject) { SECTION("JsonObject_CreateNestedObject") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "hello"); strcpy(vla, "hello");
@ -291,7 +292,7 @@ TEST(VariableLengthArray, JsonObject_CreateNestedObject) {
obj.createNestedObject(vla); obj.createNestedObject(vla);
} }
TEST(VariableLengthArray, JsonArray_Add) { SECTION("JsonArray_Add") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "world"); strcpy(vla, "world");
@ -300,10 +301,10 @@ TEST(VariableLengthArray, JsonArray_Add) {
JsonArray& arr = jsonBuffer.createArray(); JsonArray& arr = jsonBuffer.createArray();
arr.add(vla); arr.add(vla);
EXPECT_STREQ("world", arr[0]); REQUIRE(std::string("world") == arr[0]);
} }
TEST(VariableLengthArray, JsonArray_Set) { SECTION("JsonArray_Set") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "world"); strcpy(vla, "world");
@ -313,10 +314,10 @@ TEST(VariableLengthArray, JsonArray_Set) {
arr.add("hello"); arr.add("hello");
arr.set(0, vla); arr.set(0, vla);
EXPECT_STREQ("world", arr[0]); REQUIRE(std::string("world") == arr[0]);
} }
TEST(VariableLengthArray, JsonArraySubscript_Set) { SECTION("JsonArraySubscript_Set") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "world"); strcpy(vla, "world");
@ -326,10 +327,10 @@ TEST(VariableLengthArray, JsonArraySubscript_Set) {
arr.add("hello"); arr.add("hello");
arr[0].set(vla); arr[0].set(vla);
EXPECT_STREQ("world", arr[0]); REQUIRE(std::string("world") == arr[0]);
} }
TEST(VariableLengthArray, JsonArraySubscript_Assign) { SECTION("JsonArraySubscript_Assign") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "world"); strcpy(vla, "world");
@ -339,10 +340,10 @@ TEST(VariableLengthArray, JsonArraySubscript_Assign) {
arr.add("hello"); arr.add("hello");
arr[0] = vla; arr[0] = vla;
EXPECT_STREQ("world", arr[0]); REQUIRE(std::string("world") == arr[0]);
} }
TEST(VariableLengthArray, JsonBuffer_strdup) { SECTION("JsonBuffer_strdup") {
int i = 16; int i = 16;
char vla[i]; char vla[i];
strcpy(vla, "world"); strcpy(vla, "world");
@ -350,8 +351,8 @@ TEST(VariableLengthArray, JsonBuffer_strdup) {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
const char* dup = jsonBuffer.strdup(vla); const char* dup = jsonBuffer.strdup(vla);
EXPECT_NE(static_cast<const void*>(vla), static_cast<const void*>(dup)); REQUIRE(static_cast<const void*>(vla) != static_cast<const void*>(dup));
EXPECT_STREQ("world", dup); REQUIRE(std::string("world") == dup);
}
} }
#endif #endif

View File

@ -12,5 +12,5 @@ add_executable(PolyfillsTests
parseInteger.cpp parseInteger.cpp
) )
target_link_libraries(PolyfillsTests gtest) target_link_libraries(PolyfillsTests catch)
add_test(Polyfills PolyfillsTests) add_test(Polyfills PolyfillsTests)

View File

@ -5,81 +5,75 @@
// https://bblanchon.github.io/ArduinoJson/ // https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star! // If you like this project, please add a star!
#include <gtest/gtest.h>
#include <ArduinoJson/Polyfills/isFloat.hpp> #include <ArduinoJson/Polyfills/isFloat.hpp>
#include <catch.hpp>
using namespace ArduinoJson::Polyfills; using namespace ArduinoJson::Polyfills;
struct Polyfills_IsFloat_Tests : testing::Test { TEST_CASE("isFloat()") {
void check(bool expected, const char* input) { SECTION("Input is NULL") {
bool actual = isFloat(input); REQUIRE(isFloat(NULL) == false);
EXPECT_EQ(expected, actual) << input;
}
};
#define TEST_(X) TEST_F(Polyfills_IsFloat_Tests, X)
TEST_(Null) {
check(false, NULL);
} }
TEST_(NoExponent) { SECTION("NoExponent") {
check(true, "3.14"); REQUIRE(isFloat("3.14"));
check(true, "-3.14"); REQUIRE(isFloat("-3.14"));
check(true, "+3.14"); REQUIRE(isFloat("+3.14"));
} }
TEST_(IntegralPartMissing) { SECTION("IntegralPartMissing") {
check(true, ".14"); REQUIRE(isFloat(".14"));
check(true, "-.14"); REQUIRE(isFloat("-.14"));
check(true, "+.14"); REQUIRE(isFloat("+.14"));
} }
TEST_(FractionalPartMissing) { SECTION("FractionalPartMissing") {
check(true, "3."); REQUIRE(isFloat("3."));
check(true, "-3.e14"); REQUIRE(isFloat("-3.e14"));
check(true, "+3.e-14"); REQUIRE(isFloat("+3.e-14"));
} }
TEST_(NoDot) { SECTION("NoDot") {
check(true, "3e14"); REQUIRE(isFloat("3e14"));
check(true, "3e-14"); REQUIRE(isFloat("3e-14"));
check(true, "3e+14"); REQUIRE(isFloat("3e+14"));
} }
TEST_(Integer) { SECTION("Integer") {
check(false, "14"); REQUIRE_FALSE(isFloat("14"));
check(false, "-14"); REQUIRE_FALSE(isFloat("-14"));
check(false, "+14"); REQUIRE_FALSE(isFloat("+14"));
} }
TEST_(ExponentMissing) { SECTION("ExponentMissing") {
check(false, "3.14e"); REQUIRE_FALSE(isFloat("3.14e"));
check(false, "3.14e-"); REQUIRE_FALSE(isFloat("3.14e-"));
check(false, "3.14e+"); REQUIRE_FALSE(isFloat("3.14e+"));
} }
TEST_(JustASign) { SECTION("JustASign") {
check(false, "-"); REQUIRE_FALSE(isFloat("-"));
check(false, "+"); REQUIRE_FALSE(isFloat("+"));
} }
TEST_(Empty) { SECTION("Empty") {
check(false, ""); REQUIRE_FALSE(isFloat(""));
} }
TEST_(NaN) { SECTION("NaN") {
check(true, "NaN"); REQUIRE(isFloat("NaN"));
check(false, "n"); REQUIRE_FALSE(isFloat("n"));
check(false, "N"); REQUIRE_FALSE(isFloat("N"));
check(false, "nan"); REQUIRE_FALSE(isFloat("nan"));
check(false, "-NaN"); REQUIRE_FALSE(isFloat("-NaN"));
check(false, "+NaN"); REQUIRE_FALSE(isFloat("+NaN"));
} }
TEST_(Infinity) { SECTION("Infinity") {
check(true, "Infinity"); REQUIRE(isFloat("Infinity"));
check(true, "+Infinity"); REQUIRE(isFloat("+Infinity"));
check(true, "-Infinity"); REQUIRE(isFloat("-Infinity"));
check(false, "infinity"); REQUIRE_FALSE(isFloat("infinity"));
check(false, "Inf"); REQUIRE_FALSE(isFloat("Inf"));
}
} }

View File

@ -5,41 +5,35 @@
// https://bblanchon.github.io/ArduinoJson/ // https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star! // If you like this project, please add a star!
#include <gtest/gtest.h>
#include <ArduinoJson/Polyfills/isInteger.hpp> #include <ArduinoJson/Polyfills/isInteger.hpp>
#include <catch.hpp>
using namespace ArduinoJson::Polyfills; using namespace ArduinoJson::Polyfills;
struct Polyfills_IsInteger_Tests : testing::Test { TEST_CASE("isInteger()") {
void check(bool expected, const char* input) { SECTION("Null") {
bool actual = isInteger(input); REQUIRE_FALSE(isInteger(NULL));
EXPECT_EQ(expected, actual) << input;
}
};
#define TEST_(X) TEST_F(Polyfills_IsInteger_Tests, X)
TEST_(Null) {
check(false, NULL);
} }
TEST_(FloatNotInteger) { SECTION("FloatNotInteger") {
check(false, "3.14"); REQUIRE_FALSE(isInteger("3.14"));
check(false, "-3.14"); REQUIRE_FALSE(isInteger("-3.14"));
check(false, "+3.14"); REQUIRE_FALSE(isInteger("+3.14"));
} }
TEST_(Spaces) { SECTION("Spaces") {
check(false, "42 "); REQUIRE_FALSE(isInteger("42 "));
check(false, " 42"); REQUIRE_FALSE(isInteger(" 42"));
} }
TEST_(Valid) { SECTION("Valid") {
check(true, "42"); REQUIRE(isInteger("42"));
check(true, "-42"); REQUIRE(isInteger("-42"));
check(true, "+42"); REQUIRE(isInteger("+42"));
} }
TEST_(ExtraSign) { SECTION("ExtraSign") {
check(false, "--42"); REQUIRE_FALSE(isInteger("--42"));
check(false, "++42"); REQUIRE_FALSE(isInteger("++42"));
}
} }

View File

@ -5,179 +5,166 @@
// https://bblanchon.github.io/ArduinoJson/ // https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star! // If you like this project, please add a star!
#include <gtest/gtest.h>
#include <ArduinoJson/Polyfills/parseFloat.hpp> #include <ArduinoJson/Polyfills/parseFloat.hpp>
#include <catch.hpp>
using namespace ArduinoJson::Polyfills; using namespace ArduinoJson::Polyfills;
struct Polyfills_ParseFloat_Float_Tests : testing::Test { template <typename T>
void check(const char* input, float expected) { void check(const char* input, T expected) {
float actual = parseFloat<float>(input); CAPTURE(input);
EXPECT_FLOAT_EQ(expected, actual) << input; REQUIRE(parseFloat<T>(input) == Approx(expected));
} }
template <typename T>
void checkNaN(const char* input) { void checkNaN(const char* input) {
float result = parseFloat<float>(input); CAPTURE(input);
EXPECT_TRUE(result != result) << input; T result = parseFloat<T>(input);
REQUIRE(result != result);
} }
template <typename T>
void checkInf(const char* input, bool negative) { void checkInf(const char* input, bool negative) {
float x = parseFloat<float>(input); CAPTURE(input);
T x = parseFloat<T>(input);
if (negative) if (negative)
EXPECT_TRUE(x < 0) << input; REQUIRE(x < 0);
else else
EXPECT_TRUE(x > 0) << input; REQUIRE(x > 0);
EXPECT_TRUE(x == x && x * 2 == x) << input; REQUIRE(x == x); // not a NaN
} REQUIRE(x * 2 == x); // a property of infinity
};
#define TEST_FLOAT(X) TEST_F(Polyfills_ParseFloat_Float_Tests, X)
struct Polyfills_ParseFloat_Double_Tests : testing::Test {
void check(const char* input, double expected) {
double actual = parseFloat<double>(input);
EXPECT_DOUBLE_EQ(expected, actual) << input;
} }
void checkNaN(const char* input) { TEST_CASE("parseFloat<float>()") {
double result = parseFloat<double>(input); SECTION("Null") {
EXPECT_TRUE(result != result) << input; check<float>(NULL, 0);
} }
void checkInf(const char* input, bool negative) { SECTION("Float_Short_NoExponent") {
double x = parseFloat<double>(input); check<float>("3.14", 3.14f);
if (negative) check<float>("-3.14", -3.14f);
EXPECT_TRUE(x < 0) << input; check<float>("+3.14", +3.14f);
else
EXPECT_TRUE(x > 0) << input;
EXPECT_TRUE(x == x && x * 2 == x) << input;
}
};
#define TEST_DOUBLE(X) TEST_F(Polyfills_ParseFloat_Double_Tests, X)
TEST_DOUBLE(Null) {
check(NULL, 0);
} }
TEST_FLOAT(Null) { SECTION("Short_NoDot") {
check(NULL, 0); check<float>("1E+38", 1E+38f);
check<float>("-1E+38", -1E+38f);
check<float>("+1E-38", +1E-38f);
check<float>("+1e+38", +1e+38f);
check<float>("-1e-38", -1e-38f);
} }
TEST_DOUBLE(Short_NoExponent) { SECTION("Max") {
check("3.14", 3.14); check<float>("340.2823e+36", 3.402823e+38f);
check("-3.14", -3.14); check<float>("34.02823e+37", 3.402823e+38f);
check("+3.14", +3.14); check<float>("3.402823e+38", 3.402823e+38f);
check<float>("0.3402823e+39", 3.402823e+38f);
check<float>("0.03402823e+40", 3.402823e+38f);
check<float>("0.003402823e+41", 3.402823e+38f);
} }
TEST_FLOAT(Float_Short_NoExponent) { SECTION("VeryLong") {
check("3.14", 3.14f); check<float>("0.00000000000000000000000000000001", 1e-32f);
check("-3.14", -3.14f); check<float>("100000000000000000000000000000000.0", 1e+32f);
check("+3.14", +3.14f); check<float>(
} "100000000000000000000000000000000.00000000000000000000000000000",
TEST_DOUBLE(Short_NoDot) {
check("1E+308", 1E+308);
check("-1E+308", -1E+308);
check("+1E-308", +1E-308);
check("+1e+308", +1e+308);
check("-1e-308", -1e-308);
}
TEST_FLOAT(Short_NoDot) {
check("1E+38", 1E+38f);
check("-1E+38", -1E+38f);
check("+1E-38", +1E-38f);
check("+1e+38", +1e+38f);
check("-1e-38", -1e-38f);
}
TEST_FLOAT(Max) {
check("340.2823e+36", 3.402823e+38f);
check("34.02823e+37", 3.402823e+38f);
check("3.402823e+38", 3.402823e+38f);
check("0.3402823e+39", 3.402823e+38f);
check("00.3402823e+40", 3.402823e+38f);
check("000.3402823e+41", 3.402823e+38f);
}
TEST_DOUBLE(Max) {
check(".017976931348623147e+310", 1.7976931348623147e+308);
check(".17976931348623147e+309", 1.7976931348623147e+308);
check("1.7976931348623147e+308", 1.7976931348623147e+308);
check("17.976931348623147e+307", 1.7976931348623147e+308);
check("179.76931348623147e+306", 1.7976931348623147e+308);
}
TEST_DOUBLE(Min) {
check(".022250738585072014e-306", 2.2250738585072014e-308);
check(".22250738585072014e-307", 2.2250738585072014e-308);
check("2.2250738585072014e-308", 2.2250738585072014e-308);
check("22.250738585072014e-309", 2.2250738585072014e-308);
check("222.50738585072014e-310", 2.2250738585072014e-308);
}
TEST_DOUBLE(VeryLong) {
check("0.00000000000000000000000000000001", 1e-32);
check("100000000000000000000000000000000.0", 1e+32);
check("100000000000000000000000000000000.00000000000000000000000000000",
1e+32);
}
TEST_FLOAT(VeryLong) {
check("0.00000000000000000000000000000001", 1e-32f);
check("100000000000000000000000000000000.0", 1e+32f);
check("100000000000000000000000000000000.00000000000000000000000000000",
1e+32f); 1e+32f);
} }
TEST_DOUBLE(MantissaTooLongToFit) { SECTION("MantissaTooLongToFit") {
check("0.179769313486231571111111111111", 0.17976931348623157); check<float>("0.340282346638528861111111111111", 0.34028234663852886f);
check("17976931348623157.11111111111111", 17976931348623157.0); check<float>("34028234663852886.11111111111111", 34028234663852886.0f);
check("1797693.134862315711111111111111", 1797693.1348623157); check<float>("34028234.66385288611111111111111", 34028234.663852886f);
check("-0.179769313486231571111111111111", -0.17976931348623157); check<float>("-0.340282346638528861111111111111", -0.34028234663852886f);
check("-17976931348623157.11111111111111", -17976931348623157.0); check<float>("-34028234663852886.11111111111111", -34028234663852886.0f);
check("-1797693.134862315711111111111111", -1797693.1348623157); check<float>("-34028234.66385288611111111111111", -34028234.663852886f);
} }
TEST_FLOAT(MantissaTooLongToFit) { SECTION("ExponentTooBig") {
check("0.340282346638528861111111111111", 0.34028234663852886f); checkInf<float>("1e39", false);
check("34028234663852886.11111111111111", 34028234663852886.0f); checkInf<float>("-1e39", true);
check("34028234.66385288611111111111111", 34028234.663852886f); checkInf<float>("1e255", false);
check<float>("1e-255", 0.0f);
check("-0.340282346638528861111111111111", -0.34028234663852886f);
check("-34028234663852886.11111111111111", -34028234663852886.0f);
check("-34028234.66385288611111111111111", -34028234.663852886f);
} }
TEST_DOUBLE(ExponentTooBig) { SECTION("NaN") {
checkInf("1e309", false); checkNaN<float>("NaN");
checkInf("-1e309", true); checkNaN<float>("nan");
checkInf("1e65535", false);
check("1e-65535", 0.0);
} }
TEST_FLOAT(ExponentTooBig) { SECTION("Infinity") {
checkInf("1e39", false); checkInf<float>("Infinity", false);
checkInf("-1e39", true); checkInf<float>("+Infinity", false);
checkInf("1e255", false); checkInf<float>("-Infinity", true);
check("1e-255", 0.0f); checkInf<float>("inf", false);
checkInf<float>("+inf", false);
checkInf<float>("-inf", true);
}
} }
TEST_DOUBLE(NaN) { TEST_CASE("parseFloat<double>()") {
checkNaN("NaN"); SECTION("Null") {
checkNaN("nan"); check<double>(NULL, 0);
} }
TEST_FLOAT(NaN) { SECTION("Short_NoExponent") {
checkNaN("NaN"); check<double>("3.14", 3.14);
checkNaN("nan"); check<double>("-3.14", -3.14);
check<double>("+3.14", +3.14);
} }
TEST_FLOAT(Infinity) { SECTION("Short_NoDot") {
checkInf("Infinity", false); check<double>("1E+308", 1E+308);
checkInf("+Infinity", false); check<double>("-1E+308", -1E+308);
checkInf("-Infinity", true); check<double>("+1E-308", +1E-308);
checkInf("inf", false); check<double>("+1e+308", +1e+308);
checkInf("+inf", false); check<double>("-1e-308", -1e-308);
checkInf("-inf", true); }
SECTION("Max") {
check<double>(".017976931348623147e+310", 1.7976931348623147e+308);
check<double>(".17976931348623147e+309", 1.7976931348623147e+308);
check<double>("1.7976931348623147e+308", 1.7976931348623147e+308);
check<double>("17.976931348623147e+307", 1.7976931348623147e+308);
check<double>("179.76931348623147e+306", 1.7976931348623147e+308);
}
SECTION("Min") {
check<double>(".022250738585072014e-306", 2.2250738585072014e-308);
check<double>(".22250738585072014e-307", 2.2250738585072014e-308);
check<double>("2.2250738585072014e-308", 2.2250738585072014e-308);
check<double>("22.250738585072014e-309", 2.2250738585072014e-308);
check<double>("222.50738585072014e-310", 2.2250738585072014e-308);
}
SECTION("VeryLong") {
check<double>("0.00000000000000000000000000000001", 1e-32);
check<double>("100000000000000000000000000000000.0", 1e+32);
check<double>(
"100000000000000000000000000000000.00000000000000000000000000000",
1e+32);
}
SECTION("MantissaTooLongToFit") {
check<double>("0.179769313486231571111111111111", 0.17976931348623157);
check<double>("17976931348623157.11111111111111", 17976931348623157.0);
check<double>("1797693.134862315711111111111111", 1797693.1348623157);
check<double>("-0.179769313486231571111111111111", -0.17976931348623157);
check<double>("-17976931348623157.11111111111111", -17976931348623157.0);
check<double>("-1797693.134862315711111111111111", -1797693.1348623157);
}
SECTION("ExponentTooBig") {
checkInf<double>("1e309", false);
checkInf<double>("-1e309", true);
checkInf<double>("1e65535", false);
check<double>("1e-65535", 0.0);
}
SECTION("NaN") {
checkNaN<double>("NaN");
checkNaN<double>("nan");
}
} }

View File

@ -5,23 +5,20 @@
// https://bblanchon.github.io/ArduinoJson/ // https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star! // If you like this project, please add a star!
#include <gtest/gtest.h>
#include <stdint.h> #include <stdint.h>
#include <ArduinoJson/Polyfills/parseInteger.hpp> #include <ArduinoJson/Polyfills/parseInteger.hpp>
#include <catch.hpp>
using namespace ArduinoJson::Polyfills; using namespace ArduinoJson::Polyfills;
struct Polyfills_ParseInteger_Tests : testing::Test {
template <typename T> template <typename T>
void check(const char* input, T expected) { void check(const char* input, T expected) {
CAPTURE(input);
T actual = parseInteger<T>(input); T actual = parseInteger<T>(input);
EXPECT_EQ(expected, actual) << input; REQUIRE(expected == actual);
} }
};
#define TEST_(X) TEST_F(Polyfills_ParseInteger_Tests, X) TEST_CASE("parseInteger<int8_t>()") {
TEST_(int8_t) {
check<int8_t>("-128", -128); check<int8_t>("-128", -128);
check<int8_t>("127", 127); check<int8_t>("127", 127);
check<int8_t>("+127", 127); check<int8_t>("+127", 127);
@ -33,7 +30,7 @@ TEST_(int8_t) {
check<int8_t>(NULL, 0); check<int8_t>(NULL, 0);
} }
TEST_(int16_t) { TEST_CASE("parseInteger<int16_t>()") {
check<int16_t>("-32768", -32768); check<int16_t>("-32768", -32768);
check<int16_t>("32767", 32767); check<int16_t>("32767", 32767);
check<int16_t>("+32767", 32767); check<int16_t>("+32767", 32767);
@ -45,7 +42,7 @@ TEST_(int16_t) {
check<int16_t>(NULL, 0); check<int16_t>(NULL, 0);
} }
TEST_(int32_t) { TEST_CASE("parseInteger<int32_t>()") {
check<int32_t>("-2147483648", (-2147483647 - 1)); check<int32_t>("-2147483648", (-2147483647 - 1));
check<int32_t>("2147483647", 2147483647); check<int32_t>("2147483647", 2147483647);
check<int32_t>("+2147483647", 2147483647); check<int32_t>("+2147483647", 2147483647);
@ -56,7 +53,7 @@ TEST_(int32_t) {
check<int32_t>("2147483648", (-2147483647 - 1)); check<int32_t>("2147483648", (-2147483647 - 1));
} }
TEST_(uint8_t) { TEST_CASE("parseInteger<uint8_t>()") {
check<uint8_t>("0", 0); check<uint8_t>("0", 0);
check<uint8_t>("255", 255); check<uint8_t>("255", 255);
check<uint8_t>("+255", 255); check<uint8_t>("+255", 255);
@ -67,7 +64,7 @@ TEST_(uint8_t) {
check<uint8_t>("256", 0); check<uint8_t>("256", 0);
} }
TEST_(uint16_t) { TEST_CASE("parseInteger<uint16_t>()") {
check<uint16_t>("0", 0); check<uint16_t>("0", 0);
check<uint16_t>("65535", 65535); check<uint16_t>("65535", 65535);
check<uint16_t>("+65535", 65535); check<uint16_t>("+65535", 65535);

View File

@ -6,13 +6,13 @@
# If you like this project, please add a star! # If you like this project, please add a star!
add_executable(StaticJsonBufferTests add_executable(StaticJsonBufferTests
basics.cpp alloc.cpp
createArray.cpp createArray.cpp
createObject.cpp createObject.cpp
parseArray.cpp parseArray.cpp
parseObject.cpp parseObject.cpp
string.cpp startString.cpp
) )
target_link_libraries(StaticJsonBufferTests gtest) target_link_libraries(StaticJsonBufferTests catch)
add_test(StaticJsonBuffer StaticJsonBufferTests) add_test(StaticJsonBuffer StaticJsonBufferTests)

View File

@ -0,0 +1,68 @@
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <catch.hpp>
static bool isAligned(void *ptr) {
const size_t mask = sizeof(void *) - 1;
size_t addr = reinterpret_cast<size_t>(ptr);
return (addr & mask) == 0;
}
TEST_CASE("StaticJsonBuffer::alloc()") {
StaticJsonBuffer<64> buffer;
SECTION("CapacityMatchTemplateParameter") {
REQUIRE(64 == buffer.capacity());
}
SECTION("InitialSizeIsZero") {
REQUIRE(0 == buffer.size());
}
SECTION("GrowsAfterAlloc") {
buffer.alloc(1);
REQUIRE(1U <= buffer.size());
buffer.alloc(1);
REQUIRE(2U <= buffer.size());
}
SECTION("DoesntGrowWhenFull") {
buffer.alloc(64);
buffer.alloc(1);
REQUIRE(64 == buffer.size());
}
SECTION("DoesntGrowWhenTooSmall") {
buffer.alloc(65);
REQUIRE(0 == buffer.size());
}
SECTION("ReturnsNonNull") {
void *p = buffer.alloc(64);
REQUIRE(0 != p);
}
SECTION("ReturnsNullWhenFull") {
buffer.alloc(64);
void *p = buffer.alloc(1);
REQUIRE(0 == p);
}
SECTION("ReturnsNullWhenTooSmall") {
void *p = buffer.alloc(65);
REQUIRE(0 == p);
}
SECTION("Alignment") {
for (size_t size = 1; size <= sizeof(void *); size++) {
void *p = buffer.alloc(1);
REQUIRE(isAligned(p));
}
}
}

View File

@ -1,65 +0,0 @@
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
class StaticJsonBuffer_Basic_Tests : public testing::Test {
protected:
StaticJsonBuffer<64> buffer;
};
TEST_F(StaticJsonBuffer_Basic_Tests, CapacityMatchTemplateParameter) {
ASSERT_EQ(64, buffer.capacity());
}
TEST_F(StaticJsonBuffer_Basic_Tests, InitialSizeIsZero) {
ASSERT_EQ(0, buffer.size());
}
TEST_F(StaticJsonBuffer_Basic_Tests, GrowsAfterAlloc) {
buffer.alloc(1);
ASSERT_LE(1U, buffer.size());
buffer.alloc(1);
ASSERT_LE(2U, buffer.size());
}
TEST_F(StaticJsonBuffer_Basic_Tests, DoesntGrowWhenFull) {
buffer.alloc(64);
buffer.alloc(1);
ASSERT_EQ(64, buffer.size());
}
TEST_F(StaticJsonBuffer_Basic_Tests, DoesntGrowWhenTooSmall) {
buffer.alloc(65);
ASSERT_EQ(0, buffer.size());
}
TEST_F(StaticJsonBuffer_Basic_Tests, ReturnsNonNull) {
void *p = buffer.alloc(64);
ASSERT_NE(static_cast<void *>(0), p);
}
TEST_F(StaticJsonBuffer_Basic_Tests, ReturnsNullWhenFull) {
buffer.alloc(64);
void *p = buffer.alloc(1);
ASSERT_EQ(NULL, p);
}
TEST_F(StaticJsonBuffer_Basic_Tests, ReturnsNullWhenTooSmall) {
void *p = buffer.alloc(65);
ASSERT_EQ(NULL, p);
}
TEST_F(StaticJsonBuffer_Basic_Tests, Alignment) {
size_t mask = sizeof(void *) - 1;
for (size_t size = 1; size <= sizeof(void *); size++) {
size_t addr = reinterpret_cast<size_t>(buffer.alloc(1));
ASSERT_EQ(0, addr & mask);
}
}

View File

@ -6,41 +6,43 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
TEST(StaticJsonBuffer_CreateArray_Tests, GrowsWithArray) { TEST_CASE("StaticJsonBuffer::createArray()") {
SECTION("GrowsWithArray") {
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json; StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json;
JsonArray &array = json.createArray(); JsonArray &array = json.createArray();
ASSERT_EQ(JSON_ARRAY_SIZE(0), json.size()); REQUIRE(JSON_ARRAY_SIZE(0) == json.size());
array.add("hello"); array.add("hello");
ASSERT_EQ(JSON_ARRAY_SIZE(1), json.size()); REQUIRE(JSON_ARRAY_SIZE(1) == json.size());
array.add("world"); array.add("world");
ASSERT_EQ(JSON_ARRAY_SIZE(2), json.size()); REQUIRE(JSON_ARRAY_SIZE(2) == json.size());
} }
TEST(StaticJsonBuffer_CreateArray_Tests, SucceedWhenBigEnough) { SECTION("SucceedWhenBigEnough") {
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> json; StaticJsonBuffer<JSON_ARRAY_SIZE(0)> json;
JsonArray &array = json.createArray(); JsonArray &array = json.createArray();
ASSERT_TRUE(array.success()); REQUIRE(array.success());
} }
TEST(StaticJsonBuffer_CreateArray_Tests, FailsWhenTooSmall) { SECTION("FailsWhenTooSmall") {
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> json; StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> json;
JsonArray &array = json.createArray(); JsonArray &array = json.createArray();
ASSERT_FALSE(array.success()); REQUIRE_FALSE(array.success());
} }
TEST(StaticJsonBuffer_CreateArray_Tests, ArrayDoesntGrowWhenFull) { SECTION("ArrayDoesntGrowWhenFull") {
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> json; StaticJsonBuffer<JSON_ARRAY_SIZE(1)> json;
JsonArray &array = json.createArray(); JsonArray &array = json.createArray();
array.add("hello"); array.add("hello");
array.add("world"); array.add("world");
EXPECT_EQ(1, array.size()); REQUIRE(1 == array.size());
}
} }

View File

@ -6,52 +6,54 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
TEST(StaticJsonBuffer_CreateObject_Tests, GrowsWithObject) { TEST_CASE("StaticJsonBuffer::createObject()") {
SECTION("GrowsWithObject") {
StaticJsonBuffer<JSON_OBJECT_SIZE(3)> buffer; StaticJsonBuffer<JSON_OBJECT_SIZE(3)> buffer;
JsonObject &obj = buffer.createObject(); JsonObject &obj = buffer.createObject();
ASSERT_EQ(JSON_OBJECT_SIZE(0), buffer.size()); REQUIRE(JSON_OBJECT_SIZE(0) == buffer.size());
obj["hello"]; obj["hello"];
ASSERT_EQ(JSON_OBJECT_SIZE(0), buffer.size()); REQUIRE(JSON_OBJECT_SIZE(0) == buffer.size());
obj["hello"] = 1; obj["hello"] = 1;
ASSERT_EQ(JSON_OBJECT_SIZE(1), buffer.size()); REQUIRE(JSON_OBJECT_SIZE(1) == buffer.size());
obj["world"] = 2; obj["world"] = 2;
ASSERT_EQ(JSON_OBJECT_SIZE(2), buffer.size()); REQUIRE(JSON_OBJECT_SIZE(2) == buffer.size());
obj["world"] = 3; // <- same key, should not grow obj["world"] = 3; // <- same key, should not grow
ASSERT_EQ(JSON_OBJECT_SIZE(2), buffer.size()); REQUIRE(JSON_OBJECT_SIZE(2) == buffer.size());
} }
TEST(StaticJsonBuffer_CreateObject_Tests, SucceedWhenBigEnough) { SECTION("SucceedWhenBigEnough") {
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> buffer; StaticJsonBuffer<JSON_OBJECT_SIZE(0)> buffer;
JsonObject &object = buffer.createObject(); JsonObject &object = buffer.createObject();
ASSERT_TRUE(object.success()); REQUIRE(object.success());
} }
TEST(StaticJsonBuffer_CreateObject_Tests, FailsWhenTooSmall) { SECTION("FailsWhenTooSmall") {
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> buffer; StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> buffer;
JsonObject &object = buffer.createObject(); JsonObject &object = buffer.createObject();
ASSERT_FALSE(object.success()); REQUIRE_FALSE(object.success());
} }
TEST(StaticJsonBuffer_CreateObject_Tests, ObjectDoesntGrowWhenFull) { SECTION("ObjectDoesntGrowWhenFull") {
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> buffer; StaticJsonBuffer<JSON_OBJECT_SIZE(1)> buffer;
JsonObject &obj = buffer.createObject(); JsonObject &obj = buffer.createObject();
obj["hello"] = 1; obj["hello"] = 1;
obj["world"] = 2; obj["world"] = 2;
ASSERT_EQ(JSON_OBJECT_SIZE(1), buffer.size()); REQUIRE(JSON_OBJECT_SIZE(1) == buffer.size());
ASSERT_EQ(1, obj.size()); REQUIRE(1 == obj.size());
char json[64]; char json[64];
obj.printTo(json, sizeof(json)); obj.printTo(json, sizeof(json));
ASSERT_STREQ("{\"hello\":1}", json); REQUIRE(std::string("{\"hello\":1}") == json);
}
} }

View File

@ -6,91 +6,69 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
class StaticJsonBuffer_ParseArray_Tests : public testing::Test { TEST_CASE("StaticJsonBuffer::parseArray()") {
protected: SECTION("TooSmallBufferForEmptyArray") {
void with(StaticJsonBufferBase& jsonBuffer) {
_jsonBuffer = &jsonBuffer;
}
void whenInputIs(const char* json) {
strcpy(_jsonString, json);
}
void parseMustSucceed() {
EXPECT_TRUE(_jsonBuffer->parseArray(_jsonString).success());
}
void parseMustFail() {
EXPECT_FALSE(_jsonBuffer->parseArray(_jsonString).success());
}
private:
StaticJsonBufferBase* _jsonBuffer;
char _jsonString[256];
};
TEST_F(StaticJsonBuffer_ParseArray_Tests, TooSmallBufferForEmptyArray) {
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> bufferTooSmall; StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
with(bufferTooSmall); char input[] = "[]";
whenInputIs("[]"); JsonArray& arr = bufferTooSmall.parseArray(input);
parseMustFail(); REQUIRE_FALSE(arr.success());
} }
TEST_F(StaticJsonBuffer_ParseArray_Tests, BufferOfTheRightSizeForEmptyArray) { SECTION("BufferOfTheRightSizeForEmptyArray") {
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> bufferOfRightSize; StaticJsonBuffer<JSON_ARRAY_SIZE(0)> bufferOfRightSize;
with(bufferOfRightSize); char input[] = "[]";
whenInputIs("[]"); JsonArray& arr = bufferOfRightSize.parseArray(input);
parseMustSucceed(); REQUIRE(arr.success());
} }
TEST_F(StaticJsonBuffer_ParseArray_Tests, TooSmallBufferForArrayWithOneValue) { SECTION("TooSmallBufferForArrayWithOneValue") {
StaticJsonBuffer<JSON_ARRAY_SIZE(1) - 1> bufferTooSmall; StaticJsonBuffer<JSON_ARRAY_SIZE(1) - 1> bufferTooSmall;
with(bufferTooSmall); char input[] = "[1]";
whenInputIs("[1]"); JsonArray& arr = bufferTooSmall.parseArray(input);
parseMustFail(); REQUIRE_FALSE(arr.success());
} }
TEST_F(StaticJsonBuffer_ParseArray_Tests, SECTION("BufferOfTheRightSizeForArrayWithOneValue") {
BufferOfTheRightSizeForArrayWithOneValue) {
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> bufferOfRightSize; StaticJsonBuffer<JSON_ARRAY_SIZE(1)> bufferOfRightSize;
with(bufferOfRightSize); char input[] = "[1]";
whenInputIs("[1]"); JsonArray& arr = bufferOfRightSize.parseArray(input);
parseMustSucceed(); REQUIRE(arr.success());
} }
TEST_F(StaticJsonBuffer_ParseArray_Tests, SECTION("TooSmallBufferForArrayWithNestedObject") {
TooSmallBufferForArrayWithNestedObject) { StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1>
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1> bufferTooSmall; bufferTooSmall;
with(bufferTooSmall); char input[] = "[{}]";
whenInputIs("[{}]"); JsonArray& arr = bufferTooSmall.parseArray(input);
parseMustFail(); REQUIRE_FALSE(arr.success());
} }
TEST_F(StaticJsonBuffer_ParseArray_Tests, SECTION("BufferOfTheRightSizeForArrayWithNestedObject") {
BufferOfTheRightSizeForArrayWithNestedObject) { StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)>
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)> bufferOfRightSize; bufferOfRightSize;
with(bufferOfRightSize); char input[] = "[{}]";
whenInputIs("[{}]"); JsonArray& arr = bufferOfRightSize.parseArray(input);
parseMustSucceed(); REQUIRE(arr.success());
} }
TEST_F(StaticJsonBuffer_ParseArray_Tests, CharPtrNull) { SECTION("CharPtrNull") {
ASSERT_FALSE( REQUIRE_FALSE(
StaticJsonBuffer<100>().parseArray(static_cast<char*>(0)).success()); StaticJsonBuffer<100>().parseArray(static_cast<char*>(0)).success());
} }
TEST_F(StaticJsonBuffer_ParseArray_Tests, ConstCharPtrNull) { SECTION("ConstCharPtrNull") {
ASSERT_FALSE(StaticJsonBuffer<100>() REQUIRE_FALSE(StaticJsonBuffer<100>()
.parseArray(static_cast<const char*>(0)) .parseArray(static_cast<const char*>(0))
.success()); .success());
} }
TEST_F(StaticJsonBuffer_ParseArray_Tests, CopyStringNotSpaces) { SECTION("CopyStringNotSpaces") {
StaticJsonBuffer<100> jsonBuffer; StaticJsonBuffer<100> jsonBuffer;
jsonBuffer.parseArray(" [ \"1234567\" ] "); jsonBuffer.parseArray(" [ \"1234567\" ] ");
ASSERT_EQ(JSON_ARRAY_SIZE(1) + sizeof("1234567"), jsonBuffer.size()); REQUIRE(JSON_ARRAY_SIZE(1) + sizeof("1234567") == jsonBuffer.size());
// note we use a string of 8 bytes to be sure that the StaticJsonBuffer // note we use a string of 8 bytes to be sure that the StaticJsonBuffer
// will not insert bytes to enforce alignement // will not insert bytes to enforce alignement
} }
}

View File

@ -6,84 +6,60 @@
// If you like this project, please add a star! // If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h> #include <catch.hpp>
TEST_CASE("StaticJsonBuffer::parseObject()") {
class StaticJsonBuffer_ParseObject_Tests : public testing::Test { SECTION("TooSmallBufferForEmptyObject") {
protected:
void with(StaticJsonBufferBase& jsonBuffer) {
_jsonBuffer = &jsonBuffer;
}
void whenInputIs(const char* json) {
strcpy(_jsonString, json);
}
void parseMustSucceed() {
EXPECT_TRUE(_jsonBuffer->parseObject(_jsonString).success());
}
void parseMustFail() {
EXPECT_FALSE(_jsonBuffer->parseObject(_jsonString).success());
}
private:
StaticJsonBufferBase* _jsonBuffer;
char _jsonString[256];
};
TEST_F(StaticJsonBuffer_ParseObject_Tests, TooSmallBufferForEmptyObject) {
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> bufferTooSmall; StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
with(bufferTooSmall); char input[] = "{}";
whenInputIs("{}"); JsonObject& obj = bufferTooSmall.parseObject(input);
parseMustFail(); REQUIRE_FALSE(obj.success());
} }
TEST_F(StaticJsonBuffer_ParseObject_Tests, BufferOfTheRightSizeForEmptyObject) { SECTION("BufferOfTheRightSizeForEmptyObject") {
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> bufferOfRightSize; StaticJsonBuffer<JSON_OBJECT_SIZE(0)> bufferOfRightSize;
with(bufferOfRightSize); char input[] = "{}";
whenInputIs("{}"); JsonObject& obj = bufferOfRightSize.parseObject(input);
parseMustSucceed(); REQUIRE(obj.success());
} }
TEST_F(StaticJsonBuffer_ParseObject_Tests, SECTION("TooSmallBufferForObjectWithOneValue") {
TooSmallBufferForObjectWithOneValue) {
StaticJsonBuffer<JSON_OBJECT_SIZE(1) - 1> bufferTooSmall; StaticJsonBuffer<JSON_OBJECT_SIZE(1) - 1> bufferTooSmall;
with(bufferTooSmall); char input[] = "{\"a\":1}";
whenInputIs("{\"a\":1}"); JsonObject& obj = bufferTooSmall.parseObject(input);
parseMustFail(); REQUIRE_FALSE(obj.success());
} }
TEST_F(StaticJsonBuffer_ParseObject_Tests, SECTION("BufferOfTheRightSizeForObjectWithOneValue") {
BufferOfTheRightSizeForObjectWithOneValue) {
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> bufferOfRightSize; StaticJsonBuffer<JSON_OBJECT_SIZE(1)> bufferOfRightSize;
with(bufferOfRightSize); char input[] = "{\"a\":1}";
whenInputIs("{\"a\":1}"); JsonObject& obj = bufferOfRightSize.parseObject(input);
parseMustSucceed(); REQUIRE(obj.success());
} }
TEST_F(StaticJsonBuffer_ParseObject_Tests, SECTION("TooSmallBufferForObjectWithNestedObject") {
TooSmallBufferForObjectWithNestedObject) { StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1>
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1> bufferTooSmall; bufferTooSmall;
with(bufferTooSmall); char input[] = "{\"a\":[]}";
whenInputIs("{\"a\":[]}"); JsonObject& obj = bufferTooSmall.parseObject(input);
parseMustFail(); REQUIRE_FALSE(obj.success());
} }
TEST_F(StaticJsonBuffer_ParseObject_Tests, SECTION("BufferOfTheRightSizeForObjectWithNestedObject") {
BufferOfTheRightSizeForObjectWithNestedObject) { StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)>
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)> bufferOfRightSize; bufferOfRightSize;
with(bufferOfRightSize); char input[] = "{\"a\":[]}";
whenInputIs("{\"a\":[]}"); JsonObject& obj = bufferOfRightSize.parseObject(input);
parseMustSucceed(); REQUIRE(obj.success());
} }
TEST_F(StaticJsonBuffer_ParseObject_Tests, CharPtrNull) { SECTION("CharPtrNull") {
ASSERT_FALSE( REQUIRE_FALSE(
StaticJsonBuffer<100>().parseObject(static_cast<char*>(0)).success()); StaticJsonBuffer<100>().parseObject(static_cast<char*>(0)).success());
} }
TEST_F(StaticJsonBuffer_ParseObject_Tests, ConstCharPtrNull) { SECTION("ConstCharPtrNull") {
ASSERT_FALSE(StaticJsonBuffer<100>() REQUIRE_FALSE(StaticJsonBuffer<100>()
.parseObject(static_cast<const char*>(0)) .parseObject(static_cast<const char*>(0))
.success()); .success());
} }
}

View File

@ -0,0 +1,50 @@
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("StaticJsonBuffer::startString()") {
SECTION("WorksWhenBufferIsBigEnough") {
StaticJsonBuffer<6> jsonBuffer;
StaticJsonBufferBase::String str = jsonBuffer.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
REQUIRE(std::string("hello") == str.c_str());
}
SECTION("ReturnsNullWhenTooSmall") {
StaticJsonBuffer<5> jsonBuffer;
StaticJsonBufferBase::String str = jsonBuffer.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
REQUIRE(0 == str.c_str());
}
SECTION("SizeIncreases") {
StaticJsonBuffer<5> jsonBuffer;
StaticJsonBufferBase::String str = jsonBuffer.startString();
REQUIRE(0 == jsonBuffer.size());
str.append('h');
REQUIRE(1 == jsonBuffer.size());
str.c_str();
REQUIRE(2 == jsonBuffer.size());
}
}

View File

@ -1,48 +0,0 @@
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
TEST(StaticJsonBuffer_String_Tests, WorksWhenBufferIsBigEnough) {
StaticJsonBuffer<6> jsonBuffer;
StaticJsonBufferBase::String str = jsonBuffer.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
ASSERT_STREQ("hello", str.c_str());
}
TEST(StaticJsonBuffer_String_Tests, ReturnsNullWhenTooSmall) {
StaticJsonBuffer<5> jsonBuffer;
StaticJsonBufferBase::String str = jsonBuffer.startString();
str.append('h');
str.append('e');
str.append('l');
str.append('l');
str.append('o');
ASSERT_EQ(NULL, str.c_str());
}
TEST(StaticJsonBuffer_String_Tests, SizeIncreases) {
StaticJsonBuffer<5> jsonBuffer;
StaticJsonBufferBase::String str = jsonBuffer.startString();
ASSERT_EQ(0, jsonBuffer.size());
str.append('h');
ASSERT_EQ(1, jsonBuffer.size());
str.c_str();
ASSERT_EQ(2, jsonBuffer.size());
}

View File

@ -1,24 +0,0 @@
set(GTEST_DIR ../third-party/gtest-1.7.0)
add_library(gtest
${GTEST_DIR}/src/gtest-all.cc
${GTEST_DIR}/src/gtest_main.cc
)
target_include_directories(gtest
PUBLIC
${GTEST_DIR}
${GTEST_DIR}/include
)
target_compile_definitions(gtest PUBLIC -DGTEST_HAS_PTHREAD=0)
if (MSVC)
if (MSVC_VERSION EQUAL 1700)
# Workaround for Visual Studio 2012
target_compile_definitions(gtest PUBLIC -D_VARIADIC_MAX=10)
endif()
target_compile_definitions(gtest PUBLIC -D_CRT_SECURE_NO_WARNINGS)
endif()

16
third-party/catch/CMakeLists.txt vendored Normal file
View File

@ -0,0 +1,16 @@
# Copyright Benoit Blanchon 2014-2017
# MIT License
#
# Arduino JSON library
# https://bblanchon.github.io/ArduinoJson/
# If you like this project, please add a star!
add_library(catch
catch.hpp
catch.cpp
)
target_include_directories(catch
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)

9
third-party/catch/catch.cpp vendored Normal file
View File

@ -0,0 +1,9 @@
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
#define CATCH_CONFIG_MAIN
#include "catch.hpp"

11420
third-party/catch/catch.hpp vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,157 +0,0 @@
Changes for 1.7.0:
* New feature: death tests are supported on OpenBSD and in iOS
simulator now.
* New feature: Google Test now implements a protocol to allow
a test runner to detect that a test program has exited
prematurely and report it as a failure (before it would be
falsely reported as a success if the exit code is 0).
* New feature: Test::RecordProperty() can now be used outside of the
lifespan of a test method, in which case it will be attributed to
the current test case or the test program in the XML report.
* New feature (potentially breaking): --gtest_list_tests now prints
the type parameters and value parameters for each test.
* Improvement: char pointers and char arrays are now escaped properly
in failure messages.
* Improvement: failure summary in XML reports now includes file and
line information.
* Improvement: the <testsuites> XML element now has a timestamp attribute.
* Improvement: When --gtest_filter is specified, XML report now doesn't
contain information about tests that are filtered out.
* Fixed the bug where long --gtest_filter flag values are truncated in
death tests.
* Potentially breaking change: RUN_ALL_TESTS() is now implemented as a
function instead of a macro in order to work better with Clang.
* Compatibility fixes with C++ 11 and various platforms.
* Bug/warning fixes.
Changes for 1.6.0:
* New feature: ADD_FAILURE_AT() for reporting a test failure at the
given source location -- useful for writing testing utilities.
* New feature: the universal value printer is moved from Google Mock
to Google Test.
* New feature: type parameters and value parameters are reported in
the XML report now.
* A gtest_disable_pthreads CMake option.
* Colored output works in GNU Screen sessions now.
* Parameters of value-parameterized tests are now printed in the
textual output.
* Failures from ad hoc test assertions run before RUN_ALL_TESTS() are
now correctly reported.
* Arguments of ASSERT_XY and EXPECT_XY no longer need to support << to
ostream.
* More complete handling of exceptions.
* GTEST_ASSERT_XY can be used instead of ASSERT_XY in case the latter
name is already used by another library.
* --gtest_catch_exceptions is now true by default, allowing a test
program to continue after an exception is thrown.
* Value-parameterized test fixtures can now derive from Test and
WithParamInterface<T> separately, easing conversion of legacy tests.
* Death test messages are clearly marked to make them more
distinguishable from other messages.
* Compatibility fixes for Android, Google Native Client, MinGW, HP UX,
PowerPC, Lucid autotools, libCStd, Sun C++, Borland C++ Builder (Code Gear),
IBM XL C++ (Visual Age C++), and C++0x.
* Bug fixes and implementation clean-ups.
* Potentially incompatible changes: disables the harmful 'make install'
command in autotools.
Changes for 1.5.0:
* New feature: assertions can be safely called in multiple threads
where the pthreads library is available.
* New feature: predicates used inside EXPECT_TRUE() and friends
can now generate custom failure messages.
* New feature: Google Test can now be compiled as a DLL.
* New feature: fused source files are included.
* New feature: prints help when encountering unrecognized Google Test flags.
* Experimental feature: CMake build script (requires CMake 2.6.4+).
* Experimental feature: the Pump script for meta programming.
* double values streamed to an assertion are printed with enough precision
to differentiate any two different values.
* Google Test now works on Solaris and AIX.
* Build and test script improvements.
* Bug fixes and implementation clean-ups.
Potentially breaking changes:
* Stopped supporting VC++ 7.1 with exceptions disabled.
* Dropped support for 'make install'.
Changes for 1.4.0:
* New feature: the event listener API
* New feature: test shuffling
* New feature: the XML report format is closer to junitreport and can
be parsed by Hudson now.
* New feature: when a test runs under Visual Studio, its failures are
integrated in the IDE.
* New feature: /MD(d) versions of VC++ projects.
* New feature: elapsed time for the tests is printed by default.
* New feature: comes with a TR1 tuple implementation such that Boost
is no longer needed for Combine().
* New feature: EXPECT_DEATH_IF_SUPPORTED macro and friends.
* New feature: the Xcode project can now produce static gtest
libraries in addition to a framework.
* Compatibility fixes for Solaris, Cygwin, minGW, Windows Mobile,
Symbian, gcc, and C++Builder.
* Bug fixes and implementation clean-ups.
Changes for 1.3.0:
* New feature: death tests on Windows, Cygwin, and Mac.
* New feature: ability to use Google Test assertions in other testing
frameworks.
* New feature: ability to run disabled test via
--gtest_also_run_disabled_tests.
* New feature: the --help flag for printing the usage.
* New feature: access to Google Test flag values in user code.
* New feature: a script that packs Google Test into one .h and one
.cc file for easy deployment.
* New feature: support for distributing test functions to multiple
machines (requires support from the test runner).
* Bug fixes and implementation clean-ups.
Changes for 1.2.1:
* Compatibility fixes for Linux IA-64 and IBM z/OS.
* Added support for using Boost and other TR1 implementations.
* Changes to the build scripts to support upcoming release of Google C++
Mocking Framework.
* Added Makefile to the distribution package.
* Improved build instructions in README.
Changes for 1.2.0:
* New feature: value-parameterized tests.
* New feature: the ASSERT/EXPECT_(NON)FATAL_FAILURE(_ON_ALL_THREADS)
macros.
* Changed the XML report format to match JUnit/Ant's.
* Added tests to the Xcode project.
* Added scons/SConscript for building with SCons.
* Added src/gtest-all.cc for building Google Test from a single file.
* Fixed compatibility with Solaris and z/OS.
* Enabled running Python tests on systems with python 2.3 installed,
e.g. Mac OS X 10.4.
* Bug fixes.
Changes for 1.1.0:
* New feature: type-parameterized tests.
* New feature: exception assertions.
* New feature: printing elapsed time of tests.
* Improved the robustness of death tests.
* Added an Xcode project and samples.
* Adjusted the output format on Windows to be understandable by Visual Studio.
* Minor bug fixes.
Changes for 1.0.1:
* Added project files for Visual Studio 7.1.
* Fixed issues with compiling on Mac OS X.
* Fixed issues with compiling on Cygwin.
Changes for 1.0.0:
* Initial Open Source release of Google Test

View File

@ -1,252 +0,0 @@
########################################################################
# CMake build script for Google Test.
#
# To run the tests for Google Test itself on Linux, use 'make test' or
# ctest. You can select which tests to run using 'ctest -R regex'.
# For more options, run 'ctest --help'.
# BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to
# make it prominent in the GUI.
option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF)
# When other libraries are using a shared version of runtime libraries,
# Google Test also has to use one.
option(
gtest_force_shared_crt
"Use shared (DLL) run-time lib even when Google Test is built as static lib."
OFF)
option(gtest_build_tests "Build all of gtest's own tests." OFF)
option(gtest_build_samples "Build gtest's sample programs." OFF)
option(gtest_disable_pthreads "Disable uses of pthreads in gtest." OFF)
# Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build().
include(cmake/hermetic_build.cmake OPTIONAL)
if (COMMAND pre_project_set_up_hermetic_build)
pre_project_set_up_hermetic_build()
endif()
########################################################################
#
# Project-wide settings
# Name of the project.
#
# CMake files in this project can refer to the root source directory
# as ${gtest_SOURCE_DIR} and to the root binary directory as
# ${gtest_BINARY_DIR}.
# Language "C" is required for find_package(Threads).
project(gtest CXX C)
cmake_minimum_required(VERSION 2.6.2)
if (COMMAND set_up_hermetic_build)
set_up_hermetic_build()
endif()
# Define helper functions and macros used by Google Test.
include(cmake/internal_utils.cmake)
config_compiler_and_linker() # Defined in internal_utils.cmake.
# Where Google Test's .h files can be found.
include_directories(
${gtest_SOURCE_DIR}/include
${gtest_SOURCE_DIR})
# Where Google Test's libraries can be found.
link_directories(${gtest_BINARY_DIR}/src)
########################################################################
#
# Defines the gtest & gtest_main libraries. User tests should link
# with one of them.
# Google Test libraries. We build them using more strict warnings than what
# are used for other targets, to ensure that gtest can be compiled by a user
# aggressive about warnings.
cxx_library(gtest "${cxx_strict}" src/gtest-all.cc)
cxx_library(gtest_main "${cxx_strict}" src/gtest_main.cc)
target_link_libraries(gtest_main gtest)
########################################################################
#
# Samples on how to link user tests with gtest or gtest_main.
#
# They are not built by default. To build them, set the
# gtest_build_samples option to ON. You can do it by running ccmake
# or specifying the -Dgtest_build_samples=ON flag when running cmake.
if (gtest_build_samples)
cxx_executable(sample1_unittest samples gtest_main samples/sample1.cc)
cxx_executable(sample2_unittest samples gtest_main samples/sample2.cc)
cxx_executable(sample3_unittest samples gtest_main)
cxx_executable(sample4_unittest samples gtest_main samples/sample4.cc)
cxx_executable(sample5_unittest samples gtest_main samples/sample1.cc)
cxx_executable(sample6_unittest samples gtest_main)
cxx_executable(sample7_unittest samples gtest_main)
cxx_executable(sample8_unittest samples gtest_main)
cxx_executable(sample9_unittest samples gtest)
cxx_executable(sample10_unittest samples gtest)
endif()
########################################################################
#
# Google Test's own tests.
#
# You can skip this section if you aren't interested in testing
# Google Test itself.
#
# The tests are not built by default. To build them, set the
# gtest_build_tests option to ON. You can do it by running ccmake
# or specifying the -Dgtest_build_tests=ON flag when running cmake.
if (gtest_build_tests)
# This must be set in the root directory for the tests to be run by
# 'make test' or ctest.
enable_testing()
############################################################
# C++ tests built with standard compiler flags.
cxx_test(gtest-death-test_test gtest_main)
cxx_test(gtest_environment_test gtest)
cxx_test(gtest-filepath_test gtest_main)
cxx_test(gtest-linked_ptr_test gtest_main)
cxx_test(gtest-listener_test gtest_main)
cxx_test(gtest_main_unittest gtest_main)
cxx_test(gtest-message_test gtest_main)
cxx_test(gtest_no_test_unittest gtest)
cxx_test(gtest-options_test gtest_main)
cxx_test(gtest-param-test_test gtest
test/gtest-param-test2_test.cc)
cxx_test(gtest-port_test gtest_main)
cxx_test(gtest_pred_impl_unittest gtest_main)
cxx_test(gtest_premature_exit_test gtest
test/gtest_premature_exit_test.cc)
cxx_test(gtest-printers_test gtest_main)
cxx_test(gtest_prod_test gtest_main
test/production.cc)
cxx_test(gtest_repeat_test gtest)
cxx_test(gtest_sole_header_test gtest_main)
cxx_test(gtest_stress_test gtest)
cxx_test(gtest-test-part_test gtest_main)
cxx_test(gtest_throw_on_failure_ex_test gtest)
cxx_test(gtest-typed-test_test gtest_main
test/gtest-typed-test2_test.cc)
cxx_test(gtest_unittest gtest_main)
cxx_test(gtest-unittest-api_test gtest)
############################################################
# C++ tests built with non-standard compiler flags.
# MSVC 7.1 does not support STL with exceptions disabled.
if (NOT MSVC OR MSVC_VERSION GREATER 1310)
cxx_library(gtest_no_exception "${cxx_no_exception}"
src/gtest-all.cc)
cxx_library(gtest_main_no_exception "${cxx_no_exception}"
src/gtest-all.cc src/gtest_main.cc)
endif()
cxx_library(gtest_main_no_rtti "${cxx_no_rtti}"
src/gtest-all.cc src/gtest_main.cc)
cxx_test_with_flags(gtest-death-test_ex_nocatch_test
"${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=0"
gtest test/gtest-death-test_ex_test.cc)
cxx_test_with_flags(gtest-death-test_ex_catch_test
"${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=1"
gtest test/gtest-death-test_ex_test.cc)
cxx_test_with_flags(gtest_no_rtti_unittest "${cxx_no_rtti}"
gtest_main_no_rtti test/gtest_unittest.cc)
cxx_shared_library(gtest_dll "${cxx_default}"
src/gtest-all.cc src/gtest_main.cc)
cxx_executable_with_flags(gtest_dll_test_ "${cxx_default}"
gtest_dll test/gtest_all_test.cc)
set_target_properties(gtest_dll_test_
PROPERTIES
COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
if (NOT MSVC OR NOT MSVC_VERSION EQUAL 1600)
# The C++ Standard specifies tuple_element<int, class>.
# Yet MSVC 10's <utility> declares tuple_element<size_t, class>.
# That declaration conflicts with our own standard-conforming
# tuple implementation. Therefore using our own tuple with
# MSVC 10 doesn't compile.
cxx_library(gtest_main_use_own_tuple "${cxx_use_own_tuple}"
src/gtest-all.cc src/gtest_main.cc)
cxx_test_with_flags(gtest-tuple_test "${cxx_use_own_tuple}"
gtest_main_use_own_tuple test/gtest-tuple_test.cc)
cxx_test_with_flags(gtest_use_own_tuple_test "${cxx_use_own_tuple}"
gtest_main_use_own_tuple
test/gtest-param-test_test.cc test/gtest-param-test2_test.cc)
endif()
############################################################
# Python tests.
cxx_executable(gtest_break_on_failure_unittest_ test gtest)
py_test(gtest_break_on_failure_unittest)
# MSVC 7.1 does not support STL with exceptions disabled.
if (NOT MSVC OR MSVC_VERSION GREATER 1310)
cxx_executable_with_flags(
gtest_catch_exceptions_no_ex_test_
"${cxx_no_exception}"
gtest_main_no_exception
test/gtest_catch_exceptions_test_.cc)
endif()
cxx_executable_with_flags(
gtest_catch_exceptions_ex_test_
"${cxx_exception}"
gtest_main
test/gtest_catch_exceptions_test_.cc)
py_test(gtest_catch_exceptions_test)
cxx_executable(gtest_color_test_ test gtest)
py_test(gtest_color_test)
cxx_executable(gtest_env_var_test_ test gtest)
py_test(gtest_env_var_test)
cxx_executable(gtest_filter_unittest_ test gtest)
py_test(gtest_filter_unittest)
cxx_executable(gtest_help_test_ test gtest_main)
py_test(gtest_help_test)
cxx_executable(gtest_list_tests_unittest_ test gtest)
py_test(gtest_list_tests_unittest)
cxx_executable(gtest_output_test_ test gtest)
py_test(gtest_output_test)
cxx_executable(gtest_shuffle_test_ test gtest)
py_test(gtest_shuffle_test)
# MSVC 7.1 does not support STL with exceptions disabled.
if (NOT MSVC OR MSVC_VERSION GREATER 1310)
cxx_executable(gtest_throw_on_failure_test_ test gtest_no_exception)
set_target_properties(gtest_throw_on_failure_test_
PROPERTIES
COMPILE_FLAGS "${cxx_no_exception}")
py_test(gtest_throw_on_failure_test)
endif()
cxx_executable(gtest_uninitialized_test_ test gtest)
py_test(gtest_uninitialized_test)
cxx_executable(gtest_xml_outfile1_test_ test gtest_main)
cxx_executable(gtest_xml_outfile2_test_ test gtest_main)
py_test(gtest_xml_outfiles_test)
cxx_executable(gtest_xml_output_unittest_ test gtest)
py_test(gtest_xml_output_unittest)
endif()

View File

@ -1,37 +0,0 @@
# This file contains a list of people who've made non-trivial
# contribution to the Google C++ Testing Framework project. People
# who commit code to the project are encouraged to add their names
# here. Please keep the list sorted by first names.
Ajay Joshi <jaj@google.com>
Balázs Dán <balazs.dan@gmail.com>
Bharat Mediratta <bharat@menalto.com>
Chandler Carruth <chandlerc@google.com>
Chris Prince <cprince@google.com>
Chris Taylor <taylorc@google.com>
Dan Egnor <egnor@google.com>
Eric Roman <eroman@chromium.org>
Hady Zalek <hady.zalek@gmail.com>
Jeffrey Yasskin <jyasskin@google.com>
Jói Sigurðsson <joi@google.com>
Keir Mierle <mierle@gmail.com>
Keith Ray <keith.ray@gmail.com>
Kenton Varda <kenton@google.com>
Manuel Klimek <klimek@google.com>
Markus Heule <markus.heule@gmail.com>
Mika Raento <mikie@iki.fi>
Miklós Fazekas <mfazekas@szemafor.com>
Pasi Valminen <pasi.valminen@gmail.com>
Patrick Hanna <phanna@google.com>
Patrick Riley <pfr@google.com>
Peter Kaminski <piotrk@google.com>
Preston Jackson <preston.a.jackson@gmail.com>
Rainer Klaffenboeck <rainer.klaffenboeck@dynatrace.com>
Russ Cox <rsc@google.com>
Russ Rufer <russ@pentad.com>
Sean Mcafee <eefacm@gmail.com>
Sigurður Ásgeirsson <siggi@google.com>
Tracy Bialik <tracy@pentad.com>
Vadim Berman <vadimb@google.com>
Vlad Losev <vladl@google.com>
Zhanyong Wan <wan@google.com>

View File

@ -1,28 +0,0 @@
Copyright 2008, Google Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -1,306 +0,0 @@
# Automake file
ACLOCAL_AMFLAGS = -I m4
# Nonstandard package files for distribution
EXTRA_DIST = \
CHANGES \
CONTRIBUTORS \
LICENSE \
include/gtest/gtest-param-test.h.pump \
include/gtest/internal/gtest-param-util-generated.h.pump \
include/gtest/internal/gtest-tuple.h.pump \
include/gtest/internal/gtest-type-util.h.pump \
make/Makefile \
scripts/fuse_gtest_files.py \
scripts/gen_gtest_pred_impl.py \
scripts/pump.py \
scripts/test/Makefile
# gtest source files that we don't compile directly. They are
# #included by gtest-all.cc.
GTEST_SRC = \
src/gtest-death-test.cc \
src/gtest-filepath.cc \
src/gtest-internal-inl.h \
src/gtest-port.cc \
src/gtest-printers.cc \
src/gtest-test-part.cc \
src/gtest-typed-test.cc \
src/gtest.cc
EXTRA_DIST += $(GTEST_SRC)
# Sample files that we don't compile.
EXTRA_DIST += \
samples/prime_tables.h \
samples/sample2_unittest.cc \
samples/sample3_unittest.cc \
samples/sample4_unittest.cc \
samples/sample5_unittest.cc \
samples/sample6_unittest.cc \
samples/sample7_unittest.cc \
samples/sample8_unittest.cc \
samples/sample9_unittest.cc
# C++ test files that we don't compile directly.
EXTRA_DIST += \
test/gtest-death-test_ex_test.cc \
test/gtest-death-test_test.cc \
test/gtest-filepath_test.cc \
test/gtest-linked_ptr_test.cc \
test/gtest-listener_test.cc \
test/gtest-message_test.cc \
test/gtest-options_test.cc \
test/gtest-param-test2_test.cc \
test/gtest-param-test2_test.cc \
test/gtest-param-test_test.cc \
test/gtest-param-test_test.cc \
test/gtest-param-test_test.h \
test/gtest-port_test.cc \
test/gtest_premature_exit_test.cc \
test/gtest-printers_test.cc \
test/gtest-test-part_test.cc \
test/gtest-tuple_test.cc \
test/gtest-typed-test2_test.cc \
test/gtest-typed-test_test.cc \
test/gtest-typed-test_test.h \
test/gtest-unittest-api_test.cc \
test/gtest_break_on_failure_unittest_.cc \
test/gtest_catch_exceptions_test_.cc \
test/gtest_color_test_.cc \
test/gtest_env_var_test_.cc \
test/gtest_environment_test.cc \
test/gtest_filter_unittest_.cc \
test/gtest_help_test_.cc \
test/gtest_list_tests_unittest_.cc \
test/gtest_main_unittest.cc \
test/gtest_no_test_unittest.cc \
test/gtest_output_test_.cc \
test/gtest_pred_impl_unittest.cc \
test/gtest_prod_test.cc \
test/gtest_repeat_test.cc \
test/gtest_shuffle_test_.cc \
test/gtest_sole_header_test.cc \
test/gtest_stress_test.cc \
test/gtest_throw_on_failure_ex_test.cc \
test/gtest_throw_on_failure_test_.cc \
test/gtest_uninitialized_test_.cc \
test/gtest_unittest.cc \
test/gtest_unittest.cc \
test/gtest_xml_outfile1_test_.cc \
test/gtest_xml_outfile2_test_.cc \
test/gtest_xml_output_unittest_.cc \
test/production.cc \
test/production.h
# Python tests that we don't run.
EXTRA_DIST += \
test/gtest_break_on_failure_unittest.py \
test/gtest_catch_exceptions_test.py \
test/gtest_color_test.py \
test/gtest_env_var_test.py \
test/gtest_filter_unittest.py \
test/gtest_help_test.py \
test/gtest_list_tests_unittest.py \
test/gtest_output_test.py \
test/gtest_output_test_golden_lin.txt \
test/gtest_shuffle_test.py \
test/gtest_test_utils.py \
test/gtest_throw_on_failure_test.py \
test/gtest_uninitialized_test.py \
test/gtest_xml_outfiles_test.py \
test/gtest_xml_output_unittest.py \
test/gtest_xml_test_utils.py
# CMake script
EXTRA_DIST += \
CMakeLists.txt \
cmake/internal_utils.cmake
# MSVC project files
EXTRA_DIST += \
msvc/gtest-md.sln \
msvc/gtest-md.vcproj \
msvc/gtest.sln \
msvc/gtest.vcproj \
msvc/gtest_main-md.vcproj \
msvc/gtest_main.vcproj \
msvc/gtest_prod_test-md.vcproj \
msvc/gtest_prod_test.vcproj \
msvc/gtest_unittest-md.vcproj \
msvc/gtest_unittest.vcproj
# xcode project files
EXTRA_DIST += \
xcode/Config/DebugProject.xcconfig \
xcode/Config/FrameworkTarget.xcconfig \
xcode/Config/General.xcconfig \
xcode/Config/ReleaseProject.xcconfig \
xcode/Config/StaticLibraryTarget.xcconfig \
xcode/Config/TestTarget.xcconfig \
xcode/Resources/Info.plist \
xcode/Scripts/runtests.sh \
xcode/Scripts/versiongenerate.py \
xcode/gtest.xcodeproj/project.pbxproj
# xcode sample files
EXTRA_DIST += \
xcode/Samples/FrameworkSample/Info.plist \
xcode/Samples/FrameworkSample/WidgetFramework.xcodeproj/project.pbxproj \
xcode/Samples/FrameworkSample/runtests.sh \
xcode/Samples/FrameworkSample/widget.cc \
xcode/Samples/FrameworkSample/widget.h \
xcode/Samples/FrameworkSample/widget_test.cc
# C++Builder project files
EXTRA_DIST += \
codegear/gtest.cbproj \
codegear/gtest.groupproj \
codegear/gtest_all.cc \
codegear/gtest_link.cc \
codegear/gtest_main.cbproj \
codegear/gtest_unittest.cbproj
# Distribute and install M4 macro
m4datadir = $(datadir)/aclocal
m4data_DATA = m4/gtest.m4
EXTRA_DIST += $(m4data_DATA)
# We define the global AM_CPPFLAGS as everything we compile includes from these
# directories.
AM_CPPFLAGS = -I$(srcdir) -I$(srcdir)/include
# Modifies compiler and linker flags for pthreads compatibility.
if HAVE_PTHREADS
AM_CXXFLAGS = @PTHREAD_CFLAGS@ -DGTEST_HAS_PTHREAD=1
AM_LIBS = @PTHREAD_LIBS@
else
AM_CXXFLAGS = -DGTEST_HAS_PTHREAD=0
endif
# Build rules for libraries.
lib_LTLIBRARIES = lib/libgtest.la lib/libgtest_main.la
lib_libgtest_la_SOURCES = src/gtest-all.cc
pkginclude_HEADERS = \
include/gtest/gtest-death-test.h \
include/gtest/gtest-message.h \
include/gtest/gtest-param-test.h \
include/gtest/gtest-printers.h \
include/gtest/gtest-spi.h \
include/gtest/gtest-test-part.h \
include/gtest/gtest-typed-test.h \
include/gtest/gtest.h \
include/gtest/gtest_pred_impl.h \
include/gtest/gtest_prod.h
pkginclude_internaldir = $(pkgincludedir)/internal
pkginclude_internal_HEADERS = \
include/gtest/internal/gtest-death-test-internal.h \
include/gtest/internal/gtest-filepath.h \
include/gtest/internal/gtest-internal.h \
include/gtest/internal/gtest-linked_ptr.h \
include/gtest/internal/gtest-param-util-generated.h \
include/gtest/internal/gtest-param-util.h \
include/gtest/internal/gtest-port.h \
include/gtest/internal/gtest-string.h \
include/gtest/internal/gtest-tuple.h \
include/gtest/internal/gtest-type-util.h
lib_libgtest_main_la_SOURCES = src/gtest_main.cc
lib_libgtest_main_la_LIBADD = lib/libgtest.la
# Bulid rules for samples and tests. Automake's naming for some of
# these variables isn't terribly obvious, so this is a brief
# reference:
#
# TESTS -- Programs run automatically by "make check"
# check_PROGRAMS -- Programs built by "make check" but not necessarily run
noinst_LTLIBRARIES = samples/libsamples.la
samples_libsamples_la_SOURCES = \
samples/sample1.cc \
samples/sample1.h \
samples/sample2.cc \
samples/sample2.h \
samples/sample3-inl.h \
samples/sample4.cc \
samples/sample4.h
TESTS=
TESTS_ENVIRONMENT = GTEST_SOURCE_DIR="$(srcdir)/test" \
GTEST_BUILD_DIR="$(top_builddir)/test"
check_PROGRAMS=
# A simple sample on using gtest.
TESTS += samples/sample1_unittest
check_PROGRAMS += samples/sample1_unittest
samples_sample1_unittest_SOURCES = samples/sample1_unittest.cc
samples_sample1_unittest_LDADD = lib/libgtest_main.la \
lib/libgtest.la \
samples/libsamples.la
# Another sample. It also verifies that libgtest works.
TESTS += samples/sample10_unittest
check_PROGRAMS += samples/sample10_unittest
samples_sample10_unittest_SOURCES = samples/sample10_unittest.cc
samples_sample10_unittest_LDADD = lib/libgtest.la
# This tests most constructs of gtest and verifies that libgtest_main
# and libgtest work.
TESTS += test/gtest_all_test
check_PROGRAMS += test/gtest_all_test
test_gtest_all_test_SOURCES = test/gtest_all_test.cc
test_gtest_all_test_LDADD = lib/libgtest_main.la \
lib/libgtest.la
# Tests that fused gtest files compile and work.
FUSED_GTEST_SRC = \
fused-src/gtest/gtest-all.cc \
fused-src/gtest/gtest.h \
fused-src/gtest/gtest_main.cc
if HAVE_PYTHON
TESTS += test/fused_gtest_test
check_PROGRAMS += test/fused_gtest_test
test_fused_gtest_test_SOURCES = $(FUSED_GTEST_SRC) \
samples/sample1.cc samples/sample1_unittest.cc
test_fused_gtest_test_CPPFLAGS = -I"$(srcdir)/fused-src"
# Build rules for putting fused Google Test files into the distribution
# package. The user can also create those files by manually running
# scripts/fuse_gtest_files.py.
$(test_fused_gtest_test_SOURCES): fused-gtest
fused-gtest: $(pkginclude_HEADERS) $(pkginclude_internal_HEADERS) \
$(GTEST_SRC) src/gtest-all.cc src/gtest_main.cc \
scripts/fuse_gtest_files.py
mkdir -p "$(srcdir)/fused-src"
chmod -R u+w "$(srcdir)/fused-src"
rm -f "$(srcdir)/fused-src/gtest/gtest-all.cc"
rm -f "$(srcdir)/fused-src/gtest/gtest.h"
"$(srcdir)/scripts/fuse_gtest_files.py" "$(srcdir)/fused-src"
cp -f "$(srcdir)/src/gtest_main.cc" "$(srcdir)/fused-src/gtest/"
maintainer-clean-local:
rm -rf "$(srcdir)/fused-src"
endif
# Death tests may produce core dumps in the build directory. In case
# this happens, clean them to keep distcleancheck happy.
CLEANFILES = core
# Disables 'make install' as installing a compiled version of Google
# Test can lead to undefined behavior due to violation of the
# One-Definition Rule.
install-exec-local:
echo "'make install' is dangerous and not supported. Instead, see README for how to integrate Google Test into your build system."
false
install-data-local:
echo "'make install' is dangerous and not supported. Instead, see README for how to integrate Google Test into your build system."
false

File diff suppressed because it is too large Load Diff

View File

@ -1,435 +0,0 @@
Google C++ Testing Framework
============================
http://code.google.com/p/googletest/
Overview
--------
Google's framework for writing C++ tests on a variety of platforms
(Linux, Mac OS X, Windows, Windows CE, Symbian, etc). Based on the
xUnit architecture. Supports automatic test discovery, a rich set of
assertions, user-defined assertions, death tests, fatal and non-fatal
failures, various options for running the tests, and XML test report
generation.
Please see the project page above for more information as well as the
mailing list for questions, discussions, and development. There is
also an IRC channel on OFTC (irc.oftc.net) #gtest available. Please
join us!
Requirements for End Users
--------------------------
Google Test is designed to have fairly minimal requirements to build
and use with your projects, but there are some. Currently, we support
Linux, Windows, Mac OS X, and Cygwin. We will also make our best
effort to support other platforms (e.g. Solaris, AIX, and z/OS).
However, since core members of the Google Test project have no access
to these platforms, Google Test may have outstanding issues there. If
you notice any problems on your platform, please notify
googletestframework@googlegroups.com. Patches for fixing them are
even more welcome!
### Linux Requirements ###
These are the base requirements to build and use Google Test from a source
package (as described below):
* GNU-compatible Make or gmake
* POSIX-standard shell
* POSIX(-2) Regular Expressions (regex.h)
* A C++98-standard-compliant compiler
### Windows Requirements ###
* Microsoft Visual C++ 7.1 or newer
### Cygwin Requirements ###
* Cygwin 1.5.25-14 or newer
### Mac OS X Requirements ###
* Mac OS X 10.4 Tiger or newer
* Developer Tools Installed
Also, you'll need CMake 2.6.4 or higher if you want to build the
samples using the provided CMake script, regardless of the platform.
Requirements for Contributors
-----------------------------
We welcome patches. If you plan to contribute a patch, you need to
build Google Test and its own tests from an SVN checkout (described
below), which has further requirements:
* Python version 2.3 or newer (for running some of the tests and
re-generating certain source files from templates)
* CMake 2.6.4 or newer
Getting the Source
------------------
There are two primary ways of getting Google Test's source code: you
can download a stable source release in your preferred archive format,
or directly check out the source from our Subversion (SVN) repositary.
The SVN checkout requires a few extra steps and some extra software
packages on your system, but lets you track the latest development and
make patches much more easily, so we highly encourage it.
### Source Package ###
Google Test is released in versioned source packages which can be
downloaded from the download page [1]. Several different archive
formats are provided, but the only difference is the tools used to
manipulate them, and the size of the resulting file. Download
whichever you are most comfortable with.
[1] http://code.google.com/p/googletest/downloads/list
Once the package is downloaded, expand it using whichever tools you
prefer for that type. This will result in a new directory with the
name "gtest-X.Y.Z" which contains all of the source code. Here are
some examples on Linux:
tar -xvzf gtest-X.Y.Z.tar.gz
tar -xvjf gtest-X.Y.Z.tar.bz2
unzip gtest-X.Y.Z.zip
### SVN Checkout ###
To check out the main branch (also known as the "trunk") of Google
Test, run the following Subversion command:
svn checkout http://googletest.googlecode.com/svn/trunk/ gtest-svn
Setting up the Build
--------------------
To build Google Test and your tests that use it, you need to tell your
build system where to find its headers and source files. The exact
way to do it depends on which build system you use, and is usually
straightforward.
### Generic Build Instructions ###
Suppose you put Google Test in directory ${GTEST_DIR}. To build it,
create a library build target (or a project as called by Visual Studio
and Xcode) to compile
${GTEST_DIR}/src/gtest-all.cc
with ${GTEST_DIR}/include in the system header search path and ${GTEST_DIR}
in the normal header search path. Assuming a Linux-like system and gcc,
something like the following will do:
g++ -isystem ${GTEST_DIR}/include -I${GTEST_DIR} \
-pthread -c ${GTEST_DIR}/src/gtest-all.cc
ar -rv libgtest.a gtest-all.o
(We need -pthread as Google Test uses threads.)
Next, you should compile your test source file with
${GTEST_DIR}/include in the system header search path, and link it
with gtest and any other necessary libraries:
g++ -isystem ${GTEST_DIR}/include -pthread path/to/your_test.cc libgtest.a \
-o your_test
As an example, the make/ directory contains a Makefile that you can
use to build Google Test on systems where GNU make is available
(e.g. Linux, Mac OS X, and Cygwin). It doesn't try to build Google
Test's own tests. Instead, it just builds the Google Test library and
a sample test. You can use it as a starting point for your own build
script.
If the default settings are correct for your environment, the
following commands should succeed:
cd ${GTEST_DIR}/make
make
./sample1_unittest
If you see errors, try to tweak the contents of make/Makefile to make
them go away. There are instructions in make/Makefile on how to do
it.
### Using CMake ###
Google Test comes with a CMake build script (CMakeLists.txt) that can
be used on a wide range of platforms ("C" stands for cross-platofrm.).
If you don't have CMake installed already, you can download it for
free from http://www.cmake.org/.
CMake works by generating native makefiles or build projects that can
be used in the compiler environment of your choice. The typical
workflow starts with:
mkdir mybuild # Create a directory to hold the build output.
cd mybuild
cmake ${GTEST_DIR} # Generate native build scripts.
If you want to build Google Test's samples, you should replace the
last command with
cmake -Dgtest_build_samples=ON ${GTEST_DIR}
If you are on a *nix system, you should now see a Makefile in the
current directory. Just type 'make' to build gtest.
If you use Windows and have Vistual Studio installed, a gtest.sln file
and several .vcproj files will be created. You can then build them
using Visual Studio.
On Mac OS X with Xcode installed, a .xcodeproj file will be generated.
### Legacy Build Scripts ###
Before settling on CMake, we have been providing hand-maintained build
projects/scripts for Visual Studio, Xcode, and Autotools. While we
continue to provide them for convenience, they are not actively
maintained any more. We highly recommend that you follow the
instructions in the previous two sections to integrate Google Test
with your existing build system.
If you still need to use the legacy build scripts, here's how:
The msvc\ folder contains two solutions with Visual C++ projects.
Open the gtest.sln or gtest-md.sln file using Visual Studio, and you
are ready to build Google Test the same way you build any Visual
Studio project. Files that have names ending with -md use DLL
versions of Microsoft runtime libraries (the /MD or the /MDd compiler
option). Files without that suffix use static versions of the runtime
libraries (the /MT or the /MTd option). Please note that one must use
the same option to compile both gtest and the test code. If you use
Visual Studio 2005 or above, we recommend the -md version as /MD is
the default for new projects in these versions of Visual Studio.
On Mac OS X, open the gtest.xcodeproj in the xcode/ folder using
Xcode. Build the "gtest" target. The universal binary framework will
end up in your selected build directory (selected in the Xcode
"Preferences..." -> "Building" pane and defaults to xcode/build).
Alternatively, at the command line, enter:
xcodebuild
This will build the "Release" configuration of gtest.framework in your
default build location. See the "xcodebuild" man page for more
information about building different configurations and building in
different locations.
If you wish to use the Google Test Xcode project with Xcode 4.x and
above, you need to either:
* update the SDK configuration options in xcode/Config/General.xconfig.
Comment options SDKROOT, MACOS_DEPLOYMENT_TARGET, and GCC_VERSION. If
you choose this route you lose the ability to target earlier versions
of MacOS X.
* Install an SDK for an earlier version. This doesn't appear to be
supported by Apple, but has been reported to work
(http://stackoverflow.com/questions/5378518).
Tweaking Google Test
--------------------
Google Test can be used in diverse environments. The default
configuration may not work (or may not work well) out of the box in
some environments. However, you can easily tweak Google Test by
defining control macros on the compiler command line. Generally,
these macros are named like GTEST_XYZ and you define them to either 1
or 0 to enable or disable a certain feature.
We list the most frequently used macros below. For a complete list,
see file include/gtest/internal/gtest-port.h.
### Choosing a TR1 Tuple Library ###
Some Google Test features require the C++ Technical Report 1 (TR1)
tuple library, which is not yet available with all compilers. The
good news is that Google Test implements a subset of TR1 tuple that's
enough for its own need, and will automatically use this when the
compiler doesn't provide TR1 tuple.
Usually you don't need to care about which tuple library Google Test
uses. However, if your project already uses TR1 tuple, you need to
tell Google Test to use the same TR1 tuple library the rest of your
project uses, or the two tuple implementations will clash. To do
that, add
-DGTEST_USE_OWN_TR1_TUPLE=0
to the compiler flags while compiling Google Test and your tests. If
you want to force Google Test to use its own tuple library, just add
-DGTEST_USE_OWN_TR1_TUPLE=1
to the compiler flags instead.
If you don't want Google Test to use tuple at all, add
-DGTEST_HAS_TR1_TUPLE=0
and all features using tuple will be disabled.
### Multi-threaded Tests ###
Google Test is thread-safe where the pthread library is available.
After #include "gtest/gtest.h", you can check the GTEST_IS_THREADSAFE
macro to see whether this is the case (yes if the macro is #defined to
1, no if it's undefined.).
If Google Test doesn't correctly detect whether pthread is available
in your environment, you can force it with
-DGTEST_HAS_PTHREAD=1
or
-DGTEST_HAS_PTHREAD=0
When Google Test uses pthread, you may need to add flags to your
compiler and/or linker to select the pthread library, or you'll get
link errors. If you use the CMake script or the deprecated Autotools
script, this is taken care of for you. If you use your own build
script, you'll need to read your compiler and linker's manual to
figure out what flags to add.
### As a Shared Library (DLL) ###
Google Test is compact, so most users can build and link it as a
static library for the simplicity. You can choose to use Google Test
as a shared library (known as a DLL on Windows) if you prefer.
To compile *gtest* as a shared library, add
-DGTEST_CREATE_SHARED_LIBRARY=1
to the compiler flags. You'll also need to tell the linker to produce
a shared library instead - consult your linker's manual for how to do
it.
To compile your *tests* that use the gtest shared library, add
-DGTEST_LINKED_AS_SHARED_LIBRARY=1
to the compiler flags.
Note: while the above steps aren't technically necessary today when
using some compilers (e.g. GCC), they may become necessary in the
future, if we decide to improve the speed of loading the library (see
http://gcc.gnu.org/wiki/Visibility for details). Therefore you are
recommended to always add the above flags when using Google Test as a
shared library. Otherwise a future release of Google Test may break
your build script.
### Avoiding Macro Name Clashes ###
In C++, macros don't obey namespaces. Therefore two libraries that
both define a macro of the same name will clash if you #include both
definitions. In case a Google Test macro clashes with another
library, you can force Google Test to rename its macro to avoid the
conflict.
Specifically, if both Google Test and some other code define macro
FOO, you can add
-DGTEST_DONT_DEFINE_FOO=1
to the compiler flags to tell Google Test to change the macro's name
from FOO to GTEST_FOO. Currently FOO can be FAIL, SUCCEED, or TEST.
For example, with -DGTEST_DONT_DEFINE_TEST=1, you'll need to write
GTEST_TEST(SomeTest, DoesThis) { ... }
instead of
TEST(SomeTest, DoesThis) { ... }
in order to define a test.
Upgrating from an Earlier Version
---------------------------------
We strive to keep Google Test releases backward compatible.
Sometimes, though, we have to make some breaking changes for the
users' long-term benefits. This section describes what you'll need to
do if you are upgrading from an earlier version of Google Test.
### Upgrading from 1.3.0 or Earlier ###
You may need to explicitly enable or disable Google Test's own TR1
tuple library. See the instructions in section "Choosing a TR1 Tuple
Library".
### Upgrading from 1.4.0 or Earlier ###
The Autotools build script (configure + make) is no longer officially
supportted. You are encouraged to migrate to your own build system or
use CMake. If you still need to use Autotools, you can find
instructions in the README file from Google Test 1.4.0.
On platforms where the pthread library is available, Google Test uses
it in order to be thread-safe. See the "Multi-threaded Tests" section
for what this means to your build script.
If you use Microsoft Visual C++ 7.1 with exceptions disabled, Google
Test will no longer compile. This should affect very few people, as a
large portion of STL (including <string>) doesn't compile in this mode
anyway. We decided to stop supporting it in order to greatly simplify
Google Test's implementation.
Developing Google Test
----------------------
This section discusses how to make your own changes to Google Test.
### Testing Google Test Itself ###
To make sure your changes work as intended and don't break existing
functionality, you'll want to compile and run Google Test's own tests.
For that you can use CMake:
mkdir mybuild
cd mybuild
cmake -Dgtest_build_tests=ON ${GTEST_DIR}
Make sure you have Python installed, as some of Google Test's tests
are written in Python. If the cmake command complains about not being
able to find Python ("Could NOT find PythonInterp (missing:
PYTHON_EXECUTABLE)"), try telling it explicitly where your Python
executable can be found:
cmake -DPYTHON_EXECUTABLE=path/to/python -Dgtest_build_tests=ON ${GTEST_DIR}
Next, you can build Google Test and all of its own tests. On *nix,
this is usually done by 'make'. To run the tests, do
make test
All tests should pass.
### Regenerating Source Files ###
Some of Google Test's source files are generated from templates (not
in the C++ sense) using a script. A template file is named FOO.pump,
where FOO is the name of the file it will generate. For example, the
file include/gtest/internal/gtest-type-util.h.pump is used to generate
gtest-type-util.h in the same directory.
Normally you don't need to worry about regenerating the source files,
unless you need to modify them. In that case, you should modify the
corresponding .pump files instead and run the pump.py Python script to
regenerate them. You can find pump.py in the scripts/ directory.
Read the Pump manual [2] for how to use it.
[2] http://code.google.com/p/googletest/wiki/PumpManual
### Contributing a Patch ###
We welcome patches. Please read the Google Test developer's guide [3]
for how you can contribute. In particular, make sure you have signed
the Contributor License Agreement, or we won't be able to accept the
patch.
[3] http://code.google.com/p/googletest/wiki/GoogleTestDevGuide
Happy testing!

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,69 +0,0 @@
/* build-aux/config.h.in. Generated from configure.ac by autoheader. */
/* Define to 1 if you have the <dlfcn.h> header file. */
#undef HAVE_DLFCN_H
/* Define to 1 if you have the <inttypes.h> header file. */
#undef HAVE_INTTYPES_H
/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H
/* Define if you have POSIX threads libraries and header files. */
#undef HAVE_PTHREAD
/* Define to 1 if you have the <stdint.h> header file. */
#undef HAVE_STDINT_H
/* Define to 1 if you have the <stdlib.h> header file. */
#undef HAVE_STDLIB_H
/* Define to 1 if you have the <strings.h> header file. */
#undef HAVE_STRINGS_H
/* Define to 1 if you have the <string.h> header file. */
#undef HAVE_STRING_H
/* Define to 1 if you have the <sys/stat.h> header file. */
#undef HAVE_SYS_STAT_H
/* Define to 1 if you have the <sys/types.h> header file. */
#undef HAVE_SYS_TYPES_H
/* Define to 1 if you have the <unistd.h> header file. */
#undef HAVE_UNISTD_H
/* Define to the sub-directory in which libtool stores uninstalled libraries.
*/
#undef LT_OBJDIR
/* Name of package */
#undef PACKAGE
/* Define to the address where bug reports for this package should be sent. */
#undef PACKAGE_BUGREPORT
/* Define to the full name of this package. */
#undef PACKAGE_NAME
/* Define to the full name and version of this package. */
#undef PACKAGE_STRING
/* Define to the one symbol short name of this package. */
#undef PACKAGE_TARNAME
/* Define to the home page for this package. */
#undef PACKAGE_URL
/* Define to the version of this package. */
#undef PACKAGE_VERSION
/* Define to necessary symbol if this constant uses a non-standard name on
your system. */
#undef PTHREAD_CREATE_JOINABLE
/* Define to 1 if you have the ANSI C header files. */
#undef STDC_HEADERS
/* Version number of package */
#undef VERSION

File diff suppressed because it is too large Load Diff

View File

@ -1,688 +0,0 @@
#! /bin/sh
# depcomp - compile a program generating dependencies as side-effects
scriptversion=2011-12-04.11; # UTC
# Copyright (C) 1999, 2000, 2003, 2004, 2005, 2006, 2007, 2009, 2010,
# 2011 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# As a special exception to the GNU General Public License, if you
# distribute this file as part of a program that contains a
# configuration script generated by Autoconf, you may include it under
# the same distribution terms that you use for the rest of that program.
# Originally written by Alexandre Oliva <oliva@dcc.unicamp.br>.
case $1 in
'')
echo "$0: No command. Try \`$0 --help' for more information." 1>&2
exit 1;
;;
-h | --h*)
cat <<\EOF
Usage: depcomp [--help] [--version] PROGRAM [ARGS]
Run PROGRAMS ARGS to compile a file, generating dependencies
as side-effects.
Environment variables:
depmode Dependency tracking mode.
source Source file read by `PROGRAMS ARGS'.
object Object file output by `PROGRAMS ARGS'.
DEPDIR directory where to store dependencies.
depfile Dependency file to output.
tmpdepfile Temporary file to use when outputting dependencies.
libtool Whether libtool is used (yes/no).
Report bugs to <bug-automake@gnu.org>.
EOF
exit $?
;;
-v | --v*)
echo "depcomp $scriptversion"
exit $?
;;
esac
if test -z "$depmode" || test -z "$source" || test -z "$object"; then
echo "depcomp: Variables source, object and depmode must be set" 1>&2
exit 1
fi
# Dependencies for sub/bar.o or sub/bar.obj go into sub/.deps/bar.Po.
depfile=${depfile-`echo "$object" |
sed 's|[^\\/]*$|'${DEPDIR-.deps}'/&|;s|\.\([^.]*\)$|.P\1|;s|Pobj$|Po|'`}
tmpdepfile=${tmpdepfile-`echo "$depfile" | sed 's/\.\([^.]*\)$/.T\1/'`}
rm -f "$tmpdepfile"
# Some modes work just like other modes, but use different flags. We
# parameterize here, but still list the modes in the big case below,
# to make depend.m4 easier to write. Note that we *cannot* use a case
# here, because this file can only contain one case statement.
if test "$depmode" = hp; then
# HP compiler uses -M and no extra arg.
gccflag=-M
depmode=gcc
fi
if test "$depmode" = dashXmstdout; then
# This is just like dashmstdout with a different argument.
dashmflag=-xM
depmode=dashmstdout
fi
cygpath_u="cygpath -u -f -"
if test "$depmode" = msvcmsys; then
# This is just like msvisualcpp but w/o cygpath translation.
# Just convert the backslash-escaped backslashes to single forward
# slashes to satisfy depend.m4
cygpath_u='sed s,\\\\,/,g'
depmode=msvisualcpp
fi
if test "$depmode" = msvc7msys; then
# This is just like msvc7 but w/o cygpath translation.
# Just convert the backslash-escaped backslashes to single forward
# slashes to satisfy depend.m4
cygpath_u='sed s,\\\\,/,g'
depmode=msvc7
fi
case "$depmode" in
gcc3)
## gcc 3 implements dependency tracking that does exactly what
## we want. Yay! Note: for some reason libtool 1.4 doesn't like
## it if -MD -MP comes after the -MF stuff. Hmm.
## Unfortunately, FreeBSD c89 acceptance of flags depends upon
## the command line argument order; so add the flags where they
## appear in depend2.am. Note that the slowdown incurred here
## affects only configure: in makefiles, %FASTDEP% shortcuts this.
for arg
do
case $arg in
-c) set fnord "$@" -MT "$object" -MD -MP -MF "$tmpdepfile" "$arg" ;;
*) set fnord "$@" "$arg" ;;
esac
shift # fnord
shift # $arg
done
"$@"
stat=$?
if test $stat -eq 0; then :
else
rm -f "$tmpdepfile"
exit $stat
fi
mv "$tmpdepfile" "$depfile"
;;
gcc)
## There are various ways to get dependency output from gcc. Here's
## why we pick this rather obscure method:
## - Don't want to use -MD because we'd like the dependencies to end
## up in a subdir. Having to rename by hand is ugly.
## (We might end up doing this anyway to support other compilers.)
## - The DEPENDENCIES_OUTPUT environment variable makes gcc act like
## -MM, not -M (despite what the docs say).
## - Using -M directly means running the compiler twice (even worse
## than renaming).
if test -z "$gccflag"; then
gccflag=-MD,
fi
"$@" -Wp,"$gccflag$tmpdepfile"
stat=$?
if test $stat -eq 0; then :
else
rm -f "$tmpdepfile"
exit $stat
fi
rm -f "$depfile"
echo "$object : \\" > "$depfile"
alpha=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
## The second -e expression handles DOS-style file names with drive letters.
sed -e 's/^[^:]*: / /' \
-e 's/^['$alpha']:\/[^:]*: / /' < "$tmpdepfile" >> "$depfile"
## This next piece of magic avoids the `deleted header file' problem.
## The problem is that when a header file which appears in a .P file
## is deleted, the dependency causes make to die (because there is
## typically no way to rebuild the header). We avoid this by adding
## dummy dependencies for each header file. Too bad gcc doesn't do
## this for us directly.
tr ' ' '
' < "$tmpdepfile" |
## Some versions of gcc put a space before the `:'. On the theory
## that the space means something, we add a space to the output as
## well. hp depmode also adds that space, but also prefixes the VPATH
## to the object. Take care to not repeat it in the output.
## Some versions of the HPUX 10.20 sed can't process this invocation
## correctly. Breaking it into two sed invocations is a workaround.
sed -e 's/^\\$//' -e '/^$/d' -e "s|.*$object$||" -e '/:$/d' \
| sed -e 's/$/ :/' >> "$depfile"
rm -f "$tmpdepfile"
;;
hp)
# This case exists only to let depend.m4 do its work. It works by
# looking at the text of this script. This case will never be run,
# since it is checked for above.
exit 1
;;
sgi)
if test "$libtool" = yes; then
"$@" "-Wp,-MDupdate,$tmpdepfile"
else
"$@" -MDupdate "$tmpdepfile"
fi
stat=$?
if test $stat -eq 0; then :
else
rm -f "$tmpdepfile"
exit $stat
fi
rm -f "$depfile"
if test -f "$tmpdepfile"; then # yes, the sourcefile depend on other files
echo "$object : \\" > "$depfile"
# Clip off the initial element (the dependent). Don't try to be
# clever and replace this with sed code, as IRIX sed won't handle
# lines with more than a fixed number of characters (4096 in
# IRIX 6.2 sed, 8192 in IRIX 6.5). We also remove comment lines;
# the IRIX cc adds comments like `#:fec' to the end of the
# dependency line.
tr ' ' '
' < "$tmpdepfile" \
| sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' | \
tr '
' ' ' >> "$depfile"
echo >> "$depfile"
# The second pass generates a dummy entry for each header file.
tr ' ' '
' < "$tmpdepfile" \
| sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' -e 's/$/:/' \
>> "$depfile"
else
# The sourcefile does not contain any dependencies, so just
# store a dummy comment line, to avoid errors with the Makefile
# "include basename.Plo" scheme.
echo "#dummy" > "$depfile"
fi
rm -f "$tmpdepfile"
;;
aix)
# The C for AIX Compiler uses -M and outputs the dependencies
# in a .u file. In older versions, this file always lives in the
# current directory. Also, the AIX compiler puts `$object:' at the
# start of each line; $object doesn't have directory information.
# Version 6 uses the directory in both cases.
dir=`echo "$object" | sed -e 's|/[^/]*$|/|'`
test "x$dir" = "x$object" && dir=
base=`echo "$object" | sed -e 's|^.*/||' -e 's/\.o$//' -e 's/\.lo$//'`
if test "$libtool" = yes; then
tmpdepfile1=$dir$base.u
tmpdepfile2=$base.u
tmpdepfile3=$dir.libs/$base.u
"$@" -Wc,-M
else
tmpdepfile1=$dir$base.u
tmpdepfile2=$dir$base.u
tmpdepfile3=$dir$base.u
"$@" -M
fi
stat=$?
if test $stat -eq 0; then :
else
rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
exit $stat
fi
for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3"
do
test -f "$tmpdepfile" && break
done
if test -f "$tmpdepfile"; then
# Each line is of the form `foo.o: dependent.h'.
# Do two passes, one to just change these to
# `$object: dependent.h' and one to simply `dependent.h:'.
sed -e "s,^.*\.[a-z]*:,$object:," < "$tmpdepfile" > "$depfile"
# That's a tab and a space in the [].
sed -e 's,^.*\.[a-z]*:[ ]*,,' -e 's,$,:,' < "$tmpdepfile" >> "$depfile"
else
# The sourcefile does not contain any dependencies, so just
# store a dummy comment line, to avoid errors with the Makefile
# "include basename.Plo" scheme.
echo "#dummy" > "$depfile"
fi
rm -f "$tmpdepfile"
;;
icc)
# Intel's C compiler understands `-MD -MF file'. However on
# icc -MD -MF foo.d -c -o sub/foo.o sub/foo.c
# ICC 7.0 will fill foo.d with something like
# foo.o: sub/foo.c
# foo.o: sub/foo.h
# which is wrong. We want:
# sub/foo.o: sub/foo.c
# sub/foo.o: sub/foo.h
# sub/foo.c:
# sub/foo.h:
# ICC 7.1 will output
# foo.o: sub/foo.c sub/foo.h
# and will wrap long lines using \ :
# foo.o: sub/foo.c ... \
# sub/foo.h ... \
# ...
"$@" -MD -MF "$tmpdepfile"
stat=$?
if test $stat -eq 0; then :
else
rm -f "$tmpdepfile"
exit $stat
fi
rm -f "$depfile"
# Each line is of the form `foo.o: dependent.h',
# or `foo.o: dep1.h dep2.h \', or ` dep3.h dep4.h \'.
# Do two passes, one to just change these to
# `$object: dependent.h' and one to simply `dependent.h:'.
sed "s,^[^:]*:,$object :," < "$tmpdepfile" > "$depfile"
# Some versions of the HPUX 10.20 sed can't process this invocation
# correctly. Breaking it into two sed invocations is a workaround.
sed 's,^[^:]*: \(.*\)$,\1,;s/^\\$//;/^$/d;/:$/d' < "$tmpdepfile" |
sed -e 's/$/ :/' >> "$depfile"
rm -f "$tmpdepfile"
;;
hp2)
# The "hp" stanza above does not work with aCC (C++) and HP's ia64
# compilers, which have integrated preprocessors. The correct option
# to use with these is +Maked; it writes dependencies to a file named
# 'foo.d', which lands next to the object file, wherever that
# happens to be.
# Much of this is similar to the tru64 case; see comments there.
dir=`echo "$object" | sed -e 's|/[^/]*$|/|'`
test "x$dir" = "x$object" && dir=
base=`echo "$object" | sed -e 's|^.*/||' -e 's/\.o$//' -e 's/\.lo$//'`
if test "$libtool" = yes; then
tmpdepfile1=$dir$base.d
tmpdepfile2=$dir.libs/$base.d
"$@" -Wc,+Maked
else
tmpdepfile1=$dir$base.d
tmpdepfile2=$dir$base.d
"$@" +Maked
fi
stat=$?
if test $stat -eq 0; then :
else
rm -f "$tmpdepfile1" "$tmpdepfile2"
exit $stat
fi
for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2"
do
test -f "$tmpdepfile" && break
done
if test -f "$tmpdepfile"; then
sed -e "s,^.*\.[a-z]*:,$object:," "$tmpdepfile" > "$depfile"
# Add `dependent.h:' lines.
sed -ne '2,${
s/^ *//
s/ \\*$//
s/$/:/
p
}' "$tmpdepfile" >> "$depfile"
else
echo "#dummy" > "$depfile"
fi
rm -f "$tmpdepfile" "$tmpdepfile2"
;;
tru64)
# The Tru64 compiler uses -MD to generate dependencies as a side
# effect. `cc -MD -o foo.o ...' puts the dependencies into `foo.o.d'.
# At least on Alpha/Redhat 6.1, Compaq CCC V6.2-504 seems to put
# dependencies in `foo.d' instead, so we check for that too.
# Subdirectories are respected.
dir=`echo "$object" | sed -e 's|/[^/]*$|/|'`
test "x$dir" = "x$object" && dir=
base=`echo "$object" | sed -e 's|^.*/||' -e 's/\.o$//' -e 's/\.lo$//'`
if test "$libtool" = yes; then
# With Tru64 cc, shared objects can also be used to make a
# static library. This mechanism is used in libtool 1.4 series to
# handle both shared and static libraries in a single compilation.
# With libtool 1.4, dependencies were output in $dir.libs/$base.lo.d.
#
# With libtool 1.5 this exception was removed, and libtool now
# generates 2 separate objects for the 2 libraries. These two
# compilations output dependencies in $dir.libs/$base.o.d and
# in $dir$base.o.d. We have to check for both files, because
# one of the two compilations can be disabled. We should prefer
# $dir$base.o.d over $dir.libs/$base.o.d because the latter is
# automatically cleaned when .libs/ is deleted, while ignoring
# the former would cause a distcleancheck panic.
tmpdepfile1=$dir.libs/$base.lo.d # libtool 1.4
tmpdepfile2=$dir$base.o.d # libtool 1.5
tmpdepfile3=$dir.libs/$base.o.d # libtool 1.5
tmpdepfile4=$dir.libs/$base.d # Compaq CCC V6.2-504
"$@" -Wc,-MD
else
tmpdepfile1=$dir$base.o.d
tmpdepfile2=$dir$base.d
tmpdepfile3=$dir$base.d
tmpdepfile4=$dir$base.d
"$@" -MD
fi
stat=$?
if test $stat -eq 0; then :
else
rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3" "$tmpdepfile4"
exit $stat
fi
for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3" "$tmpdepfile4"
do
test -f "$tmpdepfile" && break
done
if test -f "$tmpdepfile"; then
sed -e "s,^.*\.[a-z]*:,$object:," < "$tmpdepfile" > "$depfile"
# That's a tab and a space in the [].
sed -e 's,^.*\.[a-z]*:[ ]*,,' -e 's,$,:,' < "$tmpdepfile" >> "$depfile"
else
echo "#dummy" > "$depfile"
fi
rm -f "$tmpdepfile"
;;
msvc7)
if test "$libtool" = yes; then
showIncludes=-Wc,-showIncludes
else
showIncludes=-showIncludes
fi
"$@" $showIncludes > "$tmpdepfile"
stat=$?
grep -v '^Note: including file: ' "$tmpdepfile"
if test "$stat" = 0; then :
else
rm -f "$tmpdepfile"
exit $stat
fi
rm -f "$depfile"
echo "$object : \\" > "$depfile"
# The first sed program below extracts the file names and escapes
# backslashes for cygpath. The second sed program outputs the file
# name when reading, but also accumulates all include files in the
# hold buffer in order to output them again at the end. This only
# works with sed implementations that can handle large buffers.
sed < "$tmpdepfile" -n '
/^Note: including file: *\(.*\)/ {
s//\1/
s/\\/\\\\/g
p
}' | $cygpath_u | sort -u | sed -n '
s/ /\\ /g
s/\(.*\)/ \1 \\/p
s/.\(.*\) \\/\1:/
H
$ {
s/.*/ /
G
p
}' >> "$depfile"
rm -f "$tmpdepfile"
;;
msvc7msys)
# This case exists only to let depend.m4 do its work. It works by
# looking at the text of this script. This case will never be run,
# since it is checked for above.
exit 1
;;
#nosideeffect)
# This comment above is used by automake to tell side-effect
# dependency tracking mechanisms from slower ones.
dashmstdout)
# Important note: in order to support this mode, a compiler *must*
# always write the preprocessed file to stdout, regardless of -o.
"$@" || exit $?
# Remove the call to Libtool.
if test "$libtool" = yes; then
while test "X$1" != 'X--mode=compile'; do
shift
done
shift
fi
# Remove `-o $object'.
IFS=" "
for arg
do
case $arg in
-o)
shift
;;
$object)
shift
;;
*)
set fnord "$@" "$arg"
shift # fnord
shift # $arg
;;
esac
done
test -z "$dashmflag" && dashmflag=-M
# Require at least two characters before searching for `:'
# in the target name. This is to cope with DOS-style filenames:
# a dependency such as `c:/foo/bar' could be seen as target `c' otherwise.
"$@" $dashmflag |
sed 's:^[ ]*[^: ][^:][^:]*\:[ ]*:'"$object"'\: :' > "$tmpdepfile"
rm -f "$depfile"
cat < "$tmpdepfile" > "$depfile"
tr ' ' '
' < "$tmpdepfile" | \
## Some versions of the HPUX 10.20 sed can't process this invocation
## correctly. Breaking it into two sed invocations is a workaround.
sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' | sed -e 's/$/ :/' >> "$depfile"
rm -f "$tmpdepfile"
;;
dashXmstdout)
# This case only exists to satisfy depend.m4. It is never actually
# run, as this mode is specially recognized in the preamble.
exit 1
;;
makedepend)
"$@" || exit $?
# Remove any Libtool call
if test "$libtool" = yes; then
while test "X$1" != 'X--mode=compile'; do
shift
done
shift
fi
# X makedepend
shift
cleared=no eat=no
for arg
do
case $cleared in
no)
set ""; shift
cleared=yes ;;
esac
if test $eat = yes; then
eat=no
continue
fi
case "$arg" in
-D*|-I*)
set fnord "$@" "$arg"; shift ;;
# Strip any option that makedepend may not understand. Remove
# the object too, otherwise makedepend will parse it as a source file.
-arch)
eat=yes ;;
-*|$object)
;;
*)
set fnord "$@" "$arg"; shift ;;
esac
done
obj_suffix=`echo "$object" | sed 's/^.*\././'`
touch "$tmpdepfile"
${MAKEDEPEND-makedepend} -o"$obj_suffix" -f"$tmpdepfile" "$@"
rm -f "$depfile"
# makedepend may prepend the VPATH from the source file name to the object.
# No need to regex-escape $object, excess matching of '.' is harmless.
sed "s|^.*\($object *:\)|\1|" "$tmpdepfile" > "$depfile"
sed '1,2d' "$tmpdepfile" | tr ' ' '
' | \
## Some versions of the HPUX 10.20 sed can't process this invocation
## correctly. Breaking it into two sed invocations is a workaround.
sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' | sed -e 's/$/ :/' >> "$depfile"
rm -f "$tmpdepfile" "$tmpdepfile".bak
;;
cpp)
# Important note: in order to support this mode, a compiler *must*
# always write the preprocessed file to stdout.
"$@" || exit $?
# Remove the call to Libtool.
if test "$libtool" = yes; then
while test "X$1" != 'X--mode=compile'; do
shift
done
shift
fi
# Remove `-o $object'.
IFS=" "
for arg
do
case $arg in
-o)
shift
;;
$object)
shift
;;
*)
set fnord "$@" "$arg"
shift # fnord
shift # $arg
;;
esac
done
"$@" -E |
sed -n -e '/^# [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \
-e '/^#line [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' |
sed '$ s: \\$::' > "$tmpdepfile"
rm -f "$depfile"
echo "$object : \\" > "$depfile"
cat < "$tmpdepfile" >> "$depfile"
sed < "$tmpdepfile" '/^$/d;s/^ //;s/ \\$//;s/$/ :/' >> "$depfile"
rm -f "$tmpdepfile"
;;
msvisualcpp)
# Important note: in order to support this mode, a compiler *must*
# always write the preprocessed file to stdout.
"$@" || exit $?
# Remove the call to Libtool.
if test "$libtool" = yes; then
while test "X$1" != 'X--mode=compile'; do
shift
done
shift
fi
IFS=" "
for arg
do
case "$arg" in
-o)
shift
;;
$object)
shift
;;
"-Gm"|"/Gm"|"-Gi"|"/Gi"|"-ZI"|"/ZI")
set fnord "$@"
shift
shift
;;
*)
set fnord "$@" "$arg"
shift
shift
;;
esac
done
"$@" -E 2>/dev/null |
sed -n '/^#line [0-9][0-9]* "\([^"]*\)"/ s::\1:p' | $cygpath_u | sort -u > "$tmpdepfile"
rm -f "$depfile"
echo "$object : \\" > "$depfile"
sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s:: \1 \\:p' >> "$depfile"
echo " " >> "$depfile"
sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s::\1\::p' >> "$depfile"
rm -f "$tmpdepfile"
;;
msvcmsys)
# This case exists only to let depend.m4 do its work. It works by
# looking at the text of this script. This case will never be run,
# since it is checked for above.
exit 1
;;
none)
exec "$@"
;;
*)
echo "Unknown depmode $depmode" 1>&2
exit 1
;;
esac
exit 0
# Local Variables:
# mode: shell-script
# sh-indentation: 2
# eval: (add-hook 'write-file-hooks 'time-stamp)
# time-stamp-start: "scriptversion="
# time-stamp-format: "%:y-%02m-%02d.%02H"
# time-stamp-time-zone: "UTC"
# time-stamp-end: "; # UTC"
# End:

View File

@ -1,527 +0,0 @@
#!/bin/sh
# install - install a program, script, or datafile
scriptversion=2011-01-19.21; # UTC
# This originates from X11R5 (mit/util/scripts/install.sh), which was
# later released in X11R6 (xc/config/util/install.sh) with the
# following copyright and license.
#
# Copyright (C) 1994 X Consortium
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Except as contained in this notice, the name of the X Consortium shall not
# be used in advertising or otherwise to promote the sale, use or other deal-
# ings in this Software without prior written authorization from the X Consor-
# tium.
#
#
# FSF changes to this file are in the public domain.
#
# Calling this script install-sh is preferred over install.sh, to prevent
# `make' implicit rules from creating a file called install from it
# when there is no Makefile.
#
# This script is compatible with the BSD install script, but was written
# from scratch.
nl='
'
IFS=" "" $nl"
# set DOITPROG to echo to test this script
# Don't use :- since 4.3BSD and earlier shells don't like it.
doit=${DOITPROG-}
if test -z "$doit"; then
doit_exec=exec
else
doit_exec=$doit
fi
# Put in absolute file names if you don't have them in your path;
# or use environment vars.
chgrpprog=${CHGRPPROG-chgrp}
chmodprog=${CHMODPROG-chmod}
chownprog=${CHOWNPROG-chown}
cmpprog=${CMPPROG-cmp}
cpprog=${CPPROG-cp}
mkdirprog=${MKDIRPROG-mkdir}
mvprog=${MVPROG-mv}
rmprog=${RMPROG-rm}
stripprog=${STRIPPROG-strip}
posix_glob='?'
initialize_posix_glob='
test "$posix_glob" != "?" || {
if (set -f) 2>/dev/null; then
posix_glob=
else
posix_glob=:
fi
}
'
posix_mkdir=
# Desired mode of installed file.
mode=0755
chgrpcmd=
chmodcmd=$chmodprog
chowncmd=
mvcmd=$mvprog
rmcmd="$rmprog -f"
stripcmd=
src=
dst=
dir_arg=
dst_arg=
copy_on_change=false
no_target_directory=
usage="\
Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
or: $0 [OPTION]... SRCFILES... DIRECTORY
or: $0 [OPTION]... -t DIRECTORY SRCFILES...
or: $0 [OPTION]... -d DIRECTORIES...
In the 1st form, copy SRCFILE to DSTFILE.
In the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
In the 4th, create DIRECTORIES.
Options:
--help display this help and exit.
--version display version info and exit.
-c (ignored)
-C install only if different (preserve the last data modification time)
-d create directories instead of installing files.
-g GROUP $chgrpprog installed files to GROUP.
-m MODE $chmodprog installed files to MODE.
-o USER $chownprog installed files to USER.
-s $stripprog installed files.
-t DIRECTORY install into DIRECTORY.
-T report an error if DSTFILE is a directory.
Environment variables override the default commands:
CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
RMPROG STRIPPROG
"
while test $# -ne 0; do
case $1 in
-c) ;;
-C) copy_on_change=true;;
-d) dir_arg=true;;
-g) chgrpcmd="$chgrpprog $2"
shift;;
--help) echo "$usage"; exit $?;;
-m) mode=$2
case $mode in
*' '* | *' '* | *'
'* | *'*'* | *'?'* | *'['*)
echo "$0: invalid mode: $mode" >&2
exit 1;;
esac
shift;;
-o) chowncmd="$chownprog $2"
shift;;
-s) stripcmd=$stripprog;;
-t) dst_arg=$2
# Protect names problematic for `test' and other utilities.
case $dst_arg in
-* | [=\(\)!]) dst_arg=./$dst_arg;;
esac
shift;;
-T) no_target_directory=true;;
--version) echo "$0 $scriptversion"; exit $?;;
--) shift
break;;
-*) echo "$0: invalid option: $1" >&2
exit 1;;
*) break;;
esac
shift
done
if test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
# When -d is used, all remaining arguments are directories to create.
# When -t is used, the destination is already specified.
# Otherwise, the last argument is the destination. Remove it from $@.
for arg
do
if test -n "$dst_arg"; then
# $@ is not empty: it contains at least $arg.
set fnord "$@" "$dst_arg"
shift # fnord
fi
shift # arg
dst_arg=$arg
# Protect names problematic for `test' and other utilities.
case $dst_arg in
-* | [=\(\)!]) dst_arg=./$dst_arg;;
esac
done
fi
if test $# -eq 0; then
if test -z "$dir_arg"; then
echo "$0: no input file specified." >&2
exit 1
fi
# It's OK to call `install-sh -d' without argument.
# This can happen when creating conditional directories.
exit 0
fi
if test -z "$dir_arg"; then
do_exit='(exit $ret); exit $ret'
trap "ret=129; $do_exit" 1
trap "ret=130; $do_exit" 2
trap "ret=141; $do_exit" 13
trap "ret=143; $do_exit" 15
# Set umask so as not to create temps with too-generous modes.
# However, 'strip' requires both read and write access to temps.
case $mode in
# Optimize common cases.
*644) cp_umask=133;;
*755) cp_umask=22;;
*[0-7])
if test -z "$stripcmd"; then
u_plus_rw=
else
u_plus_rw='% 200'
fi
cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
*)
if test -z "$stripcmd"; then
u_plus_rw=
else
u_plus_rw=,u+rw
fi
cp_umask=$mode$u_plus_rw;;
esac
fi
for src
do
# Protect names problematic for `test' and other utilities.
case $src in
-* | [=\(\)!]) src=./$src;;
esac
if test -n "$dir_arg"; then
dst=$src
dstdir=$dst
test -d "$dstdir"
dstdir_status=$?
else
# Waiting for this to be detected by the "$cpprog $src $dsttmp" command
# might cause directories to be created, which would be especially bad
# if $src (and thus $dsttmp) contains '*'.
if test ! -f "$src" && test ! -d "$src"; then
echo "$0: $src does not exist." >&2
exit 1
fi
if test -z "$dst_arg"; then
echo "$0: no destination specified." >&2
exit 1
fi
dst=$dst_arg
# If destination is a directory, append the input filename; won't work
# if double slashes aren't ignored.
if test -d "$dst"; then
if test -n "$no_target_directory"; then
echo "$0: $dst_arg: Is a directory" >&2
exit 1
fi
dstdir=$dst
dst=$dstdir/`basename "$src"`
dstdir_status=0
else
# Prefer dirname, but fall back on a substitute if dirname fails.
dstdir=`
(dirname "$dst") 2>/dev/null ||
expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
X"$dst" : 'X\(//\)[^/]' \| \
X"$dst" : 'X\(//\)$' \| \
X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
echo X"$dst" |
sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
s//\1/
q
}
/^X\(\/\/\)[^/].*/{
s//\1/
q
}
/^X\(\/\/\)$/{
s//\1/
q
}
/^X\(\/\).*/{
s//\1/
q
}
s/.*/./; q'
`
test -d "$dstdir"
dstdir_status=$?
fi
fi
obsolete_mkdir_used=false
if test $dstdir_status != 0; then
case $posix_mkdir in
'')
# Create intermediate dirs using mode 755 as modified by the umask.
# This is like FreeBSD 'install' as of 1997-10-28.
umask=`umask`
case $stripcmd.$umask in
# Optimize common cases.
*[2367][2367]) mkdir_umask=$umask;;
.*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
*[0-7])
mkdir_umask=`expr $umask + 22 \
- $umask % 100 % 40 + $umask % 20 \
- $umask % 10 % 4 + $umask % 2
`;;
*) mkdir_umask=$umask,go-w;;
esac
# With -d, create the new directory with the user-specified mode.
# Otherwise, rely on $mkdir_umask.
if test -n "$dir_arg"; then
mkdir_mode=-m$mode
else
mkdir_mode=
fi
posix_mkdir=false
case $umask in
*[123567][0-7][0-7])
# POSIX mkdir -p sets u+wx bits regardless of umask, which
# is incompatible with FreeBSD 'install' when (umask & 300) != 0.
;;
*)
tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
if (umask $mkdir_umask &&
exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
then
if test -z "$dir_arg" || {
# Check for POSIX incompatibilities with -m.
# HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
# other-writeable bit of parent directory when it shouldn't.
# FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
ls_ld_tmpdir=`ls -ld "$tmpdir"`
case $ls_ld_tmpdir in
d????-?r-*) different_mode=700;;
d????-?--*) different_mode=755;;
*) false;;
esac &&
$mkdirprog -m$different_mode -p -- "$tmpdir" && {
ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
}
}
then posix_mkdir=:
fi
rmdir "$tmpdir/d" "$tmpdir"
else
# Remove any dirs left behind by ancient mkdir implementations.
rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
fi
trap '' 0;;
esac;;
esac
if
$posix_mkdir && (
umask $mkdir_umask &&
$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
)
then :
else
# The umask is ridiculous, or mkdir does not conform to POSIX,
# or it failed possibly due to a race condition. Create the
# directory the slow way, step by step, checking for races as we go.
case $dstdir in
/*) prefix='/';;
[-=\(\)!]*) prefix='./';;
*) prefix='';;
esac
eval "$initialize_posix_glob"
oIFS=$IFS
IFS=/
$posix_glob set -f
set fnord $dstdir
shift
$posix_glob set +f
IFS=$oIFS
prefixes=
for d
do
test X"$d" = X && continue
prefix=$prefix$d
if test -d "$prefix"; then
prefixes=
else
if $posix_mkdir; then
(umask=$mkdir_umask &&
$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
# Don't fail if two instances are running concurrently.
test -d "$prefix" || exit 1
else
case $prefix in
*\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
*) qprefix=$prefix;;
esac
prefixes="$prefixes '$qprefix'"
fi
fi
prefix=$prefix/
done
if test -n "$prefixes"; then
# Don't fail if two instances are running concurrently.
(umask $mkdir_umask &&
eval "\$doit_exec \$mkdirprog $prefixes") ||
test -d "$dstdir" || exit 1
obsolete_mkdir_used=true
fi
fi
fi
if test -n "$dir_arg"; then
{ test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
{ test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
{ test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
else
# Make a couple of temp file names in the proper directory.
dsttmp=$dstdir/_inst.$$_
rmtmp=$dstdir/_rm.$$_
# Trap to clean up those temp files at exit.
trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
# Copy the file name to the temp name.
(umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
# and set any options; do chmod last to preserve setuid bits.
#
# If any of these fail, we abort the whole thing. If we want to
# ignore errors from any of these, just make sure not to ignore
# errors from the above "$doit $cpprog $src $dsttmp" command.
#
{ test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
{ test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
{ test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
{ test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
# If -C, don't bother to copy if it wouldn't change the file.
if $copy_on_change &&
old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` &&
new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` &&
eval "$initialize_posix_glob" &&
$posix_glob set -f &&
set X $old && old=:$2:$4:$5:$6 &&
set X $new && new=:$2:$4:$5:$6 &&
$posix_glob set +f &&
test "$old" = "$new" &&
$cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
then
rm -f "$dsttmp"
else
# Rename the file to the real destination.
$doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
# The rename failed, perhaps because mv can't rename something else
# to itself, or perhaps because mv is so ancient that it does not
# support -f.
{
# Now remove or move aside any old file at destination location.
# We try this two ways since rm can't unlink itself on some
# systems and the destination file might be busy for other
# reasons. In this case, the final cleanup might fail but the new
# file should still install successfully.
{
test ! -f "$dst" ||
$doit $rmcmd -f "$dst" 2>/dev/null ||
{ $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
{ $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
} ||
{ echo "$0: cannot unlink or rename $dst" >&2
(exit 1); exit 1
}
} &&
# Now rename the file to the real destination.
$doit $mvcmd "$dsttmp" "$dst"
}
fi || exit 1
trap '' 0
fi
done
# Local variables:
# eval: (add-hook 'write-file-hooks 'time-stamp)
# time-stamp-start: "scriptversion="
# time-stamp-format: "%:y-%02m-%02d.%02H"
# time-stamp-time-zone: "UTC"
# time-stamp-end: "; # UTC"
# End:

File diff suppressed because it is too large Load Diff

Some files were not shown because too many files have changed in this diff Show More