Skip to content

Commit

Permalink
Merge pull request #59 from gkjohnson/params-update
Browse files Browse the repository at this point in the history
Quoted Macro Params Test
  • Loading branch information
gkjohnson authored Apr 23, 2021
2 parents db65b79 + 55133ba commit f8c998c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/XacroParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ export class XacroParser {

obj.name = name;
if (def.startsWith('\'') && def.endsWith('\'')) {
obj.def = def.substr(1, def.length - 2);
// strip quotes from the default value if it happens to be a string like so:
// a:='0.0 1.0 2.0'
obj.def = def.substring(1, def.length - 1);
} else {
obj.def = def;
}
Expand All @@ -244,9 +246,10 @@ export class XacroParser {
// parse params
const inputMap = {};
if (params) {
// find param definitions including string values like a:='0.0 1.0 2.0'
const inputs = params
.trim()
.match(/[^\s']+(['][^']*['])?/g)
.match(/[^\s']+('[^']*')?/g)
.map(s => parseMacroParam(s));
inputs.forEach(inp => {
inputMap[inp.name] = inp;
Expand Down
27 changes: 27 additions & 0 deletions test/XacroLoader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,33 @@ describe('XacroLoader', () => {
);
});

it.only('should resolve params and defaults with string properties.', done => {
const content =
`<?xml version="1.0"?>
<robot xmlns:xacro="http://ros.org/wiki/xacro">
<xacro:property name="input" value="10"/>
<xacro:macro name="test" params="a:='1.0 2.0 3.0' b:='1.0 2.0'">
<child a="\${a}" b="\${b}"/>
</xacro:macro>
<xacro:test/>
<xacro:test a="1"/>
</robot>
`;
const loader = new XacroLoader();
loader.parse(
content, res => {
const str = new XMLSerializer().serializeToString(res);
expect(unformat(str)).toEqual(unformat(
`<robot>
<child a="1.0 2.0 3.0" b="1.0 2.0"/>
<child a="1" b="1.0 2.0"/>
</robot>`,
));
done();
},
);
});

it('should throw an error if a parameter cannot be resolved.', done => {
const content =
`<?xml version="1.0"?>
Expand Down

0 comments on commit f8c998c

Please sign in to comment.