diff --git a/CHANGELOG.md b/CHANGELOG.md index 32b66827..5ca4c377 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package-lock.json b/package-lock.json index 614367b6..d14e44fc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "httpyac", - "version": "6.13.0", + "version": "6.13.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "httpyac", - "version": "6.13.0", + "version": "6.13.1", "license": "MIT", "dependencies": { "@cloudamqp/amqp-client": "^2.1.1", diff --git a/package.json b/package.json index ca51addd..b2223876 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/plugins/intellij/replacer/replaceIntellijVariableRandom.spec.ts b/src/plugins/intellij/replacer/replaceIntellijVariableRandom.spec.ts new file mode 100644 index 00000000..e33c30fc --- /dev/null +++ b/src/plugins/intellij/replacer/replaceIntellijVariableRandom.spec.ts @@ -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(); + }); +}); diff --git a/src/plugins/intellij/replacer/replaceIntellijVariableRandom.ts b/src/plugins/intellij/replacer/replaceIntellijVariableRandom.ts index 4710276f..a34b6046 100644 --- a/src/plugins/intellij/replacer/replaceIntellijVariableRandom.ts +++ b/src/plugins/intellij/replacer/replaceIntellijVariableRandom.ts @@ -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}`;