Add agi::util::try_parse

This commit is contained in:
Thomas Goyne 2013-01-01 08:54:55 -08:00
parent 4116e88fe5
commit cec5f2256c
4 changed files with 70 additions and 26 deletions

View File

@ -173,6 +173,21 @@ struct dialogue_tokens : lex::lexer<Lexer> {
}
};
template<typename Parser, typename T>
bool do_try_parse(std::string const& str, Parser parser, T *out) {
using namespace boost::spirit::qi;
T res;
char const* cstr = str.c_str();
bool parsed = parse(cstr, cstr + str.size(), parser, res);
if (parsed && cstr == &str[str.size()]) {
*out = res;
return true;
}
return false;
}
}
namespace agi {
@ -206,4 +221,15 @@ namespace ass {
return data;
}
}
namespace util {
// from util.h
bool try_parse(std::string const& str, double *out) {
return do_try_parse(str, boost::spirit::qi::double_, out);
}
bool try_parse(std::string const& str, int *out) {
return do_try_parse(str, boost::spirit::qi::int_, out);
}
}
}

View File

@ -32,7 +32,7 @@ void str_lower(std::string &str) {
boost::to_lower(str);
}
int strtoi(std::string &str) {
int strtoi(std::string const& str) {
errno = 0;
long l = strtol(str.c_str(), nullptr, 10);

View File

@ -25,8 +25,6 @@
namespace agi {
namespace util {
/// Whether the path is a file or directory.
enum PathType {
TypeFile, ///< File
@ -55,7 +53,10 @@ namespace agi {
// Convert a string to Integer.
// @param str Input string
int strtoi(std::string &str);
int strtoi(std::string const& str);
bool try_parse(std::string const& str, double *out);
bool try_parse(std::string const& str, int *out);
/// Check for amount of free space on a Path.
// @param path[in] Path to check

View File

@ -23,56 +23,52 @@
#include <libaegisub/util.h>
#include "main.h"
class lagi_util : public libagi {
protected:
// placeholder
};
class lagi_util : public libagi { };
namespace agi {
TEST_F(lagi_util, UtilDirnameEmpty) {
TEST(lagi_util, UtilDirnameEmpty) {
EXPECT_STREQ(".", util::DirName("").c_str());
}
TEST_F(lagi_util, UtilDirnameNoTrailingSlash) {
TEST(lagi_util, UtilDirnameNoTrailingSlash) {
EXPECT_STREQ(".", util::DirName("dot").c_str());
}
TEST_F(lagi_util, UtilDirnameRoot) {
TEST(lagi_util, UtilDirnameRoot) {
EXPECT_STREQ("/", util::DirName("/").c_str());
}
TEST_F(lagi_util, UtilDirnameHeir) {
TEST(lagi_util, UtilDirnameHeir) {
EXPECT_STREQ("/last/part/not_stripped/", util::DirName("/last/part/not_stripped/").c_str());
}
TEST_F(lagi_util, UtilDirnameHeirNoTrailingSlash) {
TEST(lagi_util, UtilDirnameHeirNoTrailingSlash) {
EXPECT_STREQ("/last/part/", util::DirName("/last/part/stripped").c_str());
}
TEST_F(lagi_util, UtilRenameOverwrite) {
TEST(lagi_util, UtilRenameOverwrite) {
util::Rename("./data/rename_me_overwrite", "./data/rename_me_overwrite_renamed");
util::Rename("./data/rename_me_overwrite_renamed", "./data/rename_me_overwrite");
std::ofstream fp_touch("./data/rename_me_overwrite_renamed");
}
TEST_F(lagi_util, UtilRenameNew) {
TEST(lagi_util, UtilRenameNew) {
util::Rename("./data/rename_me", "./data/rename_me_renamed");
util::Rename("./data/rename_me_renamed", "./data/rename_me");
}
TEST_F(lagi_util, UtilRenameExNotFound) {
TEST(lagi_util, UtilRenameExNotFound) {
EXPECT_THROW(util::Rename("./data/nonexistent", ""), FileNotFoundError);
}
TEST_F(lagi_util, Utilstr_lower) {
TEST(lagi_util, Utilstr_lower) {
std::string str("-!ABCDEFGHIJKLMNOPQRSTUVWXYZ123");
util::str_lower(str);
EXPECT_STREQ("-!abcdefghijklmnopqrstuvwxyz123", str.c_str());
}
TEST_F(lagi_util, UtilstrtoiInvalidRange) {
TEST(lagi_util, UtilstrtoiInvalidRange) {
std::string str("2147483650");
EXPECT_EQ(0, util::strtoi(str));
@ -80,17 +76,17 @@ TEST_F(lagi_util, UtilstrtoiInvalidRange) {
EXPECT_EQ(0, util::strtoi(str));
}
TEST_F(lagi_util, UtilstrtoiInvalidString) {
TEST(lagi_util, UtilstrtoiInvalidString) {
std::string str("bottles of beer on the wall");
EXPECT_EQ(0, util::strtoi(str));
}
TEST_F(lagi_util, UtilstrtoiNumberWithString) {
TEST(lagi_util, UtilstrtoiNumberWithString) {
std::string str("24 bottles of beer on the wall");
EXPECT_EQ(24, util::strtoi(str));
}
TEST_F(lagi_util, UtilstrtoiValidString) {
TEST(lagi_util, UtilstrtoiValidString) {
std::string str("24");
int i;
@ -98,26 +94,47 @@ TEST_F(lagi_util, UtilstrtoiValidString) {
EXPECT_EQ(24, i);
}
TEST_F(lagi_util, UtilfreespaceFile) {
TEST(lagi_util, UtilfreespaceFile) {
std::string path("./data/somefile");
EXPECT_NO_THROW(util::freespace(path, util::TypeFile));
EXPECT_ANY_THROW(util::freespace(path));
}
TEST_F(lagi_util, UtilfreespaceDir) {
TEST(lagi_util, UtilfreespaceDir) {
std::string path("./data");
EXPECT_NO_THROW(util::freespace(path));
}
TEST_F(lagi_util, UtilfreespaceNoAccess) {
TEST(lagi_util, UtilfreespaceNoAccess) {
std::string path("./data/dir_access_denied");
EXPECT_THROW(util::freespace(path), acs::Read);
}
TEST_F(lagi_util, UtilfreespaceInvalid) {
TEST(lagi_util, UtilfreespaceInvalid) {
std::string path("/nonexistent");
EXPECT_ANY_THROW(util::freespace(path));
}
TEST(lagi_util, try_parse_double) {
double d = 0.0;
EXPECT_TRUE(util::try_parse("1.0", &d));
EXPECT_EQ(1.0, d);
EXPECT_FALSE(util::try_parse("aaa", &d));
EXPECT_EQ(1.0, d);
EXPECT_FALSE(util::try_parse("2aaa", &d));
EXPECT_EQ(1.0, d);
}
TEST(lagi_util, try_parse_int) {
int i = 0;
EXPECT_TRUE(util::try_parse("1", &i));
EXPECT_EQ(1, i);
EXPECT_FALSE(util::try_parse("2.0", &i));
EXPECT_EQ(1.0, i);
}
} // namespace agi