def test_parse_dict(self): self.assertEqual(Parser.parse("{}"), {}) self.assertEqual(Parser.parse("{' ': 123,}"), {" ": 123}) self.assertEqual( Parser.parse( """ { 'abc': -123, '' : 'xyz' } """ ), {"abc": -123, "": "xyz"}, )
def test_parse_selector(self): html = """ <html> <body> <div id='caption'> abc </div> <div id='catalog'> <div class='app'> <a href='http://www.mozilla.jp/firefox/'>FireFox</a> <ul class='os'> <li>Windows</li> <li>Mac</li> <li>Linux</li> </ul> </div> <div class='app'> <a href='http://www.apple.com/safari/'>Safari</a> <ul class='os'> <li>Mac</li> </ul> </div> </div> </body> </html>""" self.assertEqual(Parser.parse("div#caption $text", html), "abc") self.assertEqual(Parser.parse("div#caption", html), "abc") # abbreviation self.assertEqual(Parser.parse("[div#catalog div.app @ a $text]", html), ["FireFox", "Safari"]) self.assertEqual( Parser.parse("[div#catalog div.app @ [ul.os li @ $text]]", html), [["Windows", "Mac", "Linux"], ["Mac"]] ) self.assertEqual( Parser.parse("[div#catalog div.app @ a $[href]]", html), ["http://www.mozilla.jp/firefox/", "http://www.apple.com/safari/"], ) self.assertEqual( Parser.parse( """ { 'caption': div#caption, 'apps' : [div#catalog div.app @ { 'name': a $text, 'link': a $[href] }] } """, html, ), { "caption": "abc", "apps": [ {"name": "FireFox", "link": "http://www.mozilla.jp/firefox/"}, {"name": "Safari", "link": "http://www.apple.com/safari/"}, ], }, ) self.assertEqual(Parser.parse(r'div[id="caption"]', html), "abc")
def test_parse_str(self): self.assertEqual(Parser.parse("'abc'"), "abc") self.assertEqual(Parser.parse('"123"'), "123") self.assertEqual(Parser.parse("''"), "") self.assertEqual(Parser.parse("'\\''"), "'") self.assertEqual(Parser.parse("'\\\\'"), "\\\\") self.assertEqual(Parser.parse("'\\\\\\''"), "\\\\'")
def test_parse_num(self): self.assertEqual(Parser.parse("123"), 123) self.assertEqual(Parser.parse("-123"), -123)
def test_parse_list(self): self.assertEqual(Parser.parse("[]"), []) self.assertEqual(Parser.parse("['abc',]"), ["abc"]) self.assertEqual(Parser.parse("['abc', 123]"), ["abc", 123])