Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if parameter is number before Number.parse #191

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions e2e/specs/parameters.spec
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
## Custom Parameters in steps

* This step uses a custom parameter of type Person and value "{\"name\":\"John\",\"age\":30}"
* This step checks if strings with numbers for example "3 % 4" is correct
6 changes: 6 additions & 0 deletions e2e/tests/parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ export default class Parameter {
assert.strictEqual(person.age, 30);
assert.ok(person.isAdult());
}
@Step(
"This step checks if strings with numbers for example <value> is correct",
)
async setFilter(value: string) {
assert.strictEqual(value, "3 % 4");
}
}
8 changes: 6 additions & 2 deletions gauge-ts/src/processors/params/PrimitiveParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ export class PrimitiveParser implements ParameterParser {
}

private convertToNumber(value: string): number | undefined {
const num = Number.parseFloat(value);
return Number.isNaN(num) ? undefined : num;
const trimmedValue = value.trim();
if (/^-?\d+(\.\d+)?$/.test(trimmedValue)) {
Copy link

@anthony-legay anthony-legay Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of using a regex but wanted to avoid it because it seemed overkill, but I'm afraid we don't have a choice.
I came up during my investigations with this regex: /^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$/ (thanks ChatGPT) to handle extreme case where users would use exponential notations (e.g. 2e-32). Seems unlikely, but...well better safe than sorry 😄

What if Gauge allowed the "special parameter" notation to specify a plain string like <string:0123456> and then it would bypass this? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if Gauge allowed the "special parameter" notation to specify a plain string like string:0123456 and then it would bypass this?

Thanks for the suggestion! The idea of using a special parameter like <string:0123456> makes sense. However, it's best to keep Gauge specs as readable and close to markdown as possible. This is to have that clear separation and make the reports readable to people who are not that technical.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anthony-legay pushed a simpler approach refer 910e614

Is there a way you can test this locally?

const num = Number.parseFloat(trimmedValue);
return Number.isFinite(num) ? num : undefined;
}
return undefined;
}

private convertToBoolean(value: string): boolean | undefined {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading