|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var tape = require('../'); |
| 4 | +var tap = require('tap'); |
| 5 | +var concat = require('concat-stream'); |
| 6 | +var inspect = require('object-inspect'); |
| 7 | +var forEach = require('for-each'); |
| 8 | +var v = require('es-value-fixtures'); |
| 9 | + |
| 10 | +var stripFullStack = require('./common').stripFullStack; |
| 11 | + |
| 12 | +tap.test('capture: output', function (tt) { |
| 13 | + tt.plan(1); |
| 14 | + |
| 15 | + var test = tape.createHarness(); |
| 16 | + var count = 0; |
| 17 | + test.createStream().pipe(concat(function (body) { |
| 18 | + tt.same(stripFullStack(body.toString('utf8')), [].concat( |
| 19 | + 'TAP version 13', |
| 20 | + '# argument validation', |
| 21 | + v.primitives.map(function (x) { |
| 22 | + return 'ok ' + ++count + ' ' + inspect(x) + ' is not an Object'; |
| 23 | + }), |
| 24 | + v.nonPropertyKeys.map(function (x) { |
| 25 | + return 'ok ' + ++count + ' ' + inspect(x) + ' is not a valid property key'; |
| 26 | + }), |
| 27 | + v.nonFunctions.filter(function (x) { return typeof x !== 'undefined'; }).map(function (x) { |
| 28 | + return 'ok ' + ++count + ' ' + inspect(x) + ' is not a function'; |
| 29 | + }), |
| 30 | + '# captures calls', |
| 31 | + 'ok ' + ++count + ' property has expected initial value', |
| 32 | + '# capturing', |
| 33 | + 'ok ' + ++count + ' throwing implementation throws', |
| 34 | + 'ok ' + ++count + ' should be equivalent', |
| 35 | + 'ok ' + ++count + ' should be equivalent', |
| 36 | + 'ok ' + ++count + ' should be equivalent', |
| 37 | + 'ok ' + ++count + ' should be equivalent', |
| 38 | + 'ok ' + ++count + ' should be equivalent', |
| 39 | + '# post-capturing', |
| 40 | + 'ok ' + ++count + ' property is restored', |
| 41 | + 'ok ' + ++count + ' added property is removed', |
| 42 | + '', |
| 43 | + '1..' + count, |
| 44 | + '# tests ' + count, |
| 45 | + '# pass ' + count, |
| 46 | + '', |
| 47 | + '# ok', |
| 48 | + '' |
| 49 | + )); |
| 50 | + })); |
| 51 | + |
| 52 | + test('argument validation', function (t) { |
| 53 | + forEach(v.primitives, function (primitive) { |
| 54 | + t.throws( |
| 55 | + function () { t.capture(primitive, ''); }, |
| 56 | + TypeError, |
| 57 | + inspect(primitive) + ' is not an Object' |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + forEach(v.nonPropertyKeys, function (nonPropertyKey) { |
| 62 | + t.throws( |
| 63 | + function () { t.capture({}, nonPropertyKey); }, |
| 64 | + TypeError, |
| 65 | + inspect(nonPropertyKey) + ' is not a valid property key' |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + forEach(v.nonFunctions, function (nonFunction) { |
| 70 | + if (typeof nonFunction !== 'undefined') { |
| 71 | + t.throws( |
| 72 | + function () { t.capture({}, '', nonFunction); }, |
| 73 | + TypeError, |
| 74 | + inspect(nonFunction) + ' is not a function' |
| 75 | + ); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + t.end(); |
| 80 | + }); |
| 81 | + |
| 82 | + test('captures calls', function (t) { |
| 83 | + var sentinel = { sentinel: true, inspect: function () { return '{ SENTINEL OBJECT }'; } }; |
| 84 | + var o = { foo: sentinel, inspect: function () { return '{ o OBJECT }'; } }; |
| 85 | + t.equal(o.foo, sentinel, 'property has expected initial value'); |
| 86 | + |
| 87 | + t.test('capturing', function (st) { |
| 88 | + var results = st.capture(o, 'foo', function () { return sentinel; }); |
| 89 | + var results2 = st.capture(o, 'foo2'); |
| 90 | + var up = new SyntaxError('foo'); |
| 91 | + var resultsThrow = st.capture(o, 'fooThrow', function () { throw up; }); |
| 92 | + |
| 93 | + o.foo(1, 2, 3); |
| 94 | + o.foo(3, 4, 5); |
| 95 | + o.foo2.call(sentinel, 1); |
| 96 | + st.throws( |
| 97 | + function () { o.fooThrow(1, 2, 3); }, |
| 98 | + SyntaxError, |
| 99 | + 'throwing implementation throws' |
| 100 | + ); |
| 101 | + |
| 102 | + st.deepEqual(results(), [ |
| 103 | + { args: [1, 2, 3], receiver: o, returned: sentinel }, |
| 104 | + { args: [3, 4, 5], receiver: o, returned: sentinel } |
| 105 | + ]); |
| 106 | + st.deepEqual(results(), []); |
| 107 | + |
| 108 | + o.foo(6, 7, 8); |
| 109 | + st.deepEqual(results(), [ |
| 110 | + { args: [6, 7, 8], receiver: o, returned: sentinel } |
| 111 | + ]); |
| 112 | + |
| 113 | + st.deepEqual(results2(), [ |
| 114 | + { args: [1], receiver: sentinel, returned: undefined } |
| 115 | + ]); |
| 116 | + st.deepEqual(resultsThrow(), [ |
| 117 | + { args: [1, 2, 3], receiver: o, threw: true } |
| 118 | + ]); |
| 119 | + |
| 120 | + st.end(); |
| 121 | + }); |
| 122 | + |
| 123 | + t.test('post-capturing', function (st) { |
| 124 | + st.equal(o.foo, sentinel, 'property is restored'); |
| 125 | + st.notOk('foo2' in o, 'added property is removed'); |
| 126 | + |
| 127 | + st.end(); |
| 128 | + }); |
| 129 | + |
| 130 | + t.end(); |
| 131 | + }); |
| 132 | +}); |
0 commit comments