Skip to content

Commit

Permalink
add tests on nullish coalescing
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Nov 12, 2020
1 parent fa71477 commit 1dce17e
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 0 deletions.
Expand Up @@ -70,6 +70,13 @@ class C {
return o?.#a.b?.c.d && o?.#a?.b.c.d;
}

static testNullishCoalescing(o) {
if (o?.#a.b?.c.non_existent ?? o?.#a.b?.c.d) {
return o?.#a.b?.c.non_existent ?? o?.#a.b?.c.d;
}
return o?.#a.b?.c.non_existent ?? o;
}

static test() {
const c = C;
expect(C.testIf(c)).toBe(true);
Expand All @@ -84,6 +91,8 @@ class C {

expect(C.testLogicalInIf(c)).toBe(true);
expect(C.testLogicalInReturn(c)).toBe(2);

expect(C.testNullishCoalescing(c)).toBe(2);
}

static testNullish() {
Expand All @@ -100,6 +109,8 @@ class C {

expect(C.testLogicalInIf(n)).toBe(false);
expect(C.testLogicalInReturn(n)).toBe(undefined);

expect(C.testNullishCoalescing(n)).toBe(n);
}
}
}
Expand Down
Expand Up @@ -69,6 +69,13 @@ class C {
static testLogicalInReturn(o) {
return o?.#a.b?.c.d && o?.#a?.b.c.d;
}

static testNullishCoalescing(o) {
if (o?.#a.b?.c.non_existent ?? o?.#a.b?.c.d) {
return o?.#a.b?.c.non_existent ?? o?.#a.b?.c.d;
}
return o?.#a.b?.c.non_existent ?? o;
}
}

C.test();
Expand Down
Expand Up @@ -93,6 +93,14 @@ class C {
return (o === null || o === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(o, _a)[_a].b)?.c.d && (o === null || o === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(o, _a)[_a])?.b.c.d;
}

static testNullishCoalescing(o) {
if ((o === null || o === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(o, _a)[_a].b)?.c.non_existent ?? (o === null || o === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(o, _a)[_a].b)?.c.d) {
return (o === null || o === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(o, _a)[_a].b)?.c.non_existent ?? (o === null || o === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(o, _a)[_a].b)?.c.d;
}

return (o === null || o === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(o, _a)[_a].b)?.c.non_existent ?? o;
}

}

