Ticket #13682: 0001-Add-unit-tests-for-BRelativeDateTimeFormat.patch

File 0001-Add-unit-tests-for-BRelativeDateTimeFormat.patch, 6.7 KB (added by akshay, 7 years ago)
  • src/tests/kits/locale/Jamfile

    From 7a6b350b1cab588e17358bdabf12220a9d6d9207 Mon Sep 17 00:00:00 2001
    From: Akshay Agarwal <agarwal.akshay.akshay8@gmail.com>
    Date: Sat, 26 Aug 2017 10:18:10 +0530
    Subject: [PATCH] Add unit tests for BRelativeDateTimeFormat.
    
    ---
     src/tests/kits/locale/Jamfile                      |   1 +
     src/tests/kits/locale/LocaleKitTestAddon.cpp       |   2 +
     .../kits/locale/RelativeDateTimeFormatTest.cpp     | 124 +++++++++++++++++++++
     src/tests/kits/locale/RelativeDateTimeFormatTest.h |  28 +++++
     4 files changed, 155 insertions(+)
     create mode 100644 src/tests/kits/locale/RelativeDateTimeFormatTest.cpp
     create mode 100644 src/tests/kits/locale/RelativeDateTimeFormatTest.h
    
    diff --git a/src/tests/kits/locale/Jamfile b/src/tests/kits/locale/Jamfile
    index 2184393..20e1e4b 100644
    a b UnitTestLib localekittest.so :  
    5151    DurationFormatTest.cpp
    5252    LanguageTest.cpp
    5353    MessageFormatTest.cpp
     54    RelativeDateTimeFormatTest.cpp
    5455    UnicodeCharTest.cpp
    5556
    5657    : be [ TargetLibstdc++ ]
  • src/tests/kits/locale/LocaleKitTestAddon.cpp

    diff --git a/src/tests/kits/locale/LocaleKitTestAddon.cpp b/src/tests/kits/locale/LocaleKitTestAddon.cpp
    index 2d04a79..01ea7d2 100644
    a b  
    1212#include "DurationFormatTest.h"
    1313#include "LanguageTest.h"
    1414#include "MessageFormatTest.h"
     15#include "RelativeDateTimeFormatTest.h"
    1516#include "UnicodeCharTest.h"
    1617
    1718
    getTestSuite()  
    2526    DurationFormatTest::AddTests(*suite);
    2627    LanguageTest::AddTests(*suite);
    2728    MessageFormatTest::AddTests(*suite);
     29    RelativeDateTimeFormatTest::AddTests(*suite);
    2830    UnicodeCharTest::AddTests(*suite);
    2931
    3032    return suite;
  • new file src/tests/kits/locale/RelativeDateTimeFormatTest.cpp

    diff --git a/src/tests/kits/locale/RelativeDateTimeFormatTest.cpp b/src/tests/kits/locale/RelativeDateTimeFormatTest.cpp
    new file mode 100644
    index 0000000..9307b4b
    - +  
     1/*
     2 * Copyright 2017, Haiku, Inc. All Rights Reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Authors:
     6 *      Akshay Agarwal <agarwal.akshay.akshay8@gmail.com>
     7 */
     8
     9#include "RelativeDateTimeFormatTest.h"
     10
     11#include <time.h>
     12
     13#include <Locale.h>
     14#include <RelativeDateTimeFormat.h>
     15
     16#include <cppunit/TestCaller.h>
     17#include <cppunit/TestSuite.h>
     18
     19
     20const int kMonthsPerYear = 12;
     21const int kMaxDaysPerMonth = 31;
     22const int kDaysPerWeek = 7;
     23const int kHoursPerDay = 24;
     24const int kMinutesPerHour = 60;
     25const int kSecondsPerMinute = 60;
     26
     27
     28RelativeDateTimeFormatTest::RelativeDateTimeFormatTest()
     29{
     30}
     31
     32
     33RelativeDateTimeFormatTest::~RelativeDateTimeFormatTest()
     34{
     35}
     36
     37
     38void
     39RelativeDateTimeFormatTest::TestDefault()
     40{
     41    BRelativeDateTimeFormat format;
     42    BString buffer;
     43    BString expected;
     44
     45    status_t result = format.Format(buffer, time(NULL));
     46    CPPUNIT_ASSERT_EQUAL(B_OK, result);
     47    // The exact format and language used depends on the locale settings, but
     48    // we can assume that whatever they are, it should put something in the
     49    // string.
     50    CPPUNIT_ASSERT(buffer.Length() > 0);
     51}
     52
     53
     54void
     55RelativeDateTimeFormatTest::TestFormat()
     56{
     57    struct Value {
     58        const char* language;
     59        const char* convention;
     60        time_t timeDelta;
     61        const char* relativeDate;
     62    };
     63
     64    static const Value values[] = {
     65        {"en", "en_US", 0, "in 0 seconds"},
     66        {"en", "en_US", 20, "in 20 seconds"},
     67        {"en", "en_US", -20, "20 seconds ago"},
     68        {"en", "en_US", 5 * kSecondsPerMinute, "in 5 minutes"},
     69        {"en", "en_US", -5 * kSecondsPerMinute, "5 minutes ago"},
     70        {"en", "en_US", 2 * kMinutesPerHour * kSecondsPerMinute, "in 2 hours"},
     71        {"en", "en_US", -2 * kMinutesPerHour * kSecondsPerMinute, "2 hours ago"},
     72        {"fr", "fr_FR", 2 * kMinutesPerHour * kSecondsPerMinute, "dans 2 heures"},
     73        {"fr", "fr_FR", -2 * kMinutesPerHour * kSecondsPerMinute, "il y a 2 heures"},
     74        {"en", "en_US", 5 * kHoursPerDay * kMinutesPerHour * kSecondsPerMinute,
     75            "in 5 days"},
     76        {"en", "en_US", -5 * kHoursPerDay * kMinutesPerHour * kSecondsPerMinute,
     77            "5 days ago"},
     78        {"de", "de_DE", 5 * kHoursPerDay * kMinutesPerHour * kSecondsPerMinute,
     79            "in 5 Tagen"},
     80        {"de", "de_DE", -5 * kHoursPerDay * kMinutesPerHour * kSecondsPerMinute,
     81            "vor 5 Tagen"},
     82        {"en", "en_US", 3 * kDaysPerWeek * kHoursPerDay * kMinutesPerHour
     83            * kSecondsPerMinute, "in 3 weeks"},
     84        {"en", "en_US", -3 * kDaysPerWeek * kHoursPerDay * kMinutesPerHour
     85            * kSecondsPerMinute, "3 weeks ago"},
     86        {"en", "en_US", 4 * kMaxDaysPerMonth * kHoursPerDay * kMinutesPerHour
     87            * kSecondsPerMinute, "in 4 months"},
     88        {"en", "en_US", -4 * kMaxDaysPerMonth * kHoursPerDay * kMinutesPerHour
     89            * kSecondsPerMinute, "4 months ago"},
     90        {"en", "en_US", 2 * kMonthsPerYear * kMaxDaysPerMonth * kHoursPerDay
     91            * kMinutesPerHour * kSecondsPerMinute, "in 2 years"},
     92        {"en", "en_US", -2 * kMonthsPerYear * kMaxDaysPerMonth * kHoursPerDay
     93            * kMinutesPerHour * kSecondsPerMinute, "2 years ago"},
     94        {NULL}
     95    };
     96
     97    status_t result;
     98
     99    for (int i = 0; values[i].language != NULL; i++) {
     100        NextSubTest();
     101
     102        BString output;
     103        BLanguage language(values[i].language);
     104        BFormattingConventions formatting(values[i].convention);
     105        BRelativeDateTimeFormat format(language, formatting);
     106
     107        result = format.Format(output, time(NULL) + values[i].timeDelta);
     108        CPPUNIT_ASSERT_EQUAL(B_OK, result);
     109        CPPUNIT_ASSERT_EQUAL(BString(values[i].relativeDate), output);
     110    }
     111}
     112
     113
     114/*static*/ void
     115RelativeDateTimeFormatTest::AddTests(BTestSuite& parent)
     116{
     117    CppUnit::TestSuite& suite = *new CppUnit::TestSuite("RelativeDateTimeFormatTest");
     118
     119    suite.addTest(new CppUnit::TestCaller<RelativeDateTimeFormatTest>(
     120        "RelativeDateTimeFormatTest::TestDefault", &RelativeDateTimeFormatTest::TestDefault));
     121    suite.addTest(new CppUnit::TestCaller<RelativeDateTimeFormatTest>(
     122        "RelativeDateTimeFormatTest::TestFormat", &RelativeDateTimeFormatTest::TestFormat));
     123    parent.addTest("RelativeDateTimeFormatTest", &suite);
     124}
  • new file src/tests/kits/locale/RelativeDateTimeFormatTest.h

    diff --git a/src/tests/kits/locale/RelativeDateTimeFormatTest.h b/src/tests/kits/locale/RelativeDateTimeFormatTest.h
    new file mode 100644
    index 0000000..adaa13c
    - +  
     1/*
     2 * Copyright 2017, Haiku, Inc. All Rights Reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Authors:
     6 *      Akshay Agarwal <agarwal.akshay.akshay8@gmail.com>
     7 */
     8#ifndef RELATIVE_DATE_TIME_FORMAT_TEST_H
     9#define RELATIVE_DATE_TIME_FORMAT_TEST_H
     10
     11
     12#include <TestCase.h>
     13#include <TestSuite.h>
     14
     15
     16class RelativeDateTimeFormatTest: public BTestCase {
     17public:
     18                    RelativeDateTimeFormatTest();
     19    virtual         ~RelativeDateTimeFormatTest();
     20
     21            void    TestDefault();
     22            void    TestFormat();
     23
     24    static  void    AddTests(BTestSuite& suite);
     25};
     26
     27
     28#endif