2019年8月23日金曜日

開発環境

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

コード

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)

    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 = 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(Frisbee('赤'))
        self.assertIsNotNone(self.dog.frisbee)

    def test_give(self):
        self.dog.catch(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(Frisbee('赤'))
        self.assertEqual(str(self.dog), "I'm a dog Name and I have a frisbee")


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 __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 __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))

$ ./sample5.py
EEEEE
======================================================================
ERROR: test_bark_frisbee (__main__.FrisbeeDogTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample5.py", line 8, in setUp
    self.dog = FrisbeeDog('Name', 10, 20)
NameError: name 'FrisbeeDog' is not defined

======================================================================
ERROR: test_bark_none (__main__.FrisbeeDogTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample5.py", line 8, in setUp
    self.dog = FrisbeeDog('Name', 10, 20)
NameError: name 'FrisbeeDog' is not defined

======================================================================
ERROR: test_catch (__main__.FrisbeeDogTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample5.py", line 8, in setUp
    self.dog = FrisbeeDog('Name', 10, 20)
NameError: name 'FrisbeeDog' is not defined

======================================================================
ERROR: test_give (__main__.FrisbeeDogTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample5.py", line 8, in setUp
    self.dog = FrisbeeDog('Name', 10, 20)
NameError: name 'FrisbeeDog' is not defined

======================================================================
ERROR: test_str (__main__.FrisbeeDogTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample5.py", line 8, in setUp
    self.dog = FrisbeeDog('Name', 10, 20)
NameError: name 'FrisbeeDog' is not defined

----------------------------------------------------------------------
Ran 5 tests in 0.001s

FAILED (errors=5)
$ ./sample5.py
.....
----------------------------------------------------------------------
Ran 5 tests in 0.000s

OK
$ 

0 コメント:

コメントを投稿