|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { DOMParser } from "@xmldom/xmldom"; |
| 3 | +import UrdfJoint from "../src/urdf/UrdfJoint"; |
| 4 | + |
| 5 | +describe("UrdfJoint", () => { |
| 6 | + it("should parse axis correctly from URDF", () => { |
| 7 | + const jointWithAxisUrdf = ` |
| 8 | + <joint name="test_joint" type="revolute"> |
| 9 | + <parent link="link1"/> |
| 10 | + <child link="link2"/> |
| 11 | + <axis xyz="0 1 0"/> |
| 12 | + </joint>`; |
| 13 | + const parser = new DOMParser(); |
| 14 | + const xml = parser.parseFromString( |
| 15 | + jointWithAxisUrdf, |
| 16 | + "text/xml", |
| 17 | + ).documentElement; |
| 18 | + if (!xml) { |
| 19 | + throw new Error("Failed to parse XML"); |
| 20 | + } |
| 21 | + const joint = new UrdfJoint({ xml }); |
| 22 | + expect(joint.axis.x).toBe(0); |
| 23 | + expect(joint.axis.y).toBe(1); |
| 24 | + expect(joint.axis.z).toBe(0); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should default axis to (1,0,0) if not present", () => { |
| 28 | + const jointNoAxisUrdf = ` |
| 29 | + <joint name="test_joint" type="revolute"> |
| 30 | + <parent link="link1"/> |
| 31 | + <child link="link2"/> |
| 32 | + </joint> |
| 33 | + `; |
| 34 | + const parser = new DOMParser(); |
| 35 | + const xml = parser.parseFromString( |
| 36 | + jointNoAxisUrdf, |
| 37 | + "text/xml", |
| 38 | + ).documentElement; |
| 39 | + if (!xml) { |
| 40 | + throw new Error("Failed to parse XML"); |
| 41 | + } |
| 42 | + const joint = new UrdfJoint({ xml }); |
| 43 | + expect(joint.axis.x).toBe(1); |
| 44 | + expect(joint.axis.y).toBe(0); |
| 45 | + expect(joint.axis.z).toBe(0); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should throw if axis xyz is malformed", () => { |
| 49 | + const jointMalformedAxisUrdf = ` |
| 50 | + <joint name="test_joint" type="revolute"> |
| 51 | + <parent link="link1"/> |
| 52 | + <child link="link2"/> |
| 53 | + <axis xyz="malformed data"/> |
| 54 | + </joint> |
| 55 | + `; |
| 56 | + const parser = new DOMParser(); |
| 57 | + const xml = parser.parseFromString( |
| 58 | + jointMalformedAxisUrdf, |
| 59 | + "text/xml", |
| 60 | + ).documentElement; |
| 61 | + if (!xml) { |
| 62 | + throw new Error("Failed to parse XML"); |
| 63 | + } |
| 64 | + expect(() => new UrdfJoint({ xml })).toThrowError( |
| 65 | + "If specified, axis must have an xyz value composed of three numbers", |
| 66 | + ); |
| 67 | + }); |
| 68 | +}); |
0 commit comments