Skip to content

Commit 16c97c9

Browse files
committed
Get Param test passing in both ROS 1 and ROS 2
1 parent e05d367 commit 16c97c9

File tree

3 files changed

+55
-55
lines changed

3 files changed

+55
-55
lines changed

src/core/Param.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default class Param<T = unknown> {
6161
* @param [failedCallback] - The callback function when the service call failed or the parameter setting was unsuccessful.
6262
*/
6363
set(
64-
value: object,
64+
value: T,
6565
callback?: (message: rosapi.SetParamResponse) => void,
6666
failedCallback?: (error: string) => void,
6767
) {

src/core/Ros.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,8 +843,8 @@ export default class Ros extends EventEmitter<
843843
Topic(options) {
844844
return new Topic({ ros: this, ...options });
845845
}
846-
Param(options) {
847-
return new Param({ ros: this, ...options });
846+
Param<T>(options) {
847+
return new Param<T>({ ros: this, ...options });
848848
}
849849
Service(options) {
850850
return new Service({ ros: this, ...options });

test/examples/params.example.ts

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,63 @@
1-
import { describe, it, expect } from "vitest";
1+
import { describe, it, expect, vi } from "vitest";
22
import * as ROSLIB from "../../src/RosLib.js";
33

4+
const PARAM_NAME =
5+
process.env.ROS_DISTRO === "noetic"
6+
? "/test/foo"
7+
: // Is it crazy to muck around with use_sim_time here? I feel like it shouldn't matter..
8+
"/add_two_ints_server:use_sim_time";
9+
410
describe("Param setting", function () {
511
const ros = new ROSLIB.Ros({
612
url: "ws://localhost:9090",
713
});
8-
const param = ros.Param({
9-
name: "/test/foo",
14+
const param = ros.Param<boolean>({
15+
name: PARAM_NAME,
1016
});
1117

1218
it("Param generics", function () {
1319
expect(param).to.be.instanceOf(ROSLIB.Param);
14-
expect(param.name).to.be.equal("/test/foo");
15-
});
16-
17-
it("Param.set no callback", () =>
18-
new Promise((done) => {
19-
param.set("foo");
20-
setTimeout(done, 500);
21-
}));
22-
23-
it("Param.get", () =>
24-
new Promise<void>((done) => {
25-
param.get(function (result) {
26-
expect(result).to.be.equal("foo");
27-
done();
28-
});
29-
}));
30-
31-
it("Param.set w/ callback", () =>
32-
new Promise<void>((done) => {
33-
param.set("bar", function () {
34-
done();
35-
});
36-
}));
37-
38-
it("Param.get", () =>
39-
new Promise<void>((done) => {
40-
param.get(function (result) {
41-
expect(result).to.be.equal("bar");
42-
done();
43-
});
44-
}));
45-
46-
it("ros.getParams", () =>
47-
new Promise<void>((done) => {
48-
ros.getParams(function (params) {
49-
expect(params).to.include(param.name);
50-
done();
51-
});
52-
}));
53-
54-
it("Param.delete", () =>
55-
new Promise<void>((done) => {
56-
param.delete(function () {
57-
ros.getParams(function (params) {
58-
expect(params).to.not.include(param.name);
59-
done();
60-
});
61-
});
62-
}));
20+
expect(param.name).to.be.equal(PARAM_NAME);
21+
});
22+
23+
it("Param.set no callback", () => {
24+
param.set(true);
25+
});
26+
27+
it("Param.get", { retry: 3 }, async () => {
28+
const callback = vi.fn();
29+
param.get(callback);
30+
await vi.waitFor(() => expect(callback).toHaveBeenCalledWith(true));
31+
});
32+
33+
it("Param.set w/ callback", async () => {
34+
const callback = vi.fn();
35+
param.set(false, callback);
36+
await vi.waitFor(() => expect(callback).toHaveBeenCalled());
37+
});
38+
39+
it("Param.get", async () => {
40+
const callback = vi.fn();
41+
param.get(callback);
42+
await vi.waitFor(() => expect(callback).toHaveBeenCalledWith(false));
43+
});
44+
45+
it("ros.getParams", async () => {
46+
const callback = vi.fn();
47+
ros.getParams(callback);
48+
await vi.waitFor(() =>
49+
expect(callback.mock.calls[0][0]).to.include(PARAM_NAME),
50+
);
51+
});
52+
53+
it("Param.delete", async () => {
54+
const callback = vi.fn();
55+
param.delete(callback);
56+
await vi.waitFor(() => expect(callback).toHaveBeenCalled());
57+
const getParamsCallback = vi.fn();
58+
ros.getParams(getParamsCallback);
59+
vi.waitFor(() =>
60+
expect(getParamsCallback.mock.calls[0][0]).to.not.include(PARAM_NAME),
61+
);
62+
});
6363
});

0 commit comments

Comments
 (0)