1 | var Hoek = require('hoek');
|
2 | var Lab = require('lab');
|
3 | var Purdy = require('../');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var expect = Lab.expect;
|
9 | var describe = Lab.experiment;
|
10 | var it = Lab.test;
|
11 |
|
12 |
|
13 | describe('Purdy', function () {
|
14 |
|
15 | it('should indent array correctly', function (done) {
|
16 |
|
17 | var out = Purdy.stringify([1, 2, 1, 1, 1, 1, 12, [1, 2]]);
|
18 | expect(out).to.equal('[\n [\u001b[1m\u001b[37m0\u001b[39m\u001b[22m] \u001b[1m\u001b[34m1\u001b[39m\u001b[22m,\n [\u001b[1m\u001b[37m1\u001b[39m\u001b[22m] \u001b[1m\u001b[34m2\u001b[39m\u001b[22m,\n [\u001b[1m\u001b[37m2\u001b[39m\u001b[22m] \u001b[1m\u001b[34m1\u001b[39m\u001b[22m,\n [\u001b[1m\u001b[37m3\u001b[39m\u001b[22m] \u001b[1m\u001b[34m1\u001b[39m\u001b[22m,\n [\u001b[1m\u001b[37m4\u001b[39m\u001b[22m] \u001b[1m\u001b[34m1\u001b[39m\u001b[22m,\n [\u001b[1m\u001b[37m5\u001b[39m\u001b[22m] \u001b[1m\u001b[34m1\u001b[39m\u001b[22m,\n [\u001b[1m\u001b[37m6\u001b[39m\u001b[22m] \u001b[1m\u001b[34m12\u001b[39m\u001b[22m,\n [\u001b[1m\u001b[37m7\u001b[39m\u001b[22m] [\n [\u001b[1m\u001b[37m0\u001b[39m\u001b[22m] \u001b[1m\u001b[34m1\u001b[39m\u001b[22m,\n [\u001b[1m\u001b[37m1\u001b[39m\u001b[22m] \u001b[1m\u001b[34m2\u001b[39m\u001b[22m\n ]\n]');
|
19 | done();
|
20 | });
|
21 |
|
22 | it('should print plain', function (done) {
|
23 |
|
24 | var out = Purdy.stringify('plain', { plain: true });
|
25 | expect(out).to.equal('\'plain\'');
|
26 | done();
|
27 | });
|
28 |
|
29 | it('should handle circular references', function (done) {
|
30 | var circularObj = { };
|
31 | circularObj.a = circularObj;
|
32 | var circ = [];
|
33 | circ.push(circ);
|
34 | var out = Purdy.stringify(circularObj);
|
35 | expect(out).to.equal('{\n \u001b[1m\u001b[37m a\u001b[39m\u001b[22m: \u001b[1m\u001b[90m[Circular~]\u001b[39m\u001b[22m\n}');
|
36 | out = Purdy.stringify(circ);
|
37 | expect(out).to.equal('[\n [\u001b[1m\u001b[37m0\u001b[39m\u001b[22m] \u001b[1m\u001b[90m[Circular~]\u001b[39m\u001b[22m\n]');
|
38 | done();
|
39 | });
|
40 |
|
41 | it('should print a function', function (done) {
|
42 |
|
43 | var out = Purdy.stringify(Array.isArray);
|
44 | expect(out).to.equal('\u001b[36m\u001b[36m[Function: isArray]\u001b[39m\u001b[39m');
|
45 | done();
|
46 | });
|
47 |
|
48 | it('should print an anonymous function', function (done) {
|
49 |
|
50 | var out = Purdy.stringify(it);
|
51 | expect(out).to.equal('\u001b[36m\u001b[36m[Function: ?]\u001b[39m\u001b[39m');
|
52 | done();
|
53 | });
|
54 |
|
55 |
|
56 | it('should print a string', function (done) {
|
57 |
|
58 | var out = Purdy.stringify('hello purdy');
|
59 | expect(out).to.equal('\u001b[33m\'hello purdy\'\u001b[39m');
|
60 | done();
|
61 | });
|
62 |
|
63 | it('should print a date', function (done) {
|
64 |
|
65 | var out = Purdy.stringify(new Date(2014, 5, 6, 21, 22, 21));
|
66 | expect(out).to.equal('\u001b[32mFri Jun 06 2014 21:22:21 GMT-0700 (PDT)\u001b[39m');
|
67 | done();
|
68 | });
|
69 |
|
70 | it('should print a number', function (done) {
|
71 |
|
72 | var out = Purdy.stringify(123);
|
73 | expect(out).to.equal('\u001b[1m\u001b[34m123\u001b[39m\u001b[22m');
|
74 | done();
|
75 | });
|
76 |
|
77 | it('should print null', function (done) {
|
78 |
|
79 | var out = Purdy.stringify(null);
|
80 | expect(out).to.equal('\u001b[1m\u001b[31mnull\u001b[39m\u001b[22m');
|
81 | done();
|
82 | });
|
83 |
|
84 | it('should print undefined', function (done) {
|
85 |
|
86 | var out = Purdy.stringify(undefined);
|
87 | expect(out).to.equal('\u001b[7m\u001b[31mundefined\u001b[39m\u001b[27m');
|
88 | done();
|
89 | });
|
90 |
|
91 | it('should print a regular expression', function (done) {
|
92 |
|
93 | var out = Purdy.stringify(new RegExp());
|
94 | expect(out).to.equal('\u001b[35m/(?:)/\u001b[39m');
|
95 | done();
|
96 | });
|
97 |
|
98 | it('should print a false boolean', function (done) {
|
99 |
|
100 | var out = Purdy.stringify(false);
|
101 | expect(out).to.equal('\u001b[1m\u001b[31mfalse\u001b[39m\u001b[22m');
|
102 | done();
|
103 | });
|
104 |
|
105 | it('should print a true boolean', function (done) {
|
106 |
|
107 | var out = Purdy.stringify(true);
|
108 | expect(out).to.equal('\u001b[1m\u001b[32mtrue\u001b[39m\u001b[22m');
|
109 | done();
|
110 | });
|
111 |
|
112 | it('should print an empty array without extra indentation', function (done) {
|
113 |
|
114 | var out = Purdy.stringify([]);
|
115 | expect(out).to.equal('[]');
|
116 | done();
|
117 | });
|
118 |
|
119 | it('should print an empty object without extra indentation', function (done) {
|
120 |
|
121 | var out = Purdy.stringify({});
|
122 | expect(out).to.equal('{}');
|
123 | done();
|
124 | });
|
125 |
|
126 | it('should print a more complex object', function (done) {
|
127 |
|
128 | var out = Purdy.stringify({
|
129 | array: [1, 2, [1, 2]],
|
130 | object: { another: 'string' }
|
131 | });
|
132 |
|
133 | expect(out).to.equal('{\n \u001b[1m\u001b[37m array\u001b[39m\u001b[22m: [\n [\u001b[1m\u001b[37m0\u001b[39m\u001b[22m] \u001b[1m\u001b[34m1\u001b[39m\u001b[22m,\n [\u001b[1m\u001b[37m1\u001b[39m\u001b[22m] \u001b[1m\u001b[34m2\u001b[39m\u001b[22m,\n [\u001b[1m\u001b[37m2\u001b[39m\u001b[22m] [\n [\u001b[1m\u001b[37m0\u001b[39m\u001b[22m] \u001b[1m\u001b[34m1\u001b[39m\u001b[22m,\n [\u001b[1m\u001b[37m1\u001b[39m\u001b[22m] \u001b[1m\u001b[34m2\u001b[39m\u001b[22m\n ]\n ],\n \u001b[1m\u001b[37mobject\u001b[39m\u001b[22m: {\n \u001b[1m\u001b[37manother\u001b[39m\u001b[22m: \u001b[33m\'string\'\u001b[39m\n }\n}');
|
134 | done();
|
135 | });
|
136 |
|
137 | it('should not print circular after second use', function (done) {
|
138 |
|
139 | var obj = { a: 3 };
|
140 | Purdy.stringify(obj);
|
141 | var out = Purdy.stringify(obj, { plain: true });
|
142 | expect(out).to.not.equal('[Circular]');
|
143 | done();
|
144 | });
|
145 |
|
146 | it('shows circular reference with path', function (done) {
|
147 |
|
148 | var repeatObj = {
|
149 | text: 'repeating object'
|
150 | };
|
151 | var repeatArr = ['repeating array'];
|
152 |
|
153 | var obj = {
|
154 | a: { nested: repeatObj },
|
155 | b: { alsoNested: repeatObj },
|
156 | more: [repeatArr],
|
157 | array: [repeatArr]
|
158 |
|
159 | };
|
160 | obj.top = obj;
|
161 | Purdy.stringify(obj);
|
162 | var out = Purdy.stringify(obj, { plain: true });
|
163 | expect(out).to.equal("{\n a: {\n nested: {\n text: 'repeating object'\n }\n },\n b: {\n alsoNested: [Circular~ a.nested]\n },\n more: [\n [0] [\n [0] 'repeating array'\n ]\n ],\n array: [\n [0] [Circular~ more.0]\n ],\n top: [Circular~]\n}");
|
164 | done();
|
165 | });
|
166 |
|
167 | it('will keep a path for an object in Hoek format', function (done) {
|
168 |
|
169 | var obj = {
|
170 | travel: {
|
171 | down: {
|
172 | a: [{
|
173 | path: 'to get here'
|
174 | }]
|
175 | }
|
176 | }
|
177 | };
|
178 | var orig = Hoek.clone(obj);
|
179 |
|
180 | var out = Purdy.stringify(obj, { plain: false, path: true });
|
181 |
|
182 | expect(out).to.equal('{\n \u001b[1m\u001b[37mtravel\u001b[39m\u001b[22m: {\n \u001b[34m// \u001b[39m\u001b[34mtravel.down\u001b[39m\n \u001b[1m\u001b[37mdown\u001b[39m\u001b[22m: {\n \u001b[34m// \u001b[39m\u001b[34mtravel.down.a\u001b[39m\n \u001b[1m\u001b[37m a\u001b[39m\u001b[22m: [\n \u001b[34m// \u001b[39m\u001b[34mtravel.down.a.0\u001b[39m\n [\u001b[1m\u001b[37m0\u001b[39m\u001b[22m] {\n \u001b[34m// \u001b[39m\u001b[34mtravel.down.a.0.path\u001b[39m\n \u001b[1m\u001b[37mpath\u001b[39m\u001b[22m: \u001b[33m\'to get here\'\u001b[39m\n }\n ]\n }\n }\n}');
|
183 | expect(obj).to.deep.equal(orig);
|
184 | done();
|
185 | });
|
186 |
|
187 | });
|
188 |
|