Ex49 Match Test

I’m having trouble with the match function. When I test it with assert_equal(), it only passes when I return the entire word_list. I thought the match function was supposed to only return the first word in the word_list?

Test below, can you help me understand what I’m missing?


from nose.tools import *

from ex49 import parser

def test_user_string():

    assert_raises(TypeError, parser.Sentence('bear', 'go', 'south'), )

def test_peek():

    assert_equal(parser.peek([('bear', 'go')]), 'bear')

def test_match():

    assert_equal(parser.match([('bear', 'go')], 'go'), None)

    assert_equal(parser.match([('bear', 'go')], 'bear'), 'bear')


FAIL: parser_tests.test_match


Traceback (most recent call last):

  File "/Users/sean_mc/mystuff/projects/env/lib/python3.6/site-packages/nose/case.py", line 198, in runTest


  File "/Users/sean_mc/mystuff/projects/ex49/tests/parser_tests.py", line 12, in test_match

    assert_equal(parser.match([('bear', 'go')], 'bear'), 'bear')

AssertionError: ('bear', 'go') != 'bear'


Ran 3 tests in 0.010s

FAILED (failures=1)

I added:


Around your code so that it formats correctly. For an answer though, go check out what match is returning. I suspect you are returning a tuple but you need to return a string or vv.

Thanks Zed! I see what I was misinterpreting now. I thought match was supposed to return a single string, but it’s meant to return a tuple - assuming the first value in that tuple matches ‘expected’. Right?

Take a sneak peak at the next exercise to confirm how I’m using it just to be sure, but I think you’re correct. It’s been a while since I’ve seen that code.

1 Like
A free service run by Zed A. Shaw for learncodethehardway.org.