Object.defineProperty(C, _a, {
Expand Down
Expand Up @@ -70,6 +70,13 @@ class C {
return o?.#a.b?.c.d && o?.#a?.b.c.d;
}

static testNullishCoalescing(o) {
if (o?.#a.b?.c.non_existent ?? o?.#a.b?.c.d) {
return o?.#a.b?.c.non_existent ?? o?.#a.b?.c.d;
}
return o?.#a.b?.c.non_existent ?? o;
}

static test() {
const c = C;
expect(C.testIf(c)).toBe(true);
Expand All @@ -84,6 +91,8 @@ class C {

expect(C.testLogicalInIf(c)).toBe(true);
expect(C.testLogicalInReturn(c)).toBe(2);

expect(C.testNullishCoalescing(c)).toBe(2);
}

static testNullish() {
Expand All @@ -100,6 +109,8 @@ class C {

expect(C.testLogicalInIf(n)).toBe(false);
expect(C.testLogicalInReturn(n)).toBe(undefined);

expect(C.testNullishCoalescing(n)).toBe(n);
}
}
}
Expand Down
Expand Up @@ -69,6 +69,13 @@ class C {
static testLogicalInReturn(o) {
return o?.#a.b?.c.d && o?.#a?.b.c.d;
}

static testNullishCoalescing(o) {
if (o?.#a.b?.c.non_existent ?? o?.#a.b?.c.d) {
return o?.#a.b?.c.non_existent ?? o?.#a.b?.c.d;
}
return o?.#a.b?.c.non_existent ?? o;
}
}

C.test();
Expand Down
Expand Up @@ -91,6 +91,14 @@ class C {
return (o === null || o === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(o, C, _a).b)?.c.d && (o === null || o === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(o, C, _a))?.b.c.d;
}

static testNullishCoalescing(o) {
if ((o === null || o === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(o, C, _a).b)?.c.non_existent ?? (o === null || o === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(o, C, _a).b)?.c.d) {
return (o === null || o === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(o, C, _a).b)?.c.non_existent ?? (o === null || o === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(o, C, _a).b)?.c.d;
}

return (o === null || o === void 0 ? void 0 : babelHelpers.classStaticPrivateFieldSpecGet(o, C, _a).b)?.c.non_existent ?? o;
}

}

var _a = {
Expand Down
Expand Up @@ -63,6 +63,13 @@ class C {
return o?.a.b?.c.d && o?.a?.b.c.d;
}

static testNullishCoalescing(o) {
if (o?.a.b?.c.non_existent ?? o?.a.b?.c.d) {
return o?.a.b?.c.non_existent ?? o?.a.b?.c.d;
}
return o?.a.b?.c.non_existent ?? o;
}

static test() {
const c = {
a: {
Expand All @@ -85,6 +92,8 @@ class C {

expect(C.testLogicalInIf(c)).toBe(true);
expect(C.testLogicalInReturn(c)).toBe(2);

expect(C.testNullishCoalescing(c)).toBe(2);
}

static testNullish() {
Expand All @@ -101,6 +110,8 @@ class C {

expect(C.testLogicalInIf(n)).toBe(false);
expect(C.testLogicalInReturn(n)).toBe(undefined);

expect(C.testNullishCoalescing(n)).toBe(n);
}
}
}
Expand Down
Expand Up @@ -62,6 +62,13 @@ class C {
static testLogicalInReturn(o) {
return o?.a.b?.c.d && o?.a?.b.c.d;
}

static testNullishCoalescing(o) {
if (o?.a.b?.c.non_existent ?? o?.a.b?.c.d) {
return o?.a.b?.c.non_existent ?? o?.a.b?.c.d;
}
return o?.a.b?.c.non_existent ?? o;
}
}

C.test();
Expand Down
Expand Up @@ -103,6 +103,18 @@ class C {
return (o === null || o === void 0 ? void 0 : (_o$a$b5 = o.a.b) === null || _o$a$b5 === void 0 ? void 0 : _o$a$b5.c.d) && (o === null || o === void 0 ? void 0 : (_o$a2 = o.a) === null || _o$a2 === void 0 ? void 0 : _o$a2.b.c.d);
}

static testNullishCoalescing(o) {
var _o$a$b6, _o$a$b7, _o$a$b10;

if ((o === null || o === void 0 ? void 0 : (_o$a$b6 = o.a.b) === null || _o$a$b6 === void 0 ? void 0 : _o$a$b6.c.non_existent) ?? (o === null || o === void 0 ? void 0 : (_o$a$b7 = o.a.b) === null || _o$a$b7 === void 0 ? void 0 : _o$a$b7.c.d)) {
var _o$a$b8, _o$a$b9;

return (o === null || o === void 0 ? void 0 : (_o$a$b8 = o.a.b) === null || _o$a$b8 === void 0 ? void 0 : _o$a$b8.c.non_existent) ?? (o === null || o === void 0 ? void 0 : (_o$a$b9 = o.a.b) === null || _o$a$b9 === void 0 ? void 0 : _o$a$b9.c.d);
}

return (o === null || o === void 0 ? void 0 : (_o$a$b10 = o.a.b) === null || _o$a$b10 === void 0 ? void 0 : _o$a$b10.c.non_existent) ?? o;
}

}

C.test();
Expand Down
Expand Up @@ -63,6 +63,13 @@ class C {
return o?.a.b?.c.d && o?.a?.b.c.d;
}

static testNullishCoalescing(o) {
if (o?.a.b?.c.non_existent ?? o?.a.b?.c.d) {
return o?.a.b?.c.non_existent ?? o?.a.b?.c.d;
}
return o?.a.b?.c.non_existent ?? o;
}

static test() {
const c = {
a: {
Expand All @@ -85,6 +92,8 @@ class C {

expect(C.testLogicalInIf(c)).toBe(true);
expect(C.testLogicalInReturn(c)).toBe(2);

expect(C.testNullishCoalescing(c)).toBe(2);
}

static testNullish() {
Expand All @@ -101,6 +110,8 @@ class C {

expect(C.testLogicalInIf(n)).toBe(false);
expect(C.testLogicalInReturn(n)).toBe(undefined);

expect(C.testNullishCoalescing(n)).toBe(n);
}
}
}
Expand Down
Expand Up @@ -62,6 +62,13 @@ class C {
static testLogicalInReturn(o) {
return o?.a.b?.c.d && o?.a?.b.c.d;
}

static testNullishCoalescing(o) {
if (o?.a.b?.c.non_existent ?? o?.a.b?.c.d) {
return o?.a.b?.c.non_existent ?? o?.a.b?.c.d;
}
return o?.a.b?.c.non_existent ?? o;
}
}

C.test();
Expand Down
Expand Up @@ -103,6 +103,18 @@ class C {
return (o == null ? void 0 : (_o$a$b5 = o.a.b) == null ? void 0 : _o$a$b5.c.d) && (o == null ? void 0 : (_o$a2 = o.a) == null ? void 0 : _o$a2.b.c.d);
}

static testNullishCoalescing(o) {
var _o$a$b6, _o$a$b7, _o$a$b10;

if ((o == null ? void 0 : (_o$a$b6 = o.a.b) == null ? void 0 : _o$a$b6.c.non_existent) ?? (o == null ? void 0 : (_o$a$b7 = o.a.b) == null ? void 0 : _o$a$b7.c.d)) {
var _o$a$b8, _o$a$b9;

return (o == null ? void 0 : (_o$a$b8 = o.a.b) == null ? void 0 : _o$a$b8.c.non_existent) ?? (o == null ? void 0 : (_o$a$b9 = o.a.b) == null ? void 0 : _o$a$b9.c.d);
}

return (o == null ? void 0 : (_o$a$b10 = o.a.b) == null ? void 0 : _o$a$b10.c.non_existent) ?? o;
}

}

C.test();
Expand Down

0 comments on commit 1dce17e

Please sign in to comment.