From 8280e0be94b12ffaca562f44a87266061a0856ae Mon Sep 17 00:00:00 2001 From: Manuel Friedli Date: Fri, 31 Aug 2018 23:59:52 +0200 Subject: [PATCH] Fixed HexToDecConverter file name and added tests; made conversion from binary and hexadecimal to decimal stricter. --- src/app/converter-registry.service.ts | 2 +- src/app/converter/bin-to-dec-converter.spec.ts | 2 +- src/app/converter/bin-to-dec-converter.ts | 2 +- src/app/converter/hex-to-dec-converter.spec.ts | 16 ++++++++++++++++ ...todecconverter.ts => hex-to-dec-converter.ts} | 2 +- 5 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 src/app/converter/hex-to-dec-converter.spec.ts rename src/app/converter/{hextodecconverter.ts => hex-to-dec-converter.ts} (85%) diff --git a/src/app/converter-registry.service.ts b/src/app/converter-registry.service.ts index 4df04c0..dd6e19d 100644 --- a/src/app/converter-registry.service.ts +++ b/src/app/converter-registry.service.ts @@ -4,7 +4,7 @@ import {BinToDecConverter} from './converter/bin-to-dec-converter'; import {Converter} from './converter/converter'; import {DecToBinConverter} from './converter/dec-to-bin-converter'; import {DecToHexConverter} from './converter/dec-to-hex-converter'; -import {HexToDecConverter} from './converter/hextodecconverter'; +import {HexToDecConverter} from './converter/hex-to-dec-converter'; import {HTMLEntitiesDecoder} from './converter/htmlentitiesdecoder'; import {HTMLEntitiesEncoder} from './converter/htmlentitiesencoder'; import {Injectable} from '@angular/core'; diff --git a/src/app/converter/bin-to-dec-converter.spec.ts b/src/app/converter/bin-to-dec-converter.spec.ts index cf65375..a67bbb0 100644 --- a/src/app/converter/bin-to-dec-converter.spec.ts +++ b/src/app/converter/bin-to-dec-converter.spec.ts @@ -11,6 +11,6 @@ describe('BinToDecConverter', () => { expect(new BinToDecConverter().convert('11011')).toEqual('27'); }); it('should raise an exception on invalid input', () => { - expect(() => new BinToDecConverter().convert('foo bar')).toThrowError('The input seems not to be a valid binary number.'); + expect(() => new BinToDecConverter().convert('1foo bar')).toThrowError('The input seems not to be a valid binary number.'); }); }); diff --git a/src/app/converter/bin-to-dec-converter.ts b/src/app/converter/bin-to-dec-converter.ts index 3315882..fe95bff 100644 --- a/src/app/converter/bin-to-dec-converter.ts +++ b/src/app/converter/bin-to-dec-converter.ts @@ -11,7 +11,7 @@ export class BinToDecConverter implements Converter { convert(input: string): string { const n: number = parseInt(input, 2); - if (isNaN(n)) { + if (isNaN(n) || !input.trim().match(/^([01]+)$/)) { throw new Error('The input seems not to be a valid binary number.'); } return n.toString(10); diff --git a/src/app/converter/hex-to-dec-converter.spec.ts b/src/app/converter/hex-to-dec-converter.spec.ts new file mode 100644 index 0000000..f01c833 --- /dev/null +++ b/src/app/converter/hex-to-dec-converter.spec.ts @@ -0,0 +1,16 @@ +import {HexToDecConverter} from './hex-to-dec-converter'; + +describe('HexToDecConverter', () => { + it('should create an instance', () => { + expect(new HexToDecConverter()).toBeTruthy(); + }); + it('should have the id "hextodec"', () => { + expect(new HexToDecConverter().getId()).toEqual('hextodec'); + }); + it('should convert "ab" to "171"', () => { + expect(new HexToDecConverter().convert('ab')).toEqual('171'); + }); + it('should raise an exception on invalid input', () => { + expect(() => new HexToDecConverter().convert('foo bar')).toThrowError('The input seems not to be a valid hexadecimal number.'); + }); +}); diff --git a/src/app/converter/hextodecconverter.ts b/src/app/converter/hex-to-dec-converter.ts similarity index 85% rename from src/app/converter/hextodecconverter.ts rename to src/app/converter/hex-to-dec-converter.ts index 4b78345..7ac803e 100644 --- a/src/app/converter/hextodecconverter.ts +++ b/src/app/converter/hex-to-dec-converter.ts @@ -11,7 +11,7 @@ export class HexToDecConverter implements Converter { convert(input: string): string { const n: number = parseInt(input, 16); - if (isNaN(n)) { + if (isNaN(n) || !input.trim().match(/^((0x|0X)?[0-9a-fA-F]+)$/)) { throw new Error('The input seems not to be a valid hexadecimal number.'); } return n.toString(10);