Skip to content

Commit bb36372

Browse files
theStacksipa
authored andcommitted
test: add unit tests for Span-parsing helpers
tests the following four functions: - Const() [parse constant] - Func() [parse function] - Expr() [parse expression] - Split() [split up a string]
1 parent 5e69aee commit bb36372

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

src/test/util_tests.cpp

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <util/strencodings.h>
1313
#include <util/string.h>
1414
#include <util/time.h>
15+
#include <util/spanparsing.h>
1516

1617
#include <stdint.h>
1718
#include <thread>
@@ -1572,4 +1573,127 @@ BOOST_AUTO_TEST_CASE(test_Capitalize)
15721573
BOOST_CHECK_EQUAL(Capitalize("\x00\xfe\xff"), "\x00\xfe\xff");
15731574
}
15741575

1576+
static std::string SpanToStr(Span<const char>& span)
1577+
{
1578+
return std::string(span.begin(), span.end());
1579+
}
1580+
1581+
BOOST_AUTO_TEST_CASE(test_spanparsing)
1582+
{
1583+
using namespace spanparsing;
1584+
std::string input;
1585+
Span<const char> sp;
1586+
bool success;
1587+
1588+
// Const(...): parse a constant, update span to skip it if successful
1589+
input = "MilkToastHoney";
1590+
sp = MakeSpan(input);
1591+
success = Const("", sp); // empty
1592+
BOOST_CHECK(success);
1593+
BOOST_CHECK_EQUAL(SpanToStr(sp), "MilkToastHoney");
1594+
1595+
success = Const("Milk", sp);
1596+
BOOST_CHECK(success);
1597+
BOOST_CHECK_EQUAL(SpanToStr(sp), "ToastHoney");
1598+
1599+
success = Const("Bread", sp);
1600+
BOOST_CHECK(!success);
1601+
1602+
success = Const("Toast", sp);
1603+
BOOST_CHECK(success);
1604+
BOOST_CHECK_EQUAL(SpanToStr(sp), "Honey");
1605+
1606+
success = Const("Honeybadger", sp);
1607+
BOOST_CHECK(!success);
1608+
1609+
success = Const("Honey", sp);
1610+
BOOST_CHECK(success);
1611+
BOOST_CHECK_EQUAL(SpanToStr(sp), "");
1612+
1613+
// Func(...): parse a function call, update span to argument if successful
1614+
input = "Foo(Bar(xy,z()))";
1615+
sp = MakeSpan(input);
1616+
1617+
success = Func("FooBar", sp);
1618+
BOOST_CHECK(!success);
1619+
1620+
success = Func("Foo(", sp);
1621+
BOOST_CHECK(!success);
1622+
1623+
success = Func("Foo", sp);
1624+
BOOST_CHECK(success);
1625+
BOOST_CHECK_EQUAL(SpanToStr(sp), "Bar(xy,z())");
1626+
1627+
success = Func("Bar", sp);
1628+
BOOST_CHECK(success);
1629+
BOOST_CHECK_EQUAL(SpanToStr(sp), "xy,z()");
1630+
1631+
success = Func("xy", sp);
1632+
BOOST_CHECK(!success);
1633+
1634+
// Expr(...): return expression that span begins with, update span to skip it
1635+
Span<const char> result;
1636+
1637+
input = "(n*(n-1))/2";
1638+
sp = MakeSpan(input);
1639+
result = Expr(sp);
1640+
BOOST_CHECK_EQUAL(SpanToStr(result), "(n*(n-1))/2");
1641+
BOOST_CHECK_EQUAL(SpanToStr(sp), "");
1642+
1643+
input = "foo,bar";
1644+
sp = MakeSpan(input);
1645+
result = Expr(sp);
1646+
BOOST_CHECK_EQUAL(SpanToStr(result), "foo");
1647+
BOOST_CHECK_EQUAL(SpanToStr(sp), ",bar");
1648+
1649+
input = "(aaaaa,bbbbb()),c";
1650+
sp = MakeSpan(input);
1651+
result = Expr(sp);
1652+
BOOST_CHECK_EQUAL(SpanToStr(result), "(aaaaa,bbbbb())");
1653+
BOOST_CHECK_EQUAL(SpanToStr(sp), ",c");
1654+
1655+
input = "xyz)foo";
1656+
sp = MakeSpan(input);
1657+
result = Expr(sp);
1658+
BOOST_CHECK_EQUAL(SpanToStr(result), "xyz");
1659+
BOOST_CHECK_EQUAL(SpanToStr(sp), ")foo");
1660+
1661+
input = "((a),(b),(c)),xxx";
1662+
sp = MakeSpan(input);
1663+
result = Expr(sp);
1664+
BOOST_CHECK_EQUAL(SpanToStr(result), "((a),(b),(c))");
1665+
BOOST_CHECK_EQUAL(SpanToStr(sp), ",xxx");
1666+
1667+
// Split(...): split a string on every instance of sep, return vector
1668+
std::vector<Span<const char>> results;
1669+
1670+
input = "xxx";
1671+
results = Split(MakeSpan(input), 'x');
1672+
BOOST_CHECK_EQUAL(results.size(), 4);
1673+
BOOST_CHECK_EQUAL(SpanToStr(results[0]), "");
1674+
BOOST_CHECK_EQUAL(SpanToStr(results[1]), "");
1675+
BOOST_CHECK_EQUAL(SpanToStr(results[2]), "");
1676+
BOOST_CHECK_EQUAL(SpanToStr(results[3]), "");
1677+
1678+
input = "one#two#three";
1679+
results = Split(MakeSpan(input), '-');
1680+
BOOST_CHECK_EQUAL(results.size(), 1);
1681+
BOOST_CHECK_EQUAL(SpanToStr(results[0]), "one#two#three");
1682+
1683+
input = "one#two#three";
1684+
results = Split(MakeSpan(input), '#');
1685+
BOOST_CHECK_EQUAL(results.size(), 3);
1686+
BOOST_CHECK_EQUAL(SpanToStr(results[0]), "one");
1687+
BOOST_CHECK_EQUAL(SpanToStr(results[1]), "two");
1688+
BOOST_CHECK_EQUAL(SpanToStr(results[2]), "three");
1689+
1690+
input = "*foo*bar*";
1691+
results = Split(MakeSpan(input), '*');
1692+
BOOST_CHECK_EQUAL(results.size(), 4);
1693+
BOOST_CHECK_EQUAL(SpanToStr(results[0]), "");
1694+
BOOST_CHECK_EQUAL(SpanToStr(results[1]), "foo");
1695+
BOOST_CHECK_EQUAL(SpanToStr(results[2]), "bar");
1696+
BOOST_CHECK_EQUAL(SpanToStr(results[3]), "");
1697+
}
1698+
15751699
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)