Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
from django.test import TestCase
from trivia.check_answers import check
import itertools
# Create your tests here.
import unittest
class TestStringMethods(unittest.TestCase):
def test_caseinsensitive(self):
self.assertTrue(check("", "Optimus Prime", ["Optimus Prime"]))
self.assertTrue(check("", "OPTIMUS PRIME", ["Optimus Prime"]))
self.assertTrue(check("", "optimus prime", ["Optimus Prime"]))
def test_check_multiple_answers(self):
answers = ["baal", "mephisto", "diablo"]
for answer in answers:
self.assertTrue(check("", answer, answers))
self.assertFalse(check("", " ".join(answers), answers))
# ALL_ANYORDER should accept all values in any order
for p in itertools.permutations(answers):
self.assertTrue(check("ALL_ANYORDER", " ".join(p), answers))
# should support answers joined by AND
self.assertTrue(check("ALL_ANYORDER", " and ".join(p), answers))
self.assertTrue(check("ALL_ANYORDER", " AND ".join(p), answers))
# should support answers joined by ,
self.assertTrue(check("ALL_ANYORDER", ", ".join(p), answers))
def test_check_nonalpha(self):
# nonalpha should be treated as whitespace
self.assertFalse(check("", "abc'sdef", ["abcsdef"]))
self.assertTrue(check("", "wreck-it ralph", ["wreck it ralph"]))
# special exception for tokens ending in 's'
self.assertTrue(check("", "abc's", ["abcs"]))
self.assertTrue(check("", "abc's def", ["abcs def"]))
self.assertFalse(check("", "abc'st def", ["abcst def"]))
# nonalphs should be interchangable
self.assertTrue(check("", "Ranma 1.2", ["Ranma 1/2"]))
# special exception for 't
self.assertTrue(check("", "don't", ["dont"]))
self.assertTrue(check("", "don't speak", ["dont speak"]))
self.assertFalse(check("", "don'ta speak", ["donta speak"]))
# if EXACT, nonalphas are checked as normal
self.assertFalse(check("EXACT", "Ranmas fiancee", ["Ranma's fiancee"]))
self.assertTrue(check("EXACT", "Ranma's fiancee", ["Ranma's fiancee"]))
self.assertFalse(check("EXACT", "Ranma 1.2", ["Ranma 1/2"]))
self.assertTrue(check("EXACT", "Ranma 1/2", ["Ranma 1/2"]))
def test_check_numeric(self):
# exact match
self.assertFalse(check("EXACT", "SEVEN", ["7"]))
self.assertTrue(check("EXACT", "SEVEN", ["SEVEN"]))
# default case
self.assertTrue(check("", "SEVEN", ["seven"]))
self.assertTrue(check("", "SEVEN", ["SEVEN"]))
# match numbers to words
self.assertTrue(check("", "7", ["SEVEN"]))
self.assertTrue(check("", "SEVEN", ["7"]))
# match numbers inside phrases
self.assertTrue(check("", "7 of 9", ["SEVEN of NINE"]))
def test_check_soundex(self):
self.assertTrue(check("EXACT", "bajor", ["bajor"]))
self.assertFalse(check("EXACT", "bajar", ["bajor"]))
self.assertTrue(check("", "bajor", ["bajor"]))
self.assertTrue(check("", "bajar", ["bajor"]))
# does this help plurals?
self.assertTrue(check("", "apol", ["apple"]))
def test1(self):
self.assertTrue(check("", "f scott fitzgerald", ["F. Scott Fitzgerald"]))
def test_articles(self):
# articles (the/a) at the start of a phrase are optional
self.assertTrue(check("", "the legend of zelda", ["The Legend of Zelda"]))
self.assertTrue(check("", "a link between worlds", ["A Link Between Worlds"]))
self.assertTrue(check("", "legend of zelda", ["The Legend of Zelda"]))
self.assertTrue(check("", "link between worlds", ["A Link Between Worlds"]))
# only articles at the start of a phrase are optional
self.assertTrue(check("", "revenge of the sith", ["Revenge of the Sith"]))
self.assertFalse(check("", "revenge of sith", ["Revenge of the Sith"]))
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.