-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathstring_at.js
134 lines (121 loc) · 6.09 KB
/
string_at.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation and contributors. All rights reserved.
// Copyright (c) 2021 ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
const StringPrototype = String.prototype;
const at = StringPrototype.at;
const call = Function.call.bind(Function.call); // (fn: Function, thisArg: any, ...args: any[]) => any
const tests = [
{
name: "Existnce of String.prototype.at",
body: function () {
assert.isTrue(typeof at === "function", "String.prototype.at should be a function");
}
},
{
name: "String.prototype.at's descriptor",
body: function () {
const desc = Object.getOwnPropertyDescriptor(StringPrototype, "at");
assert.areEqual(desc.configurable, true, "String.prototype.at should be configurable");
assert.areEqual(desc.writable, true, "String.prototype.at should be writable");
assert.areEqual(desc.enumerable, false, "String.prototype.at should not be enumerable");
}
},
{
name: "Properties of String.prototype.at",
body: function () {
const nameDesc = Object.getOwnPropertyDescriptor(at, "name");
assert.areEqual(at.name, "at", 'String.prototype.at.name should be "at"');
assert.areEqual(nameDesc.configurable, true, "String.prototype.at.name should be configurable");
assert.areEqual(nameDesc.writable, false, "String.prototype.at.name should not be writable");
assert.areEqual(nameDesc.enumerable, false, "String.prototype.at.name should not be enumerable");
const lengthDesc = Object.getOwnPropertyDescriptor(at, "length");
assert.areEqual(at.length, 1, 'String.prototype.at.length should be 1');
assert.areEqual(lengthDesc.configurable, true, "String.prototype.at.length should be configurable");
assert.areEqual(lengthDesc.writable, false, "String.prototype.at.length should not be writable");
assert.areEqual(lengthDesc.enumerable, false, "String.prototype.at.length should not be enumerable");
}
},
{
name: "String.prototype.at called on invalid object",
body: function () {
// on object without toString() should throw
assert.throws(() => { call(at, Object.create(null)) }, TypeError);
assert.throws(() => { call(at, null) }, TypeError);
assert.throws(() => { call(at, undefined) }, TypeError);
}
},
{
name: "Indexation of string with non-numerical argument",
body: function () {
const string = "01";
assert.areEqual(string.at(false), "0", '"01".at(false) should be "0"');
assert.areEqual(string.at(null), "0", '"01".at(null) should be "0"');
assert.areEqual(string.at(undefined), "0", '"01".at(undefined) should be "0"');
assert.areEqual(string.at(""), "0", '"01".at("") should be "0"');
assert.areEqual(string.at(function () { }), "0", '"01".at(function() {}) should be "0"');
assert.areEqual(string.at([]), "0", '"01".at([]) should be "0"');
assert.areEqual(string.at(true), "1", '"01".at(true) should be "1"');
assert.areEqual(string.at("1"), "1", '"01".at("1") should be "1"');
}
},
{
name: "Indexation of string with negative numerical argument",
body: function () {
const string = "0\u26052";
assert.areEqual(string.at(0), "0", '"0\u26052".at(0) should be "0"');
assert.areEqual(string.at(-1), "2", '"0\u26052".at(-1) should be "2"');
assert.areEqual(string.at(-2), "\u2605", '"0\u26052".at(-2) should be "\u2605"');
}
},
{
name: "Indexation of string with positive numerical argument",
body: function () {
const string = "0\u26052";
assert.areEqual(string.at(0), "0", '"0\u26052".at(0) should be "0"');
assert.areEqual(string.at(1), "\u2605", '"0\u26052".at(-1) should be "\u2605"');
assert.areEqual(string.at(2), "2", '"0\u26052".at(-2) should be "2"');
}
},
{
name: "Out of bounds",
body: function () {
const string = "";
assert.areEqual(string.at(0), undefined, `"".at(0) should be undefined`);
assert.areEqual(string.at(-1), undefined, `"".at(-1) should be undefined`);
assert.areEqual(string.at(2), undefined, `"".at(2) should be undefined`);
}
},
{
name: "Argument ToInteger()",
body: function () {
let count = 0;
const index = {
valueOf() {
count++;
return 1;
}
};
const string = "123";
assert.areEqual(string.at(index), "2", 'result of string.at(index) should be "2"');
assert.areEqual(count, 1, 'The value of count should be 1');
}
},
{
name: "String.prototype.at called on to-stringable object",
body: function () {
const object = {
toString() {
return "Test";
}
}
assert.areEqual(call(at, object, 0), "T", 'call(at, object, 0) should be "T"');
assert.areEqual(call(at, object, -1), "t", 'call(at, object, -1) should be "t"');
assert.areEqual(call(at, object, 1), "e", 'call(at, object, 1) should be "e"');
assert.areEqual(call(at, object, 2), "s", 'call(at, object, 2) should be "T"');
}
}
];
testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });