Ticket #13684: 0001-Fix-BDateFormatTest-Consider-timezone-in-test-cases.patch

File 0001-Fix-BDateFormatTest-Consider-timezone-in-test-cases.patch, 6.4 KB (added by akshay, 7 years ago)
  • src/tests/kits/locale/DateFormatTest.cpp

    From 814f2b4b7c65b7a171429defa98fe24f8f2ff471 Mon Sep 17 00:00:00 2001
    From: Akshay Agarwal <agarwal.akshay.akshay8@gmail.com>
    Date: Sat, 26 Aug 2017 15:22:09 +0530
    Subject: [PATCH] Fix BDateFormatTest: Consider timezone in test cases.
    
    * Issue: A time_t value of say '12345678' results into different timestamps
    in different timezones. So the expected output will not match the result of
    BDateFormat::Format() if the calendar's timezone is different from the
    timezone of the expected output, and the tests 'TestFormat' and
    'TestCustomFormat' will fail.
    * Fix: Add timezone information in the test cases and pass the timezone
    while calling BDateFormat::Format() in order to set the calendar's
    timezone same as that of the expected output.
    ---
     src/tests/kits/locale/DateFormatTest.cpp | 43 +++++++++++++++++++-------------
     1 file changed, 25 insertions(+), 18 deletions(-)
    
    diff --git a/src/tests/kits/locale/DateFormatTest.cpp b/src/tests/kits/locale/DateFormatTest.cpp
    index 574a3ab..856ecd4 100644
    a b  
    1010#include <FormattingConventions.h>
    1111#include <Language.h>
    1212#include <TimeFormat.h>
     13#include <TimeZone.h>
    1314
    1415#include <cppunit/TestCaller.h>
    1516#include <cppunit/TestSuite.h>
    DateFormatTest::TestCustomFormat()  
    3132    struct Test {
    3233        const char* language;
    3334        const char* formatting;
     35        const char* timeZone;
    3436        int32 fields;
    3537
    3638        BString expected;
    DateFormatTest::TestCustomFormat()  
    4143    BString buffer;
    4244
    4345    const Test tests[] = {
    44         { "en", "en_US", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE,
     46        { "en", "en_US", "GMT+1", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE,
    4547            "10:21 PM", "22:21", "10:21 PM" },
    46         { "en", "en_US",
     48        { "en", "en_US", "GMT+1",
    4749            B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE | B_DATE_ELEMENT_SECOND,
    4850            "10:21:18 PM", "22:21:18", "10:21:18 PM" },
    49         // FIXME we need a way to set the timezone for this test to work
    50         // reliably.
    51         { "en", "en_US", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE
     51        { "en", "en_US", "GMT+1", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE
    5252            | B_DATE_ELEMENT_TIMEZONE,
    5353            "10:21 PM GMT+1", "22:21 GMT+1", "10:21 PM GMT+1" },
    54         { "en", "en_US", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE
     54        { "en", "en_US", "GMT+1", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE
    5555            | B_DATE_ELEMENT_SECOND | B_DATE_ELEMENT_TIMEZONE,
    5656            "10:21:18 PM GMT+1", "22:21:18 GMT+1", "10:21:18 PM GMT+1" },
    57         { "fr", "fr_FR", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE,
     57        { "fr", "fr_FR", "GMT+1", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE,
    5858            "22:21", "22:21", "10:21 PM" },
    5959        { NULL }
    6060    };
    DateFormatTest::TestCustomFormat()  
    6565
    6666        BLanguage language(tests[i].language);
    6767        BFormattingConventions formatting(tests[i].formatting);
     68        BTimeZone timeZone(tests[i].timeZone);
    6869        status_t result;
    6970
    7071        // Test default for language/formatting
    DateFormatTest::TestCustomFormat()  
    7475                tests[i].fields);
    7576
    7677            result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
    77                 B_SHORT_TIME_FORMAT);
     78                B_SHORT_TIME_FORMAT, &timeZone);
    7879
    7980            CPPUNIT_ASSERT_EQUAL(B_OK, result);
    8081            CPPUNIT_ASSERT_EQUAL(tests[i].expected, buffer);
    DateFormatTest::TestCustomFormat()  
    8788            format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
    8889                tests[i].fields);
    8990            result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
    90                 B_SHORT_TIME_FORMAT);
     91                B_SHORT_TIME_FORMAT, &timeZone);
    9192
    9293            CPPUNIT_ASSERT_EQUAL(B_OK, result);
    9394            CPPUNIT_ASSERT_EQUAL(tests[i].force24, buffer);
    DateFormatTest::TestCustomFormat()  
    100101            format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
    101102                tests[i].fields);
    102103            result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
    103                 B_SHORT_TIME_FORMAT);
     104                B_SHORT_TIME_FORMAT, &timeZone);
    104105
    105106            CPPUNIT_ASSERT_EQUAL(B_OK, result);
    106107            CPPUNIT_ASSERT_EQUAL(tests[i].force12, buffer);
    DateFormatTest::TestFormat()  
    115116    struct Value {
    116117        const char* language;
    117118        const char* convention;
     119        const char* timeZone;
    118120        time_t time;
    119121        const char* shortDate;
    120122        const char* longDate;
    DateFormatTest::TestFormat()  
    124126    };
    125127
    126128    static const Value values[] = {
    127         {"en", "en_US", 12345, "1/1/70", "January 1, 1970",
     129        {"en", "en_US", "GMT+1", 12345, "1/1/70", "January 1, 1970",
    128130            "4:25 AM", "4:25:45 AM", "1/1/70, 4:25 AM"},
    129         {"fr", "fr_FR", 12345, "01/01/1970", "1 janvier 1970",
     131        {"fr", "fr_FR", "GMT+1", 12345, "01/01/1970", "1 janvier 1970",
    130132            "04:25", "04:25:45", "01/01/1970 04:25"},
    131         {"fr", "fr_FR", 12345678, "23/05/1970", "23 mai 1970",
     133        {"fr", "fr_FR", "GMT+1", 12345678, "23/05/1970", "23 mai 1970",
    132134            "22:21", "22:21:18", "23/05/1970 22:21"},
    133135        {NULL}
    134136    };
    DateFormatTest::TestFormat()  
    141143
    142144        BLanguage language(values[i].language);
    143145        BFormattingConventions formatting(values[i].convention);
     146        BTimeZone timeZone(values[i].timeZone);
    144147        BDateFormat dateFormat(language, formatting);
    145148        BTimeFormat timeFormat(language, formatting);
    146149        BDateTimeFormat dateTimeFormat(language, formatting);
    147150
    148         result = dateFormat.Format(output, values[i].time, B_SHORT_DATE_FORMAT);
     151        result = dateFormat.Format(output, values[i].time, B_SHORT_DATE_FORMAT,
     152            &timeZone);
    149153        CPPUNIT_ASSERT_EQUAL(B_OK, result);
    150154        CPPUNIT_ASSERT_EQUAL(BString(values[i].shortDate), output);
    151155
    152         result = dateFormat.Format(output, values[i].time, B_LONG_DATE_FORMAT);
     156        result = dateFormat.Format(output, values[i].time, B_LONG_DATE_FORMAT,
     157            &timeZone);
    153158        CPPUNIT_ASSERT_EQUAL(B_OK, result);
    154159        CPPUNIT_ASSERT_EQUAL(BString(values[i].longDate), output);
    155160
    156         result = timeFormat.Format(output, values[i].time, B_SHORT_TIME_FORMAT);
     161        result = timeFormat.Format(output, values[i].time, B_SHORT_TIME_FORMAT,
     162            &timeZone);
    157163        CPPUNIT_ASSERT_EQUAL(B_OK, result);
    158164        CPPUNIT_ASSERT_EQUAL(BString(values[i].shortTime), output);
    159165
    160         result = timeFormat.Format(output, values[i].time, B_MEDIUM_TIME_FORMAT);
     166        result = timeFormat.Format(output, values[i].time, B_MEDIUM_TIME_FORMAT,
     167            &timeZone);
    161168        CPPUNIT_ASSERT_EQUAL(B_OK, result);
    162169        CPPUNIT_ASSERT_EQUAL(BString(values[i].longTime), output);
    163170
    164171        result = dateTimeFormat.Format(output, values[i].time,
    165             B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT);
     172            B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, &timeZone);
    166173        CPPUNIT_ASSERT_EQUAL(B_OK, result);
    167174        CPPUNIT_ASSERT_EQUAL(BString(values[i].shortDateTime), output);
    168175    }