<s> import unittest , StringIO , robotparser <EOL> from test import test_support <EOL> class RobotTestCase ( unittest . TestCase ) : <EOL> def __init__ ( self , index , parser , url , good , agent ) : <EOL> unittest . TestCase . __init__ ( self ) <EOL> if good : <EOL> self . str = "<STR_LIT>" % ( index , url ) <EOL> else : <EOL> self . str = "<STR_LIT>" % ( index , url ) <EOL> self . parser = parser <EOL> self . url = url <EOL> self . good = good <EOL> self . agent = agent <EOL> def runTest ( self ) : <EOL> if isinstance ( self . url , tuple ) : <EOL> agent , url = self . url <EOL> else : <EOL> url = self . url <EOL> agent = self . agent <EOL> if self . good : <EOL> self . assertTrue ( self . parser . can_fetch ( agent , url ) ) <EOL> else : <EOL> self . assertFalse ( self . parser . can_fetch ( agent , url ) ) <EOL> def __str__ ( self ) : <EOL> return self . str <EOL> tests = unittest . TestSuite ( ) <EOL> def RobotTest ( index , robots_txt , good_urls , bad_urls , <EOL> agent = "<STR_LIT>" ) : <EOL> lines = StringIO . StringIO ( robots_txt ) . readlines ( ) <EOL> parser = robotparser . RobotFileParser ( ) <EOL> parser . parse ( lines ) <EOL> for url in good_urls : <EOL> tests . addTest ( RobotTestCase ( index , parser , url , <NUM_LIT:1> , agent ) ) <EOL> for url in bad_urls : <EOL> tests . addTest ( RobotTestCase ( index , parser , url , <NUM_LIT:0> , agent ) ) <EOL> doc = """<STR_LIT>""" <EOL> good = [ '<STR_LIT:/>' , '<STR_LIT>' ] <EOL> bad = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> RobotTest ( <NUM_LIT:1> , doc , good , bad ) <EOL> doc = """<STR_LIT>""" <EOL> good = [ '<STR_LIT:/>' , '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> bad = [ '<STR_LIT>' ] <EOL> RobotTest ( <NUM_LIT:2> , doc , good , bad ) <EOL> doc = """<STR_LIT>""" <EOL> good = [ ] <EOL> bad = [ '<STR_LIT>' , '<STR_LIT:/>' , '<STR_LIT>' ] <EOL> RobotTest ( <NUM_LIT:3> , doc , good , bad ) <EOL> doc = """<STR_LIT>""" <EOL> good = [ ] <EOL> bad = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> RobotTest ( <NUM_LIT:4> , doc , good , bad , '<STR_LIT>' ) <EOL> RobotTest ( <NUM_LIT:5> , doc , good , bad , '<STR_LIT>' ) <EOL> doc = """<STR_LIT>""" <EOL> good = [ '<STR_LIT>' , ] <EOL> bad = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , "<STR_LIT>" , <EOL> '<STR_LIT>' ] <EOL> RobotTest ( <NUM_LIT:6> , doc , good , bad ) <EOL> doc = """<STR_LIT>""" <EOL> good = [ '<STR_LIT>' ] <EOL> bad = [ ] <EOL> RobotTest ( <NUM_LIT:7> , doc , good , bad ) <EOL> doc = """<STR_LIT>""" <EOL> good = [ '<STR_LIT>' ] <EOL> bad = [ '<STR_LIT>' ] <EOL> RobotTest ( <NUM_LIT:8> , doc , good , bad , agent = "<STR_LIT>" ) <EOL> doc = """<STR_LIT>""" <EOL> good = [ ] <EOL> bad = [ '<STR_LIT>' ] <EOL> RobotTest ( <NUM_LIT:9> , doc , good , bad , agent = "<STR_LIT>" ) <EOL> good = [ ] <EOL> bad = [ '<STR_LIT>' ] <EOL> RobotTest ( <NUM_LIT:10> , doc , good , bad , agent = "<STR_LIT>" ) <EOL> doc = """<STR_LIT>""" <EOL> good = [ ] <EOL> bad = [ '<STR_LIT>' ] <EOL> RobotTest ( <NUM_LIT:11> , doc , good , bad , agent = "<STR_LIT>" ) <EOL> good = [ '<STR_LIT>' ] <EOL> bad = [ ] <EOL> RobotTest ( <NUM_LIT:12> , doc , good , bad , agent = "<STR_LIT>" ) <EOL> doc = """<STR_LIT>""" <EOL> good = [ '<STR_LIT>' ] <EOL> bad = [ '<STR_LIT>' ] <EOL> RobotTest ( <NUM_LIT> , doc , good , bad , agent = "<STR_LIT>" ) <EOL> doc = """<STR_LIT>""" <EOL> good = [ '<STR_LIT>' ] <EOL> bad = [ '<STR_LIT>' ] <EOL> RobotTest ( <NUM_LIT> , doc , good , bad ) <EOL> doc = """<STR_LIT>""" <EOL> good = [ '<STR_LIT>' ] <EOL> bad = [ '<STR_LIT>' ] <EOL> RobotTest ( <NUM_LIT:15> , doc , good , bad ) <EOL> class NetworkTestCase ( unittest . TestCase ) : <EOL> def testPasswordProtectedSite ( self ) : <EOL> test_support . requires ( '<STR_LIT>' ) <EOL> with test_support . transient_internet ( '<STR_LIT>' ) : <EOL> url = '<STR_LIT>' <EOL> parser = robotparser . RobotFileParser ( ) <EOL> parser . set_url ( url ) <EOL> try : <EOL> parser . read ( ) <EOL> except IOError : <EOL> self . skipTest ( '<STR_LIT>' % url ) <EOL> self . assertEqual ( parser . can_fetch ( "<STR_LIT:*>" , url + "<STR_LIT>" ) , False ) <EOL> def testPythonOrg ( self ) : <EOL> test_support . requires ( '<STR_LIT>' ) <EOL> with test_support . transient_internet ( '<STR_LIT>' ) : <EOL> parser = robotparser . RobotFileParser ( <EOL> "<STR_LIT>" ) <EOL> parser . read ( ) <EOL> self . assertTrue ( <EOL> parser . can_fetch ( "<STR_LIT:*>" , "<STR_LIT>" ) ) <EOL> def test_main ( ) : <EOL> test_support . run_unittest ( tests ) <EOL> test_support . run_unittest ( NetworkTestCase ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_support . verbose = <NUM_LIT:1> <EOL> test_main ( ) </s>
