Beispiel #1
0
 def test_validation(self):
     """
     the client should repeat the same coroutine if validation for the given
     input fails
     """
     client = TestingClient("test_client")
     menu = MenuSystem(
         prompt("What is your favorite color?", 
             validator=pick_one,
             options=(
             "red", "white", "blue"
         )),
         prompt("How old are you?", 
             validator=integer),
         end("Thanks & goodbye!")
     )
     
     client.answer("*120*USSD_SHORTCODE#", menu)
     client.answer("yellow", menu)
     client.answer("red", menu)
     client.answer("twenty nine", menu)
     client.answer("29", menu)
     
     self.assertEquals(client.outbox, [
         (msg("What is your favorite color?", ("red","white","blue")), False), # yellow => repeat
         (msg("What is your favorite color?", ("red","white","blue")), False), # red
         (msg("How old are you?", ()), False), # twenty nine => repeat
         (msg("How old are you?", ()), False), # 29
         (msg("Thanks & goodbye!", ()), True)
     ])
Beispiel #2
0
 def test_pick_first_unanswered(self):
     """
     pick_first_unanswered() accepts a number of prompts and when called 
     fires off the first prompt() it finds for which the session store does
     not have an entry yet.
     """
     
     pfu = pick_first_unanswered(
             prompt("What is your name?"),
             prompt("What is your age?"),
             prompt("What is your gender?"))
     
     session_store = {
         "What is your name?": "Simon",
         "What is your gender?": "Male"
     }
     
     pfu.next()
     [question_text, end_of_session] = pfu.send((MenuSystem(), session_store))
     
     self.assertEquals(question_text, "What is your age?")
     self.assertFalse(end_of_session)
     
     # advance coroutine
     pfu.next()
     validated_answer = pfu.send("29")
     self.assertEquals(session_store['What is your age?'], '29')
     
     # advance coroutine
     pfu.next()
     empty_response = pfu.send((MenuSystem(), session_store))
     # all prompts have been answered, it should yield False
     self.assertFalse(empty_response)
Beispiel #3
0
 def test_stepping_through_system(self):
     """
     testing the perfect scenario
     """
     
     client = TestingClient("test_client")
     menu = MenuSystem(
         prompt("What is your name?"),
         prompt("What is your favorite color?", 
             validator=pick_one,
             options=(
             "red", "white", "blue"
         )),
         prompt("How old are you?", 
             validator=integer),
         end("Thanks & goodbye!")
     )
     
     
     client.answer("*120*USSD_SHORTCODE#", menu)
     client.answer("Simon de Haan", menu)
     client.answer("red", menu)
     client.answer("29", menu)
     
     self.assertEquals(client.outbox, [
         (msg("What is your name?", ()), False),
         (msg("What is your favorite color?", ("red","white","blue")), False),
         (msg("How old are you?", ()), False),
         (msg("Thanks & goodbye!", ()), True)
     ])
Beispiel #4
0
 def test_appending(self):
     """
     append() should clone the menu system and append the given items
     """
     menu = self.menu.append(
         prompt("Prompt 4"),
         prompt("Prompt 5")
     )
     self.assertNotEquals(menu, self.menu)
     self.assertEquals(len(menu.stack), 5)
Beispiel #5
0
def get_menu():
    return MenuSystem(
        prompt('What is your favorite programming language?', options=(
        'java', 'c', 'python', 'ruby', 'javascript', 'php', 'other'
        ), validator=pick_one),
        prompt('What is your favorite development operating system?', options=(
            'windows', 'apple', '*nix', 'other'
        ), validator=pick_one),
        prompt('What is your favorite development environment?', options=(
            'netbeans', 'eclipse', 'vim', 'emacs', 'textmate', 'notepad'
        ), validator=pick_one),
        end('Thanks! You have completed the quiz')
    )
Beispiel #6
0
 def test_case_statements_with_no_response(self):
     """
     case() should return False if none of the callbacks return True
     """
     
     case_statement = case(
         (lambda ms, session_store: False, prompt("1")),
         (lambda ms, session_store: False, prompt("2")),
         (lambda ms, session_store: False, prompt("3")),
     )
     
     # advance
     case_statement.next()
     self.assertFalse(any(case_statement.send((MenuSystem(), {}))))
