Skip to content

Commit bab5236

Browse files
qiuzhongMinggang Wang
authored andcommitted
Complete the test cases for missing rclnodejs APIs (#150)
* [test]Add 2 test cases for nodes in single process Add 2 test cases for the case when the publisher/subscription or client/service in the smae process. * [test]Add 11 test cases for rclnodejs.validator Complete the test cases for rclnodejs.validator family APIs. * [test]Add 5 test cases for expandTopicName API * [test]Add 9 test cases for new added M4 APIs
1 parent 299cb6b commit bab5236

File tree

5 files changed

+337
-23
lines changed

5 files changed

+337
-23
lines changed

test/test-existance.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,23 @@ describe('rclnodejs module existance testing', function() {
3636

3737
it('require method should exist', function() {
3838
assertMember('require', rclnodejs, rclnodejs.require, 'function');
39-
});
39+
});
4040

4141
it('shutdown method should exist', function() {
4242
assertMember('shutdown', rclnodejs, rclnodejs.shutdown, 'function');
43-
});
43+
});
4444

4545
it('spin method should exist', function() {
4646
assertMember('shutdown', rclnodejs, rclnodejs.shutdown, 'function');
47-
});
47+
});
48+
49+
it('expandTopicName method should exist', function() {
50+
assertMember('expandTopicName', rclnodejs, rclnodejs.expandTopicName, 'function');
51+
});
52+
53+
it('isTopicOrServiceHidden method should exist', function() {
54+
assertMember('isTopicOrServiceHidden', rclnodejs, rclnodejs.isTopicOrServiceHidden, 'function');
55+
});
4856
});
4957
});
5058

@@ -291,4 +299,26 @@ describe('rclnodejs class existance testing', function() {
291299
}, TypeError, 'Invalid argument', 'Failed to call setter');
292300
});
293301
});
302+
303+
describe('Validator class', function() {
304+
it('should have validateFullTopicName method', function() {
305+
assertMember('validateFullTopicName', rclnodejs.validator,
306+
rclnodejs.validator.validateFullTopicName, 'function');
307+
});
308+
309+
it('should have validateNodeName method', function() {
310+
assertMember('validateNodeName', rclnodejs.validator,
311+
rclnodejs.validator.validateNodeName, 'function');
312+
});
313+
314+
it('should have validateTopicName method', function() {
315+
assertMember('validateTopicName', rclnodejs.validator,
316+
rclnodejs.validator.validateTopicName, 'function');
317+
});
318+
319+
it('should have validateNamespace method', function() {
320+
assertMember('validateNamespace', rclnodejs.validator,
321+
rclnodejs.validator.validateNamespace, 'function');
322+
});
323+
});
294324
});

test/test-expand-topic.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) 2017 Intel Corporation. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const assert = require('assert');
18+
const rclnodejs = require('../index.js');
19+
const utils = require('./utils.js');
20+
21+
describe('rclnodejs expand topic API testing', function() {
22+
this.timeout(60 * 1000);
23+
24+
before(function() {
25+
return rclnodejs.init();
26+
});
27+
28+
after(function() {
29+
rclnodejs.shutdown();
30+
});
31+
32+
it('test_expand_topic_name', function() {
33+
var tests = {
34+
'/ns/chatter': ['chatter', 'node_name', '/ns'],
35+
'/chatter': ['chatter', 'node_name', '/'],
36+
'/ns/node_name/chatter': ['~/chatter', 'node_name', '/ns'],
37+
'/node_name/chatter': ['~/chatter', 'node_name', '/']
38+
};
39+
40+
for (let test in tests) {
41+
var [topic, name, namespace] = tests[test];
42+
assert.deepStrictEqual(test, rclnodejs.expandTopicName(topic, name, namespace));
43+
}
44+
});
45+
46+
it('expand_topic_name_invalid_node_name', function() {
47+
utils.assertThrowsError(() => {
48+
rclnodejs.expandTopicName('topic', 'invalid_node_name?', '/ns');
49+
}, Error, 'node name is invalid', 'invalid node name');
50+
});
51+
52+
it('expand_topic_name_invalid_namespace_empty', function() {
53+
utils.assertThrowsError(() => {
54+
rclnodejs.expandTopicName('topic', 'node_name', '');
55+
}, Error, 'node namespace is invalid', 'invalid namespace');
56+
});
57+
58+
it('expand_topic_name_invalid_namespace_relative', function() {
59+
utils.assertThrowsError(() => {
60+
rclnodejs.expandTopicName('topic', 'node_name', 'ns');
61+
}, Error, 'node namespace is invalid', 'invalid namespace');
62+
});
63+
64+
it('expand_topic_name_invalid_topic', function() {
65+
utils.assertThrowsError(() => {
66+
rclnodejs.expandTopicName('invalid/topic?', 'node_name', '/ns');
67+
}, Error, 'topic name is invalid', 'invalid topic name');
68+
});
69+
});

