Files
ArduinoJson/test/StringBuilderTests.cpp

61 lines
1.2 KiB
C++
Raw Normal View History

2014-10-23 23:39:22 +02:00
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
2014-10-16 16:23:24 +02:00
#include <gtest/gtest.h>
#include <ArduinoJson/Internals/StringBuilder.hpp>
2014-10-16 16:23:24 +02:00
using namespace ArduinoJson::Internals;
2014-10-23 19:54:00 +02:00
class StringBuilderTests : public testing::Test {
protected:
2014-10-23 19:54:00 +02:00
virtual void SetUp() { sb = new StringBuilder(buffer, sizeof(buffer)); }
2014-10-16 16:23:24 +02:00
2014-10-23 19:54:00 +02:00
void print(const char *value) { returnValue = sb->print(value); }
2014-10-16 16:23:24 +02:00
2014-10-23 19:54:00 +02:00
void outputMustBe(const char *expected) { EXPECT_STREQ(expected, buffer); }
2014-10-16 16:23:24 +02:00
2014-10-23 19:54:00 +02:00
void resultMustBe(size_t expected) { EXPECT_EQ(expected, returnValue); }
2014-10-16 16:23:24 +02:00
private:
2014-10-23 19:54:00 +02:00
char buffer[20];
Print *sb;
size_t returnValue;
2014-10-16 16:23:24 +02:00
};
2014-10-23 19:54:00 +02:00
TEST_F(StringBuilderTests, InitialState) { outputMustBe(""); }
2014-10-16 16:23:24 +02:00
2014-10-23 19:54:00 +02:00
TEST_F(StringBuilderTests, OverCapacity) {
print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
resultMustBe(19);
2014-10-16 16:23:24 +02:00
2014-10-23 19:54:00 +02:00
print("ABC");
resultMustBe(0);
2014-10-16 16:23:24 +02:00
2014-10-23 19:54:00 +02:00
outputMustBe("ABCDEFGHIJKLMNOPQRS");
2014-10-16 16:23:24 +02:00
}
2014-10-23 19:54:00 +02:00
TEST_F(StringBuilderTests, EmptyString) {
print("");
resultMustBe(0);
outputMustBe("");
2014-10-16 16:23:24 +02:00
}
2014-10-23 19:54:00 +02:00
TEST_F(StringBuilderTests, OneString) {
print("ABCD");
resultMustBe(4);
outputMustBe("ABCD");
2014-10-16 16:23:24 +02:00
}
2014-10-23 19:54:00 +02:00
TEST_F(StringBuilderTests, TwoStrings) {
print("ABCD");
resultMustBe(4);
2014-10-16 16:23:24 +02:00
2014-10-23 19:54:00 +02:00
print("EFGH");
resultMustBe(4);
2014-10-16 16:23:24 +02:00
2014-10-23 19:54:00 +02:00
outputMustBe("ABCDEFGH");
2014-10-23 23:45:36 +02:00
}