def test_default(self):
        new_words = ['berzuo', 'ozureb', 'eruzob', 'bourze']
        for idx, word in enumerate(new_words):
            self.assertEqual(get_anagrams(word), list())

        add_new_words(new_words)
        for idx, word in enumerate(new_words):
            with self.subTest(i=idx):
                expected = new_words[:]
                del expected[idx]
                self.assertEqual(get_anagrams(word), expected)
Exemple #2
0
    def test_all_new(self):
        words_testable = {
            'blitzkrig': [],
            'blitzsdxsskrig': [],
            'werawqpmaezig': []
        }

        for word, resultant in words_testable.items():
            got = get_anagrams(word)
            with self.subTest(word=word,
                              resultant=resultant,
                              type='Mismatch in contents'):
                self.assertEqual(sorted(got), sorted(resultant))

            with self.subTest(word=word,
                              resultant=resultant,
                              type='Mismatch in length'):
                self.assertEqual(len(got), len(resultant))
Exemple #3
0
    def test_all_present(self):
        words_testable = {
            'loop': ['polo', 'pool'],
            'loops': ['polos', 'sloop', 'spool'],
            'aeprs': ['asper', 'spear', 'prase', 'parse', 'spare', 'spaer']
        }

        for word, resultant in words_testable.items():
            got = get_anagrams(word)
            with self.subTest(word=word,
                              resultant=resultant,
                              type='Mismatch in contents'):
                self.assertEqual(sorted(got), sorted(resultant))

            with self.subTest(word=word,
                              resultant=resultant,
                              type='Mismatch in length'):
                self.assertEqual(len(got), len(resultant))
 def test_exception(self):
     with self.assertRaises(Exception) as handler:
         with patch('CharacterMap') as testMock:
             testMock.side_effect = Mock(side_effect=Exception())
             testMock()
             get_anagrams('listen', 1)
 def test_fetch_empty_anagram(self):
     expected = []
     got = sorted(get_anagrams('blitzkrig'))
     self.assertEqual(expected, got)
 def test_fetch_anagram_aeprs_length(self):
     got = sorted(get_anagrams('aeprs'))
     self.assertTrue(len(got) == 6)
 def test_fetch_anagram_aeprs(self):
     expected = sorted(['asper', 'spear', 'prase', 'parse', 'spare', 'spaer'])
     got = sorted(get_anagrams('aeprs'))
     self.assertTrue(expected == got)
 def test_fetch_anagrams_loops_with_limit(self):
     expected = ['polos', 'sloop', 'spool']
     got = sorted(get_anagrams('loops', 2))
     for anagram in got:
         self.assertTrue(anagram in expected)
 def test_fetch_anagrams_loops(self):
     expected = ['polos', 'sloop', 'spool']
     got = sorted(get_anagrams('loops'))
     self.assertEqual(expected, got)
 def test_fetch_anagrams_loop_with_limit(self):
     expected = ['polo', 'pool']
     got = get_anagrams('loop', 1)
     for anagram in got:
         self.assertTrue(anagram in expected)
Exemple #11
0
def main(stdscr):
    if len(sys.argv) != 2:
        sys.exit("\n\nHi! Thanks for trying my game.\n"
                 "This is a word guessing game, where you're given a "
                 "list of letters you must use to make words, as well as "
                 "one letter you must use in every word. You can change "
                 "it up by choosing specific letters or changing the "
                 "minimum word length.\n\n"
                 "In order to play it, you need a dictionary file in "
                 "json format. Run as follows:\n"
                 "  'python3 {} <json dictionary>'\n\n".format(sys.argv[0]))

    # Make sure terminal is big enough (curses is fussy)
    term_size = os.get_terminal_size()
    if term_size[0] < 42 or term_size[1] < 24:
        sys.exit(
            "Error: I'm feeling a little claustrophobic...\n"
            "Curses needs a bigger terminal window to display properly.\n")

    filename = sys.argv[1]
    try:
        with open(filename) as f:
            jd = set(json.loads(f.read()))
            if not jd:
                sys.exit("Error: '{}' is not a valid dictionary."\
                            .format(filename))
    except IOError:
        sys.exit("Error: could not open (or perhaps find) file '{}'."\
                    .format(filename))

    # Set up window lookin' nice (curses)
    stdscr.clear()
    stdscr.border(0)
    curses.curs_set(0)  # Make cursor invisible
    tmp = "Adam's Word Game"
    stdscr.addstr(1, curses.COLS // 2 - len(tmp) // 2, tmp)
    stdscr.hline(2, 1, curses.ACS_HLINE, curses.COLS - 2)

    # Show user settings screen
    chars, min_chars = _settings(stdscr)

    # Show something instead of hanging while getting words
    stdscr.addstr(3, 1, "Loading...")
    stdscr.refresh()
    words = get_anagrams(filename, chars)
    # Take out any words that are shorter than min chars
    words = [word for word in words if len(word) >= min_chars]

    if not words:
        sys.exit("Couldn't find any words in the dictionary with size >= {}"
                 " consisting of the letters '{}'.".format(min_chars, chars))

    # Clear screen
    fill_rect(stdscr, 3, 1, curses.LINES - 4, curses.COLS - 2, curses.ascii.SP)
    stdscr.refresh()

    # Make cursor visible again
    curses.curs_set(2)

    # Start the game
    _game(stdscr, chars, min_chars, words)