Add tests. Fix bugs.

This commit is contained in:
Sergey Magidovich 2015-10-19 22:37:05 +03:00
parent 2fd6a8cdf7
commit 451ddd7661
3 changed files with 136 additions and 5 deletions

View file

@ -119,7 +119,102 @@ BOOST_AUTO_TEST_CASE(OpeningHours_Locale)
std::locale::global(prev);
}
BOOST_AUTO_TEST_CASE(OpeningHours_ParseUnparse)
BOOST_AUTO_TEST_CASE(OpeningHours_TestTime)
{
using namespace osmoh;
{
BOOST_CHECK(!Time{}.HasValue());
}
{
Time time{10_min};
BOOST_CHECK(time.HasValue());
BOOST_CHECK(!time.IsHoursMinutes());
BOOST_CHECK(!time.IsTime());
BOOST_CHECK(time.IsMinutes());
BOOST_CHECK(!time.IsEvent());
BOOST_CHECK(!time.IsEventOffset());
}
{
Time time{100_min};
BOOST_CHECK(time.HasValue());
BOOST_CHECK(time.IsHoursMinutes());
BOOST_CHECK(time.IsTime());
BOOST_CHECK(!time.IsMinutes());
BOOST_CHECK(!time.IsEvent());
BOOST_CHECK(!time.IsEventOffset());
BOOST_CHECK_EQUAL(time.GetHoursCount(), 1);
BOOST_CHECK_EQUAL(time.GetMinutesCount(), 40);
}
{
Time time{};
time.SetHours(22_h);
time.SetMinutes(15_min);
BOOST_CHECK(time.HasValue());
BOOST_CHECK(time.IsHoursMinutes());
BOOST_CHECK(time.IsTime());
BOOST_CHECK(!time.IsMinutes());
BOOST_CHECK(!time.IsEvent());
BOOST_CHECK(!time.IsEventOffset());
BOOST_CHECK_EQUAL(time.GetHoursCount(), 22);
BOOST_CHECK_EQUAL(time.GetMinutesCount(), 15);
}
{
Time time{};
time.SetEvent(Time::EEvent::eSunrise);
BOOST_CHECK(time.HasValue());
BOOST_CHECK(!time.IsHoursMinutes());
BOOST_CHECK(time.IsTime());
BOOST_CHECK(!time.IsMinutes());
BOOST_CHECK(time.IsEvent());
BOOST_CHECK(!time.IsEventOffset());
}
{
Time time{};
time.SetEvent(Time::EEvent::eSunrise);
time.SetHours(22_h);
time.SetMinutes(15_min);
BOOST_CHECK(time.HasValue());
BOOST_CHECK(!time.IsHoursMinutes());
BOOST_CHECK(time.IsTime());
BOOST_CHECK(!time.IsMinutes());
BOOST_CHECK(time.IsEvent());
BOOST_CHECK(time.IsEventOffset());
}
{
Time time{10_min};
BOOST_CHECK_EQUAL((-time).GetMinutesCount(), -10);
}
{
Time t1{2_h};
Time t2{100_min};
Time t3 = t1 - t2;
BOOST_CHECK_EQUAL(t3.GetHoursCount(), 0);
BOOST_CHECK_EQUAL(t3.GetMinutesCount(), 20);
}
// TODO(mgsergio): more tests with event and get hours/minutes
}
BOOST_AUTO_TEST_CASE(OpeningHours_TestTimespan)
{
using namespace osmoh;
{
Timespan span;
span.SetStart(10_h);
BOOST_CHECK(span.IsOpen());
span.SetEnd(12_h);
BOOST_CHECK(!span.IsOpen());
BOOST_CHECK(!span.HasPeriod());
span.SetPeriod(10_min);
BOOST_CHECK(span.HasPeriod());
}
}
BOOST_AUTO_TEST_CASE(OpeningHours_TestParseUnparse)
{
{
auto const rule = "06:00";
@ -157,8 +252,36 @@ BOOST_AUTO_TEST_CASE(OpeningHours_ParseUnparse)
osmoh::TTimespans>(rule);
BOOST_CHECK_EQUAL(parsedUnparsed, rule);
}
{
auto const rule = "dusk";
auto const parsedUnparsed = ParseAndUnparse<osmoh::parsing::time_selector,
osmoh::TTimespans>(rule);
}
{
auto const rule = "dawn+";
auto const parsedUnparsed = ParseAndUnparse<osmoh::parsing::time_selector,
osmoh::TTimespans>(rule);
}
{
auto const rule = "sunrise-sunset";
auto const parsedUnparsed = ParseAndUnparse<osmoh::parsing::time_selector,
osmoh::TTimespans>(rule);
}
{
auto const rule = "(dusk-12:12)";
auto const parsedUnparsed = ParseAndUnparse<osmoh::parsing::time_selector,
osmoh::TTimespans>(rule);
}
{
auto const rule = "(dusk-12:12)+";
auto const parsedUnparsed = ParseAndUnparse<osmoh::parsing::time_selector,
osmoh::TTimespans>(rule);
}
{
auto const rule = "(dusk-12:12)-sunset";
auto const parsedUnparsed = ParseAndUnparse<osmoh::parsing::time_selector,
osmoh::TTimespans>(rule);
}
}

View file

@ -32,6 +32,11 @@
namespace osmoh
{
Time::Time(THours const hours)
{
SetHours(hours);
}
Time::Time(TMinutes const minutes)
{
SetMinutes(minutes);
@ -99,7 +104,7 @@ bool Time::IsEventOffset() const
bool Time::IsHoursMinutes() const
{
return !IsEvent() && (m_state & (eHaveHours | eHaveMinutes));
return !IsEvent() && ((m_state & eHaveHours) && (m_state & eHaveMinutes));
}
bool Time::IsMinutes() const
@ -157,7 +162,7 @@ std::ostream & operator<<(std::ostream & ost, Time::EEvent const event)
std::ostream & operator<<(std::ostream & ost, Time const & time)
{
std::ios_base::fmtflags backupFlags = ost.flags();
if (!time.IsTime() && !time.IsEvent())
if (!time.HasValue())
{
ost << "hh:mm";
return ost;

View file

@ -64,10 +64,13 @@ class Time
public:
Time() = default;
Time(Time const &) = default;
Time(THours const hours);
Time(TMinutes const minutes);
Time(THours const hours, TMinutes const minutes);
Time(EEvent const event);
Time & operator=(Time const &) = default;
THours::rep GetHoursCount() const;
TMinutes::rep GetMinutesCount() const;