From 51c918012842d03c4bda938c26c7887f19a53dd4 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 12 May 2019 17:59:10 +0200 Subject: [PATCH] Added tests for Easy::Filter. --- README.adoc | 2 +- tests/entities/test_filter.cpp | 96 ++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 tests/entities/test_filter.cpp diff --git a/README.adoc b/README.adoc index 88f4e3d..4763632 100644 --- a/README.adoc +++ b/README.adoc @@ -401,7 +401,7 @@ strings and you can use unsupported fields in an `Entity` by converting it to * [x] Context * [ ] Conversation * [x] Emoji -* [ ] Filter +* [x] Filter * [ ] Instance * [ ] List * [ ] Mention diff --git a/tests/entities/test_filter.cpp b/tests/entities/test_filter.cpp new file mode 100644 index 0000000..d0427ae --- /dev/null +++ b/tests/entities/test_filter.cpp @@ -0,0 +1,96 @@ +/* This file is part of mastodon-cpp. + * Copyright © 2019 tastytea + * + * 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, version 3. + * + * 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 . + */ + +#include +#include +#include +#include +#include "easy/entities/filter.hpp" +#include "easy/easy.hpp" + +using std::string; +using std::chrono::system_clock; + +using namespace Mastodon; + +SCENARIO ("Easy::Filter works as intended", "[entity]") +{ + GIVEN ("An Easy::Filter object") + { + Easy::Filter filter; + bool exception = false; + + WHEN ("It is initialized with valid filter data") + { + const string data = + "{\"whole_word\":false," + "\"phrase\":\"nöööö!\"," + "\"irreversible\":null," + "\"id\":\"2\"," + "\"expires_at\":null," + "\"context\":[\"public\",\"notifications\"]}"; + + try + { + filter.from_string(data); + } + catch (const std::exception &e) + { + exception = true; + } + + THEN ("No exception is thrown") + AND_THEN ("Filter is valid") + AND_THEN ("The attributes are set to the right values") + { + REQUIRE_FALSE(exception); + // REQUIRE(filter.valid()); // Pleroma issue #885. + REQUIRE(!filter.whole_word()); + REQUIRE(filter.phrase() == "nöööö!"); + REQUIRE(!filter.irreversible()); + REQUIRE(filter.id() == "2"); + REQUIRE(filter.expires_at().timepoint + == system_clock::time_point()); + REQUIRE(filter.context() == std::vector + ({ + Easy::context_type::Public, + Easy::context_type::Notifications + })); + } + } + + WHEN ("It is initialized with an empty string") + { + try + { + filter.from_string(""); + } + catch (const std::exception &e) + { + exception = true; + } + + THEN ("No exception is thrown") + AND_THEN ("It is not valid") + AND_THEN ("shortcode is empty") + { + REQUIRE_FALSE(exception); + REQUIRE_FALSE(filter.valid()); + REQUIRE(filter.phrase() == ""); + } + } + } +}