<s> import sys <EOL> import getopt <EOL> from supybot . test import * <EOL> from supybot . commands import * <EOL> import supybot . conf as conf <EOL> import supybot . irclib as irclib <EOL> import supybot . ircmsgs as ircmsgs <EOL> import supybot . utils . minisix as minisix <EOL> import supybot . callbacks as callbacks <EOL> class CommandsTestCase ( SupyTestCase ) : <EOL> def assertState ( self , spec , given , expected , target = '<STR_LIT:test>' , ** kwargs ) : <EOL> msg = ircmsgs . privmsg ( target , '<STR_LIT:foo>' ) <EOL> realIrc = getTestIrc ( ) <EOL> realIrc . nick = '<STR_LIT:test>' <EOL> realIrc . state . supported [ '<STR_LIT>' ] = '<STR_LIT:#>' <EOL> irc = callbacks . SimpleProxy ( realIrc , msg ) <EOL> myspec = Spec ( spec , ** kwargs ) <EOL> state = myspec ( irc , msg , given ) <EOL> self . assertEqual ( state . args , expected , <EOL> '<STR_LIT>' % ( expected , state . args ) ) <EOL> def assertError ( self , spec , given ) : <EOL> self . assertRaises ( callbacks . Error , <EOL> self . assertState , spec , given , given ) <EOL> def assertStateErrored ( self , spec , given , target = '<STR_LIT:test>' , errored = True , <EOL> ** kwargs ) : <EOL> msg = ircmsgs . privmsg ( target , '<STR_LIT:foo>' ) <EOL> realIrc = getTestIrc ( ) <EOL> realIrc . nick = '<STR_LIT:test>' <EOL> realIrc . state . supported [ '<STR_LIT>' ] = '<STR_LIT:#>' <EOL> irc = callbacks . SimpleProxy ( realIrc , msg ) <EOL> myspec = Spec ( spec , ** kwargs ) <EOL> state = myspec ( irc , msg , given ) <EOL> self . assertEqual ( state . errored , errored , <EOL> '<STR_LIT>' % ( errored , state . errored ) ) <EOL> class GeneralContextTestCase ( CommandsTestCase ) : <EOL> def testEmptySpec ( self ) : <EOL> self . assertState ( [ ] , [ ] , [ ] ) <EOL> def testSpecInt ( self ) : <EOL> self . assertState ( [ '<STR_LIT:int>' ] , [ '<STR_LIT:1>' ] , [ <NUM_LIT:1> ] ) <EOL> self . assertState ( [ '<STR_LIT:int>' , '<STR_LIT:int>' , '<STR_LIT:int>' ] , [ '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> self . assertError ( [ '<STR_LIT:int>' ] , [ '<STR_LIT>' ] ) <EOL> def testSpecNick ( self ) : <EOL> strict = conf . supybot . protocols . irc . strictRfc ( ) <EOL> try : <EOL> conf . supybot . protocols . irc . strictRfc . setValue ( True ) <EOL> self . assertError ( [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> conf . supybot . protocols . irc . strictRfc . setValue ( False ) <EOL> self . assertState ( [ '<STR_LIT>' ] , [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> finally : <EOL> conf . supybot . protocols . irc . strictRfc . setValue ( strict ) <EOL> if minisix . PY2 : <EOL> def testSpecLong ( self ) : <EOL> self . assertState ( [ '<STR_LIT>' ] , [ '<STR_LIT:1>' ] , [ long ( <NUM_LIT:1> ) ] ) <EOL> self . assertState ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , [ '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' ] , <EOL> [ long ( <NUM_LIT:1> ) , long ( <NUM_LIT:2> ) , long ( <NUM_LIT:3> ) ] ) <EOL> def testRestHandling ( self ) : <EOL> self . assertState ( [ rest ( None ) ] , [ '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> def testRestRequiresArgs ( self ) : <EOL> self . assertError ( [ rest ( '<STR_LIT>' ) ] , [ ] ) <EOL> def testOptional ( self ) : <EOL> spec = [ optional ( '<STR_LIT:int>' , <NUM_LIT> ) , None ] <EOL> self . assertState ( spec , [ '<STR_LIT>' , '<STR_LIT:foo>' ] , [ <NUM_LIT:12> , '<STR_LIT:foo>' ] ) <EOL> self . assertState ( spec , [ '<STR_LIT:foo>' ] , [ <NUM_LIT> , '<STR_LIT:foo>' ] ) <EOL> def testAdditional ( self ) : <EOL> spec = [ additional ( '<STR_LIT:int>' , <NUM_LIT> ) ] <EOL> self . assertState ( spec , [ '<STR_LIT>' ] , [ <NUM_LIT:12> ] ) <EOL> self . assertState ( spec , [ ] , [ <NUM_LIT> ] ) <EOL> self . assertError ( spec , [ '<STR_LIT:foo>' ] ) <EOL> def testReverse ( self ) : <EOL> spec = [ reverse ( '<STR_LIT>' ) , '<STR_LIT:float>' , '<STR_LIT:text>' ] <EOL> self . assertState ( spec , [ '<STR_LIT>' , '<STR_LIT:foo>' , '<STR_LIT:1>' ] , [ <NUM_LIT:1> , - <NUM_LIT:1.0> , '<STR_LIT:foo>' ] ) <EOL> def testGetopts ( self ) : <EOL> spec = [ '<STR_LIT:int>' , getopts ( { '<STR_LIT:foo>' : None , '<STR_LIT:bar>' : '<STR_LIT:int>' } ) , '<STR_LIT:int>' ] <EOL> self . assertState ( spec , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ <NUM_LIT:12> , [ ( '<STR_LIT:foo>' , '<STR_LIT>' ) , ( '<STR_LIT:bar>' , <NUM_LIT> ) ] , <NUM_LIT:15> ] ) <EOL> def testGetoptsShort ( self ) : <EOL> spec = [ '<STR_LIT:int>' , getopts ( { '<STR_LIT:foo>' : None , '<STR_LIT:bar>' : '<STR_LIT:int>' } ) , '<STR_LIT:int>' ] <EOL> self . assertState ( spec , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ <NUM_LIT:12> , [ ( '<STR_LIT:foo>' , '<STR_LIT>' ) , ( '<STR_LIT:bar>' , <NUM_LIT> ) ] , <NUM_LIT:15> ] ) <EOL> def testGetoptsConflict ( self ) : <EOL> spec = [ '<STR_LIT:int>' , getopts ( { '<STR_LIT:foo>' : None , '<STR_LIT>' : '<STR_LIT:int>' } ) , '<STR_LIT:int>' ] <EOL> self . assertRaises ( getopt . GetoptError , self . assertStateErrored , <EOL> spec , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def testAny ( self ) : <EOL> self . assertState ( [ any ( '<STR_LIT:int>' ) ] , [ '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' ] , [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] ) <EOL> self . assertState ( [ None , any ( '<STR_LIT:int>' ) ] , [ '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' ] , [ '<STR_LIT:1>' , [ <NUM_LIT:2> , <NUM_LIT:3> ] ] ) <EOL> self . assertState ( [ any ( '<STR_LIT:int>' ) ] , [ ] , [ [ ] ] ) <EOL> self . assertState ( [ any ( '<STR_LIT:int>' , continueOnError = True ) , '<STR_LIT:text>' ] , <EOL> [ '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:test>' ] , [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , '<STR_LIT:test>' ] ) <EOL> def testMany ( self ) : <EOL> spec = [ many ( '<STR_LIT:int>' ) ] <EOL> self . assertState ( spec , [ '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' ] , [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] ) <EOL> self . assertError ( spec , [ ] ) <EOL> def testChannelRespectsNetwork ( self ) : <EOL> spec = [ '<STR_LIT>' , '<STR_LIT:text>' ] <EOL> self . assertState ( spec , [ '<STR_LIT>' , '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertState ( spec , [ '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' ] , target = '<STR_LIT>' ) <EOL> def testGlob ( self ) : <EOL> spec = [ '<STR_LIT>' ] <EOL> self . assertState ( spec , [ '<STR_LIT:foo>' ] , [ '<STR_LIT>' ] ) <EOL> self . assertState ( spec , [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> self . assertState ( spec , [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> def testGetId ( self ) : <EOL> spec = [ '<STR_LIT:id>' ] <EOL> self . assertState ( spec , [ '<STR_LIT>' ] , [ <NUM_LIT:12> ] ) <EOL> def testCommaList ( self ) : <EOL> spec = [ commalist ( '<STR_LIT:int>' ) ] <EOL> self . assertState ( spec , [ '<STR_LIT>' ] , [ [ <NUM_LIT:12> ] ] ) <EOL> self . assertState ( spec , [ '<STR_LIT>' , '<STR_LIT>' ] , [ [ <NUM_LIT:12> , <NUM_LIT:10> ] ] ) <EOL> self . assertState ( spec , [ '<STR_LIT>' , '<STR_LIT>' ] , [ [ <NUM_LIT:12> , <NUM_LIT:11> , <NUM_LIT:10> , <NUM_LIT:9> ] ] ) <EOL> spec . append ( '<STR_LIT:int>' ) <EOL> self . assertState ( spec , [ '<STR_LIT>' , '<STR_LIT>' ] , [ [ <NUM_LIT:12> , <NUM_LIT:11> , <NUM_LIT:10> ] , <NUM_LIT:9> ] ) <EOL> def testLiteral ( self ) : <EOL> spec = [ ( '<STR_LIT>' , [ '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' ] ) ] <EOL> self . assertState ( spec , [ '<STR_LIT:foo>' ] , [ '<STR_LIT:foo>' ] ) <EOL> self . assertState ( spec , [ '<STR_LIT>' ] , [ '<STR_LIT:foo>' ] ) <EOL> self . assertState ( spec , [ '<STR_LIT:f>' ] , [ '<STR_LIT:foo>' ] ) <EOL> self . assertState ( spec , [ '<STR_LIT:bar>' ] , [ '<STR_LIT:bar>' ] ) <EOL> self . assertState ( spec , [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> self . assertError ( spec , [ '<STR_LIT>' ] ) <EOL> class ConverterTestCase ( CommandsTestCase ) : <EOL> def testUrlAllowsHttps ( self ) : <EOL> url = '<STR_LIT>' <EOL> self . assertState ( [ '<STR_LIT:url>' ] , [ url ] , [ url ] ) <EOL> self . assertState ( [ '<STR_LIT>' ] , [ url ] , [ url ] ) <EOL> def testEmail ( self ) : <EOL> email = '<STR_LIT>' <EOL> self . assertState ( [ '<STR_LIT:email>' ] , [ email ] , [ email ] ) <EOL> self . assertError ( [ '<STR_LIT:email>' ] , [ '<STR_LIT:foo>' ] ) <EOL> self . assertError ( [ '<STR_LIT:email>' ] , [ '<STR_LIT>' ] ) <EOL> self . assertError ( [ '<STR_LIT:email>' ] , [ '<STR_LIT>' ] ) <EOL> class FirstTestCase ( CommandsTestCase ) : <EOL> def testRepr ( self ) : <EOL> self . failUnless ( repr ( first ( '<STR_LIT:int>' ) ) ) <EOL> def testFirstConverterFailsAndNotErroredState ( self ) : <EOL> self . assertStateErrored ( [ first ( '<STR_LIT:int>' , '<STR_LIT>' ) ] , [ '<STR_LIT>' ] , <EOL> errored = False ) <EOL> def testLongRegexp ( self ) : <EOL> spec = [ first ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT:text>' ] <EOL> self . assertStateErrored ( spec , [ '<STR_LIT>' , '<STR_LIT:x>' * <NUM_LIT> ] , errored = False ) <EOL> class GetoptTestCase ( PluginTestCase ) : <EOL> plugins = ( '<STR_LIT>' , ) <EOL> class Foo ( callbacks . Plugin ) : <EOL> def bar ( self , irc , msg , args , optlist ) : <EOL> irc . reply ( '<STR_LIT:U+0020>' . join ( sorted ( [ '<STR_LIT>' % x for x in optlist ] ) ) ) <EOL> bar = wrap ( bar , [ getopts ( { '<STR_LIT:foo>' : '<STR_LIT:int>' , '<STR_LIT>' : '<STR_LIT:int>' } ) ] , <EOL> checkDoc = False ) <EOL> def testGetoptsExact ( self ) : <EOL> self . irc . addCallback ( self . Foo ( self . irc ) ) <EOL> self . assertResponse ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertResponse ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertResponse ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) </s>
