|
69 | 69 | assert(e.message === "foo"); |
70 | 70 | assert(e instanceof ReferenceError); |
71 | 71 | } |
| 72 | + |
| 73 | +// Checking behavior when there are no arguments except "this" |
| 74 | +var a = "This is a sample text string to test this function"; |
| 75 | +assert(Array.prototype.lastIndexOf.call(a) == -1); |
| 76 | + |
| 77 | + |
| 78 | +// Checking behavior when value is null |
| 79 | +try { |
| 80 | + Array.prototype.lastIndexOf.call(null, "asd"); |
| 81 | + assert(false); |
| 82 | +} catch (e) { |
| 83 | + assert(e instanceof TypeError); |
| 84 | +} |
| 85 | + |
| 86 | +// Checking behavior when length is 0 |
| 87 | +assert(Array.prototype.lastIndexOf.call("", "chocolate cake") == -1); |
| 88 | + |
| 89 | +// Checking behavior when length is not a number |
| 90 | +try { |
| 91 | + var o = {}; |
| 92 | + Object.defineProperty(o, 'toString', { 'get' : function () { throw new ReferenceError ("foo"); } }); |
| 93 | + var a = { length : o }; |
| 94 | + Array.prototype.lastIndexOf.call(a, function () { }); |
| 95 | + assert(false); |
| 96 | +} catch (e) { |
| 97 | + assert(e instanceof ReferenceError); |
| 98 | + assert(e.message == "foo"); |
| 99 | +} |
| 100 | + |
| 101 | +// Checking behavior when the 3rd argument (start index) is not a number |
| 102 | +try { |
| 103 | + var o = {}; |
| 104 | + Object.defineProperty(o, 'toString', { 'get' : function () { throw new ReferenceError ("foo"); } }); |
| 105 | + Array.prototype.lastIndexOf.call("foo", "foo", o); |
| 106 | + assert(false); |
| 107 | +} catch (e) { |
| 108 | + assert(e instanceof ReferenceError); |
| 109 | + assert(e.message == "foo"); |
| 110 | +} |
| 111 | + |
| 112 | +// Checking behavior when the 3rd argument (start index) is greater than the length of the first argument |
| 113 | +assert(Array.prototype.lastIndexOf.call("foo", "foo", 999) == -1); |
| 114 | + |
| 115 | +// Checking behavior when the starting index is out of the range of the original array, so it points |
| 116 | +// to an empty space, as we modified the length of the array before |
| 117 | +var a = [1, 2, 3]; |
| 118 | +Object.defineProperty(a, "length", {value: 10}); |
| 119 | +assert(Array.prototype.lastIndexOf.call(a, "", 6) == -1); |
0 commit comments