qunitx 0.8.2 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,67 @@
1
+ const hasOwn = Object.prototype.hasOwnProperty
2
+
3
+ function _typeof(obj) {
4
+ "@babel/helpers - typeof";
5
+
6
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
7
+ return typeof obj;
8
+ } : function (obj) {
9
+ return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
10
+ }, _typeof(obj);
11
+ }
12
+
13
+ export function objectType(obj) {
14
+ if (typeof obj === 'undefined') {
15
+ return 'undefined';
16
+ }
17
+
18
+ // Consider: typeof null === object
19
+ if (obj === null) {
20
+ return 'null';
21
+ }
22
+ var match = toString.call(obj).match(/^\[object\s(.*)\]$/);
23
+ var type = match && match[1];
24
+ switch (type) {
25
+ case 'Number':
26
+ if (isNaN(obj)) {
27
+ return 'nan';
28
+ }
29
+ return 'number';
30
+ case 'String':
31
+ case 'Boolean':
32
+ case 'Array':
33
+ case 'Set':
34
+ case 'Map':
35
+ case 'Date':
36
+ case 'RegExp':
37
+ case 'Function':
38
+ case 'Symbol':
39
+ return type.toLowerCase();
40
+ default:
41
+ return _typeof(obj);
42
+ }
43
+ }
44
+
45
+ function is(type, obj) {
46
+ return objectType(obj) === type;
47
+ }
48
+
49
+ export function objectValues(obj) {
50
+ let allowArray = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
51
+ let vals = allowArray && is('array', obj) ? [] : {};
52
+
53
+ for (var key in obj) {
54
+ if (hasOwn.call(obj, key)) {
55
+ let val = obj[key];
56
+ vals[key] = val === Object(val) ? objectValues(val, allowArray) : val;
57
+ }
58
+ }
59
+
60
+ return vals;
61
+ }
62
+
1
63
  /**
64
+ *
2
65
  * Recursively clone an object into a plain object, taking only the
3
66
  * subset of own enumerable properties that exist a given model.
4
67
  *
@@ -19,11 +82,91 @@ export function objectValuesSubset(obj, model) {
19
82
  // This enables subsetting [20, 30] with {1: 30}.
20
83
  var subset = {};
21
84
  for (var key in model) {
22
- if (hasOwn$1.call(model, key) && hasOwn$1.call(obj, key)) {
85
+ if (hasOwn.call(model, key) && hasOwn.call(obj, key)) {
23
86
  subset[key] = objectValuesSubset(obj[key], model[key]);
24
87
  }
25
88
  }
26
89
  return subset;
27
90
  }
28
91
 
29
- export default { objectValuesSubset };
92
+ export function validateExpectedExceptionArgs(expected, message, assertionMethod) {
93
+ var expectedType = objectType(expected);
94
+
95
+ // 'expected' is optional unless doing string comparison
96
+ if (expectedType === 'string') {
97
+ if (message === undefined) {
98
+ message = expected;
99
+ expected = undefined;
100
+ return [expected, message];
101
+ } else {
102
+ throw new Error('assert.' + assertionMethod + ' does not accept a string value for the expected argument.\n' + 'Use a non-string object value (e.g. RegExp or validator function) ' + 'instead if necessary.');
103
+ }
104
+ }
105
+ var valid = !expected ||
106
+ // TODO: be more explicit here
107
+ expectedType === 'regexp' || expectedType === 'function' || expectedType === 'object';
108
+ if (!valid) {
109
+ throw new Error('Invalid expected value type (' + expectedType + ') ' + 'provided to assert.' + assertionMethod + '.');
110
+ }
111
+ return [expected, message];
112
+ }
113
+
114
+ export function validateException(actual, expected, message) {
115
+ var result = false;
116
+ var expectedType = objectType(expected);
117
+
118
+ // These branches should be exhaustive, based on validation done in validateExpectedException
119
+
120
+ // We don't want to validate
121
+ if (!expected) {
122
+ result = true;
123
+
124
+ // Expected is a regexp
125
+ } else if (expectedType === 'regexp') {
126
+ result = expected.test(errorString(actual));
127
+
128
+ // Log the string form of the regexp
129
+ expected = String(expected);
130
+
131
+ // Expected is a constructor, maybe an Error constructor.
132
+ // Note the extra check on its prototype - this is an implicit
133
+ // requirement of "instanceof", else it will throw a TypeError.
134
+ } else if (expectedType === 'function' && expected.prototype !== undefined && actual instanceof expected) {
135
+ result = true;
136
+
137
+ // Expected is an Error object
138
+ } else if (expectedType === 'object') {
139
+ result = actual instanceof expected.constructor && actual.name === expected.name && actual.message === expected.message;
140
+
141
+ // Log the string form of the Error object
142
+ expected = errorString(expected);
143
+
144
+ // Expected is a validation function which returns true if validation passed
145
+ } else if (expectedType === 'function') {
146
+ // protect against accidental semantics which could hard error in the test
147
+ try {
148
+ result = expected.call({}, actual) === true;
149
+ expected = null;
150
+ } catch (e) {
151
+ // assign the "expected" to a nice error string to communicate the local failure to the user
152
+ expected = errorString(e);
153
+ }
154
+ }
155
+ return [result, expected, message];
156
+ }
157
+
158
+ function errorString(error) {
159
+ // Use String() instead of toString() to handle non-object values like undefined or null.
160
+ var resultErrorString = String(error);
161
+
162
+ // If the error wasn't a subclass of Error but something like
163
+ // an object literal with name and message properties...
164
+ if (resultErrorString.slice(0, 7) === '[object') {
165
+ // Based on https://es5.github.io/#x15.11.4.4
166
+ return (error.name || 'Error') + (error.message ? ": ".concat(error.message) : '');
167
+ } else {
168
+ return resultErrorString;
169
+ }
170
+ }
171
+
172
+ export default { objectValues, objectValuesSubset, validateExpectedExceptionArgs, validateException };