Improve the line_iterator tests

This commit is contained in:
Thomas Goyne 2014-07-03 09:55:59 -07:00
parent 14969ec2ea
commit 2f0ddb4f32
2 changed files with 46 additions and 49 deletions

View file

@ -14,68 +14,58 @@
#include <libaegisub/line_iterator.h>
#include <main.h>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include <main.h>
#include <util.h>
using agi::line_iterator;
using namespace util;
template<typename T>
struct varg_type {
typedef T type;
};
template<> struct varg_type<std::string> {
typedef const char * type;
};
void test_values(agi::line_iterator<T>& iter) {
EXPECT_EQ(iter, end(iter));
}
template<typename T, int N>
void expect_eq(const char (&str)[N], const char *charset, int num, ...) {
std::string string(str, N - 1);
agi::charset::IconvWrapper conv("utf-8", charset);
string = conv.Convert(string);
std::stringstream ss(string);
line_iterator<T> iter;
EXPECT_NO_THROW(iter = line_iterator<T>(ss, charset));
va_list argp;
va_start(argp, num);
for (; num > 0; --num) {
EXPECT_FALSE(iter == line_iterator<T>());
EXPECT_EQ(*iter, va_arg(argp, typename varg_type<T>::type));
EXPECT_NO_THROW(++iter);
}
va_end(argp);
EXPECT_TRUE(iter == line_iterator<T>());
template<typename T, typename First, typename... Values>
void test_values(agi::line_iterator<T>& iter, First first, Values... values) {
EXPECT_FALSE(iter == end(iter));
EXPECT_EQ(*iter, first);
EXPECT_NO_THROW(++iter);
test_values(iter, values...);
}
template<typename T, typename... Values>
void test(std::string const& str, const char *encoding, Values... values) {
std::stringstream ss(str);
agi::line_iterator<T> iter;
EXPECT_NO_THROW(iter = agi::line_iterator<T>(ss, encoding));
test_values(iter, values...);
}
template<typename T, typename... Values>
void expect_eq(const char *str, Values... values) {
std::string utf8(str);
test<T>(utf8, "utf-8", values...);
agi::charset::IconvWrapper conv("utf-8", "utf-16");
auto utf16 = conv.Convert(utf8);
test<T>(utf16, "utf-16", values...);
}
TEST(lagi_line, int) {
std::vector<std::string> charsets = agi::charset::GetEncodingsList<std::vector<std::string> >();
for (auto const& charset : charsets) {
expect_eq<int>("1\n2\n3\n4", charset.c_str(), 4, 1, 2, 3, 4);
expect_eq<int>("1\n2\n3\n4\n", charset.c_str(), 4, 1, 2, 3, 4);
expect_eq<int>("1\n2\nb\n3\n4", charset.c_str(), 4, 1, 2, 3, 4);
expect_eq<int>("1.0\n2.0\n3.0\n4.0", charset.c_str(), 4, 1, 2, 3, 4);
expect_eq<int>(" 0x16 \n 09 \n -2", charset.c_str(), 3, 0, 9, -2);
}
expect_eq<int>("1\n2\n3\n4", 1, 2, 3, 4);
expect_eq<int>("1\n2\n3\n4\n", 1, 2, 3, 4);
expect_eq<int>("1\n2\nb\n3\n4", 1, 2, 3, 4);
expect_eq<int>("1.0\n2.0\n3.0\n4.0", 1, 2, 3, 4);
expect_eq<int>(" 0x16 \n 09 \n -2", 0, 9, -2);
}
TEST(lagi_line, double) {
std::vector<std::string> charsets = agi::charset::GetEncodingsList<std::vector<std::string> >();
for (auto const& charset : charsets) {
expect_eq<double>("1.0\n2.0", charset.c_str(), 2, 1.0, 2.0);
expect_eq<double>("#1.0\n\t2.5", charset.c_str(), 1, 2.5);
}
expect_eq<double>("1.0\n2.0", 1.0, 2.0);
expect_eq<double>("#1.0\n\t2.5", 2.5);
}
TEST(lagi_line, string) {
std::vector<std::string> charsets = agi::charset::GetEncodingsList<std::vector<std::string> >();
for (auto const& charset : charsets) {
expect_eq<std::string>("line 1\nline 2\nline 3", charset.c_str(), 3, "line 1", "line 2", "line 3");
expect_eq<std::string>(" white space ", charset.c_str(), 1, " white space ");
expect_eq<std::string>("blank\n\nlines\n", charset.c_str(), 4, "blank", "", "lines", "");
}
expect_eq<std::string>("line 1\nline 2\nline 3", "line 1", "line 2", "line 3");
expect_eq<std::string>(" white space ", " white space ");
expect_eq<std::string>("blank\n\nlines\n", "blank", "", "lines", "");
}

View file

@ -171,3 +171,10 @@ TEST(lagi_path, make_relative_on_dummy_url) {
EXPECT_UNCHANGED("dummy-audio:silence?sr=44100&bd=16&ch=1&ln=396900000", MakeRelative);
EXPECT_UNCHANGED("?dummy:23.976000:40000:1280:720:47:163:254:", MakeRelative);
}
TEST(lagi_path, encode) {
Path p;
p.SetToken("?user", "/a/b/c");
p.SetToken("?local", "/a/b/c/d");
EXPECT_EQ("?local/e", p.Encode("/a/b/c/d/e"));
}