|
| 1 | +import checkpy.tests as t |
| 2 | +import checkpy.lib as lib |
| 3 | +import checkpy.assertlib as asserts |
| 4 | + |
| 5 | +from _extensions import * |
| 6 | + |
| 7 | + |
| 8 | +@t.test(0) |
| 9 | +def checks_check_leap_year(test): |
| 10 | + def testMethod(): |
| 11 | + fn = lib.getFunction("is_leap_year", test.fileName) |
| 12 | + if fn(2000) and fn(2020) and not fn(2100): |
| 13 | + return True |
| 14 | + else: |
| 15 | + return False |
| 16 | + |
| 17 | + test.test = testMethod |
| 18 | + test.description = lambda: "'is_leap_year' works correctly" |
| 19 | + |
| 20 | + |
| 21 | +@t.test(1) |
| 22 | +def checks_days_from_1800(test): |
| 23 | + def testMethod(): |
| 24 | + fn = lib.getFunction("days_from_1800", test.fileName) |
| 25 | + if fn(5, 2022) == 81204 and fn(8, 1996) == 71800: |
| 26 | + return True |
| 27 | + else: |
| 28 | + return False |
| 29 | + |
| 30 | + test.test = testMethod |
| 31 | + test.description = lambda: "'days_from_1800' works correctly" |
| 32 | + |
| 33 | + |
| 34 | +@t.test(2) |
| 35 | +def checks_days_from_1800_till_year(test): |
| 36 | + def testMethod(): |
| 37 | + fn = lib.getFunction("days_from_1800_until_year", test.fileName) |
| 38 | + if fn(2022) == 81084 and fn(1996) == 71587: |
| 39 | + return True |
| 40 | + else: |
| 41 | + return False |
| 42 | + |
| 43 | + test.test = testMethod |
| 44 | + test.description = lambda: "'days_from_1800_until_year' works correctly" |
| 45 | + |
| 46 | + |
| 47 | +@t.test(3) |
| 48 | +def checks_days_in_month(test): |
| 49 | + def testMethod(): |
| 50 | + fn = lib.getFunction("days_in_month", test.fileName) |
| 51 | + if fn(5, 2022) == 31 and fn(2, 2022) == 28 and fn(2, 2020) == 29: |
| 52 | + return True |
| 53 | + else: |
| 54 | + return False |
| 55 | + |
| 56 | + test.test = testMethod |
| 57 | + test.description = lambda: "'days_in_month' works correctly" |
| 58 | + |
| 59 | + |
| 60 | +@t.test(4) |
| 61 | +def checks_days_until_month(test): |
| 62 | + def testMethod(): |
| 63 | + fn = lib.getFunction("days_until_month", test.fileName) |
| 64 | + if ( |
| 65 | + fn(1, 1800, True) == 0 |
| 66 | + and fn(5, 1804, True) == 121 |
| 67 | + and fn(5, 1800, False) == 120 |
| 68 | + ): |
| 69 | + return True |
| 70 | + else: |
| 71 | + return False |
| 72 | + |
| 73 | + test.test = testMethod |
| 74 | + test.description = lambda: "'days_until_month' works correctly" |
| 75 | + |
| 76 | + |
| 77 | +@t.test(5) |
| 78 | +def checks_first_weekday_month(test): |
| 79 | + def testMethod(): |
| 80 | + fn = lib.getFunction("first_weekday_month", test.fileName) |
| 81 | + if fn(5, 2022) == 0 and fn(12, 2020) == 2: |
| 82 | + return True |
| 83 | + else: |
| 84 | + return False |
| 85 | + |
| 86 | + test.test = testMethod |
| 87 | + test.description = lambda: "'first_weekday_month' works correctly" |
| 88 | + |
| 89 | + |
| 90 | +@t.test(6) |
| 91 | +def check_header(test): |
| 92 | + def testMethod(): |
| 93 | + output = lib.outputOf( |
| 94 | + test.fileName, |
| 95 | + stdinArgs=[1800, 1], |
| 96 | + overwriteAttributes=[("__name__", "__main__")], |
| 97 | + ) |
| 98 | + if not asserts.contains(output.strip(), "Jan 1800"): |
| 99 | + return False |
| 100 | + output = lib.outputOf( |
| 101 | + test.fileName, |
| 102 | + stdinArgs=[2021, 11], |
| 103 | + overwriteAttributes=[("__name__", "__main__")], |
| 104 | + ) |
| 105 | + if not asserts.contains(output.strip(), "Nov 2021"): |
| 106 | + return False |
| 107 | + |
| 108 | + return True |
| 109 | + |
| 110 | + test.test = testMethod |
| 111 | + test.description = lambda: "year and month are printed in the header" |
| 112 | + |
| 113 | + |
| 114 | +@t.test(7) |
| 115 | +def check_grid(test): |
| 116 | + def testMethod(): |
| 117 | + output = lib.outputOf( |
| 118 | + test.fileName, |
| 119 | + stdinArgs=[1900, 2], |
| 120 | + overwriteAttributes=[("__name__", "__main__")], |
| 121 | + ) |
| 122 | + |
| 123 | + if not (asserts.contains(output, "Sun Mon Tue Wed Thu Fri Sat\n") or |
| 124 | + asserts.contains(output, "Zon Maa Din Woe Don Vri Zat\n")): |
| 125 | + return False, "the weekday names are missing or misspelled" |
| 126 | + |
| 127 | + output = lib.outputOf( |
| 128 | + test.fileName, |
| 129 | + stdinArgs=[1800, 1], |
| 130 | + overwriteAttributes=[("__name__", "__main__")], |
| 131 | + ) |
| 132 | + if not re.search(r"\ *1 2 3 4\ *\n", output): |
| 133 | + return False, "the first week of January 1800 is printed incorrectly" |
| 134 | + |
| 135 | + output = lib.outputOf( |
| 136 | + test.fileName, |
| 137 | + stdinArgs=[1800, 12], |
| 138 | + overwriteAttributes=[("__name__", "__main__")], |
| 139 | + ) |
| 140 | + if not re.search(r"\ *28 29 30 31", output): |
| 141 | + return False, "the last week of December 1800 is printed incorrectly" |
| 142 | + |
| 143 | + if not re.search(r"\ *28 29 30 31\ *\n", output): |
| 144 | + return False, "the last line of the grid does not end with a newline" |
| 145 | + |
| 146 | + return True |
| 147 | + |
| 148 | + test.test = testMethod |
| 149 | + test.description = lambda: "correctly prints a calendar grid" |
| 150 | + |
| 151 | + |
| 152 | +@t.passed(check_grid) |
| 153 | +@t.test(8) |
| 154 | +def checks_leap_years(test): |
| 155 | + def testMethod(): |
| 156 | + output = lib.outputOf( |
| 157 | + test.fileName, |
| 158 | + stdinArgs=[1900, 2], |
| 159 | + overwriteAttributes=[("__name__", "__main__")], |
| 160 | + ) |
| 161 | + if re.search(r"\ *25 26 27 28 29\ *\n", output): |
| 162 | + return False, "the year 1900 should not be a leap year, but is in your program" |
| 163 | + |
| 164 | + output = lib.outputOf( |
| 165 | + test.fileName, |
| 166 | + stdinArgs=[2000, 2], |
| 167 | + overwriteAttributes=[("__name__", "__main__")], |
| 168 | + ) |
| 169 | + if not re.search(r"\ *27 28 29\ *\n", output): |
| 170 | + return False, "the year 2000 should be a leap year, but isn't in your program" |
| 171 | + |
| 172 | + return True |
| 173 | + |
| 174 | + test.test = testMethod |
| 175 | + test.description = lambda: "correctly prints leap year calendars" |
0 commit comments