開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
Head First はじめてのプログラミング ―頭とからだで覚えるPythonプログラミング入門 (Eric Freeman(著)、嶋田 健志(監修)、木下 哲也(翻訳)、株式会社オライリー・ジャパン)を12章(オブジェクト指向プログラミング - オブジェクト村への旅)の自分で考えてみよう(545ページ)の解答を求めてみる。
コード
Python 3
sample4_test.py
#!/usr/bin/env python3
from unittest import TestCase, main
from test.support import captured_stdout
from sample4 import Car, Taxi, Limo
class MyTestCase(TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_car(self):
car = Car()
with captured_stdout() as stdout:
car.start()
car.drive()
self.assertEqual(stdout.getvalue(), 'Car is moving\n')
def test_taxi(self):
taxi = Taxi()
with captured_stdout() as stdout:
taxi.start()
taxi.hire('Kim')
taxi.drive()
taxi.pay(5.0)
taxi.drive()
s = '''Hired by Kim
Honk honk, out of the way
Car is moving
Paid 5.0
Honk honk, out of the way
Car is moving
'''
self.assertEqual(stdout.getvalue(), s)
def test_limo(self):
limo = Limo()
with captured_stdout() as stdout:
limo.start()
limo.hire('Jenn')
limo.pour_drink()
limo.pay(10.0, 5.0)
s = '''Hired by Jenn
Pouring drink
Paid 10.0 Tip 5.0
Paid 15.0
'''
self.assertEqual(stdout.getvalue(), s)
if __name__ == '__main__':
main()
sample4.py
#!/usr/bin/env python3
class Car:
def __init__(self):
self.speed = 0
self.running = False
def start(self):
self.running = True
def drive(self):
if self.running:
print('Car is moving')
else:
print('Start the car first')
class Taxi(Car):
def __init__(self):
super().__init__()
self.passenger = None
self.balance = 0.0
def drive(self):
print('Honk honk, out of the way')
super().drive()
def hire(self, passenger):
print(f'Hired by {passenger}')
self.passenger = passenger
def pay(self, amount):
print(f'Paid {amount}')
self.balance += amount
self.pasenger = None
class Limo(Taxi):
def drive(self):
print('Limo driving in luxury')
Car.drive(self)
def pay(self, amount, big_tip):
print(f'Paid {amount} Tip {big_tip}')
super().pay(amount + big_tip)
def pour_drink(self):
print('Pouring drink')
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))
$ ./sample4_test.py
E
======================================================================
ERROR: test_car (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample4_test.py", line 14, in test_car
car = Car()
NameError: name 'Car' is not defined
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
$ ./sample4_test.py
F
======================================================================
FAIL: test_car (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample4_test.py", line 19, in test_car
self.assertEqual(stdout.getvalue(), 'Car is moving')
AssertionError: 'Car is moving\n' != 'Car is moving'
- Car is moving
? -
+ Car is moving
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
$ ./sample4_test.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
$ ./sample4_test.py
.E
======================================================================
ERROR: test_taxi (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample4_test.py", line 22, in test_taxi
taxi = Taxi()
NameError: name 'Taxi' is not defined
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (errors=1)
$ ./sample4_test.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
$ ./sample4_test.py
.E.
======================================================================
ERROR: test_limo (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample4_test.py", line 39, in test_limo
limo = Limo()
NameError: name 'Limo' is not defined
----------------------------------------------------------------------
Ran 3 tests in 0.001s
FAILED (errors=1)
$ ./sample4_test.py
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
$
0 コメント:
コメントを投稿