>From fdd98adeed112396d70ed1f40a2dbbba772e5c51 Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin
Date: Sun, 7 Dec 2014 15:25:19 +0100
Subject: [PATCH 2/2] Update expiration dates test to follow the revised
specification.
Output the dates in any case, but only validate them in distribution test mode.
---
wx_test_expiry_dates.cpp | 100 +++++++++++++++++++++++++++++++---------------
1 files changed, 67 insertions(+), 33 deletions(-)
diff --git a/wx_test_expiry_dates.cpp b/wx_test_expiry_dates.cpp
index 227731a..06c019d 100644
--- a/wx_test_expiry_dates.cpp
+++ b/wx_test_expiry_dates.cpp
@@ -32,9 +32,57 @@
#include "wx_test_case.hpp"
#include "version.hpp"
+#include
+
#include
#include
+#include
+
+namespace
+{
+
+// Return a string containing both the JDN and a string representation of the
+// given date.
+//
+// This provides as much information as possible for the diagnostics.
+std::string dump_date(calendar_date const& date)
+{
+ std::ostringstream oss;
+ oss << date << " (" << date.str() << ")";
+ return oss.str();
+}
+
+// Return the date corresponding to the first day of the month following the
+// month of the given date.
+calendar_date get_first_next_month(calendar_date const& date)
+{
+ int year = date.year();
+ int month = date.month();
+ if(month == 12)
+ {
+ month = 1;
+ year++;
+ }
+ else
+ {
+ month++;
+ }
+
+ return calendar_date(year, month, 1);
+}
+
+} // Unnamed namespace.
+
+// A variant of LMI_ASSERT_EQUAL which provides more information about dates in
+// case of assertion failure.
+#define LMI_ASSERT_DATES_EQUAL(observed,expected) \
+ LMI_ASSERT_WITH_MSG \
+ ((observed) == (expected) \
+ ,"expected " << (dump_date(expected)) \
+ << " vs observed " << (dump_date(observed)) \
+ )
+
/// Validate dates in the 'expiry' file.
///
/// Write the begin and end dates to stdout, as JDN and as YYYYMMDD,
@@ -68,46 +116,32 @@
LMI_WX_TEST_CASE(expiry_dates)
{
+ // Check that the expiry file can be read and is in valid format.
fs::path expiry_path(global_settings::instance().data_directory() / "expiry");
fs::ifstream is(expiry_path);
- LMI_ASSERT(is);
+ LMI_ASSERT_WITH_MSG(is, "Failed to open \"expiry\" file for reading");
calendar_date begin(last_yyyy_date ());
calendar_date end (gregorian_epoch());
is >> begin >> end;
- LMI_ASSERT(is);
- LMI_ASSERT(is.eof());
-
-// This comment is no longer applicable in light of the revised specification:
- // The begin date must either be the first of month itself or a date in the
- // previous month, in which case we're interested in the end of the
- // following month and not the same one.
- int year = begin.year();
- int month = begin.month();
- int days_in_month;
-
- if(begin.day() == 1)
- {
- days_in_month = begin.days_in_month();
- }
- else
+ LMI_ASSERT_WITH_MSG(is, "Failed to read dates from \"expiry\" file");
+
+ wxLogMessage
+ ("Expiry dates: begin=%s, end=%s"
+ ,dump_date(begin)
+ ,dump_date(end)
+ );
+
+ LMI_ASSERT_WITH_MSG(is.eof(), "Unexpected extra data in \"expiry\" file");
+
+ if(is_distribution_test())
{
- if(month == 12)
- {
- month = 1;
- year++;
- }
- else
- {
- month++;
- }
-
- days_in_month = calendar_date(year, month, 1).days_in_month();
- }
+ calendar_date const first_next_month = get_first_next_month(today());
+ LMI_ASSERT_DATES_EQUAL(begin, first_next_month);
-// code below uses last day of month
-// instead, use first day of the month after next
- calendar_date const end_of_month(year, month, days_in_month);
- LMI_ASSERT_EQUAL(end, end_of_month);
+ calendar_date const
+ first_after_next_month = get_first_next_month(first_next_month);
+ LMI_ASSERT_DATES_EQUAL(end, first_after_next_month);
+ }
}
--
1.7.9