コード例 #1
0
add_daysMethod = 0

#TESTING THE DAY() METHOD
print("Testing the day() method...")
if testDate1.day() == 11:
    if testDate2.day() == 28:
        if testDate3.day() == 1:
            if testDate4.day() == 1:
                if testDate5.day() == 1:
                    assert testDate6.day() == 31
                    dayMethod = 1
                    print("DONE")

#TESTING THE MONTH() METHOD
print("Testing the month() method...")
if testDate1.month() == 1:
    if testDate2.month() == 2:
        if testDate3.month() == 3:
            if testDate4.month() == 3:
                if testDate5.month() == 1:
                    assert testDate6.month() == 12
                    monthMethod = 1
                    print("DONE")

#TESTING THE YEAR() METHOD
print("Testing the year() method...")
if testDate1.year() == 2020:
    if testDate2.year() == 2008:
        if testDate3.year() == 2004:
            if testDate4.year() == 2001:
                if testDate5.year() == 2000:
コード例 #2
0
def test_date_adt():
    #testing date_adt.py

    #2020 is a leap year!
    start = time.time()
    test = DateT(1, 1, 2020)

    #test getter
    compare("testing getter method for day", 1, test.day())
    compare("testing getter method for month", 1, test.month())
    compare("testing getter method for year", 2020, test.year())

    #test next function
    #ideally for a functions like this the number of tests to run should be equal to or greater than the number of execution paths
    #there are 6 cases
    # i) simply the next day within current month and year
    # ii) Transition into The next month where the current month has 30 days
    # iii) Transition into The next month where the current month has 31 days
    # iv) Transition into The next year
    # v) Transition into the next month when the current month is february and it a leap year
    # vi) Transition into the next month when current month is february and it is not a leap year

    compare("testing next method, it should return January 2nd 2020 and pass",
            DateT(2, 1, 2020), test.next())

    test = DateT(31, 1, 2020)
    compare(
        "test for transitioning into next month with current month having 31 days. It should return february 1st 2020",
        DateT(1, 2, 2020), test.next())

    test = DateT(30, 4, 2020)  #April 30th, 2020
    compare(
        "test for transitioning into next month with current month having 30 days. It should return May 1st 2020",
        DateT(1, 5, 2020), test.next())

    test = DateT(28, 2, 2020)
    compare(
        "test for transitioning into next month with current month being february and the year is a leap year. It should return Feb 29th 2020",
        DateT(29, 2, 2020), test.next())

    test = DateT(28, 2, 2021)
    compare(
        "test for transitioning into next month with current month being february and the year is NOT leap year. It should return March 1st 2021",
        DateT(1, 3, 2021), test.next())

    test = DateT(31, 12, 2020)
    compare(
        "test for transitioning into next year. It should return Jan 1st 2021 and pass",
        DateT(1, 1, 2021), test.next())

    #test prev method
    test = DateT(2, 1, 2020)
    compare("test for prev method, it should return January 1st 2020 and pass",
            DateT(1, 1, 2020), test.prev())

    test = DateT(1, 5, 2020)
    compare(
        "test for transitioning into previous month with current month having 31 days. It should return April 30th 2020",
        DateT(30, 4, 2020), test.prev())

    test = DateT(1, 6, 2020)
    compare(
        "test for transitioning into previous month with current month having 30 days. It should return May 31st 2020",
        DateT(31, 5, 2020), test.prev())

    test = DateT(1, 3, 2020)
    compare(
        "test for transitioning back into february and the year is a leap year. It should return Feb 29th 2020",
        DateT(29, 2, 2020), test.prev())

    test = DateT(1, 3, 2021)
    compare(
        "test for transitioning back into february and the year is NOT leap year. It should return Feb 28th 2021",
        DateT(28, 2, 2021), test.prev())

    test = DateT(1, 1, 2020)
    compare(
        "test for transitioning into previous year. It should return Dec 31st 2019 and pass",
        DateT(31, 12, 2019), test.prev())

    #test for before method
    test = DateT(1, 1, 2020)
    test2 = DateT(1, 5, 2020)
    compare("test for before method , it should return True and pass", True,
            test.before(test2))
    compare("test for before method , it should return False and pass", False,
            test2.before(test))

    #test for after method
    compare("test for after method , it should return True and pass", True,
            test2.after(test))
    compare("test for after method , it should return False and pass", False,
            test.after(test2))

    #test equals method
    test = DateT(1, 1, 2020)
    test2 = DateT(1, 1, 2020)
    test3 = DateT(1, 2, 2020)
    compare("test for equals method, it should return True and pass", True,
            test.equal(test2))
    compare("test for equals method, it should return False and pass", False,
            test.equal(test3))

    #test add_days method
    test = DateT(31, 1, 2020)
    compare("test add days method, it should return Feb 1st 2020 and pass",
            DateT(1, 2, 2020), test.add_days(1))
    compare("test add days method, it should return Feb 29, 2020",
            DateT(29, 2, 2020), test.add_days(29))

    test = DateT(31, 1, 2021)
    compare("test add days method, it should return March 1st, 2021",
            DateT(1, 3, 2021), test.add_days(29))

    test = DateT(1, 1, 2021)
    compare(
        "test add days method, add 365 days when current year is NOT leap year, it should return january 1st 2022",
        DateT(1, 1, 2022), test.add_days(365))

    test = DateT(1, 1, 2020)
    compare(
        "test add days method, add 365 days when current year IS LEAP YEAR. it should return Dec 31st, 2020",
        DateT(31, 12, 2020), test.add_days(365))

    test = DateT(1, 1, 2020)
    compare(
        "test add days method, add 366 days when current year IS LEAP YEAR. it should return Jan 1st, 2021",
        DateT(1, 1, 2021), test.add_days(366))

    #test days_between method
    test = DateT(31, 1, 2020)
    test2 = DateT(1, 3, 2020)

    compare(
        "test days_between method with March and January when current year is leap year, it should return 30 days",
        30, test2.days_between(test))

    test = DateT(31, 1, 2021)
    test2 = DateT(1, 3, 2021)
    compare(
        "test days_between method with March and January when current year is NOT leap year, it should return 29 days",
        29, test2.days_between(test))