Skip to content

Commit 132cd84

Browse files
authored
Add basic parameter testing (#73)
1 parent 65cd0ec commit 132cd84

2 files changed

Lines changed: 169 additions & 1 deletion

File tree

test/utils/MasterStub.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ class RosMasterStub extends EventEmitter {
1616
registerSubscriber: this._onRegisterSubscriber.bind(this),
1717
unregisterSubscriber: this._onUnregisterSubscriber.bind(this),
1818
registerPublisher: this._onRegisterPublisher.bind(this),
19-
unregisterPublisher: this._onUnregisterPublisher.bind(this)
19+
unregisterPublisher: this._onUnregisterPublisher.bind(this),
20+
deleteParam: this._deleteParam.bind(this),
21+
setParam: this._setParam.bind(this),
22+
getParam: this._getParam.bind(this),
23+
hasParam: this._hasParam.bind(this),
24+
getParamNames: this._getParamNames.bind(this)
2025
};
2126

2227
this._providedApis = new Set();
@@ -27,6 +32,8 @@ class RosMasterStub extends EventEmitter {
2732
pub: null
2833
};
2934

35+
this._params = {};
36+
3037
this.listen();
3138
}
3239

@@ -41,6 +48,7 @@ class RosMasterStub extends EventEmitter {
4148
}
4249

4350
shutdown() {
51+
this._params = {};
4452
this._providedApis.clear();
4553
this.removeAllListeners();
4654
return new Promise((resolve, reject) => {
@@ -134,6 +142,56 @@ class RosMasterStub extends EventEmitter {
134142
callback(null, resp);
135143
this._clientCache.pub = null;
136144
}
145+
146+
// Param stubbing
147+
// NOTE: this is NOT a spec ParamServer implementation,
148+
// but it provides simple stubs for calls
149+
150+
_deleteParam(err, params, callback) {
151+
const key = params[1];
152+
if (this._params.hasOwnProperty(key)) {
153+
delete this._params[key];
154+
const resp = [1, 'delete value for ' + key, 1];
155+
callback(null, resp);
156+
}
157+
else {
158+
const resp = [0, 'no value for ' + key, 1];
159+
callback(null, resp);
160+
}
161+
}
162+
163+
_setParam(err, params, callback) {
164+
const key = params[1];
165+
const val = params[2];
166+
this._params[key] = val;
167+
const resp = [1, 'set value for ' + key, 1];
168+
callback(null, resp);
169+
}
170+
171+
_getParam(err, params, callback) {
172+
const key = params[1];
173+
const val = this._params[key];
174+
if (val !== undefined) {
175+
const resp = [1, 'data for ' + key, val];
176+
callback(null, resp);
177+
}
178+
else {
179+
const resp = [0, 'no data for ' + key, null];
180+
callback(null, resp);
181+
}
182+
}
183+
184+
_hasParam(err, params, callback) {
185+
const key = params[1];
186+
const resp = [1, 'check param ' + key, this._params.hasOwnProperty(key)];
187+
callback(null, resp);
188+
}
189+
190+
_getParamNames(err, params, callback) {
191+
const names = Object.keys(this._params);
192+
const resp = [1, 'get param names', names];
193+
callback(null, resp);
194+
}
137195
}
138196

139197
module.exports = RosMasterStub;

test/xmlrpcTest.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,4 +1031,114 @@ describe('Protocol Test', () => {
10311031
});
10321032
});
10331033
});
1034+
1035+
describe('Parameters', function() {
1036+
let masterStub;
1037+
1038+
before((done) => {
1039+
masterStub = new MasterStub('localhost', MASTER_PORT);
1040+
masterStub.provideAll();
1041+
1042+
return rosnodejs.shutdown()
1043+
.then(() => {
1044+
rosnodejs.reset();
1045+
return rosnodejs.initNode(nodeName, initArgs);
1046+
})
1047+
.then(() => done());
1048+
});
1049+
1050+
after((done) => {
1051+
masterStub.shutdown();
1052+
return rosnodejs.shutdown()
1053+
.then(() => {
1054+
rosnodejs.reset();
1055+
done();
1056+
});
1057+
});
1058+
1059+
it('Set', function(done) {
1060+
const nh = rosnodejs.nh;
1061+
1062+
nh.setParam('/key', 2)
1063+
.then(() => {
1064+
expect(masterStub._params['/key']).to.equal(2);
1065+
done();
1066+
});
1067+
});
1068+
1069+
it('Get', function(done) {
1070+
const nh = rosnodejs.nh;
1071+
1072+
nh.getParam('/key')
1073+
.then((result) => {
1074+
expect(result).to.equal(2);
1075+
expect(masterStub._params['/key']).to.equal(2);
1076+
done();
1077+
});
1078+
});
1079+
1080+
it('Has', function(done) {
1081+
const nh = rosnodejs.nh;
1082+
1083+
nh.hasParam('/key')
1084+
.then((result) => {
1085+
expect(result).to.be.true;
1086+
expect(masterStub._params['/key']).to.equal(2);
1087+
done();
1088+
});
1089+
});
1090+
1091+
it('Delete', function(done) {
1092+
const nh = rosnodejs.nh;
1093+
1094+
nh.deleteParam('/key')
1095+
.then(() => {
1096+
expect(masterStub._params['/key']).to.be.undefined;
1097+
done();
1098+
});
1099+
});
1100+
1101+
it('Full', function(done) {
1102+
const nh = rosnodejs.nh;
1103+
1104+
nh.getParam('/missing')
1105+
.then(() => throwNext('Get should reject'))
1106+
.catch(() => {
1107+
return nh.hasParam('/missing')
1108+
})
1109+
.catch(() => throwNext('Has should resolve'))
1110+
.then((result) => {
1111+
expect(result).to.be.false;
1112+
return nh.setParam('/exists', 1);
1113+
})
1114+
.catch(() => throwNext('Set should resolve'))
1115+
.then(() => {
1116+
expect(masterStub._params['/exists']).to.equal(1);
1117+
return nh.hasParam('/exists');
1118+
})
1119+
.catch(() => throwNext('Has should resolve'))
1120+
.then((result) => {
1121+
expect(result).to.be.true;
1122+
return nh.getParam('/exists');
1123+
})
1124+
.catch(() => throwNext('Get should resolve'))
1125+
.then((result) => {
1126+
expect(result).to.equal(1);
1127+
return nh.deleteParam('/missing');
1128+
})
1129+
.then(() => throwNext('Delete should reject'))
1130+
.catch((err) => {
1131+
return nh.deleteParam('/exists');
1132+
})
1133+
.catch(() => throwNext('Delete should resolve'))
1134+
.then(() => {
1135+
return nh.hasParam('/exists');
1136+
})
1137+
.catch(() => throwNext('Has should resolve'))
1138+
.then((result) => {
1139+
expect(result).to.be.false;
1140+
done();
1141+
});
1142+
});
1143+
});
10341144
});

0 commit comments

Comments
 (0)