Fix pipes and testcases

This commit is contained in:
Roman Hotsiy 2015-12-14 18:31:17 +02:00
parent b5dcc7a19d
commit 832d018c1b
2 changed files with 36 additions and 10 deletions

View File

@ -1,7 +1,8 @@
'use strict'; 'use strict';
import {Pipe, InvalidPipeArgumentException} from 'angular2/core'; import {Pipe} from 'angular2/core';
import {isString} from 'angular2/src/facade/lang'; import {isString, stringify, isBlank} from 'angular2/src/facade/lang';
import {BaseException} from 'angular2/src/facade/exceptions';
import {JsonPointer} from './JsonPointer'; import {JsonPointer} from './JsonPointer';
import marked from 'marked'; import marked from 'marked';
@ -15,20 +16,27 @@ marked.setOptions({
smartypants: false smartypants: false
}); });
class InvalidPipeArgumentException extends BaseException {
constructor(type, value) {
super(`Invalid argument '${value}' for pipe '${stringify(type)}'`);
}
}
@Pipe({ name: 'keys' }) @Pipe({ name: 'keys' })
export class KeysPipe { export class KeysPipe {
transform(obj) { transform(value) {
if (typeof obj !== 'object') { if (isBlank(value)) return value;
throw new InvalidPipeArgumentException(ValuesPipe, obj); if (typeof value !== 'object') {
throw new InvalidPipeArgumentException(ValuesPipe, value);
} }
return Object.keys(obj); return Object.keys(value);
} }
} }
@Pipe({ name: 'values' }) @Pipe({ name: 'values' })
export class ValuesPipe { export class ValuesPipe {
transform(value) { transform(value) {
if (isBlank(value)) return value;
if (typeof value !== 'object') { if (typeof value !== 'object') {
throw new InvalidPipeArgumentException(ValuesPipe, value); throw new InvalidPipeArgumentException(ValuesPipe, value);
} }
@ -39,6 +47,7 @@ export class ValuesPipe {
@Pipe({ name: 'jsonPointerEscape' }) @Pipe({ name: 'jsonPointerEscape' })
export class JsonPointerEscapePipe { export class JsonPointerEscapePipe {
transform(value) { transform(value) {
if (isBlank(value)) return value;
if (!isString(value)) { if (!isString(value)) {
throw new InvalidPipeArgumentException(JsonPointerEscapePipe, value); throw new InvalidPipeArgumentException(JsonPointerEscapePipe, value);
} }
@ -49,6 +58,7 @@ export class JsonPointerEscapePipe {
@Pipe({ name: 'marked' }) @Pipe({ name: 'marked' })
export class MarkedPipe { export class MarkedPipe {
transform(value) { transform(value) {
if (isBlank(value)) return value;
if (!isString(value)) { if (!isString(value)) {
throw new InvalidPipeArgumentException(JsonPointerEscapePipe, value); throw new InvalidPipeArgumentException(JsonPointerEscapePipe, value);
} }

View File

@ -24,7 +24,11 @@ describe('KeysPipe and ValuesPipe', () => {
it('should not support other objects', () => { it('should not support other objects', () => {
(() => keysPipe.transform(45)).should.throw(); (() => keysPipe.transform(45)).should.throw();
(() => keysPipe.transform(null)).should.throw(); (() => keysPipe.transform('45')).should.throw();
});
it('should not throw on blank input', () => {
(() => valuesPipe.transform()).should.not.throw();
}); });
}); });
@ -36,7 +40,11 @@ describe('KeysPipe and ValuesPipe', () => {
it('should not support other objects', () => { it('should not support other objects', () => {
(() => valuesPipe.transform(45)).should.throw(); (() => valuesPipe.transform(45)).should.throw();
(() => valuesPipe.transform(null)).should.throw(); (() => valuesPipe.transform('45')).should.throw();
});
it('should not throw on blank input', () => {
(() => keysPipe.transform()).should.not.throw();
}); });
}); });
}); });
@ -52,7 +60,7 @@ describe('JsonPointerEscapePipe', () => {
pipe = new JsonPointerEscapePipe(); pipe = new JsonPointerEscapePipe();
}); });
describe('KeysPipe transform', () => { describe('JsonPointerEscapePipe transform', () => {
it('should escpae pointer', () => { it('should escpae pointer', () => {
var val = pipe.transform(unescaped); var val = pipe.transform(unescaped);
val.should.be.equal(escaped); val.should.be.equal(escaped);
@ -62,6 +70,10 @@ describe('JsonPointerEscapePipe', () => {
(() => pipe.transform(45)).should.throw(); (() => pipe.transform(45)).should.throw();
(() => pipe.transform({})).should.throw(); (() => pipe.transform({})).should.throw();
}); });
it('should not throw on blank input', () => {
(() => pipe.transform()).should.not.throw();
});
}); });
}); });
@ -76,7 +88,7 @@ describe('MarkedPipe', () => {
pipe = new MarkedPipe(); pipe = new MarkedPipe();
}); });
describe('KeysPipe transform', () => { describe('MarkedPipe transform', () => {
it('should escpae pointer', () => { it('should escpae pointer', () => {
var val = pipe.transform(unmarked); var val = pipe.transform(unmarked);
val.should.be.equal(marked); val.should.be.equal(marked);
@ -86,5 +98,9 @@ describe('MarkedPipe', () => {
(() => pipe.transform(45)).should.throw(); (() => pipe.transform(45)).should.throw();
(() => pipe.transform({})).should.throw(); (() => pipe.transform({})).should.throw();
}); });
it('should not throw on blank input', () => {
(() => pipe.transform()).should.not.throw();
});
}); });
}); });