Fixed BinToDecConverter file name and added more tests, also negative

tests for the Base64 converters.
This commit is contained in:
Manuel Friedli 2018-08-31 23:35:14 +02:00
parent 1b060644c3
commit 0eb69a3064
6 changed files with 24 additions and 2 deletions

View file

@ -1,6 +1,6 @@
import {Base64Decoder} from './converter/base64-decoder'; import {Base64Decoder} from './converter/base64-decoder';
import {Base64Encoder} from './converter/base64-encoder'; import {Base64Encoder} from './converter/base64-encoder';
import {BinToDecConverter} from './converter/bintodecconverter'; import {BinToDecConverter} from './converter/bin-to-dec-converter';
import {Converter} from './converter/converter'; import {Converter} from './converter/converter';
import {DecToBinConverter} from './converter/dectobinconverter'; import {DecToBinConverter} from './converter/dectobinconverter';
import {DecToHexConverter} from './converter/dectohexconverter'; import {DecToHexConverter} from './converter/dectohexconverter';

View file

@ -10,4 +10,7 @@ describe('Base64Decoder', () => {
it('should decode "SGVsbG8sIFdvcmxkIQ==" to "Hello, World!"', () => { it('should decode "SGVsbG8sIFdvcmxkIQ==" to "Hello, World!"', () => {
expect(new Base64Decoder().convert('SGVsbG8sIFdvcmxkIQ==')).toEqual('Hello, World!'); expect(new Base64Decoder().convert('SGVsbG8sIFdvcmxkIQ==')).toEqual('Hello, World!');
}); });
it('should raise an exception on invalid input', () => {
expect(() => new Base64Decoder().convert('foo bar.')).toThrowError('Could not decode base64 string. Maybe corrupt input?');
});
}); });

View file

@ -10,4 +10,7 @@ describe('Base64Encoder', () => {
it('should encode "Oh, guete Tag!" to "T2gsIGd1ZXRlIFRhZyE="', () => { it('should encode "Oh, guete Tag!" to "T2gsIGd1ZXRlIFRhZyE="', () => {
expect(new Base64Encoder().convert('Oh, guete Tag!')).toEqual('T2gsIGd1ZXRlIFRhZyE='); expect(new Base64Encoder().convert('Oh, guete Tag!')).toEqual('T2gsIGd1ZXRlIFRhZyE=');
}); });
it('should raise an exception on invalid input', () => {
expect(() => new Base64Encoder().convert('€')).toThrowError(/Looks like you've got a character outside of the Latin1 range there./);
});
}); });

View file

@ -14,7 +14,7 @@ export class Base64Encoder implements Converter {
return btoa(input); return btoa(input);
} catch (exception) { } catch (exception) {
console.error(exception); console.error(exception);
throw new Error('Ouch! Looks like you\'ve got a UTF-8 character there. Too bad, this is not supported yet. ' throw new Error('Ouch! Looks like you\'ve got a character outside of the Latin1 range there. Too bad, this is not supported yet. '
+ 'We\'re working on it and hope to be ready soon! Why don\'t you ' + 'We\'re working on it and hope to be ready soon! Why don\'t you '
+ '<a href="https://duckduckgo.com/?q=cute+kitties&iar=images">enjoy some kittens</a> meanwhile?'); + '<a href="https://duckduckgo.com/?q=cute+kitties&iar=images">enjoy some kittens</a> meanwhile?');
} }

View file

@ -0,0 +1,16 @@
import {BinToDecConverter} from './bin-to-dec-converter';
describe('BinToDecConverter', () => {
it('should create an instance', () => {
expect(new BinToDecConverter()).toBeTruthy();
});
it('should have the id "base64encode"', () => {
expect(new BinToDecConverter().getId()).toEqual('bintodec');
});
it('should convert "11011" to "27"', () => {
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.');
});
});