| Conditions | 8 |
| Total Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | # -*- coding: utf-8 -*- |
||
| 23 | def test_session(): |
||
| 24 | robot = werobot.WeRoBot(token=werobot.utils.generate_token(), |
||
| 25 | enable_session=True) |
||
| 26 | |||
| 27 | @robot.text |
||
| 28 | def first(message, session): |
||
| 29 | if 'last' in session: |
||
| 30 | return |
||
| 31 | session['last'] = message.content |
||
| 32 | return message.content |
||
| 33 | |||
| 34 | @robot.text |
||
| 35 | def second(_, session): |
||
| 36 | return session['last'] |
||
| 37 | |||
| 38 | tester = werobot.testing.WeTest(robot) |
||
| 39 | xml_1 = """ |
||
| 40 | <xml> |
||
| 41 | <ToUserName><![CDATA[toUser]]></ToUserName> |
||
| 42 | <FromUserName><![CDATA[fromUser]]></FromUserName> |
||
| 43 | <CreateTime>1348831860</CreateTime> |
||
| 44 | <MsgType><![CDATA[text]]></MsgType> |
||
| 45 | <Content><![CDATA[ss]]></Content> |
||
| 46 | <MsgId>1234567890123456</MsgId> |
||
| 47 | </xml> |
||
| 48 | """ |
||
| 49 | xml_2 = """ |
||
| 50 | <xml> |
||
| 51 | <ToUserName><![CDATA[toUser]]></ToUserName> |
||
| 52 | <FromUserName><![CDATA[fromUser]]></FromUserName> |
||
| 53 | <CreateTime>1348831860</CreateTime> |
||
| 54 | <MsgType><![CDATA[text]]></MsgType> |
||
| 55 | <Content><![CDATA[dd]]></Content> |
||
| 56 | <MsgId>1234567890123456</MsgId> |
||
| 57 | </xml> |
||
| 58 | """ |
||
| 59 | |||
| 60 | try: |
||
| 61 | os.remove(os.path.abspath("werobot_session")) |
||
| 62 | except OSError: |
||
| 63 | pass |
||
| 64 | session_storages = [ |
||
| 65 | filestorage.FileStorage(), |
||
| 66 | mongodbstorage.MongoDBStorage(pymongo.MongoClient().t.t), |
||
| 67 | redisstorage.RedisStorage(redis.Redis()), |
||
| 68 | ] |
||
| 69 | |||
| 70 | for session_storage in session_storages: |
||
| 71 | remove_session(session_storage) |
||
| 72 | robot.session_storage = session_storage |
||
| 73 | reply_1 = tester.send_xml(xml_1)._args['content'] |
||
| 74 | assert reply_1 == 'ss', (reply_1, session_storage) |
||
| 75 | reply_2 = tester.send_xml(xml_2)._args['content'] |
||
| 76 | assert reply_2 == 'ss', (reply_2, session_storage) |
||
| 77 | remove_session(session_storage) |
||
| 78 | |||
| 96 |