1#! /usr/bin/env python
2"""
3This script tests some of the base functionalities of MORSE.
4"""
5
6import sys
7from morse.testing.testing import MorseTestCase
8
9# Include this import to be able to use your test file as a regular
10# builder script, ie, usable with: 'morse [run|exec] base_testing.py
11try:
12    from morse.builder import *
13except ImportError:
14    pass
15
16class BaseTest(MorseTestCase):
17
18    def setUpEnv(self):
19        """ Defines the test scenario, using the Builder API.
20        """
21
22        # Adding 4 robots
23        # no name provided, use the name of the associated variable
24        jido = Jido()
25
26        # use explicitly name provided
27        robot2 = ATRV('mana')
28
29        # setup the name using explicitly robot3.name
30        robot3 = ATRV()
31        robot3.name = 'dala'
32
33        # no name provided, use variable name, old school style
34        atrv = ATRV()
35
36        env = Environment('empty', fastmode = True)
37
38    def test_list_robots(self):
39        """ Tests the simulator can return the list of robots
40
41        This test is guaranteed to be started only when the simulator
42        is ready.
43        """
44
45        # Initialize a socket connection to the simulator
46        import socket
47        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
48        s.connect(("localhost", 4000))
49        sockf = s.makefile()
50
51        # Queries for the list of robots
52        s.send(b"id1 simulation list_robots\n")
53
54        result = sockf.readline()
55        id, success, robots = result.strip().split(' ', 2)
56        self.assertEquals(success, "SUCCESS")
57
58        import ast
59        robotsset = set(ast.literal_eval(robots))
60        self.assertEquals(robotsset, {'jido', 'mana', 'dala', 'atrv'})
61        sockf.close()
62        s.close()
63
64    def test_socket_request_parser_resilience(self):
65        """ Tests that the socket request parser is resilient to
66        useless whitespaces.
67
68        """
69
70        # Initialize a socket connection to the simulator
71        import socket
72        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
73        s.connect(("localhost", 4000))
74        sockf = s.makefile()
75
76        queries = [b"id1 simulation list_robots\n",
77                   b"id1  simulation list_robots\n",
78                   b"id1\tsimulation\tlist_robots\n",
79                   b"id1 \t simulation \t list_robots\n",
80                   b"   id1 simulation list_robots  \n"]
81
82        for q in queries:
83            s.send(q)
84            result = sockf.readline()
85            id, success, robots = result.strip().split(' ', 2)
86            self.assertEquals(success, "SUCCESS")
87
88        sockf.close()
89        s.close()
90
91
92########################## Run these tests ##########################
93if __name__ == "__main__":
94    from morse.testing.testing import main
95    main(BaseTest)
96