From 2715f312db3bdb00d0f75cad488f21df7fba328a Mon Sep 17 00:00:00 2001 From: Frank Yung-Fong Tang <41213225+FrankYFTang@users.noreply.github.com> Date: Tue, 8 Oct 2019 11:42:27 -0700 Subject: [PATCH] Add tests for ListFormat StringListFromIterable ( iterable ) (#2380) * Add tests for StringListFromIterable * add case throw TypeError * add test for formatToParts * add test for formatToParts --- .../prototype/format/iterable-invalid.js | 48 +++++++++++++++++++ .../ListFormat/prototype/format/iterable.js | 44 +++++++++++++++++ .../formatToParts/iterable-invalid.js | 48 +++++++++++++++++++ .../prototype/formatToParts/iterable.js | 44 +++++++++++++++++ 4 files changed, 184 insertions(+) create mode 100644 test/intl402/ListFormat/prototype/format/iterable-invalid.js create mode 100644 test/intl402/ListFormat/prototype/format/iterable.js create mode 100644 test/intl402/ListFormat/prototype/formatToParts/iterable-invalid.js create mode 100644 test/intl402/ListFormat/prototype/formatToParts/iterable.js diff --git a/test/intl402/ListFormat/prototype/format/iterable-invalid.js b/test/intl402/ListFormat/prototype/format/iterable-invalid.js new file mode 100644 index 0000000000..f14e9e66e1 --- /dev/null +++ b/test/intl402/ListFormat/prototype/format/iterable-invalid.js @@ -0,0 +1,48 @@ +// Copyright 2019 Google Inc. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-Intl.ListFormat.prototype.format +description: Checks the behavior of Abstract Operation StringListFromIterable + called by Intl.ListFormat.prototype.format(). +info: | + StringListFromIterable + 1. If iterable is undefined, then + a. Return a new empty List. + 2. Let iteratorRecord be ? GetIterator(iterable). + 3. Let list be a new empty List. + 4. Let next be true. + 5. Repeat, while next is not false + a. Set next to ? IteratorStep(iteratorRecord). + b. If next is not false, then + i. Let nextValue be ? IteratorValue(next). + ii. If Type(nextValue) is not String, then + 1. Let error be ThrowCompletion(a newly created TypeError object). + 2. Return ? IteratorClose(iteratorRecord, error). + iii. Append nextValue to the end of the List list. + 6. Return list. +features: [Intl.ListFormat] +---*/ + +let lf = new Intl.ListFormat(); +// Test the failure case. +let iterable_of_strings_and_number = { + [Symbol.iterator]() { + return this; + }, + count: 0, + next() { + this.count++; + if (this.count == 3) { + return {done:false, value: 3}; + } + if (this.count < 5) { + return {done: false, value: String(this.count)}; + } + return {done:true} + } +}; +assert.throws(TypeError, + ()=> {lf.format(iterable_of_strings_and_number)}, + "Iterable yielded 3 which is not a string"); +assert.sameValue(iterable_of_strings_and_number.count, 3); diff --git a/test/intl402/ListFormat/prototype/format/iterable.js b/test/intl402/ListFormat/prototype/format/iterable.js new file mode 100644 index 0000000000..a4dfcc5996 --- /dev/null +++ b/test/intl402/ListFormat/prototype/format/iterable.js @@ -0,0 +1,44 @@ +// Copyright 2019 Google Inc. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-Intl.ListFormat.prototype.format +description: Checks the behavior of Abstract Operation StringListFromIterable + called by Intl.ListFormat.prototype.format(). +info: | + StringListFromIterable + 1. If iterable is undefined, then + a. Return a new empty List. + 2. Let iteratorRecord be ? GetIterator(iterable). + 3. Let list be a new empty List. + 4. Let next be true. + 5. Repeat, while next is not false + a. Set next to ? IteratorStep(iteratorRecord). + b. If next is not false, then + i. Let nextValue be ? IteratorValue(next). + ii. If Type(nextValue) is not String, then + 1. Let error be ThrowCompletion(a newly created TypeError object). + 2. Return ? IteratorClose(iteratorRecord, error). + iii. Append nextValue to the end of the List list. + 6. Return list. +features: [Intl.ListFormat] +---*/ + +let lf = new Intl.ListFormat(); + +// Test the success case. +let iterable_of_strings = { + [Symbol.iterator]() { + return this; + }, + count: 0, + next() { + this.count++ + if (this.count < 4) { + return {done: false, value: String(this.count)}; + } + return {done:true} + } +}; +lf.format(iterable_of_strings); +assert.sameValue(iterable_of_strings.count, 4); diff --git a/test/intl402/ListFormat/prototype/formatToParts/iterable-invalid.js b/test/intl402/ListFormat/prototype/formatToParts/iterable-invalid.js new file mode 100644 index 0000000000..bab6ea84be --- /dev/null +++ b/test/intl402/ListFormat/prototype/formatToParts/iterable-invalid.js @@ -0,0 +1,48 @@ +// Copyright 2019 Google Inc. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-Intl.ListFormat.prototype.formatToParts +description: Checks the behavior of Abstract Operation StringListFromIterable + called by Intl.ListFormat.prototype.formatToParts(). +info: | + StringListFromIterable + 1. If iterable is undefined, then + a. Return a new empty List. + 2. Let iteratorRecord be ? GetIterator(iterable). + 3. Let list be a new empty List. + 4. Let next be true. + 5. Repeat, while next is not false + a. Set next to ? IteratorStep(iteratorRecord). + b. If next is not false, then + i. Let nextValue be ? IteratorValue(next). + ii. If Type(nextValue) is not String, then + 1. Let error be ThrowCompletion(a newly created TypeError object). + 2. Return ? IteratorClose(iteratorRecord, error). + iii. Append nextValue to the end of the List list. + 6. Return list. +features: [Intl.ListFormat] +---*/ + +let lf = new Intl.ListFormat(); +// Test the failure case. +let iterable_of_strings_and_number = { + [Symbol.iterator]() { + return this; + }, + count: 0, + next() { + this.count++; + if (this.count == 3) { + return {done:false, value: 3}; + } + if (this.count < 5) { + return {done: false, value: String(this.count)}; + } + return {done:true} + } +}; +assert.throws(TypeError, + ()=> {lf.formatToParts(iterable_of_strings_and_number)}, + "Iterable yielded 3 which is not a string"); +assert.sameValue(iterable_of_strings_and_number.count, 3); diff --git a/test/intl402/ListFormat/prototype/formatToParts/iterable.js b/test/intl402/ListFormat/prototype/formatToParts/iterable.js new file mode 100644 index 0000000000..842531a68c --- /dev/null +++ b/test/intl402/ListFormat/prototype/formatToParts/iterable.js @@ -0,0 +1,44 @@ +// Copyright 2019 Google Inc. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-Intl.ListFormat.prototype.formatToParts +description: Checks the behavior of Abstract Operation StringListFromIterable + called by Intl.ListFormat.prototype.formatToParts(). +info: | + StringListFromIterable + 1. If iterable is undefined, then + a. Return a new empty List. + 2. Let iteratorRecord be ? GetIterator(iterable). + 3. Let list be a new empty List. + 4. Let next be true. + 5. Repeat, while next is not false + a. Set next to ? IteratorStep(iteratorRecord). + b. If next is not false, then + i. Let nextValue be ? IteratorValue(next). + ii. If Type(nextValue) is not String, then + 1. Let error be ThrowCompletion(a newly created TypeError object). + 2. Return ? IteratorClose(iteratorRecord, error). + iii. Append nextValue to the end of the List list. + 6. Return list. +features: [Intl.ListFormat] +---*/ + +let lf = new Intl.ListFormat(); + +// Test the success case. +let iterable_of_strings = { + [Symbol.iterator]() { + return this; + }, + count: 0, + next() { + this.count++ + if (this.count < 4) { + return {done: false, value: String(this.count)}; + } + return {done:true} + } +}; +lf.formatToParts(iterable_of_strings); +assert.sameValue(iterable_of_strings.count, 4);