UNPKG

10.8 kBJavaScriptView Raw
1function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
3function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4
5function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
6
7function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
8
9function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
10
11/**
12 * This file contains helper methods to create expected snapshot structures
13 * of both instance and ES6 exports.
14 *
15 * The files are located here and not under /test or /tools so it's transpiled
16 * into ES5 code under /lib and can be used straight by node.js
17 */
18import assert from 'assert';
19import * as allIsFunctions from './is';
20import { create } from '../core/create';
21import { endsWith } from './string';
22export function validateBundle(expectedBundleStructure, bundle) {
23 var originalWarn = console.warn;
24
25 console.warn = function () {
26 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
27 args[_key] = arguments[_key];
28 }
29
30 if (args.join(' ').indexOf('is moved to') !== -1 && args.join(' ').indexOf('Please use the new location instead') !== -1) {
31 // Ignore warnings like:
32 // Warning: math.type.isNumber is moved to math.isNumber in v6.0.0. Please use the new location instead.
33 return;
34 }
35
36 originalWarn.apply(console, args);
37 };
38
39 try {
40 var issues = []; // see whether all expected functions and objects are there
41
42 traverse(expectedBundleStructure, function (expectedType, path) {
43 var actualValue = get(bundle, path);
44 var actualType = validateTypeOf(actualValue);
45 var message = actualType === 'undefined' ? 'Missing entry in bundle. ' + "Path: ".concat(JSON.stringify(path), ", expected type: ").concat(expectedType, ", actual type: ").concat(actualType) : 'Unexpected entry type in bundle. ' + "Path: ".concat(JSON.stringify(path), ", expected type: ").concat(expectedType, ", actual type: ").concat(actualType);
46
47 if (actualType !== expectedType) {
48 issues.push({
49 actualType: actualType,
50 expectedType: expectedType,
51 message: message
52 });
53 console.warn(message);
54 }
55 }); // see whether there are any functions or objects that shouldn't be there
56
57 traverse(bundle, function (actualValue, path) {
58 var actualType = validateTypeOf(actualValue);
59 var expectedType = get(expectedBundleStructure, path) || 'undefined'; // FIXME: ugly to have these special cases
60
61 if (path.join('.').indexOf('docs.') !== -1) {
62 // ignore the contents of docs
63 return;
64 }
65
66 if (path.join('.').indexOf('all.') !== -1) {
67 // ignore the contents of all dependencies
68 return;
69 }
70
71 var message = expectedType === 'undefined' ? 'Unknown entry in bundle. ' + 'Is there a new function added which is missing in this snapshot test? ' + "Path: ".concat(JSON.stringify(path), ", expected type: ").concat(expectedType, ", actual type: ").concat(actualType) : 'Unexpected entry type in bundle. ' + "Path: ".concat(JSON.stringify(path), ", expected type: ").concat(expectedType, ", actual type: ").concat(actualType);
72
73 if (actualType !== expectedType) {
74 issues.push({
75 actualType: actualType,
76 expectedType: expectedType,
77 message: message
78 });
79 console.warn(message);
80 }
81 }); // assert on the first issue (if any)
82
83 if (issues.length > 0) {
84 var _issues$ = issues[0],
85 actualType = _issues$.actualType,
86 expectedType = _issues$.expectedType,
87 message = _issues$.message;
88 console.warn("".concat(issues.length, " bundle issues found"));
89 assert.strictEqual(actualType, expectedType, message);
90 }
91 } finally {
92 console.warn = originalWarn;
93 }
94}
95/**
96 * Based on an object with factory functions, create the expected
97 * structures for ES6 export and a mathjs instance.
98 * @param {Object} factories
99 * @return {{expectedInstanceStructure: Object, expectedES6Structure: Object}}
100 */
101
102export function createSnapshotFromFactories(factories) {
103 var math = create(factories);
104 var allFactoryFunctions = {};
105 var allFunctionsConstantsClasses = {};
106 var allFunctionsConstants = {};
107 var allTransformFunctions = {};
108 var allDependencyCollections = {};
109 var allClasses = {};
110 var allNodeClasses = {};
111 Object.keys(factories).forEach(function (factoryName) {
112 var factory = factories[factoryName];
113 var name = factory.fn;
114 var isTransformFunction = factory.meta && factory.meta.isTransformFunction;
115 var isClass = !isLowerCase(name[0]) && validateTypeOf(math[name]) === 'Function';
116 var dependenciesName = factory.fn + (isTransformFunction ? 'Transform' : '') + 'Dependencies';
117 allFactoryFunctions[factoryName] = 'Function';
118 allFunctionsConstantsClasses[name] = validateTypeOf(math[name]);
119 allDependencyCollections[dependenciesName] = 'Object';
120
121 if (isTransformFunction) {
122 allTransformFunctions[name] = 'Function';
123 }
124
125 if (isClass) {
126 if (endsWith(name, 'Node')) {
127 allNodeClasses[name] = 'Function';
128 } else {
129 allClasses[name] = 'Function';
130 }
131 } else {
132 allFunctionsConstants[name] = validateTypeOf(math[name]);
133 }
134 });
135 var embeddedDocs = {};
136 Object.keys(factories).forEach(function (factoryName) {
137 var factory = factories[factoryName];
138 var name = factory.fn;
139
140 if (isLowerCase(factory.fn[0])) {
141 // ignore class names starting with upper case
142 embeddedDocs[name] = 'Object';
143 }
144 });
145 embeddedDocs = exclude(embeddedDocs, ['equalScalar', 'apply', 'addScalar', 'multiplyScalar', 'print', 'divideScalar', 'parse', 'compile', 'parser', 'chain', 'reviver', 'replacer']);
146 var allTypeChecks = {};
147 Object.keys(allIsFunctions).forEach(function (name) {
148 if (name.indexOf('is') === 0) {
149 allTypeChecks[name] = 'Function';
150 }
151 });
152 var allErrorClasses = {
153 ArgumentsError: 'Function',
154 DimensionError: 'Function',
155 IndexError: 'Function'
156 };
157
158 var expectedInstanceStructure = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, allFunctionsConstantsClasses), {}, {
159 on: 'Function',
160 off: 'Function',
161 once: 'Function',
162 emit: 'Function',
163 "import": 'Function',
164 config: 'Function',
165 create: 'Function',
166 factory: 'Function'
167 }, allTypeChecks), allErrorClasses), {}, {
168 expression: {
169 transform: _objectSpread({}, allTransformFunctions),
170 mathWithTransform: _objectSpread(_objectSpread({}, exclude(allFunctionsConstants, ['chain'])), {}, {
171 config: 'Function'
172 })
173 }
174 });
175
176 var expectedES6Structure = _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, exclude(allFunctionsConstantsClasses, ['E', 'false', 'Infinity', 'NaN', 'null', 'PI', 'true'])), {}, {
177 create: 'Function',
178 config: 'Function',
179 factory: 'Function',
180 _true: 'boolean',
181 _false: 'boolean',
182 _null: 'null',
183 _Infinity: 'number',
184 _NaN: 'number'
185 }, allTypeChecks), allErrorClasses), allDependencyCollections), allFactoryFunctions), {}, {
186 docs: embeddedDocs
187 });
188
189 return {
190 expectedInstanceStructure: expectedInstanceStructure,
191 expectedES6Structure: expectedES6Structure
192 };
193}
194export function validateTypeOf(x) {
195 if (x && x.type === 'Unit') {
196 return 'Unit';
197 }
198
199 if (x && x.type === 'Complex') {
200 return 'Complex';
201 }
202
203 if (Array.isArray(x)) {
204 return 'Array';
205 }
206
207 if (x === null) {
208 return 'null';
209 }
210
211 if (typeof x === 'function') {
212 return 'Function';
213 }
214
215 if (_typeof(x) === 'object') {
216 return 'Object';
217 }
218
219 return _typeof(x);
220}
221
222function traverse(obj) {
223 var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (value, path) {};
224 var path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
225
226 // FIXME: ugly to have these special cases
227 if (path.length > 0 && path[0].indexOf('Dependencies') !== -1) {
228 // special case for objects holding a collection of dependencies
229 callback(obj, path);
230 } else if (validateTypeOf(obj) === 'Array') {
231 obj.map(function (item, index) {
232 return traverse(item, callback, path.concat(index));
233 });
234 } else if (validateTypeOf(obj) === 'Object') {
235 Object.keys(obj).forEach(function (key) {
236 // FIXME: ugly to have these special cases
237 // ignore special case of deprecated docs
238 if (key === 'docs' && path.join('.') === 'expression') {
239 return;
240 }
241
242 traverse(obj[key], callback, path.concat(key));
243 });
244 } else {
245 callback(obj, path);
246 }
247}
248
249function get(object, path) {
250 var child = object;
251
252 for (var i = 0; i < path.length; i++) {
253 var key = path[i];
254 child = child ? child[key] : undefined;
255 }
256
257 return child;
258}
259/**
260 * Create a copy of the provided `object` and delete
261 * all properties listed in `excludedProperties`
262 * @param {Object} object
263 * @param {string[]} excludedProperties
264 * @return {Object}
265 */
266
267
268function exclude(object, excludedProperties) {
269 var strippedObject = _extends({}, object);
270
271 excludedProperties.forEach(function (excludedProperty) {
272 delete strippedObject[excludedProperty];
273 });
274 return strippedObject;
275}
276
277function isLowerCase(text) {
278 return typeof text === 'string' && text.toLowerCase() === text;
279}
\No newline at end of file