From 3e7b397e47dd371616b0633ceb851c8bd8e2abfa Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 6 Jul 2012 15:09:10 +0300 Subject: [PATCH] Add unit tests for strings/utils.h and FS::Path --- tests/path.cpp | 105 ++++++++++++++++++++++++++++++++++++++++++ tests/stringutils.cpp | 79 +++++++++++++++++++++++++++++++ 2 files changed, 184 insertions(+) create mode 100644 tests/path.cpp create mode 100644 tests/stringutils.cpp diff --git a/tests/path.cpp b/tests/path.cpp new file mode 100644 index 0000000..fe85ab8 --- /dev/null +++ b/tests/path.cpp @@ -0,0 +1,105 @@ +#include +#include + +using namespace std; +using namespace Msp; + +class PathTests: public Test::RegisteredTest +{ +public: + PathTests(); + + static const char *get_name() { return "Path"; } + +private: + void empty(); + void normalization(); + void concatenation(); + void iterator_abs(); + void iterator_rel(); + void indexing(); +}; + +PathTests::PathTests() +{ + add(&PathTests::empty, "Emptiness"); + add(&PathTests::normalization, "Normalization"); + add(&PathTests::concatenation, "Concatenation"); + add(&PathTests::iterator_abs, "Iterator (absolute path)"); + add(&PathTests::iterator_rel, "Iterator (relative path)"); + add(&PathTests::indexing, "Indexing"); +} + +void PathTests::empty() +{ + EXPECT(FS::Path().empty()); + EXPECT(!FS::Path(".").empty()); +} + +void PathTests::normalization() +{ + FS::Path path("foo"); + EXPECT_EQUAL(path.str(), "./foo"); + path = "foo/"; + EXPECT_EQUAL(path.str(), "./foo"); + path = "foo/."; + EXPECT_EQUAL(path.str(), "./foo"); + path = "foo/.."; + EXPECT_EQUAL(path.str(), "."); + path = "//foo"; + EXPECT_EQUAL(path.str(), "/foo"); + path = "/.."; + EXPECT_EQUAL(path.str(), "/"); +} + +void PathTests::concatenation() +{ + FS::Path path("."); + path /= "foo"; + EXPECT_EQUAL(path.str(), "./foo"); + path /= "."; + EXPECT_EQUAL(path.str(), "./foo"); + path /= "bar"; + EXPECT_EQUAL(path.str(), "./foo/bar"); + path /= "../../quux"; + EXPECT_EQUAL(path.str(), "./quux"); + path /= "/"; + EXPECT_EQUAL(path.str(), "/"); + path /= ".."; + EXPECT_EQUAL(path.str(), "/"); +} + +void PathTests::iterator_abs() +{ + FS::Path path("/foo/bar"); + FS::Path::Iterator iter = path.begin(); + EXPECT(iter==path.begin()); + EXPECT_EQUAL(*iter, "/"); + ++iter; + EXPECT_EQUAL(*iter, "foo"); + ++iter; + EXPECT_EQUAL(*iter, "bar"); + ++iter; + EXPECT(iter==path.end()); +} + +void PathTests::iterator_rel() +{ + FS::Path path("./foo"); + FS::Path::Iterator iter = path.begin(); + EXPECT(iter==path.begin()); + EXPECT_EQUAL(*iter, "."); + ++iter; + EXPECT_EQUAL(*iter, "foo"); + ++iter; + EXPECT(iter==path.end()); +} + +void PathTests::indexing() +{ + FS::Path path("/foo/bar"); + EXPECT_EQUAL(path.size(), 3); + EXPECT_EQUAL(path[0], "/"); + EXPECT_EQUAL(path[1], "foo"); + EXPECT_EQUAL(path[-1], "bar"); +} diff --git a/tests/stringutils.cpp b/tests/stringutils.cpp new file mode 100644 index 0000000..f39b2a7 --- /dev/null +++ b/tests/stringutils.cpp @@ -0,0 +1,79 @@ +#include +#include + +using namespace std; +using namespace Msp; + +class StringUtilsTests: public Test::RegisteredTest +{ +private: + static const char *strings[]; + +public: + StringUtilsTests(); + + static const char *get_name() { return "stringutils"; } + +private: + void escape(); + void unescape(); + void unescape_invalid(); + void split(); +}; + +const char *StringUtilsTests::strings[] = +{ + "new\nline", "new\\nline", + "\ttab", "\\ttab", + "\"quotes'", "\\\"quotes\\'", + "back\\slash", "back\\\\slash", + "\001ACTION ducks\001", "\\001ACTION ducks\\001", + "g\303\266tterd\303\244mmerung", "g\\303\\266tterd\\303\\244mmerung", + 0 +}; + +StringUtilsTests::StringUtilsTests() +{ + add(&StringUtilsTests::escape, "c_escape"); + add(&StringUtilsTests::unescape, "c_unescape"); + add(&StringUtilsTests::unescape_invalid, "c_unescape (invalid)").expect_throw(); + add(&StringUtilsTests::split, "split"); +} + +void StringUtilsTests::escape() +{ + for(const char **i=strings; *i; i+=2) + { + string str = c_escape(*i); + expect_equal(str, str==*(i+1), format("str == '%s'", *(i+1))); + } +} + +void StringUtilsTests::unescape() +{ + for(const char **i=strings; *i; i+=2) + { + string str = c_unescape(*(i+1)); + expect_equal(str, str==*i, format("str == '%s'", *i)); + } +} + +void StringUtilsTests::unescape_invalid() +{ + c_unescape("\\q"); +} + +void StringUtilsTests::split() +{ + vector parts = ::split("foo bar baz"); + EXPECT_EQUAL(parts.size(), 3); + EXPECT_EQUAL(parts[0], "foo"); + EXPECT_EQUAL(parts[1], "bar"); + EXPECT_EQUAL(parts[2], "baz"); + + parts = split_fields("one::three", ':'); + EXPECT_EQUAL(parts.size(), 3); + EXPECT_EQUAL(parts[0], "one"); + EXPECT(parts[1].empty()); + EXPECT_EQUAL(parts[2], "three"); +} -- 2.43.0