Skip to content

Commit

Permalink
fix: `$random.integer() returns valid integer (AnWeber/vscode-httpyac…
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed Apr 29, 2024
1 parent d015791 commit 40cd3cd
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

## [6.13.1] ( 2024-04-29)

### Fix
- `$random.integer() returns valid integer (Anweber/vscode-httpyac#277)

## [6.13.0]
### Features
- update to new API Changes of Intellij Http Client
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"publisher": "AnWeber",
"description": "HTTP/REST CLI Client for *.http files",
"version": "6.13.0",
"version": "6.13.1",
"homepage": "https://github.com/AnWeber/httpyac",
"repository": {
"type": "git",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { replaceIntellijVariableRandom } from './replaceIntellijVariableRandom';

describe('replaceIntellijVariableRandom', () => {
it('should return uuid', () => {
const result = replaceIntellijVariableRandom('$uuid');
expect(result).toMatch(/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/u);
});
it('should return $random.uuid', () => {
const result = replaceIntellijVariableRandom('$random.uuid');
expect(result).toMatch(/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/u);
});
it('should return $timestamp', () => {
const result = replaceIntellijVariableRandom('$timestamp');
expect(result).toMatch(/^[0-9]{13}$/u);
});
it('should return $isoTimestamp', () => {
const result = replaceIntellijVariableRandom('$isoTimestamp');
expect(result).toMatch(/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d\d\dZ/u);
});
it('should return $randomInt', () => {
const result = replaceIntellijVariableRandom('$randomInt');
expect(Number.isInteger(Number(result))).toBe(true);
});
it('should return $random.integer', () => {
const result = Number(replaceIntellijVariableRandom('$random.integer(0,10)'));
expect(Number.isInteger(result)).toBe(true);
expect(result).toBeGreaterThanOrEqual(0);
expect(result).toBeLessThanOrEqual(10);
});
it('should return $random.float', () => {
const result = replaceIntellijVariableRandom('$random.float(0,10)');
expect(result).toMatch(/^\d*\.\d*$/u);
});
it('should return $random.alphabetic', () => {
const result = replaceIntellijVariableRandom('$random.alphabetic(10)');
expect(result).toMatch(/^[a-zA-Z]*$/u);
expect(result?.length).toBe(10);
});
it('should return $random.alphanumeric', () => {
const result = replaceIntellijVariableRandom('$random.alphanumeric(10)');
expect(result?.length).toBe(10);
expect(result).toMatch(/^[0-9a-zA-Z]*$/u);
});
it('should return $random.alphanumeric', () => {
const result = replaceIntellijVariableRandom('$random.alphanumeric(10)');
expect(result?.length).toBe(10);
expect(result).toMatch(/^[0-9a-zA-Z]*$/u);
});
it('should return $random.email', () => {
const result = replaceIntellijVariableRandom('$random.email()');
expect(result).toMatch(/^[\w]+@[\w-]+\.[\w]{2,4}$/u);
});
it('should return $random.hexadecimal', () => {
const result = replaceIntellijVariableRandom('$random.hexadecimal(10)');
expect(result?.length).toBe(10);
expect(result).toMatch(/^[0-9a-fA-F]{10}$/u);
});
it('should return undefined', () => {
const result = replaceIntellijVariableRandom('$random.foo');
expect(result).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ export function replaceIntellijVariableRandom(variable: string): string | undefi
if (trimmedVariable === '$randomInt') {
return `${Math.floor(Math.random() * 1000)}`;
}

if (trimmedVariable.startsWith('$random.float') || trimmedVariable.startsWith('$random.integer')) {
if (trimmedVariable.startsWith('$random.integer')) {
const float = randomFloat(trimmedVariable);
if (float) {
return `${Math.floor(float)}`;
}
}
if (trimmedVariable.startsWith('$random.float')) {
const float = randomFloat(trimmedVariable);
if (float) {
return `${float}`;
Expand Down

0 comments on commit 40cd3cd

Please sign in to comment.