test/test-node.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,40 +32,34 @@ describe('rclnodejs node test suite', function() {
3232

3333
describe('createNode method testing', function() {
3434
it('Try creating a node', function() {
35-
const node = rclnodejs.createNode('example_node');
36-
rclnodejs.spin(node);
35+
var node = rclnodejs.createNode('example_node');
3736
node.destroy();
3837
});
3938

40-
/* Todo:
41-
** need the get_name() and get_namespace() interface.
4239
it('Try creating a node with a namespace', function() {
4340
let nodeName = 'example_node_with_ns',
44-
nodeNamespace = '/ns';
41+
nodeNamespace = '/ns';
4542

46-
const node = rclnodejs.createNode(nodeName, nodeNamespace);
47-
rclnodejs.spin(node);
48-
assert.deepStrictEqual(node.getNamespace(), '/ns');
43+
var node = rclnodejs.createNode(nodeName, nodeNamespace);
44+
assert.deepStrictEqual(node.namespace(), '/ns');
4945
});
5046

5147
it('Try creating a node with the empty namespace', function() {
5248
let nodeName = 'example_node_with_empty_ns',
53-
nodeNamespace = '';
49+
nodeNamespace = '';
5450

55-
const node = rclnodejs.createNode(nodeName, nodeNamespace);
56-
rclnodejs.spin(node);
57-
assert.deepStrictEqual(node.getNamespace(), '/');
51+
var node = rclnodejs.createNode(nodeName, nodeNamespace);
52+
assert.deepStrictEqual(node.namespace(), '/');
5853
});
5954

6055
it('Try creating a node with a relative namespace', function() {
6156
let nodeName = 'example_node_with_rel_ns',
62-
nodeNamespace = 'ns';
57+
nodeNamespace = 'ns';
6358

64-
const node = rclnodejs.createNode(nodeName, nodeNamespace);
65-
rclnodejs.spin(node);
66-
assert.deepStrictEqual(node.getNamespace(), '/ns');
59+
var node = rclnodejs.createNode(nodeName, nodeNamespace);
60+
assert.deepStrictEqual(node.namespace(), '/ns');
6761
});
68-
*/
62+
6963
it('Try creating a node with an invalid name', function() {
7064
var nodeName = 'example_node_invalid_name?',
7165
nodeNamespace = 'ns';
@@ -143,9 +137,8 @@ describe('rcl node methods testing', function() {
143137
it('node setter/getter', function() {
144138
assert.notDeepStrictEqual(null, node);
145139

146-
// Todo
147-
// assert.deepStrictEqual(node.getName(), 'my_node');
148-
// assert.deepStrictEqual(node.getNamespace(), '/my_ns');
140+
assert.deepStrictEqual(node.name(), 'my_node');
141+
assert.deepStrictEqual(node.namespace(), '/my_ns');
149142
});
150143

151144
it('node.createPublisher', function() {

test/test-single-process.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) 2017 Intel Corporation. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const assert = require('assert');
18+
const rclnodejs = require('../index.js');
19+
20+
describe('Test rclnodejs nodes in a single process', function() {
21+
this.timeout(60 * 1000);
22+
23+
before(function() {
24+
return rclnodejs.init();
25+
});
26+
27+
after(function() {
28+
rclnodejs.shutdown();
29+
});
30+
31+
it('Publisher/Subscription in one process', function(done) {
32+
var publisherNode = rclnodejs.createNode('single_ps_publisher');
33+
var subscriptionNode = rclnodejs.createNode('single_ps_subscription');
34+
const RclString = rclnodejs.require('std_msgs').msg.String;
35+
let msg = new RclString();
36+
msg.data = 'Hello World';
37+
38+
var subscription = subscriptionNode.createSubscription(RclString, 'single_ps_channel1', (msg) => {
39+
timer.cancel();
40+
assert.deepStrictEqual(msg.data, 'Hello World');
41+
done();
42+
});
43+
44+
var publisher = publisherNode.createPublisher(RclString, 'single_ps_channel1');
45+
var timer = publisherNode.createTimer(100, () => {
46+
publisher.publish(msg);
47+
});
48+
rclnodejs.spin(subscriptionNode);
49+
rclnodejs.spin(publisherNode);
50+
});
51+
52+
it('Client/Service is a one process', function(done) {
53+
var clientNode = rclnodejs.createNode('single_ps_client');
54+
var serviceNode = rclnodejs.createNode('single_ps_service');
55+
const AddTwoInts = rclnodejs.require('example_interfaces').srv.AddTwoInts;
56+
57+
var service = serviceNode.createService(AddTwoInts, 'single_ps_channel2', (request, response) => {
58+
assert.deepStrictEqual(request.a, 1);
59+
assert.deepStrictEqual(request.b, 2);
60+
response.sum = request.a + request.b;
61+
return response;
62+
});
63+
var client = clientNode.createClient(AddTwoInts, 'single_ps_channel2');
64+
let request = new AddTwoInts.Request();
65+
request.a = 1;
66+
request.b = 2;
67+
68+
var timer = clientNode.createTimer(100, () => {
69+
client.sendRequest(request, (response) => {
70+
timer.cancel();
71+
assert.deepStrictEqual(response.sum, 3);
72+
done();
73+
});
74+
});
75+
rclnodejs.spin(serviceNode);
76+
rclnodejs.spin(clientNode);
77+
});
78+
});

0 commit comments

Comments
 (0)