From 341e31f7d381c9dd0e1aedacef5de37843a92af7 Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Wed, 18 Feb 2015 12:38:02 -0500 Subject: [PATCH] Import tests from Google V8 (Arrow Functions) These tests are derived from the following files within the Google V8 project: test/mjsunit/harmony/arrow-functions.js --- .../Arrow-Function_rules-for-prototype.js | 13 +++++++ .../Arrow-Function_semantics.js | 35 +++++++++++++++++++ .../Arrow-Function_syntax-variations.js | 14 ++++++++ 3 files changed, 62 insertions(+) create mode 100644 test/language/arrow-function/Arrow-Function_rules-for-prototype.js create mode 100644 test/language/arrow-function/Arrow-Function_semantics.js create mode 100644 test/language/arrow-function/Arrow-Function_syntax-variations.js diff --git a/test/language/arrow-function/Arrow-Function_rules-for-prototype.js b/test/language/arrow-function/Arrow-Function_rules-for-prototype.js new file mode 100644 index 0000000000..2339f3d663 --- /dev/null +++ b/test/language/arrow-function/Arrow-Function_rules-for-prototype.js @@ -0,0 +1,13 @@ +// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 14.2 +description: > + Arrow functions are like functions, except they throw when using the + "new" operator on them. +---*/ + +assert.sameValue(typeof (() => {}), "function"); +assert.sameValue(Object.getPrototypeOf(() => {}), Function.prototype); +assert.throws(TypeError, function() { new (() => {}); }); +assert.sameValue("prototype" in (() => {}), false); diff --git a/test/language/arrow-function/Arrow-Function_semantics.js b/test/language/arrow-function/Arrow-Function_semantics.js new file mode 100644 index 0000000000..60c776ecd2 --- /dev/null +++ b/test/language/arrow-function/Arrow-Function_semantics.js @@ -0,0 +1,35 @@ +// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 14.2 +description: > + Semantics associated with specific Arrow Function syntactic forms. +includes: [compareArray.js] +---*/ + +// Empty arrow function returns undefined +var empty = () => {}; +assert.sameValue(empty(), undefined); + +// Single parameter case needs no parentheses around parameter list +var identity = x => x; +assert.sameValue(identity(empty), empty); + +// No need for parentheses even for lower-precedence expression body +var square = x => x * x; +assert.sameValue(square(3), 9); + +// Parenthesize the body to return an object literal expression +var key_maker = val => ({key: val}); +assert.sameValue(key_maker(empty).key, empty); + +// Expression Body implicit return +var evens = [0, 2, 4, 6, 8]; +assert(compareArray(evens.map(v => v + 1), [1, 3, 5, 7, 9])); + +// Statement body needs braces, must use 'return' explicitly if not void +var fives = []; +[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].forEach(v => { + if (v % 5 === 0) fives.push(v); +}); +assert(compareArray(fives, [5, 10])); diff --git a/test/language/arrow-function/Arrow-Function_syntax-variations.js b/test/language/arrow-function/Arrow-Function_syntax-variations.js new file mode 100644 index 0000000000..816741a7be --- /dev/null +++ b/test/language/arrow-function/Arrow-Function_syntax-variations.js @@ -0,0 +1,14 @@ +// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 14.2 +description: > + Syntax variations of valid Arrow Functions +---*/ + +assert.sameValue((() => 1)(), 1); +assert.sameValue((a => a + 1)(1), 2); +assert.sameValue((() => { return 3; })(), 3); +assert.sameValue((a => { return a + 3; })(1), 4); +assert.sameValue(((a, b) => a + b)(1, 4), 5); +assert.sameValue(((a, b) => { return a + b; })(1, 5), 6);