1 | 'use strict';
|
2 |
|
3 | var should = require('chai').should();
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var $ = require('../../lib/index').Preconditions
|
11 | var PrivateKey = require('../../lib/index').PrivateKey
|
12 |
|
13 | describe('preconditions', function() {
|
14 |
|
15 | it('can be used to assert state', function() {
|
16 | (function() {
|
17 | $.checkState(false, 'testing');
|
18 | }).should.throw('Invalid state');
|
19 | });
|
20 | it('throws no false negative', function() {
|
21 | (function() {
|
22 | $.checkState(true, 'testing');
|
23 | }).should.not.throw();
|
24 | });
|
25 |
|
26 | it('can be used to check an argument', function() {
|
27 | (function() {
|
28 | $.checkArgument(false, 'testing');
|
29 | }).should.throw('Invalid Argument');
|
30 |
|
31 | (function() {
|
32 | $.checkArgument(true, 'testing');
|
33 | }).should.not.throw('Invalid Argument');
|
34 | });
|
35 |
|
36 | it('can be used to check an argument type', function() {
|
37 | var error;
|
38 | try {
|
39 | $.checkArgumentType(1, 'string', 'argumentName');
|
40 | } catch (e) {
|
41 | error = e;
|
42 | e.message.should.equal('Invalid Argument for argumentName, expected string but got number');
|
43 | }
|
44 | should.exist(error);
|
45 | });
|
46 | it('has no false negatives when used to check an argument type', function() {
|
47 | (function() {
|
48 | $.checkArgumentType('a String', 'string', 'argumentName');
|
49 | }).should.not.throw();
|
50 | });
|
51 |
|
52 | it('can be used to check an argument type for a class', function() {
|
53 | var error;
|
54 | try {
|
55 | $.checkArgumentType(1, PrivateKey);
|
56 | } catch (e) {
|
57 | error = e;
|
58 | var fail = !(~e.message.indexOf('Invalid Argument for (unknown name)'));
|
59 | fail.should.equal(false);
|
60 | }
|
61 | should.exist(error);
|
62 | });
|
63 | it('has no false negatives when checking a type for a class', function() {
|
64 | (function() {
|
65 | $.checkArgumentType(new PrivateKey(), PrivateKey);
|
66 | }).should.not.throw();
|
67 | });
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | it('formats correctly a message on checkArgument', function() {
|
75 | var error;
|
76 | try {
|
77 | $.checkArgument(null, 'parameter must be provided');
|
78 | } catch (e) {
|
79 | error = e;
|
80 | }
|
81 | error.message.should.equal('Invalid Argument: parameter must be provided');
|
82 | });
|
83 | });
|