UNPKG

2.17 kBJavaScriptView Raw
1'use strict';
2
3const expect = require('chai').expect;
4const inherits = require('util').inherits;
5const Data = require('./data');
6
7exports.validateId = function(obj) {
8 expect(obj).to.not.be.null;
9 expect(obj).to.be.a('object');
10 expect(obj._id.toString()).to.be.a('string');
11 expect(obj._id.toString()).to.have.length.of.at.least(1);
12};
13
14exports.data1 = function() {
15 let data = Data.create();
16 data.number = 1;
17 data.source = 'arstechnica';
18 data.item = 99;
19 data.values = [33, 101, -1];
20 data.date = 1434304033241;
21 return data;
22};
23
24exports.validateData1 = function(d) {
25 expect(d.number).to.be.equal(1);
26 expect(d.source).to.be.equal('arstechnica');
27 expect(d.item).to.be.equal(99);
28 expect(d).to.have.property('values').with.length(3);
29 expect(d.date.valueOf()).to.be.equal(1434304033241);
30};
31
32exports.data2 = function() {
33 let data = Data.create();
34 data.number = 2;
35 data.source = 'reddit';
36 data.item = 26;
37 data.values = [1, 2, 3, 4];
38 data.date = 1434304039234;
39 return data;
40};
41
42exports.validateData2 = function(d) {
43 expect(d.number).to.be.equal(2);
44 expect(d.source).to.be.equal('reddit');
45 expect(d.item).to.be.equal(26);
46 expect(d).to.have.property('values').with.length(4);
47 expect(d.date.valueOf()).to.be.equal(1434304039234);
48};
49
50// If we expect an error (and check for it in 'catch'), then
51// we end up catching the error thrown when calling expect.fail.
52// This means we'll actually catch the wrong error and give
53// a false positive.
54//
55// This is my dumb way of getting around that.
56const FailError = function(expected, actual, message) {
57 Error.call(this);
58 Error.captureStackTrace(this, FailError);
59 this.name = 'FailError';
60 this.expected = expected;
61 this.actual = actual;
62 this.message = message;
63};
64inherits(FailError, Error);
65
66exports.fail = function(expected, actual, message) {
67 throw new FailError(expected, actual, message);
68};
69
70exports.expectError = function(error) {
71 if (error instanceof FailError) {
72 expect.fail(error.expected, error.actual, error.message);
73 return;
74 }
75 expect(error instanceof Error).to.be.true;
76};
\No newline at end of file