UNPKG

3.91 kBJavaScriptView Raw
1var assert = require("chai").assert;
2var HttpError = require('../errors').HttpError;
3var DataError = require('../errors').DataError;
4var HttpBadRequestError = require('../errors').HttpBadRequestError;
5var AccessDeniedError = require('../errors').AccessDeniedError;
6var DataNotFoundError = require('../errors').DataNotFoundError;
7var AbstractClassError = require('../errors').AbstractClassError;
8var AbstractMethodError = require('../errors').AbstractMethodError;
9var LangUtils = require('../utils').LangUtils;
10
11describe("test common errors", function () {
12 it("should use new HttpError", function () {
13
14 var err = new HttpError(500);
15 assert.isOk(err instanceof Error, "Expected HttpError");
16 assert.isOk(err instanceof HttpError, "Expected HttpError");
17 assert.equal(err.statusCode, 500, "Expected Error 500");
18 });
19
20 it("should use new DataError", function () {
21
22 var err = new DataError("EMSG", "A data error occured");
23 assert.isOk(err instanceof Error, "Expected Error");
24 assert.isOk(err instanceof DataError, "Expected DataError");
25 assert.equal(err.code, "EMSG", "Expected EMSG code");
26 });
27
28 it("should use new HttpBadRequestError", function () {
29
30 var err = new HttpBadRequestError();
31 assert.isOk(err instanceof Error, "Expected HttpError");
32 assert.isOk(err instanceof HttpError, "Expected HttpError");
33 assert.isOk(err instanceof HttpBadRequestError, "Expected HttpError");
34 });
35
36 it("should use new AccessDeniedError", function () {
37
38 var err = new AccessDeniedError();
39 assert.isOk(err instanceof Error, "Expected Error");
40 assert.isOk(err instanceof DataError, "Expected DataError");
41 assert.isOk(err instanceof AccessDeniedError, "Expected DataError");
42 assert.equal(err.code, "EACCESS", "Expected EACCESS code");
43 assert.equal(err.statusCode, 401, "Expected statusCode 401");
44 });
45
46 it("should use new DataNotFoundError", function () {
47
48 var err = new DataNotFoundError();
49 assert.isOk(err instanceof Error, "Expected Error");
50 assert.isOk(err instanceof DataError, "Expected DataError");
51 assert.isOk(err instanceof DataNotFoundError, "Expected DataError");
52 assert.equal(err.code, "EFOUND", "Expected EFOUND code");
53 assert.equal(err.statusCode, 404, "Expected statusCode 404");
54 });
55
56 /**
57 * @class
58 * @param {string} name
59 * @abstract
60 * @constructor
61 */
62 function Animal(name) {
63 if (this.constructor === Animal.prototype.constructor) {
64 throw new AbstractClassError();
65 }
66 this.name = name;
67 }
68
69 /**
70 * @abstract
71 */
72 Animal.prototype.getDescription = function() {
73 throw new AbstractMethodError();
74 };
75
76 /**
77 * @abstract
78 */
79 Animal.prototype.speak = function() {
80 throw new AbstractMethodError();
81 };
82
83 /**
84 * @class
85 * @constructor
86 * @param {string} name
87 * @augments Animal
88 */
89 function Dog(name) {
90 Dog.super_.bind(this)(name);
91 }
92 LangUtils.inherits(Dog, Animal);
93
94 Dog.prototype.speak = function() {
95 return this.name + ' barks.';
96 };
97
98 /**
99 * @class
100 * @constructor
101 * @param {string} name
102 * @augments Animal
103 */
104 function Cat(name) {
105 Cat.super_.bind(this)(name);
106 }
107 LangUtils.inherits(Cat, Animal);
108
109
110 it("should use abstract class and abstract method errors", function () {
111 assert.throws(function() {
112 return new Animal();
113 });
114 assert.doesNotThrow(function() {
115 return new Dog();
116 });
117 assert.throws(function() {
118 var a = new Cat();
119 return a.speak();
120 });
121 assert.doesNotThrow(function() {
122 var a = new Dog();
123 return a.speak();
124 });
125 });
126
127
128});
\No newline at end of file