python - Confusion about LPTHW exercise 49 -


I am trying to write a nosetest for the following code:

  class ParserError (exception): pass class sentences (object): def __init __ (self, subject, verb, object): self.subject = topic [1] self.verb = action [1] self.object = object [1] def peek (Word_list): if word_list: word = word_list [0] word back [0] Other Words for: Return Any def match (word-list, expected): if word_list: word = word_list.pop (0) if word [0] == Expectancy: return word else: (* WORD_LIST, before leaving) = peek (WORD_LIST): And no one not leave def return (WORD_LIST, word_type): While peek (WORD_LIST) == word_type: match (WORD_LIST, word_type) def parse_verb (WORD_LIST) Before == 'function': The return match (word-list return , 'Verb') Other: Increase the parser error ("Next to a verb, next% no."% Firs if next == 'noun': return match (word-list, 'noun') if next == 'direction 'return match (return match) WORD_LIST,' direction ') and: ParserError return ( "expect a noun next.") def parse_subject (WORD_LIST, subj): action = parse_verb (WOR D_LIST) obj = parse_object (WORD_LIST) return sentences (subj, action, obj) def parse_sentence (WORD_LIST): (, WORD_LIST 'stop') begin to drop = peek (WORD_LIST) that started == 'feminine': subj = match ( WORD_LIST, 'masculine') return Parse_subject (WORD_LIST, Subj) Elif started == 'function' returns parse_subject (word_list, ( 'noun', 'player')) other: Parsar with error Increase ( "subject, object or activity Should start,% s not, "% start)  

I tried and found ParserError: expected an action, none , which parse_verb method no The Rbhit:

  nose. Equipment port IM ex49 import parser def test_parse_subject * (): WORD_LIST = [( 'action', 'hold'), ( 'feminine' things')] subj = ( 'masculine', 'Players') action = parser .parse_verb (WORD_LIST) obj = parser.parse_object (WORD_LIST) result = parser.Sentence (subj, action, obj) assert_equal (parser.parse_subject (WORD_LIST, subj), results)  

no Even thoughts? I want to do more than just a code that had failed, but I am fighting with this chapter for a total of 8 hours and my brain shot.

parse_verb action by word_list is. parse_subject tries to find an action and fail, so the exception is

I think the practice is stupid to write a test without writing a written code, which is going to code.

You can write a test like this:

< ('Player', 'hold', 'things') assert_equal (parser) . Parse_subject (word_list, subj), results) subj = ( 'masculine', 'players') result =

... but should still fail. Because sentence does not define a __ eq __ method, two examples are compared to the identities.

It seeks instead:

  def test_parse_subject (): WORD_LIST = [( 'action', 'hold'), ( 'feminine' things') ] subj = ( 'masculine', 'players') result = parser.parse_subject (WORD_LIST, subj) Assert_equal ((result.subject, result.verb, result.object), ( 'player', 'hold', 'things' ))  

Comments