PYTHON   27

testAgent.py

Guest on 6th June 2021 05:19:37 PM

  1. #! /usr/bin/env python
  2.  
  3. import string
  4. import sys
  5.  
  6. import unittest
  7. from bot import Message
  8. from chad import Chad
  9.  
  10. class Channel:
  11.     replies = []
  12.  
  13.     def reset(self):
  14.       self.replies = []
  15.  
  16.     def action(self, re, text):
  17.       self.replies.append(text)
  18.  
  19.     def privmsg(self, re, text):
  20.       self.replies.append(text)
  21.  
  22.     def reply(self):
  23.       return "\n".join(self.replies)
  24.  
  25.  
  26. class ChadMessage(Message):
  27.     chad = {}
  28.     line = ""
  29.     nick = 'punt'
  30.     mynick = 'chad'
  31.     c = {}
  32.     reply = ""
  33.  
  34.     def __init__(self):
  35.         self.chad = Chad()
  36.         self.c = Channel()
  37.  
  38.     def leave(self, text="goodbye"):
  39.       self.c.action("", "LEAVE:"+text)
  40.  
  41.     def quit(self, text="goodbye"):
  42.       self.c.action("", "QUIT:"+text)
  43.  
  44.     def command(self, line):
  45.       self.c.reset()
  46.       self.line = line
  47.       self.chad.on_message(self)
  48.       return self.c.reply()
  49.  
  50.  
  51. class PollTest(unittest.TestCase):
  52.  
  53.     def testPing(self):
  54.       c = ChadMessage()
  55.       self.assertEqual(c.command('ping'), 'pong')
  56.       self.assertEqual(c.command('    ping    '), 'pong')
  57.       self.assertEqual(c.command('        ping    '), 'pong')
  58.       self.assertEqual(c.command('pingping'), 'pongpong')
  59.       self.assertEqual(c.command('pinging'), 'ponging')
  60.       self.assertEqual(c.command('pinging'), 'ponging')
  61.       self.assertEqual(c.command('pings'), 'pongs')
  62.       self.assertEqual(c.command('pingu'), 'pongu')
  63.       self.assertEqual(c.command('ping me when you get home'), '')
  64.       self.assertEqual(c.command('chad, ping'), 'pong')
  65.       self.assertEqual(c.command('chad,     ping    '), 'pong')
  66.       self.assertEqual(c.command('        ping    '), 'pong')
  67.       self.assertEqual(c.command('chad, pingping'), 'pongpong')
  68.       self.assertEqual(c.command('chad, pinging'), 'ponging')
  69.       self.assertEqual(c.command('chad, pinging'), 'ponging')
  70.       self.assertEqual(c.command('chad, pings'), 'pongs')
  71.       self.assertEqual(c.command('chad, pingu'), 'pongu')
  72.  
  73.     def testHelp(self):
  74.       c = ChadMessage()
  75.       t = "hi, punt! i'm chad, see: http://whatfettle.com/2005/03/chad"
  76.       self.assertEqual(c.command('chad, hello'), t)
  77.       self.assertEqual(c.command('chad, say hello'), t)
  78.       self.assertEqual(c.command('chad, say hi'), t)
  79.       self.assertEqual(c.command('chad, hi'), t)
  80.       self.assertEqual(c.command('chad, help'), t)
  81.       self.assertEqual(c.command('chad, help me'), t)
  82.       self.assertEqual(c.command('chad, intro'), t)
  83.       self.assertEqual(c.command('chad, introduce yourself'), t)
  84.  
  85.     def testIgnored(self):
  86.       c = ChadMessage()
  87.       self.assertEqual(c.command('new'), '')
  88.       self.assertEqual(c.command('new poll'), '')
  89.       self.assertEqual(c.command('reset'), '')
  90.       self.assertEqual(c.command('init'), '')
  91.       self.assertEqual(c.command('question?'), '')
  92.  
  93.     def testReset(self):
  94.       c = ChadMessage()
  95.       self.assertEqual(c.command('chad, new'), 'new poll')
  96.       self.assertEqual(c.command('chad,    new    '), 'new poll')
  97.       self.assertEqual(c.command('chad, new poll'), 'new poll')
  98.       self.assertEqual(c.command('chad, new straw poll'), 'new poll')
  99.       self.assertEqual(c.command('chad, new vote'), 'new poll')
  100.       self.assertEqual(c.command('chad, init'), 'new poll')
  101.       self.assertEqual(c.command('chad, start'), 'new poll')
  102.       self.assertEqual(c.command('chad, reset'), 'new poll')
  103.       self.assertEqual(c.command('chad, restart'), 'new poll')
  104.  
  105.     def getQuestion(self, c, q):
  106.       a = 'Question: ' + q.strip()
  107.       self.assertEqual(c.command('chad, question?'), a)
  108.       self.assertEqual(c.command('chad, what question?'), a)
  109.       self.assertEqual(c.command('chad, what\'s the question?'), a)
  110.       self.assertEqual(c.command('chad, what is the question?'), a)
  111.  
  112.     def setQuestion(self, c, q):
  113.       a = 'question: ' + q.strip()
  114.       self.assertEqual(c.command('chad, question: '+q), a)
  115.       self.getQuestion(c, q)
  116.  
  117.     def testQuestion(self):
  118.       c = ChadMessage()
  119.       self.getQuestion(c, 'unknown')
  120.       self.setQuestion(c, 'where to eat tonight?')
  121.       self.setQuestion(c, '   where to eat ?   ')
  122.       self.setQuestion(c, '   chad, what are you like?')
  123.       self.setQuestion(c, 'chad, what are you like?')
  124.       self.setQuestion(c, 'options')
  125.       self.setQuestion(c, '')
  126.       self.setQuestion(c, '11')
  127.       self.setQuestion(c, 'options for way forwar!')
  128.  
  129.     def getOption(self, c, option, description):
  130.       a = 'option ' + option.strip() + ': ' + description.strip()
  131.       self.assertEqual(c.command('chad, option ' +option+'  ?'), a)
  132.       self.assertEqual(c.command('chad, what option ' +option+'?'), a)
  133.       self.assertEqual(c.command('chad, what option ' +option+'  ?'), a)
  134.       self.assertEqual(c.command('chad, what\'s option ' +option+'?'), a)
  135.       self.assertEqual(c.command('chad, what\'s  option  ' +option+'?'), a)
  136.       self.assertEqual(c.command('chad, what is option ' +option+ '?'), a)
  137.       self.assertEqual(c.command('chad, what is option ' +option), a)
  138.  
  139.     def setOption(self, c, option, description):
  140.       a = 'option ' + option.strip() + ': ' + description.strip()
  141.       self.assertEqual(c.command('chad, option '+option+":"+description), a)
  142.       self.assertEqual(c.command('chad,  option   '+option+":"+description), a)
  143.       self.assertEqual(c.command('chad,option '+option+":"+description), a)
  144.       self.assertEqual(c.command('chad,option: '+option+":"+description), a)
  145.       self.assertEqual(c.command('chad,option:'+option+":"+description), a)
  146.       self.assertEqual(c.command('chad,option :'+option+":"+description), a)
  147.       self.assertEqual(c.command('chad option ;'+option+":"+description), a)
  148.       self.assertEqual(c.command('chad option ,'+option+":"+description), a)
  149.       self.assertEqual(c.command('chad option ,'+option+"-"+description), a)
  150.       self.assertEqual(c.command('chad option ;'+option+";"+description), a)
  151.       self.getOption(c, option, description)
  152.       self.getOption(c, option.upper(), description)
  153.       self.getOption(c, option.lower(), description)
  154.  
  155.     def testOption(self):
  156.       c = ChadMessage()
  157.       self.setOption(c, 'a', 'optionA')
  158.       self.setOption(c, 'A', 'optionA')
  159.       self.setOption(c, ' A ', 'optionA')
  160.       self.setOption(c, ' a ', 'new option A')
  161.       self.setOption(c, '1a', 'option 1a')
  162.       self.setOption(c, ' foo ', 'this is option foo')
  163.       self.setOption(c, ' bar ', 'this is option bar')
  164.       self.setOption(c, '223', 'this is option 223, innit')
  165.  
  166.     def testCaseOption(self):
  167.       c = ChadMessage()
  168.       self.setOption(c, '1AA', 'option 1AA')
  169.       self.setOption(c, '1A', 'option 1A')
  170.       self.setOption(c, '1a', 'new option 1A')
  171.       self.setOption(c, 'FuzzBall', 'option FuzzBall')
  172.       self.setOption(c, 'fuzzball', 'new option FuzzBall')
  173.  
  174.     def listOptions(self, c, a):
  175.       a = "Question: unknown\n" + a
  176.       self.assertEqual(c.command('chad, options'), a)
  177.       self.assertEqual(c.command('chad - options?'), a)
  178.       self.assertEqual(c.command('chad ; options?'), a)
  179.       self.assertEqual(c.command('chad , options?'), a)
  180.       self.assertEqual(c.command('chad options?'), a)
  181.       self.assertEqual(c.command('chad options'), a)
  182.       self.assertEqual(c.command('chad, list options'), a)
  183.       self.assertEqual(c.command('chad, what\'s the options'), a)
  184.       self.assertEqual(c.command('chad, what are the options'), a)
  185.       self.assertEqual(c.command('chad, what\'s the options ??'), a)
  186.       self.assertEqual(c.command('chad, what are the options ??'), a)
  187.       self.assertEqual(c.command('chad, what the options ??'), a)
  188.  
  189.     def testListOptions(self):
  190.       c = ChadMessage()
  191.       self.listOptions(c, 'no options set')
  192.  
  193.     def testSimpleQuestion(self):
  194.       c = ChadMessage()
  195.       self.setOption(c, '1', 'fish & chips')
  196.       self.listOptions(c, '    Option 1: fish & chips (0)')
  197.  
  198.     def testSimpleQuestion(self):
  199.       c = ChadMessage()
  200.       self.setOption(c, '1', 'fish & chips')
  201.       self.setOption(c, '3', 'skip dinner')
  202.       self.setOption(c, '2B', 'no way')
  203.       self.setOption(c, '2b', 'balti')
  204.       self.setOption(c, '2A', 'indian meal')
  205.       self.setOption(c, '2z', 'Curry')
  206.       self.listOptions(c, '    Option 1: fish & chips (0)\n    Option 2A: indian meal (0)\n    Option 2b: balti (0)\n    Option 2z: Curry (0)\n    Option 3: skip dinner (0)')
  207.  
  208.     def testDropOption(self):
  209.       c = ChadMessage()
  210.       self.assertEqual(c.command('chad drop option 3a'), "unknown option '3a', punt")
  211.       self.setOption(c, '1', 'fish & chips')
  212.       self.assertEqual(c.command('chad drop option 3a'), "unknown option '3a', punt")
  213.       self.assertEqual(c.command('chad drop option 1'), "dropped option 1")
  214.  
  215.     def testBye(self):
  216.       c = ChadMessage()
  217.       self.assertEqual(c.command('chad bye'), "LEAVE:punt said bye")
  218.       self.assertEqual(c.command('chad goodbye'), "LEAVE:punt said goodbye")
  219.       self.assertEqual(c.command('chad depart'), "LEAVE:punt said depart")
  220.       self.assertEqual(c.command('chad leave'), "LEAVE:punt said leave")
  221.       self.assertEqual(c.command('chad please leave'), "LEAVE:punt said leave")
  222.       self.assertEqual(c.command('chad please leave us'), "LEAVE:punt said leave")
  223.       self.assertEqual(c.command('chad pls leave us'), "LEAVE:punt said leave")
  224.       self.assertEqual(c.command('chad excuse us'), "LEAVE:punt said excuse")
  225.       self.assertEqual(c.command('chad part'), "LEAVE:punt said part")
  226.       self.assertEqual(c.command('chad exit'), "LEAVE:punt said exit")
  227.  
  228.     def testGarbled(self):
  229.       c = ChadMessage()
  230.       self.assertEqual(c.command('chad get stuffed'), "sorry, I don't understand you, punt")
  231.       self.assertEqual(c.command('count on it'), "")
  232.  
  233.     def testAbstains(self):
  234.       c = ChadMessage()
  235.       self.assertEqual(c.command('abstain'), "punt abstains")
  236.       self.assertEqual(c.command('abstains'), "punt abstains")
  237.       self.assertEqual(c.command('chad, abstain'), "punt abstains")
  238.       self.assertEqual(c.command('chad, abstains'), "punt abstains")
  239.       self.assertEqual(c.command('chad abstains'), "punt abstains")
  240.       self.assertEqual(c.command('chad abstain'), "punt abstains")
  241.       self.assertEqual(c.command('vote: abstain'), "punt abstains")
  242.       self.assertEqual(c.command('chad, vote: abstain'), "punt abstains")
  243.       self.assertEqual(c.command('chad, vote: abstains'), "punt abstains")
  244.  
  245.     def testListVoters(self):
  246.       c = ChadMessage()
  247.       self.assertEqual(c.command('chad votes'), "no votes cast")
  248.       self.assertEqual(c.command('chad votes?'), "no votes cast")
  249.       self.assertEqual(c.command('chad, votes?'), "no votes cast")
  250.       self.assertEqual(c.command('chad, voters?'), "no votes cast")
  251.       self.assertEqual(c.command('chad, list the voters'), "no votes cast")
  252.       self.assertEqual(c.command('chad, who has voted?'), "no votes cast")
  253.       self.assertEqual(c.command('chad list votes'), "no votes cast")
  254.  
  255.     def testListVotes3(self):
  256.       c = ChadMessage()
  257.       self.assertEqual(c.command('chad, option 1: A'), 'option 1: A')
  258.       self.assertEqual(c.command('chad, option 2: B'), 'option 2: B')
  259.       self.assertEqual(c.command('vote: 1,2'), "punt votes: 1,2")
  260.       self.assertEqual(c.command('vote: charlie  : 2,1'), "charlie votes: 2,1")
  261.       self.assertEqual(c.command('vote: roger: 2'), "roger votes: 2")
  262.       self.assertEqual(c.command('chad list votes'), "3 voters: charlie (2, 1), punt (1, 2), roger (2)")
  263.  
  264.  
  265.     def testSmiley(self):
  266.       c = ChadMessage()
  267.       self.assertEqual(c.command('chad, :-)'), "what are you like, punt?")
  268.       self.assertEqual(c.command('chad, 8-)'), "what are you like, punt?")
  269.       self.assertEqual(c.command('chad, your rock my world :-)'), "what are you like, punt?")
  270.  
  271.     def testSad(self):
  272.       c = ChadMessage()
  273.       self.assertEqual(c.command('chad, :-('), "aww, poor old punt, didums")
  274.  
  275.     def testAbstainList(self):
  276.       c = ChadMessage()
  277.       self.assertEqual(c.command('abstain'), "punt abstains")
  278.       self.assertEqual(c.command('chad, votes?'), "1 voter: punt abstained")
  279.  
  280.     def testProxyAbstainList(self):
  281.       c = ChadMessage()
  282.       self.assertEqual(c.command('vote: foo abstain'), "foo abstains")
  283.       self.assertEqual(c.command('chad, votes?'), "1 voter: foo abstained")
  284.  
  285.     def testProxyAbstainsList(self):
  286.       c = ChadMessage()
  287.       self.assertEqual(c.command('vote: foo abstain'), "foo abstains")
  288.       self.assertEqual(c.command('vote: bar abstain'), "bar abstains")
  289.       self.assertEqual(c.command('chad, votes?'), "2 voters: bar abstained, foo abstained")
  290.       self.assertEqual(c.command('vote: Bar abstain'), "Bar abstains")
  291.       self.assertEqual(c.command('vote: Bazz abstain'), "Bazz abstains")
  292.       self.assertEqual(c.command('chad, votes?'), "3 voters: Bar abstained, Bazz abstained, foo abstained")
  293.       self.assertEqual(c.command('pudding abstains'), "pudding abstains")
  294.       self.assertEqual(c.command('chad, votes?'), "4 voters: Bar abstained, Bazz abstained, foo abstained, pudding abstained")
  295.  
  296.     def testSimpleVote(self):
  297.       c = ChadMessage()
  298.       self.assertEqual(c.command('chad, option 1: A'), 'option 1: A')
  299.       self.assertEqual(c.command('vote: 1'), "punt votes: 1")
  300.       self.assertEqual(c.command('chad, count'), "Question: unknown\n    Option 1: A (1)\n1 voter: punt (1)")
  301.       self.assertEqual(c.command('vote: charlie: 1'), "charlie votes: 1")
  302.       self.assertEqual(c.command('vote: roger: 1'), "roger votes: 1")
  303.       self.assertEqual(c.command('chad, count'), "Question: unknown\n    Option 1: A (3)\n3 voters: charlie (1), punt (1), roger (1)")
  304.       self.assertEqual(c.command('chad, detail'), "sorry, punt, there are no details")
  305.       #### TBD - empty summary with no votes
  306.  
  307.     def _testSimpleVote1(self):
  308.       c = ChadMessage()
  309.       self.assertEqual(c.command('chad, option 1: A'), 'option 1: A')
  310.       self.assertEqual(c.command('chad, option 2: B'), 'option 2: B')
  311.       self.assertEqual(c.command('vote: 1'), "punt votes: 1")
  312.       self.assertEqual(c.command('vote: charlie  : 1'), "charlie votes: 1")
  313.       self.assertEqual(c.command('vote: roger: 1'), "roger votes: 1")
  314.       self.assertEqual(c.command('chad, count'), "Question: unknown\n    Option 1: A (3)\n    Option 2: B (0)\n3 voters: charlie (1), punt (1), roger (1)\nRound  1: Count of first place rankings.\n          Candidate 1 is elected.\n\nWinner is option 1 - A")
  315.  
  316.     def _testSimpleVote2(self):
  317.       c = ChadMessage()
  318.       self.assertEqual(c.command('chad, option 1: Ham'), 'option 1: Ham')
  319.       self.assertEqual(c.command('chad, option 2: Eggs'), 'option 2: Eggs')
  320.       self.assertEqual(c.command('vote: 1,2'), "punt votes: 1,2")
  321.       self.assertEqual(c.command('vote: charlie: 1,2'), "charlie votes: 1,2")
  322.       self.assertEqual(c.command('vote: roger: 2'), "roger votes: 2")
  323.       self.assertEqual(c.command('chad, count'), "Question: unknown\n    Option 1: Ham (2)\n    Option 2: Eggs (1)\n3 voters: charlie (1, 2), punt (1, 2), roger (2)\nRound  1: Count of first place rankings.\n          Candidate 1 is elected.\n\nWinner is option 1 - Ham")
  324.       self.assertEqual(c.command('chad, detail'), "Election: unknown\nMethod: British Columbia STV\nNumber of Ballots: 3\nThreshold Name: Droop Static Whole\nThreshold Value:  2.0\nDelayed Transfer of Surplus: Not Enabled.\nBatch Elimination: None\n2 candidates running for 1 seats.\n\n R|   1|   2|Exha|Surp\n--+----+----+----+----\n 1| 2.0| 1.0| 0.0| 0.0\n\nRound  1: Count of first place rankings.\n          Candidate 1 is elected.\n\nWinner is 1.\n")
  325.  
  326.     def vote(self, c, name, opts):
  327.       l = ",".join(opts)
  328.       cmd = 'vote: ' +name +":"+ l
  329.       self.assertEqual(c.command(cmd), name +" votes: "+ l)
  330.  
  331.     def testLongSimpleVote(self):
  332.       c = ChadMessage()
  333.       q = 'question: this is a longer test'
  334.       self.assertEqual(c.command('chad, ' + q), q)
  335.       for opt in ['a','b','c','d']:
  336.           o = 'option '+opt+': ' 'option '+opt
  337.           self.assertEqual(c.command('chad, '+o), o)
  338.       self.vote(c, 'charlie', ['a','b','c'])
  339.  
  340. if __name__ == "__main__":
  341.     unittest.main()

Raw Paste


Login or Register to edit or fork this paste. It's free.