コード例 #1
0
    def test_add_chained_phrases(self):
        c = Context().add_chained_phrases(("0", 0), ("1", 1), ("2", 2),
                                          ("3", 3))

        c.add_response("q", "q")

        # Walk through chain
        self.assertEqual(c.get_response("0")[0], 0)
        self.assertEqual(c.get_response("1")[0], 1)
        self.assertEqual(c.get_response("2")[0], 2)
        self.assertEqual(c.get_response("3")[0], 3)

        # Should be able to go back one step
        self.assertEqual(c.get_response("2")[0], 2)
        self.assertEqual(c.get_response("3")[0], 3)

        # Invalid input (no response) shouldn't exit the chain
        self.assertEqual(c.get_response("h")[0], NoResponse)
        self.assertEqual(c.get_response("3")[0], 3)

        # Exit chain with valid response from context.responses
        self.assertEqual(c.get_response("q")[0], "q")
        self.assertEqual(c.get_response("3")[0], NoResponse)

        # Make sure we can re-enter the chain
        self.assertEqual(c.get_response("0")[0], 0)
        self.assertEqual(c.get_response("1")[0], 1)
        self.assertEqual(c.get_response("2")[0], 2)
        self.assertEqual(c.get_response("3")[0], 3)
コード例 #2
0
    def test_add_entry_phrases_list_or_string(self):
        c1 = Context().add_entry_phrases(("a|b", 1), ("c", 2))
        c1.add_response("d", 3)

        c2 = Context().add_entry_phrases((["a", "b"], 4), (["c"], 5))
        c2.add_response("e", 6)

        r1 = Responder().add_response("x", 7).add_context(c1)
        r2 = Responder().add_response("y", 8).add_context(c2)

        self.assertEqual(r1.get_response("x")[0], 7)
        self.assertEqual(r1.get_response("d")[0], NoResponse)

        self.assertEqual(r2.get_response("y")[0], 8)
        self.assertEqual(r1.get_response("e")[0], NoResponse)

        # Verify we can enter context c1 with all 3 version of entry phrase
        self.assertEqual(r1.get_response("a")[0], 1)
        self.assertEqual(r1.get_response("d")[0], 3)
        self.assertEqual(r1.get_response("x")[0], 7)

        self.assertEqual(r1.get_response("b")[0], 1)
        self.assertEqual(r1.get_response("d")[0], 3)
        self.assertEqual(r1.get_response("x")[0], 7)

        self.assertEqual(r1.get_response("c")[0], 2)
        self.assertEqual(r1.get_response("d")[0], 3)
        self.assertEqual(r1.get_response("x")[0], 7)

        # Verify we can enter context c2 with all 3 version of entry phrase
        self.assertEqual(r2.get_response("a")[0], 4)
        self.assertEqual(r2.get_response("e")[0], 6)
        self.assertEqual(r2.get_response("y")[0], 8)

        self.assertEqual(r2.get_response("b")[0], 4)
        self.assertEqual(r2.get_response("e")[0], 6)
        self.assertEqual(r2.get_response("y")[0], 8)

        self.assertEqual(r2.get_response("c")[0], 5)
        self.assertEqual(r2.get_response("e")[0], 6)
        self.assertEqual(r2.get_response("y")[0], 8)
コード例 #3
0
    def test_add_entry_phrases(self):
        c = Context().add_entry_phrases(("a", 1), ("b", 2))

        c.add_response("c", 3)

        r = Responder().add_response("d", 4).add_context(c)

        # Verify not in context
        self.assertEqual(r.get_response("d")[0], 4)
        self.assertEqual(r.get_response("c")[0], NoResponse)
        self.assertFalse(r.context)

        # Enter context
        self.assertEqual(r.get_response("a")[0], 1)
        self.assertEqual(r.get_response("c")[0], 3)
        self.assertIs(r.context, c)

        # Exit context
        self.assertEqual(r.get_response("d")[0], 4)

        # Enter context
        self.assertEqual(r.get_response("b")[0], 2)
        self.assertEqual(r.get_response("c")[0], 3)
        self.assertIs(r.context, c)