開発環境
- macOS Catalina - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.8 (プログラミング言語)
Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 14(Object-Oriented Programming)、Exercise 1の解答を求めてみる。
コード
#!/usr/bin/env python3
from unittest import TestCase, main
print('1.')
class TestCountry(TestCase):
def setUp(self):
self.canada = Country('Canada', 34482779, 9984670)
self.usa = Country('United States of America', 313914040, 9826675)
def test_init(self):
self.assertEqual(self.canada.name, 'Canada')
self.assertEqual(self.canada.population, 34482779)
self.assertEqual(self.canada.area, 9984670)
def test_is_larger(self):
self.assertTrue(self.canada.is_larger(self.usa))
def test_population_density(self):
self.assertEqual(self.canada.population_density(),
3.4535722262227995)
def test_str(self):
self.assertEqual(
str(self.usa),
'United States of America has a population of 313914040 and is '
'9826675 square km.'
)
def test_repr(self):
self.assertEqual(repr(self.canada),
"Country('Canada', 34482779, 9984670)")
self.assertEqual(
repr([self.canada]), "[Country('Canada', 34482779, 9984670)]")
class Country:
def __init__(self, name: str, population: int, area: int) -> None:
self.name: str = name
self.population: int = population
self.area: int = area
def is_larger(self, other) -> bool:
return isinstance(other, Country) and self.area > other.area
def population_density(self) -> float:
return self.population / self.area
def __str__(self) -> str:
return f'{self.name} has a population of {self.population} and is '\
f'{self.area} square km.'
def __repr__(self) -> str:
return f"Country('{self.name}', {self.population}, {self.area})"
if __name__ == "__main__":
main()
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./sample1.py
1.
E
======================================================================
ERROR: test_init (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 9, in setUp
self.canada = Country('Canada', 34482779, 9984678)
NameError: name 'Country' is not defined
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
% mypy sample1.py
Success: no issues found in 1 source file
% ./sample1.py
1.
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
% ./sample1.py
1.
.E
======================================================================
ERROR: test_is_larger (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 18, in test_is_larger
self.assertTrue(self.canada.is_larger(self.usa))
AttributeError: 'Country' object has no attribute 'is_larger'
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (errors=1)
% mypy sample1.py
Success: no issues found in 1 source file
% ./sample1.py
1.
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
% ./sample1.py
1.
..E
======================================================================
ERROR: test_population_density (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 21, in test_population_density
self.assertEqual(self.canada.population_density(),
AttributeError: 'Country' object has no attribute 'population_density'
----------------------------------------------------------------------
Ran 3 tests in 0.000s
FAILED (errors=1)
% mypy sample1.py
Success: no issues found in 1 source file
% ./sample1.py
1.
..E
======================================================================
ERROR: test_population_density (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 21, in test_population_density
self.assertEqual(self.canada.population_density(),
AttributeError: 'Country' object has no attribute 'population_density'
----------------------------------------------------------------------
Ran 3 tests in 0.000s
FAILED (errors=1)
% ./sample1.py
1.
..F
======================================================================
FAIL: test_population_density (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 21, in test_population_density
self.assertEqual(self.canada.population_density(),
AssertionError: 3.453569459125272 != 3.4535722262227995
----------------------------------------------------------------------
Ran 3 tests in 0.000s
FAILED (failures=1)
% ./sample1.py
1.
..F
======================================================================
FAIL: test_population_density (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 21, in test_population_density
self.assertEqual(self.canada.population_density(),
AssertionError: 0.2895554908727049 != 3.4535722262227995
----------------------------------------------------------------------
Ran 3 tests in 0.000s
FAILED (failures=1)
% ./sample1.py
1.
..F
======================================================================
FAIL: test_population_density (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 21, in test_population_density
self.assertEqual(self.canada.population_density(),
AssertionError: 3.453569459125272 != 3.4535722262227995
----------------------------------------------------------------------
Ran 3 tests in 0.000s
FAILED (failures=1)
% ./sample1.py
1.
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
% ./sample1.py
1.
...F
======================================================================
FAIL: test_str (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 25, in test_str
self.assertEqual(
AssertionError: '<__main__.Country object at 0x10b38a280>' != 'United States of America has a population[35 chars] km.'
- <__main__.Country object at 0x10b38a280>
+ United States of America has a population of 313914040 and is 9826675 square km.
----------------------------------------------------------------------
Ran 4 tests in 0.001s
FAILED (failures=1)
% ./sample1.py
File "./sample1.py", line 46
return f'{self.name} has a population of {self.population} and is ' \
{self.area} square km.'
^
SyntaxError: invalid syntax
% ./sample1.py
File "./sample1.py", line 46
return f'{self.name} has a population of {self.population} and is ' +\
{self.area} square km.'
^
SyntaxError: invalid syntax
% ./sample1.py
1.
...F
======================================================================
FAIL: test_str (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 25, in test_str
self.assertEqual(
AssertionError: 'Unit[18 chars]ca has a population of 313914040 and is {self.area} square km.' != 'Unit[18 chars]ca has a population of 313914040 and is 9826675 square km.'
- United States of America has a population of 313914040 and is {self.area} square km.
? ^^^^^^^^^^^
+ United States of America has a population of 313914040 and is 9826675 square km.
? ^^^^^^^
----------------------------------------------------------------------
Ran 4 tests in 0.001s
FAILED (failures=1)
% ./sample1.py
1.
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s
OK
% ./sample1.py
File "./sample1.py", line 33
self.assertEqual([self.canada, ["Country('Canada', 34482779, 9984670)"])
^
SyntaxError: closing parenthesis ')' does not match opening parenthesis '['
% ./sample1.py
1.
...FF
======================================================================
FAIL: test_repr (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 32, in test_repr
self.assertEqual(self.canada, "Country('Canada', 34482779, 9984670)")
AssertionError: <__main__.Country object at 0x10db61340> != "Country('Canada', 34482779, 9984670)"
======================================================================
FAIL: test_str (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 25, in test_str
self.assertEqual(
AssertionError: 'Unit[14 chars]merica has a population of 313914040 and is ' != 'Unit[14 chars]merica has a population of 313914040 and is 9826675 square km.'
- United States of America has a population of 313914040 and is
+ United States of America has a population of 313914040 and is 9826675 square km.
? ++++++++++++++++++
----------------------------------------------------------------------
Ran 5 tests in 0.001s
FAILED (failures=2)
% ./sample1.py
1.
...F.
======================================================================
FAIL: test_repr (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 32, in test_repr
self.assertEqual(self.canada, "Country('Canada', 34482779, 9984670)")
AssertionError: <__main__.Country object at 0x106a94340> != "Country('Canada', 34482779, 9984670)"
----------------------------------------------------------------------
Ran 5 tests in 0.001s
FAILED (failures=1)
% ./sample1.py
1.
...F.
======================================================================
FAIL: test_repr (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 32, in test_repr
self.assertEqual(self.canada, "Country('Canada', 34482779, 9984670)")
AssertionError: Country('Canada', 34482779, 9984670) != "Country('Canada', 34482779, 9984670)"
----------------------------------------------------------------------
Ran 5 tests in 0.001s
FAILED (failures=1)
% ./sample1.py
1.
...F.
======================================================================
FAIL: test_repr (__main__.TestCountry)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 32, in test_repr
self.assertEqual(self.canada, Country('Canada', 34482779, 9984670))
AssertionError: Country('Canada', 34482779, 9984670) != Country('Canada', 34482779, 9984670)
----------------------------------------------------------------------
Ran 5 tests in 0.001s
FAILED (failures=1)
% ./sample1.py
1.
.....
----------------------------------------------------------------------
Ran 5 tests in 0.000s
OK
% mypy sample1.py
Success: no issues found in 1 source file
% ./sample1.py -v
1.
test_init (__main__.TestCountry) ... ok
test_is_larger (__main__.TestCountry) ... ok
test_population_density (__main__.TestCountry) ... ok
test_repr (__main__.TestCountry) ... ok
test_str (__main__.TestCountry) ... ok
----------------------------------------------------------------------
Ran 5 tests in 0.001s
OK
%
0 コメント:
コメントを投稿