123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2020-08-14
- * Changed: 2021-07-08
- *
- * */
- #include <gtest/gtest.h>
- #include <ls_std/ls_std.hpp>
- #include <TestHelper.hpp>
- namespace
- {
- class StringTest : public ::testing::Test
- {
- protected:
- StringTest() = default;
- ~StringTest() override = default;
- void SetUp() override
- {}
- void TearDown() override
- {}
- static std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::String>> createPersistentTestString()
- {
- std::shared_ptr<ls_std::String> sentence = std::make_shared<ls_std::String>();
- std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_string.json";
- std::shared_ptr<ls_std::File> file = std::make_shared<ls_std::File>(path);
- file->createNewFile();
- ls_std::FileWriter writer{*file};
- writer.write(R"({"value":"Hello!"})");
- auto serializable = std::make_shared<ls_std::SerializableJsonString>(sentence);
- sentence->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
- auto storable = std::make_shared<ls_std::StorableFile>(path);
- sentence->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
- return std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::String>>(file, sentence);
- }
- };
- // assignment operators
- TEST_F(StringTest, operator_assignment)
- {
- ls_std::String text{};
- text = "Hi!";
- ASSERT_STREQ("Hi!", text.toString().c_str());
- }
- // arithmetic operators
- TEST_F(StringTest, operator_add)
- {
- ls_std::String greetings{"Hello! "};
- ls_std::String question{"How are you? "};
- const std::string& answer = "I'm good by the way!";
- greetings = greetings + question + answer;
- ASSERT_STREQ("Hello! How are you? I'm good by the way!", greetings.toString().c_str());
- }
- TEST_F(StringTest, operator_minus)
- {
- ls_std::String text{"abcdefghij"};
- text = text - 5;
- ASSERT_STREQ("abcde", text.toString().c_str());
- }
- // compound operators
- TEST_F(StringTest, operator_add_assign_with_reference)
- {
- ls_std::String text{};
- ls_std::String hello{"Hi!"};
- text += hello;
- ASSERT_STREQ("Hi!", text.toString().c_str());
- }
- TEST_F(StringTest, operator_add_assign_with_value)
- {
- ls_std::String text{};
- text += "Hi!";
- ASSERT_STREQ("Hi!", text.toString().c_str());
- }
- // comparison operators
- TEST_F(StringTest, operator_equals_with_reference)
- {
- ls_std::String text{"Hi!"};
- ls_std::String hello{"Hi!"};
- ASSERT_TRUE(text == hello);
- ASSERT_TRUE(hello == text);
- }
- TEST_F(StringTest, operator_equals_with_value)
- {
- ls_std::String hello{"Hi!"};
- ASSERT_TRUE(hello == "Hi!");
- }
- TEST_F(StringTest, operator_not_equals_with_reference)
- {
- ls_std::String text{"Hi!"};
- ls_std::String hello{"Hello!"};
- ASSERT_TRUE(text != hello);
- }
- TEST_F(StringTest, operator_not_equals_with_value)
- {
- ls_std::String text{"Hi!"};
- ASSERT_TRUE(text != "Hello!");
- }
- // implementation
- TEST_F(StringTest, load)
- {
- // preparation
- auto storableString = createPersistentTestString();
- // check
- storableString.second->load();
- ASSERT_STREQ("Hello!", *storableString.second);
- storableString.first->remove();
- }
- TEST_F(StringTest, marshal)
- {
- std::shared_ptr<ls_std::String> sentence = std::make_shared<ls_std::String>("Hello!");
- auto serializable = std::make_shared<ls_std::SerializableJsonString>(sentence);
- sentence->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
- ASSERT_STREQ(R"({"value":"Hello!"})", sentence->marshal().c_str());
- }
- TEST_F(StringTest, parse)
- {
- ls_std::String text{};
- text.parse("Hello!");
- ASSERT_STREQ("Hello!", text.toString().c_str());
- }
- TEST_F(StringTest, toString)
- {
- ls_std::String text{"Hello!"};
- ASSERT_STREQ("Hello!", text.toString().c_str());
- }
- TEST_F(StringTest, unmarshal)
- {
- std::shared_ptr<ls_std::String> sentence = std::make_shared<ls_std::String>("Hello!");
- auto serializable = std::make_shared<ls_std::SerializableJsonString>(sentence);
- sentence->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
- sentence->unmarshal(R"({"value":"Test!"})");
- ASSERT_STREQ("Test!", *sentence);
- }
- // additional functionality
- TEST_F(StringTest, contains)
- {
- ls_std::String text{};
- text = "Hey, I'm searching for the keyword 'cake'!";
- ASSERT_TRUE(text.contains("cake"));
- }
- TEST_F(StringTest, contains_does_not_contain_search_word)
- {
- ls_std::String text{};
- text = "Hey, I'm searching for the keyword 'cake'!";
- ASSERT_FALSE(text.contains("butter"));
- }
- TEST_F(StringTest, endsWith)
- {
- ls_std::String text{};
- text = "abcdef";
- ASSERT_TRUE(text.endsWith("ef"));
- }
- TEST_F(StringTest, endsWith_does_not_end_with_pattern)
- {
- ls_std::String text{};
- text = "abcdef";
- ASSERT_FALSE(text.endsWith("efg"));
- }
- TEST_F(StringTest, equalsIgnoreCase)
- {
- ls_std::String text{"Hello!"};
- ls_std::String hello{"HeLLo!"};
- ASSERT_TRUE(text.equalsIgnoreCase(hello));
- ASSERT_TRUE(text.equalsIgnoreCase("HeLLO!"));
- }
- TEST_F(StringTest, getByteData)
- {
- ls_std::String text{"Hallo!"};
- ASSERT_STREQ("Hallo!", text.getByteData().data());
- }
- TEST_F(StringTest, padLeft)
- {
- ls_std::String text{"abcdef"};
- ls_std::String anotherText{"ab"};
- ls_std::String emptyText{};
- ls_std::String longText{"This text is too long to fill!"};
- ASSERT_STREQ(" abcdef", text.padLeft(10, ' ').c_str());
- ASSERT_STREQ(" ab", anotherText.padLeft(10, ' ').c_str());
- ASSERT_STREQ(" ", emptyText.padLeft(10, ' ').c_str());
- ASSERT_STREQ("This text is too long to fill!", longText.padLeft(10, ' ').c_str());
- }
- TEST_F(StringTest, padRight)
- {
- ls_std::String text{"abcdef"};
- ls_std::String anotherText{"ab"};
- ls_std::String emptyText{};
- ls_std::String longText{"This text is too long to fill!"};
- ASSERT_STREQ("abcdef ", text.padRight(10, ' ').c_str());
- ASSERT_STREQ("ab ", anotherText.padRight(10, ' ').c_str());
- ASSERT_STREQ(" ", emptyText.padRight(10, ' ').c_str());
- ASSERT_STREQ("This text is too long to fill!", longText.padRight(10, ' ').c_str());
- }
- TEST_F(StringTest, reverse)
- {
- ls_std::String text{};
- text = "abcdef";
- ASSERT_STREQ("fedcba", text.reverse().c_str());
- ASSERT_STREQ("abcdef", text); // verify, that original string didn't change
- }
- TEST_F(StringTest, startsWith)
- {
- ls_std::String text{};
- text = "abcdef";
- ASSERT_TRUE(text.startsWith("abc"));
- }
- TEST_F(StringTest, startsWith_does_not_start_with_pattern)
- {
- ls_std::String text{};
- text = "abcdef";
- ASSERT_FALSE(text.startsWith("bc"));
- }
- TEST_F(StringTest, toLowerCase)
- {
- ls_std::String text{};
- text = "aBCdeFgHIJKLmn";
- ASSERT_STREQ("abcdefghijklmn", text.toLowerCase().c_str());
- ASSERT_STREQ("aBCdeFgHIJKLmn", text); // verify, that original String didn't change
- }
- TEST_F(StringTest, toUpperCase)
- {
- ls_std::String text{};
- text = "aBCdeFgHIJKLmn";
- ASSERT_STREQ("ABCDEFGHIJKLMN", text.toUpperCase().c_str());
- ASSERT_STREQ("aBCdeFgHIJKLmn", text); // verify, that original String didn't change
- }
- TEST_F(StringTest, setSerializable_no_reference)
- {
- ls_std::String x{};
- EXPECT_THROW({
- try
- {
- x.setSerializable(nullptr);
- }
- catch (const ls_std::IllegalArgumentException &_exception)
- {
- throw;
- }
- }, ls_std::IllegalArgumentException);
- }
- TEST_F(StringTest, setStorable_no_reference)
- {
- ls_std::String x{};
- EXPECT_THROW({
- try
- {
- x.setStorable(nullptr);
- }
- catch (const ls_std::IllegalArgumentException &_exception)
- {
- throw;
- }
- }, ls_std::IllegalArgumentException);
- }
- }
|