示例#1
0
 def testParseNoConfig(self):
     poor_parser = Parser({})
     self.assertRaises(
         NoSmartLinkConfFoundException,
         poor_parser.parse,
         SmartLinkParser.finder.match("[[ mad max ]]"),
     )
示例#2
0
 def setUp(self):
     self.conf = MySmartLinkConf()
     self.p = Parser(dict(
         movie=self.conf,
         m=self.conf
     ))
示例#3
0
class ParserTest(TestCase):
    def setUp(self):
        self.conf = MySmartLinkConf()
        self.p = Parser(dict(
            movie=self.conf,
            m=self.conf
        ))

    def testProcessSmartLinks(self):
        input_text = "hello1 hello2 hello3"
        self.p.parse = lambda match: "*"
        self.p.finder = re.compile(r"\w+")
        out = self.p.process_smartlinks(input_text)

        self.assertIsInstance(out, SafeString)

        self.assertEqual(
            out,
            "* * *"
        )

    def testParseNormal(self):
        self.p.parse(self._create_match(
            model="movie",
            query=" Mad Max ",
            verbose_text="    the awesome movie   "
        ))

        self.assertIsInstance(
            self.p.conf,
            MySmartLinkConf
        )

        self.assertEquals(
            self.p.obj,
            "Object: Mad Max"
        )

        self.assertEqual(
            self.p.verbose_text,
            "the awesome movie"
        )

        self.p.parse(self._create_match(
            model="movie",
            query=" Mad Max ",
        ))

        # If no verbose text is provided, the query itself is used.
        self.assertEqual(
            self.p.verbose_text,
            "Mad Max"
        )

    def testParseAmbiguous(self):
        ret = self.p.parse(
            SmartLinkParser.finder.match(
                "[[ movie->more then one | the awesome movie ]]")
        )

        self.assertEqual(
            ret,
            SmartLinkConf.ambiguous_template.render(
                Context(dict(
                    verbose_text="the awesome movie"
                ))
            )
        )

    def testParseNoModel(self):
        smartlink = "[[ photo->Mad Max ]]"

        match = SmartLinkParser.finder.match(smartlink)

        ret = self.p.parse(match)

        self.assertEqual(
            ret,
            SmartLinkConf.model_unresolved_template.render(
                Context(dict(
                    smartlink_text=smartlink,
                    verbose_text="Mad Max",
                    query="photo"
                ))
            )
        )

    def testParseNotFound(self):
        ret = self.p.parse(self._create_match(
            model="movie",
            query=" no such object ",
            verbose_text="    the awesome movie   "
        ))

        # Early bail out, the object was not found =>
        # unresolved template is rendered.
        self.assertEqual(
            ret,
            SmartLinkConf.unresolved_template.render(
                Context(dict(
                    verbose_text="the awesome movie"
                ))
            )
        )

    def testParseNoModel(self):
        self.p.parse(self._create_match(
            query=" obj ",
            verbose_text="    the awesome movie   "
        ))
        self.assertEqual(
            self.p.conf,
            self.conf
        )

    def testParseNoConfig(self):
        poor_parser = Parser({})
        self.assertRaises(
            NoSmartLinkConfFoundException,
            poor_parser.parse,
            SmartLinkParser.finder.match("[[ mad max ]]"),
        )

    def _create_match(self, model=None, query="", verbose_text=None):
        return type(
            "Match",
            (object,),
            {
                "group": lambda self, key: dict(
                    ModelName=model,
                    Query=query,
                    VerboseText=verbose_text
                )[key],
                "groupdict": lambda self: dict(
                    ModelName=model,
                    Query=query,
                    VerboseText=verbose_text
                )
            }
        )()