Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
gyeates committed Oct 7, 2014
1 parent 3536a82 commit a69ba50
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 3 deletions.
35 changes: 32 additions & 3 deletions test/examples/check-topics.examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,44 @@ var expectedTopics = [
];

describe('Example topics are live', function(done) {
var ros = new ROSLIB.Ros({
url: 'ws://localhost:9090'
});

it('getTopics', function(done) {
var ros = new ROSLIB.Ros({
url: 'ws://localhost:9090'
});
ros.getTopics(function(topics) {
expectedTopics.forEach(function(topic) {
expect(topics).to.contain(topic, 'Couldn\'t find topic: ' + topic);
});
done();
});
});

var example = ros.Topic({
name: '/some_test_topic',
messageType: 'std_msgs/String'
});

it('doesn\'t automatically advertise the topic', function(done) {
ros.getTopics(function(topics) {
expect(topics).not.to.contain('/some_test_topic');
example.advertise();
done();
});
});

it('advertise broadcasts the topic', function(done) {
ros.getTopics(function(topics) {
expect(topics).to.contain('/some_test_topic');
example.unadvertise();
done();
});
});

it('unadvertise will end the topic (if it\s the last around)', function(done) {
ros.getTopics(function(topics) {
expect(topics).not.to.contain('/some_test_topic');
done();
});
});
});
100 changes: 100 additions & 0 deletions test/examples/pubsub.example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
var expect = require('chai').expect;
var ROSLIB = require('../..');

describe('Topics Example', function() {
this.timeout(1000);

var ros = new ROSLIB.Ros({
url: 'ws://localhost:9090'
});

var example = ros.Topic({
name: '/example_topic',
messageType: 'std_msgs/String'
});

function format(msg) {
return {data: msg};
}
var messages1 = ['Hello Example2!', 'Whats good?'].map(format);
var messages2 = ['Hi there', 'this example working'].map(format);

var example2 = ros.Topic({
name: '/example_topic',
messageType: 'std_msgs/String'
});

it('Listening and publishing to a topic', function(done) {
// Kind of harry...
var topic1msg = messages1[0],
topic2msg = {};
example.subscribe(function(message) {
if (message.data === topic1msg.data) return;
topic1msg = messages1[0];
expect(message).to.be.eql(messages2.shift());
if (messages1.length) example.publish(topic1msg);
else done();
});
example2.subscribe(function(message) {
if (message.data === topic2msg.data) return;
topic2msg = messages2[0];
expect(message).to.be.eql(messages1.shift());
if (messages2.length) example2.publish(topic2msg);
else done();
});
example.publish(topic1msg);
});

it('unsubscribe doesn\'t affect other topics', function(done) {
example2.subscribe(function(message) {
// should never be called
expect(false).to.be.ok;
});
example.unsubscribe();
example2.removeAllListeners('message');
example2.subscribe(function(message) {
expect(message).to.be.eql({
data: 'hi'
});
done();
});
example.publish({
data: 'hi'
});
});

it('unadvertise doesn\'t affect other topics', function(done) {
example.unsubscribe();
example2.unadvertise();
example2.removeAllListeners('message');
example2.subscribe(function(message) {
expect(example2.isAdvertised).to.be.false;
expect(message).to.be.eql({
data: 'hi'
});
done();
});
example.publish({
data: 'hi'
});
});

it('unsubscribing from all Topics should stop the socket from receiving data (on that topic', function(done) {
example.unsubscribe();
example2.unsubscribe();
ros.on('/example_topic', function() {
expect(false).to.be.ok;
});
example.publish({
data: 'sup'
});
setTimeout(done, 500);
});

this.afterAll(function() {
example.unadvertise();
example.unsubscribe();
example2.unadvertise();
example2.unsubscribe();
});
});

0 comments on commit a69ba50

Please sign in to comment.