Fix naming, add tests #5

Merged
manuel merged 19 commits from feature/use-proper-dirstructure into develop 2018-09-01 01:34:35 +02:00
5 changed files with 20 additions and 4 deletions
Showing only changes of commit 8280e0be94 - Show all commits

View file

@ -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';

View file

@ -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.');
});
});

View file

@ -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);

View file

@ -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.');
});
});

View file

@ -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);