Beispiel #7
0
 def test_coroutines_returning_nothing(self):
     """
     the client should forward to the next coroutine if a given coroutine 
     does not return a message to return to the client
     """
     
     client = TestingClient("test_client")
     menu = MenuSystem(
         prompt("What is your age?", validator=integer),
         case(
             (lambda ms, session: False, prompt("This should never be displayed"))
         ),
         end("Goodbye!")
     )
     
     client.answer("*120*USSD_SHORTCODE#", menu)
     client.answer("29", menu)
     
     self.assertEquals(client.outbox, [
         ("What is your age?", False),
         ("Goodbye!", True)
     ])
Beispiel #8
0
 def test_case_statements(self):
     """
     case() accepts a list of (callback, coroutine) tuples. 
     The callback is given the instance of the menu system and the session 
     store return True or False. 
     """
     
     def test_one(menu_system, session_store):
         return "one" in session_store
     
     def test_two(menu_system, session_store):
         return "two" in session_store
     
     def test_three(menu_system, session_store):
         return "three" in session_store
     
     case_statement = case(
         (test_one, prompt("test one")),
         (test_two, prompt("test two")),
         (test_three, prompt("test three"))
     )
     
     # the first two prompts shouldn't respond with this session store
     session_store = {
         "three": "exists"
     }
     
     # advance
     case_statement.next()
     response_text, end_of_session = case_statement.send(
                                         (MenuSystem(), session_store))
     self.assertEquals(response_text, "test three")
     self.assertFalse(end_of_session)
     
     # advance
     case_statement.next()
     validated_answer = case_statement.send("ok")
     self.assertEquals(validated_answer, "ok")
Beispiel #9
0
def get_menu():
    return MenuSystem(
        prompt('Which solution is your favorite?', options=(
        'Tbl1-Edu', 
        'Tbl2-Edu', 
        'Tbl3-Commun. info', 
        'Tbl4-Mob. consc.', 
        'Tbl5-Health 1', 
        'Tbl5-Health 2', 
        'Tbl6-Sec',
        'Tbl7-Sec',
        'Tbl8-Sec',
        'Tbl8-Edu'
        ), validator=pick_one),
        end('Thanks for voting!')
    )
Beispiel #10
0
 def test_character_response(self):
     """
     prompt() with a pick_one validator should accept character input 
     that match the menu values for the menu items
     """
     p = prompt(self.question_text, validator=pick_one, 
                 options=self.question_options)
     # advance coroutine to yield statement, FIXME use coroutine decorator
     p.next()
     question, end_of_session = p.send((MenuSystem(), self.session_store))
     
     self.assertEquals(question, msg(self.question_text, 
                         self.question_options))
     self.assertFalse(end_of_session)
     
     # advance again - FIXME this manual advancing is sloppy
     p.next()
     validated_answer = p.send("blue")
     self.assertEquals(validated_answer, "blue")
     
     # check session store
     self.assertEquals(self.session_store['What is your favorite color?'], 
                         "blue")
Beispiel #11
0
 def do_question(self, *args, **kwargs):
     return prompt(*args, validator=pick_one, **kwargs)
Beispiel #12
0
    return not all_questions_answered(menu_system, session)

def print_storage(ms, session):
    print table('session', session.items())

def check_question(ms, session):
    print session.keys()
    answer = session.get('Can traditional medicine cure HIV/AIDS?\n1: yes\n2: no', None)
    return answer == [(1, 'yes')]


ms = MenuSystem(
    prompt('Thnx 4 taking the Quiz! Answer 3 questions and see how much you know. '
            'Pick your language:', options=(
        'English',
        'Zulu',
        'Afrikaans',
        'Sotho',
    ), validator=pick_one),
    prompt(_('You will be asked to answer 3 questions regarding HIV. '
        'Answer them correctly and stand a chance to win airtime! Press 1 to continue.'
    )),
    pick_first_unanswered(
        prompt(_('Can traditional medicine cure HIV/AIDS?'), **yes_or_no),
        prompt(_('Is an HIV test at any government clinic free of charge?'), **yes_or_no),
        prompt(_('Is it possible to test HIV-negative for up to 3-months after becoming HIV-infected?'), **yes_or_no),
    ),
    # do(print_storage),    
    case(
            (check_question, prompt('Correct! Press 1 to continue.'))
    ),
Beispiel #13
0
 def setUp(self):
     self.menu = MenuSystem(
         prompt("Prompt 1"),
         prompt("Prompt 2"),
         prompt("Prompt 3")
     )