2019年8月24日土曜日

開発環境

Head First はじめてのプログラミング ―頭とからだで覚えるPythonプログラミング入門 (Eric Freeman(著)、嶋田 健志(監修)、木下 哲也(翻訳)、株式会社オライリー・ジャパン)を12章(オブジェクト指向プログラミング - オブジェクト村への旅)のエクササイズ(557ページ)の解答を求めてみる。

コード

Python 3

#!/usr/bin/env python3
from unittest import TestCase, main
from test.support import captured_stdout


class FrisbeeDogTest(TestCase):
    def setUp(self):
        self.dog = FrisbeeDog('Name', 10, 20)
        self.frisbee = Frisbee('赤')

    def tearDown(self):
        pass

    def test_bark_none(self):
        with captured_stdout() as stdout:
            self.dog.bark()
        self.assertEqual(stdout.getvalue(),
                         "I can't bark, I have a frisbee in my mouth.\n")

    def test_bark_frisbee(self):
        self.dog.frisbee = self.frisbee
        with captured_stdout() as stdout:
            self.dog.bark()
        self.assertEqual(stdout.getvalue(),
                         'Name says "woof woof"\n')

    def test_catch(self):
        self.assertIsNone(self.dog.frisbee)
        self.dog.catch(self.frisbee)
        self.assertIsNotNone(self.dog.frisbee)

    def test_give(self):
        self.dog.catch(self.frisbee)
        self.assertIsNotNone(self.dog.frisbee)
        self.dog.give()
        self.assertIsNone(self.dog.frisbee)

    def test_str(self):
        self.assertEqual(str(self.dog), "I'm a dog named Name")
        self.dog.catch(self.frisbee)
        self.assertEqual(str(self.dog), "I'm a dog Name and I have a frisbee")

    def test_walk(self):
        with captured_stdout() as stdout:
            self.dog.walk()
        self.assertEqual(stdout.getvalue(), 'Name is walking\n')

    def test_walk_frisbee(self):
        self.dog.frisbee = self.frisbee
        with captured_stdout() as stdout:
            self.dog.walk()
        self.assertEqual(stdout.getvalue(), 'フリスビーで遊んでいるので歩けない。\n')


class Dog:
    def __init__(self, name, age, weight):
        self.name = name
        self.age = age
        self.weight = weight

    def bark(self):
        if self.weight > 29:
            print(f'{self.name} says "WOOF WOOF"')
        else:
            print(f'{self.name} says "woof woof"')

    def walk(self):
        print(f'{self.name} is walking')

    def __str__(self):
        return f"I'm a dog named {self.name}"


class Frisbee:
    def __init__(self, color):
        self.color = color

    def __str__(self):
        return f"I'm a {self.color} frisbee"


class FrisbeeDog(Dog):
    def __init__(self, name, age, weight):
        super().__init__(name, age, weight)
        self.frisbee = None

    def bark(self):
        if self.frisbee is None:
            print(f"I can't bark, I have a frisbee in my mouth.")
        else:
            super().bark()

    def catch(self, frisbee):
        self.frisbee = frisbee

    def give(self):
        self.frisbee = None

    def walk(self):
        if self.frisbee is None:
            super().walk()
        else:
            print('フリスビーで遊んでいるので歩けない。')

    def __str__(self):
        if self.frisbee is None:
            return super().__str__()
        return f"I'm a dog {self.name} and I have a frisbee"


if __name__ == '__main__':
    main()

入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))

$ ./sample6.py
......F
======================================================================
FAIL: test_walk_frisbee (__main__.FrisbeeDogTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample6.py", line 52, in test_walk_frisbee
    self.assertEqual(stdout.getvalue(), 'フリスビーで遊んでいるので歩けない。\n')
AssertionError: 'Name is walking\n' != 'フリスビーで遊んでいるので歩けない。\n'
- Name is walking
+ フリスビーで遊んでいるので歩けない。


----------------------------------------------------------------------
Ran 7 tests in 0.001s

FAILED (failures=1)
$ ./sample6.py
.......
----------------------------------------------------------------------
Ran 7 tests in 0.001s

OK
$ 

0 コメント:

コメントを投稿