From 2b666c91f5160af4e4b9c308ddeec81dea68148d Mon Sep 17 00:00:00 2001 From: Ariya Hidayat Date: Tue, 31 Dec 2019 01:17:35 -0800 Subject: [PATCH] Add some tests for ES2015 features (#15402) --- test/standards/javascript/arrow.js | 5 ++++ test/standards/javascript/class.js | 34 ++++++++++++++++++++++ test/standards/javascript/default.js | 5 ++++ test/standards/javascript/destructuring.js | 24 +++++++++++++++ test/standards/javascript/isarray.js | 8 +++++ test/standards/javascript/let.js | 8 +++++ test/standards/javascript/shorthand.js | 8 +++++ 7 files changed, 92 insertions(+) create mode 100644 test/standards/javascript/arrow.js create mode 100644 test/standards/javascript/class.js create mode 100644 test/standards/javascript/default.js create mode 100644 test/standards/javascript/destructuring.js create mode 100644 test/standards/javascript/isarray.js create mode 100644 test/standards/javascript/let.js create mode 100644 test/standards/javascript/shorthand.js diff --git a/test/standards/javascript/arrow.js b/test/standards/javascript/arrow.js new file mode 100644 index 000000000..d45b6aeca --- /dev/null +++ b/test/standards/javascript/arrow.js @@ -0,0 +1,5 @@ +test(function () { + var result = [2, 3, 5].map(x => x * x); + assert_equals(result.length, 3); + assert_equals(result.join(' '), '4 9 25'); +}, "ES2015 arrow function"); diff --git a/test/standards/javascript/class.js b/test/standards/javascript/class.js new file mode 100644 index 000000000..114babe82 --- /dev/null +++ b/test/standards/javascript/class.js @@ -0,0 +1,34 @@ +test(function () { + class Vehicle { }; + assert_type_of(Vehicle, 'function'); +}, "ES2015 class declaration"); + +test(function () { + class Vehicle { constructor(n) { this.name = n } }; + var v = new Vehicle('daily driver'); + assert_type_of(Vehicle, 'function'); + assert_type_of(Vehicle.constructor, 'function'); + assert_type_of(v, 'object') +}, "ES2015 class constructor"); + + +test(function () { + class Vehicle { + constructor(n) { this._name = n } + get name() { return this._name } + } + var v = new Vehicle('daily driver'); + assert_type_of(v.name, 'string') + assert_equals(v.name, 'daily driver'); +}, "ES2015 class getter"); + +test(function () { + class Vehicle { + get name() { return this._name } + set name(n) { this._name = n } + } + var v = new Vehicle(); + v.name = 'daily driver'; + assert_type_of(v.name, 'string') + assert_equals(v.name, 'daily driver'); +}, "ES2015 class setter"); diff --git a/test/standards/javascript/default.js b/test/standards/javascript/default.js new file mode 100644 index 000000000..70d8aa543 --- /dev/null +++ b/test/standards/javascript/default.js @@ -0,0 +1,5 @@ +test(function () { + function inc(x, step = 1) { return x + step } + assert_equals(inc(4), 5); + assert_equals(inc(4, 3), 7); +}, "ES2015 default parameter value"); diff --git a/test/standards/javascript/destructuring.js b/test/standards/javascript/destructuring.js new file mode 100644 index 000000000..46f072fee --- /dev/null +++ b/test/standards/javascript/destructuring.js @@ -0,0 +1,24 @@ +test(function () { + var o = { d: 14, m: 3 }; + var { d: foo, m: bar } = o; + assert_type_of(foo, 'number'); + assert_type_of(bar, 'number'); + assert_equals(foo, 14); + assert_equals(bar, 3); +}, "ES2015 object destructuring"); + +test(function () { + var d, m; + [d, m] = [14, 3]; + assert_type_of(d, 'number'); + assert_type_of(m, 'number'); + assert_equals(d, 14); + assert_equals(m, 3); +}, "ES2015 array destructuring"); + +test(function () { + var x = 14, y = 3; + [x, y] = [y, x]; + assert_equals(x, 3); + assert_equals(y, 14); +}, "ES2015 variable swap"); diff --git a/test/standards/javascript/isarray.js b/test/standards/javascript/isarray.js new file mode 100644 index 000000000..4312a3d92 --- /dev/null +++ b/test/standards/javascript/isarray.js @@ -0,0 +1,8 @@ +test(function () { + assert_type_of(Array.isArray, 'function'); + assert_is_true(Array.isArray([])); + assert_is_true(Array.isArray([1])); + assert_is_true(Array.isArray([1, 2])); + assert_is_false(Array.isArray({})); + assert_is_false(Array.isArray(null)); +}, "ES2015 Array.isArray"); diff --git a/test/standards/javascript/let.js b/test/standards/javascript/let.js new file mode 100644 index 000000000..2afb645eb --- /dev/null +++ b/test/standards/javascript/let.js @@ -0,0 +1,8 @@ +test(function () { + let x = 4; + if (true) { + let x = 7; + assert_equals(x, 7); + } + assert_equals(x, 4); +}, "ES2015 block-scope let"); diff --git a/test/standards/javascript/shorthand.js b/test/standards/javascript/shorthand.js new file mode 100644 index 000000000..a9fa5b227 --- /dev/null +++ b/test/standards/javascript/shorthand.js @@ -0,0 +1,8 @@ +test(function () { + var x = 14, y = 3; + var obj = { x, y }; + assert_type_of(obj.x, 'number'); + assert_type_of(obj.y, 'number'); + assert_equals(obj.x, 14); + assert_equals(obj.y, 3); +}, "ES2015 object literal property shorthand");