Skip to content

Commit

Permalink
Merge pull request #1180 from timponce/master
Browse files Browse the repository at this point in the history
Implement Issue #1179 - Tests for Leap Year Plugin
  • Loading branch information
pnhofmann committed Jun 21, 2024
2 parents e23eadd + 22a3c44 commit 6077dc9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
31 changes: 17 additions & 14 deletions jarviscli/plugins/leap_year.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@

@plugin("leap year")
def leap_year(jarvis, s):
leap = False
from collections import namedtuple
year = int(input("Enter a year: ").strip())
if s == "":
s = jarvis.input("Enter a year: ")
try:
year = int(year)
except:
jarvis.say('Wrong input. Please make sure you just enter an integer e.g. \'2012\'.')

if (year % 400 == 0) and (year % 100 == 0) or (year % 4 ==0) and (year % 100 != 0):
leap = True
if leap:
jarvis.say(str(year) + ' is a leap year.')
year = int(s)
except ValueError:
jarvis.say(
"Wrong input. Please make sure you just enter an integer e.g. '2012'."
)
return
else:
jarvis.say(str(year) + ' is not a leap year.')


if (
(year % 400 == 0)
and (year % 100 == 0)
or (year % 4 == 0)
and (year % 100 != 0)
):
jarvis.say(f"{year} is a leap year.")
else:
jarvis.say(f"{year} is not a leap year.")
35 changes: 35 additions & 0 deletions jarviscli/tests/test_leap_year.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import unittest
from tests import PluginTest
from plugins import leap_year


class LeapYearTest(PluginTest):
def setUp(self):
self.test = self.load_plugin(leap_year.leap_year)

def test_divisible_by_400_and_100(self):
self.test.run("2000")
self.assertEqual(self.history_say().last_text(), "2000 is a leap year.")

def test_divisible_by_100_and_not_400(self):
self.test.run("1900")
self.assertEqual(self.history_say().last_text(), "1900 is not a leap year.")

def test_divisible_by_4_and_not_100(self):
self.test.run("2008")
self.assertEqual(self.history_say().last_text(), "2008 is a leap year.")

def test_not_divisible_by_4(self):
self.test.run("2017")
self.assertEqual(self.history_say().last_text(), "2017 is not a leap year.")

def test_invalid_input(self):
self.test.run("abc")
self.assertEqual(
self.history_say().last_text(),
"Wrong input. Please make sure you just enter an integer e.g. '2012'.",
)


if __name__ == "__main__":
unittest.main()

0 comments on commit 6077dc9

Please sign in to comment.