Skip to content

Commit

Permalink
remove weak type-guards
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Schmidt committed Aug 8, 2018
1 parent eaeb550 commit 10e131b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
8 changes: 4 additions & 4 deletions detox/src/android/espressoapi/DetoxAssertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DetoxAssertion {
if (typeof m !== 'object' || typeof m.constructor !== 'function' || m.constructor.name.indexOf('Matcher') === -1) {
const isObject = typeof m === 'object';
const additionalErrorInfo = isObject ? typeof m.constructor === 'object' ? 'the constructor is no object' : 'it has a wrong class name: "' + m.constructor.name + '"' : 'it is no object';
throw new Error('m should be an instance of Matcher, got "' + m + '", it appears that ' + additionalErrorInfo);
console.error('m should be an instance of Matcher, got "' + m + '", it appears that ' + additionalErrorInfo);
}

return {
Expand Down Expand Up @@ -65,7 +65,7 @@ class DetoxAssertion {
if (typeof m !== 'object' || typeof m.constructor !== 'function' || m.constructor.name.indexOf('Matcher') === -1) {
const isObject = typeof m === 'object';
const additionalErrorInfo = isObject ? typeof m.constructor === 'object' ? 'the constructor is no object' : 'it has a wrong class name: "' + m.constructor.name + '"' : 'it is no object';
throw new Error('m should be an instance of Matcher, got "' + m + '", it appears that ' + additionalErrorInfo);
console.error('m should be an instance of Matcher, got "' + m + '", it appears that ' + additionalErrorInfo);
}

if (typeof timeoutSeconds !== "number") throw new Error("timeoutSeconds should be a number, but got " + (timeoutSeconds + (" (" + (typeof timeoutSeconds + ")"))));
Expand All @@ -92,13 +92,13 @@ class DetoxAssertion {
if (typeof m !== 'object' || typeof m.constructor !== 'function' || m.constructor.name.indexOf('Matcher') === -1) {
const isObject = typeof m === 'object';
const additionalErrorInfo = isObject ? typeof m.constructor === 'object' ? 'the constructor is no object' : 'it has a wrong class name: "' + m.constructor.name + '"' : 'it is no object';
throw new Error('m should be an instance of Matcher, got "' + m + '", it appears that ' + additionalErrorInfo);
console.error('m should be an instance of Matcher, got "' + m + '", it appears that ' + additionalErrorInfo);
}

if (typeof searchMatcher !== 'object' || typeof searchMatcher.constructor !== 'function' || searchMatcher.constructor.name.indexOf('Matcher') === -1) {
const isObject = typeof searchMatcher === 'object';
const additionalErrorInfo = isObject ? typeof searchMatcher.constructor === 'object' ? 'the constructor is no object' : 'it has a wrong class name: "' + searchMatcher.constructor.name + '"' : 'it is no object';
throw new Error('searchMatcher should be an instance of Matcher, got "' + searchMatcher + '", it appears that ' + additionalErrorInfo);
console.error('searchMatcher should be an instance of Matcher, got "' + searchMatcher + '", it appears that ' + additionalErrorInfo);
}

return {
Expand Down
20 changes: 10 additions & 10 deletions detox/src/android/espressoapi/DetoxMatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ class DetoxMatcher {
if (typeof m1 !== 'object' || typeof m1.constructor !== 'function' || m1.constructor.name.indexOf('Matcher') === -1) {
const isObject = typeof m1 === 'object';
const additionalErrorInfo = isObject ? typeof m1.constructor === 'object' ? 'the constructor is no object' : 'it has a wrong class name: "' + m1.constructor.name + '"' : 'it is no object';
throw new Error('m1 should be an instance of Matcher, got "' + m1 + '", it appears that ' + additionalErrorInfo);
console.error('m1 should be an instance of Matcher, got "' + m1 + '", it appears that ' + additionalErrorInfo);
}

if (typeof m2 !== 'object' || typeof m2.constructor !== 'function' || m2.constructor.name.indexOf('Matcher') === -1) {
const isObject = typeof m2 === 'object';
const additionalErrorInfo = isObject ? typeof m2.constructor === 'object' ? 'the constructor is no object' : 'it has a wrong class name: "' + m2.constructor.name + '"' : 'it is no object';
throw new Error('m2 should be an instance of Matcher, got "' + m2 + '", it appears that ' + additionalErrorInfo);
console.error('m2 should be an instance of Matcher, got "' + m2 + '", it appears that ' + additionalErrorInfo);
}

return {
Expand All @@ -79,13 +79,13 @@ class DetoxMatcher {
if (typeof m1 !== 'object' || typeof m1.constructor !== 'function' || m1.constructor.name.indexOf('Matcher') === -1) {
const isObject = typeof m1 === 'object';
const additionalErrorInfo = isObject ? typeof m1.constructor === 'object' ? 'the constructor is no object' : 'it has a wrong class name: "' + m1.constructor.name + '"' : 'it is no object';
throw new Error('m1 should be an instance of Matcher, got "' + m1 + '", it appears that ' + additionalErrorInfo);
console.error('m1 should be an instance of Matcher, got "' + m1 + '", it appears that ' + additionalErrorInfo);
}

if (typeof m2 !== 'object' || typeof m2.constructor !== 'function' || m2.constructor.name.indexOf('Matcher') === -1) {
const isObject = typeof m2 === 'object';
const additionalErrorInfo = isObject ? typeof m2.constructor === 'object' ? 'the constructor is no object' : 'it has a wrong class name: "' + m2.constructor.name + '"' : 'it is no object';
throw new Error('m2 should be an instance of Matcher, got "' + m2 + '", it appears that ' + additionalErrorInfo);
console.error('m2 should be an instance of Matcher, got "' + m2 + '", it appears that ' + additionalErrorInfo);
}

return {
Expand All @@ -108,7 +108,7 @@ class DetoxMatcher {
if (typeof m !== 'object' || typeof m.constructor !== 'function' || m.constructor.name.indexOf('Matcher') === -1) {
const isObject = typeof m === 'object';
const additionalErrorInfo = isObject ? typeof m.constructor === 'object' ? 'the constructor is no object' : 'it has a wrong class name: "' + m.constructor.name + '"' : 'it is no object';
throw new Error('m should be an instance of Matcher, got "' + m + '", it appears that ' + additionalErrorInfo);
console.error('m should be an instance of Matcher, got "' + m + '", it appears that ' + additionalErrorInfo);
}

return {
Expand All @@ -128,13 +128,13 @@ class DetoxMatcher {
if (typeof m !== 'object' || typeof m.constructor !== 'function' || m.constructor.name.indexOf('Matcher') === -1) {
const isObject = typeof m === 'object';
const additionalErrorInfo = isObject ? typeof m.constructor === 'object' ? 'the constructor is no object' : 'it has a wrong class name: "' + m.constructor.name + '"' : 'it is no object';
throw new Error('m should be an instance of Matcher, got "' + m + '", it appears that ' + additionalErrorInfo);
console.error('m should be an instance of Matcher, got "' + m + '", it appears that ' + additionalErrorInfo);
}

if (typeof ancestorMatcher !== 'object' || typeof ancestorMatcher.constructor !== 'function' || ancestorMatcher.constructor.name.indexOf('Matcher') === -1) {
const isObject = typeof ancestorMatcher === 'object';
const additionalErrorInfo = isObject ? typeof ancestorMatcher.constructor === 'object' ? 'the constructor is no object' : 'it has a wrong class name: "' + ancestorMatcher.constructor.name + '"' : 'it is no object';
throw new Error('ancestorMatcher should be an instance of Matcher, got "' + ancestorMatcher + '", it appears that ' + additionalErrorInfo);
console.error('ancestorMatcher should be an instance of Matcher, got "' + ancestorMatcher + '", it appears that ' + additionalErrorInfo);
}

return {
Expand All @@ -157,13 +157,13 @@ class DetoxMatcher {
if (typeof m !== 'object' || typeof m.constructor !== 'function' || m.constructor.name.indexOf('Matcher') === -1) {
const isObject = typeof m === 'object';
const additionalErrorInfo = isObject ? typeof m.constructor === 'object' ? 'the constructor is no object' : 'it has a wrong class name: "' + m.constructor.name + '"' : 'it is no object';
throw new Error('m should be an instance of Matcher, got "' + m + '", it appears that ' + additionalErrorInfo);
console.error('m should be an instance of Matcher, got "' + m + '", it appears that ' + additionalErrorInfo);
}

if (typeof descendantMatcher !== 'object' || typeof descendantMatcher.constructor !== 'function' || descendantMatcher.constructor.name.indexOf('Matcher') === -1) {
const isObject = typeof descendantMatcher === 'object';
const additionalErrorInfo = isObject ? typeof descendantMatcher.constructor === 'object' ? 'the constructor is no object' : 'it has a wrong class name: "' + descendantMatcher.constructor.name + '"' : 'it is no object';
throw new Error('descendantMatcher should be an instance of Matcher, got "' + descendantMatcher + '", it appears that ' + additionalErrorInfo);
console.error('descendantMatcher should be an instance of Matcher, got "' + descendantMatcher + '", it appears that ' + additionalErrorInfo);
}

return {
Expand Down Expand Up @@ -244,7 +244,7 @@ class DetoxMatcher {
if (typeof innerMatcher !== 'object' || typeof innerMatcher.constructor !== 'function' || innerMatcher.constructor.name.indexOf('Matcher') === -1) {
const isObject = typeof innerMatcher === 'object';
const additionalErrorInfo = isObject ? typeof innerMatcher.constructor === 'object' ? 'the constructor is no object' : 'it has a wrong class name: "' + innerMatcher.constructor.name + '"' : 'it is no object';
throw new Error('innerMatcher should be an instance of Matcher, got "' + innerMatcher + '", it appears that ' + additionalErrorInfo);
console.error('innerMatcher should be an instance of Matcher, got "' + innerMatcher + '", it appears that ' + additionalErrorInfo);
}

return {
Expand Down
2 changes: 1 addition & 1 deletion detox/src/android/espressoapi/ViewActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ class ViewActions {
if (typeof desiredStateMatcher !== 'object' || typeof desiredStateMatcher.constructor !== 'function' || desiredStateMatcher.constructor.name.indexOf('Matcher') === -1) {
const isObject = typeof desiredStateMatcher === 'object';
const additionalErrorInfo = isObject ? typeof desiredStateMatcher.constructor === 'object' ? 'the constructor is no object' : 'it has a wrong class name: "' + desiredStateMatcher.constructor.name + '"' : 'it is no object';
throw new Error('desiredStateMatcher should be an instance of Matcher, got "' + desiredStateMatcher + '", it appears that ' + additionalErrorInfo);
console.error('desiredStateMatcher should be an instance of Matcher, got "' + desiredStateMatcher + '", it appears that ' + additionalErrorInfo);
}

if (typeof maxAttempts !== "number") throw new Error("maxAttempts should be a number, but got " + (maxAttempts + (" (" + (typeof maxAttempts + ")"))));
Expand Down
4 changes: 0 additions & 4 deletions generation/__tests__/__snapshots__/android.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,3 @@ Object {
},
}
`;

exports[`Android generation validation Matcher<View> should fail getting no object 1`] = `"m1 should be an instance of Matcher, got \\"I am a string\\", it appears that it is no object"`;
exports[`Android generation validation Matcher<View> should fail with a wrong class 1`] = `"m1 should be an instance of Matcher, got \\"[object Object]\\", it appears that it has a wrong class name: \\"AnotherClass\\""`;
17 changes: 13 additions & 4 deletions generation/__tests__/android.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,28 @@ describe('Android generation', () => {

describe('validation', () => {
describe('Matcher<View>', () => {
it('should fail getting no object', () => {
it('should log that it gets no object', () => {
const spy = jest.spyOn(console, "error");
expect(() => {
ExampleClass.matcherForAnd('I am a string', 'I am one too');
}).toThrowErrorMatchingSnapshot();
}).not.toThrow();


expect(spy).toHaveBeenCalled();
spy.mockRestore();
});

it('should fail with a wrong class', () => {
it('should log with a wrong class', () => {
const spy = jest.spyOn(console, "error");
class AnotherClass {}
const x = new AnotherClass();

expect(() => {
ExampleClass.matcherForAnd(x, x);
}).toThrowErrorMatchingSnapshot();
}).not.toThrow();

expect(spy).toHaveBeenCalled();
spy.mockRestore();
});

it("should succeed with the 'right' class", () => {
Expand Down
2 changes: 1 addition & 1 deletion generation/core/type-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function isOfClass(className) {
const isObject = typeof ARG === 'object';
const additionalErrorInfo = isObject ? (typeof ARG.constructor === 'object' ? 'the constructor is no object' : 'it has a wrong class name: "' + ARG.constructor.name +'"') : 'it is no object';
throw new Error('${name} should be an instance of ${className}, got "' + ARG + '", it appears that ' + additionalErrorInfo);
console.error('${name} should be an instance of ${className}, got "' + ARG + '", it appears that ' + additionalErrorInfo);
}
`)({
ARG: t.identifier(name)
Expand Down

0 comments on commit 10e131b

Please sign in to comment.