|
| 1 | +var expect = require('chai').expect; |
| 2 | +var binary = require('node-pre-gyp'); |
| 3 | +var path = require('path'); |
| 4 | +var binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json'))); |
| 5 | +var NODE_NEXT = require('../tools/NODE_NEXT.js'); |
| 6 | + |
| 7 | +if (!NODE_NEXT) return; |
| 8 | + |
| 9 | +describe('binding', function() { |
| 10 | + var binding = require(binding_path); |
| 11 | + it('source was builded and can be accessed from script', function() { |
| 12 | + expect(binding.InjectedScriptHost).to.be.instanceof(Object); |
| 13 | + }); |
| 14 | + |
| 15 | + describe('InjectedScriptHost', function() { |
| 16 | + var host = binding.InjectedScriptHost; |
| 17 | + |
| 18 | + describe('function `subtype`', function() { |
| 19 | + checksTypeValid(new Array(), 'array'); |
| 20 | + checksTypeValid(new Date(), 'date'); |
| 21 | + checksTypeValid(new RegExp(), 'regexp'); |
| 22 | + checksTypeValid(new Error(), 'error'); |
| 23 | + checksTypeValid(new String(), undefined); |
| 24 | + |
| 25 | + function checksTypeValid(value, type) { |
| 26 | + it('checks ' + type + ' subtype', function() { |
| 27 | + expect(host.subtype(value)).to.equal(type); |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + it('should throw on wrong arguments', function() { |
| 32 | + expect(host.subtype).to.throw(); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('function `setNonEnumProperty`', function() { |
| 37 | + it('should set non enumerable property to object', function() { |
| 38 | + var object = { |
| 39 | + 'visibleProp': '1' |
| 40 | + }; |
| 41 | + host.setNonEnumProperty(object, 'hiddenProp', 'value'); |
| 42 | + var keys = Object.keys(object); |
| 43 | + expect(keys).to.deep.equal(['visibleProp']); |
| 44 | + expect(object.hiddenProp).to.equal('value'); |
| 45 | + }); |
| 46 | + |
| 47 | + throwsOnArgs([]); |
| 48 | + throwsOnArgs([{}, 'a']); |
| 49 | + throwsOnArgs([{}, null, 'b']); |
| 50 | + throwsOnArgs([null, {}, 'b']); |
| 51 | + |
| 52 | + function throwsOnArgs(argvList) { |
| 53 | + it('should throw on wrong arguments ' + JSON.stringify(argvList), function() { |
| 54 | + expect(host.setNonEnumProperty.bind.apply( |
| 55 | + host.setNonEnumProperty, [host].concat(argvList))).to.throw(); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + it('should not throw on valid arguments', function() { |
| 60 | + expect(host.setNonEnumProperty.bind(host, {}, 'a', null)).to.not.throw(); |
| 61 | + expect(host.setNonEnumProperty.bind(host, {}, 'a', 'b')).to.not.throw(); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('function `internalConstructorName`', function() { |
| 66 | + checksNameValid(new Number(), 'Number'); |
| 67 | + checksNameValid(new Object(), 'Object'); |
| 68 | + |
| 69 | + function checksNameValid(value, name) { |
| 70 | + it('checks new ' + name + '() constructor name', function() { |
| 71 | + expect(host.internalConstructorName(value)).to.equal(name); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + throwsOnArgs([]); |
| 76 | + throwsOnArgs([1]); |
| 77 | + throwsOnArgs([null]); |
| 78 | + |
| 79 | + function throwsOnArgs(argvList) { |
| 80 | + it('should throw on wrong arguments ' + JSON.stringify(argvList), function() { |
| 81 | + expect(host.internalConstructorName.bind.apply( |
| 82 | + host.internalConstructorName, [host].concat(argvList))).to.throw(); |
| 83 | + }); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + describe('function `functionDetailsWithoutScopes`', function() { |
| 88 | + it('should return valid details', function() { |
| 89 | + function example() {} |
| 90 | + |
| 91 | + var details = host.functionDetailsWithoutScopes(example); |
| 92 | + expect(details).to.include.keys(['location', 'functionName']); |
| 93 | + expect(details.location).to.include.keys(['lineNumber', 'columnNumber', 'scriptId']); |
| 94 | + }); |
| 95 | + |
| 96 | + throwsOnArgs([]); |
| 97 | + throwsOnArgs([null]); |
| 98 | + |
| 99 | + function throwsOnArgs(argvList) { |
| 100 | + it('should throw on wrong arguments ' + JSON.stringify(argvList), function() { |
| 101 | + expect(host.functionDetailsWithoutScopes.bind.apply( |
| 102 | + host.functionDetailsWithoutScopes, [host].concat(argvList))).to.throw(); |
| 103 | + }); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + describe('function `eval`', function() { |
| 108 | + it('should evaluate expression', function() { |
| 109 | + expect(host.eval("[1]")).to.deep.equal([1]); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should throw on wrong arguments', function() { |
| 113 | + expect(host.eval).to.throw(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should throw on wrong expression', function() { |
| 117 | + expect(host.eval.bind(null, "[1")).to.throw(SyntaxError); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('function `evaluateWithExceptionDetails`', function() { |
| 122 | + it('should evaluate expression', function() { |
| 123 | + expect(host.evaluateWithExceptionDetails("[1]")).to.deep.equal({ |
| 124 | + result: [1], |
| 125 | + exceptionDetails: undefined |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should throw on wrong arguments', function() { |
| 130 | + expect(host.evaluateWithExceptionDetails).to.throw(); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe('function `callFunction`', function() { |
| 135 | + it('should call function without args', function(done) { |
| 136 | + host.callFunction(done, this); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should call function with args', function(done) { |
| 140 | + host.callFunction(function(arg) { |
| 141 | + expect(arg).to.equal(1); |
| 142 | + done(); |
| 143 | + }, this, [1]); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should throw on wrong arguments', function() { |
| 147 | + expect(host.callFunction.bind(null, null, null, [1])).to.throw(); |
| 148 | + expect(host.callFunction.bind(null, null, null, 1)).to.throw(); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should rethrow ReferenceError', function() { |
| 152 | + expect(host.callFunction.bind(null, function() { |
| 153 | + 'use strict'; |
| 154 | + if (error_here) return; |
| 155 | + }, this)).to.throw(ReferenceError); |
| 156 | + }); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments