questions.yn: Perform string, not identity, comparison against 'y'

The `is` operator performs object identity comparison.  Changing to `==`
implements the expected behavior.

Use the mock library to add tests verifying the API of questions.yn.
This commit is contained in:
venth 2014-05-05 19:15:34 +02:00 committed by James McCoy
parent 6456a35817
commit 76599db944
3 changed files with 104 additions and 1 deletions

View File

@ -123,6 +123,10 @@ setup(
'python-dateutil <2.0,>=1.3',
'feedparser',
],
tests_require=[
'mock',
]
)

View File

@ -112,7 +112,7 @@ def yn(prompt, default=None):
else:
default = 'n'
s = expect(prompt, ['y', 'n'], default=default)
if s is 'y':
if s == 'y':
return True
else:
return False

99
test/test_yn.py Normal file
View File

@ -0,0 +1,99 @@
###
# Copyright (c) 2014, Artur Krysiak
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
from supybot import questions
from supybot.test import SupyTestCase
import mock
# so complicated construction because I want to
# gain the string 'y' instead of the character 'y'
# the reason of usage this construction is to prove
# that comparing strings by 'is' is wrong
# better solution is usage of '==' operator ;)
_yes_answer = ''.join(['', 'y'])
class TestYn(SupyTestCase):
def test_default_yes_selected(self):
questions.expect = mock.Mock(return_value=_yes_answer)
answer = questions.yn('up', default='y')
self.assertTrue(answer)
def test_default_no_selected(self):
questions.expect = mock.Mock(return_value='n')
answer = questions.yn('up', default='n')
self.assertFalse(answer)
def test_yes_selected_without_defaults(self):
questions.expect = mock.Mock(return_value=_yes_answer)
answer = questions.yn('up')
self.assertTrue(answer)
def test_no_selected_without_defaults(self):
questions.expect = mock.Mock(return_value='n')
answer = questions.yn('up')
self.assertFalse(answer)
def test_no_selected_with_default_yes(self):
questions.expect = mock.Mock(return_value='n')
answer = questions.yn('up', default='y')
self.assertFalse(answer)
def test_yes_selected_with_default_yes(self):
questions.expect = mock.Mock(return_value=_yes_answer)
answer = questions.yn('up', default='y')
self.assertTrue(answer)
def test_yes_selected_with_default_no(self):
questions.expect = mock.Mock(return_value=_yes_answer)
answer = questions.yn('up', default='n')
self.assertTrue(answer)
def test_no_selected_with_default_no(self):
questions.expect = mock.Mock(return_value='n')
answer = questions.yn('up', default='n')
self.assertFalse(answer)
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: