|
1 | 1 | import pymatbridge as pymat |
2 | 2 | import numpy.testing as npt |
| 3 | +import test_utils as tu |
3 | 4 |
|
4 | | -# Add 1 to the argument and return it |
5 | | -def test_demo_func(): |
6 | | - mlab = pymat.Matlab() |
7 | | - mlab.start() |
8 | | - npt.assert_(mlab.is_connected(), msg = "Connection failed") |
| 5 | +class TestJson: |
9 | 6 |
|
10 | | - for i in range(5): |
11 | | - res = mlab.run_func('demo_func.m', {'a': i})['result'] |
12 | | - ans = i + 1 |
13 | | - npt.assert_equal(res, ans, err_msg = "demo_func.m test failed") |
| 7 | + # Start a Matlab session before doing any tests |
| 8 | + @classmethod |
| 9 | + def setup_class(cls): |
| 10 | + cls.mlab = tu.connect_to_matlab() |
14 | 11 |
|
15 | | - mlab.stop() |
16 | | - npt.assert_(not mlab.is_connected(), msg = "Disconnection failed") |
| 12 | + # Tear down the Matlab session after all the tests are done |
| 13 | + @classmethod |
| 14 | + def teardown_class(cls): |
| 15 | + tu.stop_matlab(cls.mlab) |
17 | 16 |
|
18 | | -# Print some strange characters in Matlab, get them back and compare. |
19 | | -def test_special_character(): |
20 | | - mlab = pymat.Matlab() |
21 | | - mlab.start() |
22 | | - npt.assert_(mlab.is_connected(), msg = "Connection failed") |
23 | 17 |
|
24 | | - # Test 1:"hi"\n |
25 | | - res = mlab.run_code('fprintf(1, char([34, 104, 105, 34, 10]));')['content']['stdout'] |
26 | | - ans = unichr(34) + unichr(104) + unichr(105) + unichr(34) + unichr(10) |
27 | | - npt.assert_equal(res, ans, err_msg = "Special Character Test 1 failed") |
| 18 | + # Add 1 to the argument and return it |
| 19 | + def test_demo_func(self): |
| 20 | + for i in range(5): |
| 21 | + res = self.mlab.run_func('demo_func.m', {'a': i})['result'] |
| 22 | + ans = i + 1 |
| 23 | + npt.assert_equal(res, ans, err_msg = "demo_func.m test failed") |
28 | 24 |
|
29 | | - # Test 2:\b\n |
30 | | - res = mlab.run_code('fprintf(1, char([8,10]));')['content']['stdout'] |
31 | | - ans = unichr(8) + unichr(10) |
32 | | - npt.assert_equal(res, ans, err_msg = "Special Character Test 2 failed") |
| 25 | + # Print some strange characters in Matlab, get them back and compare. |
| 26 | + def test_special_character(self): |
33 | 27 |
|
34 | | - # Test 3:\f\n |
35 | | - res = mlab.run_code('fprintf(1, char([12,10]));')['content']['stdout'] |
36 | | - ans = unichr(12) + unichr(10) |
37 | | - npt.assert_equal(res, ans, err_msg = "Special Character Test 3 failed") |
| 28 | + # Test 1:"hi"\n |
| 29 | + res = self.mlab.run_code('fprintf(1, char([34, 104, 105, 34, 10]));')['content']['stdout'] |
| 30 | + ans = unichr(34) + unichr(104) + unichr(105) + unichr(34) + unichr(10) |
| 31 | + npt.assert_equal(res, ans, err_msg = "Special Character Test 1 failed") |
38 | 32 |
|
39 | | - # Test 4:\r\n |
40 | | - res = mlab.run_code('fprintf(1, char([13,10]));')['content']['stdout'] |
41 | | - ans = unichr(13) + unichr(10) |
42 | | - npt.assert_equal(res, ans, err_msg = "Special Character Test 4 failed") |
| 33 | + # Test 2:\b\n |
| 34 | + res = self.mlab.run_code('fprintf(1, char([8,10]));')['content']['stdout'] |
| 35 | + ans = unichr(8) + unichr(10) |
| 36 | + npt.assert_equal(res, ans, err_msg = "Special Character Test 2 failed") |
43 | 37 |
|
44 | | - # Test 5:\t\n |
45 | | - res = mlab.run_code('fprintf(1, char([9,10]));')['content']['stdout'] |
46 | | - ans = unichr(9) + unichr(10) |
47 | | - npt.assert_equal(res, ans, err_msg = "Special Character Test 5 failed") |
| 38 | + # Test 3:\f\n |
| 39 | + res = self.mlab.run_code('fprintf(1, char([12,10]));')['content']['stdout'] |
| 40 | + ans = unichr(12) + unichr(10) |
| 41 | + npt.assert_equal(res, ans, err_msg = "Special Character Test 3 failed") |
48 | 42 |
|
49 | | - # Test 6:\\\n |
50 | | - res = mlab.run_code('fprintf(1, char([92,92,10]));')['content']['stdout'] |
51 | | - ans = unichr(92) + unichr(10) # Python already encoded double-slash as 92 |
52 | | - npt.assert_equal(res, ans, err_msg = "Special Character Test 6 failed") |
| 43 | + # Test 4:\r\n |
| 44 | + res = self.mlab.run_code('fprintf(1, char([13,10]));')['content']['stdout'] |
| 45 | + ans = unichr(13) + unichr(10) |
| 46 | + npt.assert_equal(res, ans, err_msg = "Special Character Test 4 failed") |
53 | 47 |
|
54 | | - # Test 7:/\n |
55 | | - res = mlab.run_code('fprintf(1, char([47,10]));')['content']['stdout'] |
56 | | - ans = unichr(47) + unichr(10) |
57 | | - npt.assert_equal(res, ans, err_msg = "Special Character Test 7 failed") |
| 48 | + # Test 5:\t\n |
| 49 | + res = self.mlab.run_code('fprintf(1, char([9,10]));')['content']['stdout'] |
| 50 | + ans = unichr(9) + unichr(10) |
| 51 | + npt.assert_equal(res, ans, err_msg = "Special Character Test 5 failed") |
58 | 52 |
|
59 | | - # Test 8: Lots of strange characters |
60 | | - res = mlab.run_code('fprintf(1,char([47,92,92,47,8,12,10,13,9,12,8,8,12,47,34,47,10,10,47,34]));')['content']['stdout'] |
61 | | - ans = unichr(47) + unichr(92) + unichr(47) + unichr(8) + unichr(12) + unichr(10) \ |
62 | | - + unichr(13) + unichr(9) + unichr(12) + unichr(8) + unichr(8) + unichr(12) \ |
63 | | - + unichr(47) + unichr(34) + unichr(47) + unichr(10) + unichr(10) + unichr(47) + unichr(34) |
64 | | - npt.assert_equal(res, ans, err_msg = "Special Character Test 8 failed") |
| 53 | + # Test 6:\\\n |
| 54 | + res = self.mlab.run_code('fprintf(1, char([92,92,10]));')['content']['stdout'] |
| 55 | + ans = unichr(92) + unichr(10) # Python already encoded double-slash as 92 |
| 56 | + npt.assert_equal(res, ans, err_msg = "Special Character Test 6 failed") |
65 | 57 |
|
66 | | - mlab.stop() |
67 | | - npt.assert_(not mlab.is_connected(), msg = "Disconnection failed") |
| 58 | + # Test 7:/\n |
| 59 | + res = self.mlab.run_code('fprintf(1, char([47,10]));')['content']['stdout'] |
| 60 | + ans = unichr(47) + unichr(10) |
| 61 | + npt.assert_equal(res, ans, err_msg = "Special Character Test 7 failed") |
| 62 | + |
| 63 | + # Test 8: Lots of strange characters |
| 64 | + res = self.mlab.run_code('fprintf(1,char([47,92,92,47,8,12,10,13,9,12,8,8,12,47,34,47,10,10,47,34]));')['content']['stdout'] |
| 65 | + ans = unichr(47) + unichr(92) + unichr(47) + unichr(8) + unichr(12) + unichr(10) \ |
| 66 | + + unichr(13) + unichr(9) + unichr(12) + unichr(8) + unichr(8) + unichr(12) \ |
| 67 | + + unichr(47) + unichr(34) + unichr(47) + unichr(10) + unichr(10) + unichr(47) + unichr(34) |
| 68 | + npt.assert_equal(res, ans, err_msg = "Special Character Test 8 failed") |
68 | 69 |
|
0 commit comments