|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +package org.mozilla.javascript.tests; |
| 6 | + |
| 7 | +import org.junit.Test; |
| 8 | +import org.mozilla.javascript.testutils.Utils; |
| 9 | + |
| 10 | +/** |
| 11 | + * Test for setting a property defined in the prototype chain. |
| 12 | + * |
| 13 | + * @author Ronald Brill |
| 14 | + */ |
| 15 | +public class PutPropertyTest { |
| 16 | + |
| 17 | + public void setPropKnownAtPrototypeObject() throws Exception { |
| 18 | + final String script = |
| 19 | + "var WithObjectPrototype = function(array) {\n" |
| 20 | + + " this.prop = array.length;\n" |
| 21 | + + " return this;\n" |
| 22 | + + "}\n" |
| 23 | + + "var nlp = WithObjectPrototype.prototype = { prop: 7 };\n" |
| 24 | + + "var test = new WithObjectPrototype(['abc']);\n" |
| 25 | + + "'' + nlp.prop + ' # ' + test.prop"; |
| 26 | + |
| 27 | + Utils.assertWithAllModes("7 # 1", script); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void setPropNotKnownAtPrototypeObject() throws Exception { |
| 32 | + final String script = |
| 33 | + "var WithObjectPrototype = function(array) {\n" |
| 34 | + + " this.prop = array.length;\n" |
| 35 | + + " return this;\n" |
| 36 | + + "}\n" |
| 37 | + + "var nlp = WithObjectPrototype.prototype = { length: 7 };\n" |
| 38 | + + "var test = new WithObjectPrototype(['abc']);\n" |
| 39 | + + "'' + nlp.prop + ' # ' + test.prop"; |
| 40 | + |
| 41 | + Utils.assertWithAllModes("undefined # 1", script); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void setLengthKnownAtPrototypeObject() throws Exception { |
| 46 | + final String script = |
| 47 | + "var WithObjectPrototype = function(array) {\n" |
| 48 | + + " this.length = array.length;\n" |
| 49 | + + " return this;\n" |
| 50 | + + "}\n" |
| 51 | + + "var nlp = WithObjectPrototype.prototype = { length: 7 };\n" |
| 52 | + + "var test = new WithObjectPrototype(['abc']);\n" |
| 53 | + + "'' + nlp.length + ' # ' + test.length"; |
| 54 | + |
| 55 | + Utils.assertWithAllModes("7 # 1", script); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void setLengthNotKnownAtPrototypeObject() throws Exception { |
| 60 | + final String script = |
| 61 | + "var WithObjectPrototype = function(array) {\n" |
| 62 | + + " this.length = array.length;\n" |
| 63 | + + " return this;\n" |
| 64 | + + "}\n" |
| 65 | + + "var nlp = WithObjectPrototype.prototype = { prop: 7 };\n" |
| 66 | + + "var test = new WithObjectPrototype(['abc']);\n" |
| 67 | + + "'' + nlp.length + ' # ' + test.length"; |
| 68 | + |
| 69 | + Utils.assertWithAllModes("undefined # 1", script); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void setLengthKnownAtPrototypeArray() throws Exception { |
| 74 | + final String script = |
| 75 | + "var WithArrayPrototype = function(array) {\n" |
| 76 | + + " this.length = array.length;\n" |
| 77 | + + " return this;\n" |
| 78 | + + "}\n" |
| 79 | + + "var nlp = WithArrayPrototype.prototype = [];\n" |
| 80 | + + "var test = new WithArrayPrototype(['abc']);\n" |
| 81 | + + "'' + nlp.length + ' # ' + test.length"; |
| 82 | + |
| 83 | + Utils.assertWithAllModes("0 # 1", script); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void setPropKnownAtPrototypePrototypeObject() throws Exception { |
| 88 | + final String script = |
| 89 | + "var WithObjectPrototype = function(array) {\n" |
| 90 | + + " this.prop = array.length;\n" |
| 91 | + + " return this;\n" |
| 92 | + + "}\n" |
| 93 | + + "var nlp = {}.prototype = { prop: 7 };\n" |
| 94 | + + "var nlpp = WithObjectPrototype.prototype = nlp;\n" |
| 95 | + + "var test = new WithObjectPrototype(['abc']);\n" |
| 96 | + + "'' + nlp.prop + ' # ' + nlpp.prop + ' # ' + test.prop"; |
| 97 | + |
| 98 | + Utils.assertWithAllModes("7 # 7 # 1", script); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void setPropNotKnownAtPrototypePrototypeObject() throws Exception { |
| 103 | + final String script = |
| 104 | + "var WithObjectPrototype = function(array) {\n" |
| 105 | + + " this.prop = array.length;\n" |
| 106 | + + " return this;\n" |
| 107 | + + "}\n" |
| 108 | + + "var nlp = {}.prototype = { length: 7 };\n" |
| 109 | + + "var nlpp = WithObjectPrototype.prototype = nlp;\n" |
| 110 | + + "var test = new WithObjectPrototype(['abc']);\n" |
| 111 | + + "'' + nlp.prop + ' # ' + nlpp.prop + ' # ' + test.prop"; |
| 112 | + |
| 113 | + Utils.assertWithAllModes("undefined # undefined # 1", script); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void setLengthKnownAtPrototypePrototypeObject() throws Exception { |
| 118 | + final String script = |
| 119 | + "var WithObjectPrototype = function(array) {\n" |
| 120 | + + " this.length = array.length;\n" |
| 121 | + + " return this;\n" |
| 122 | + + "}\n" |
| 123 | + + "var nlp = {}.prototype = { length: 7 };\n" |
| 124 | + + "var nlpp = WithObjectPrototype.prototype = nlp;\n" |
| 125 | + + "var test = new WithObjectPrototype(['abc']);\n" |
| 126 | + + "'' + nlp.length + ' # ' + nlpp.length + ' # ' + test.length"; |
| 127 | + |
| 128 | + Utils.assertWithAllModes("7 # 7 # 1", script); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void setLengthNotKnownAtPrototypePrototypeObject() throws Exception { |
| 133 | + final String script = |
| 134 | + "var WithObjectPrototype = function(array) {\n" |
| 135 | + + " this.length = array.length;\n" |
| 136 | + + " return this;\n" |
| 137 | + + "}\n" |
| 138 | + + "var nlp = {}.prototype = { prop: 7 };\n" |
| 139 | + + "var nlpp = WithObjectPrototype.prototype = nlp;\n" |
| 140 | + + "var test = new WithObjectPrototype(['abc']);\n" |
| 141 | + + "'' + nlp.length + ' # ' + nlpp.length + ' # ' + test.length"; |
| 142 | + |
| 143 | + Utils.assertWithAllModes("undefined # undefined # 1", script); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void setLengthKnownAtPrototypePrototypeArray() throws Exception { |
| 148 | + final String script = |
| 149 | + "var WithArrayPrototype = function(array) {\n" |
| 150 | + + " this.length = array.length;\n" |
| 151 | + + " return this;\n" |
| 152 | + + "}\n" |
| 153 | + + "var nlp = {}.prototype = [];\n" |
| 154 | + + "var nlpp = WithArrayPrototype.prototype = nlp;\n" |
| 155 | + + "var test = new WithArrayPrototype(['abc']);\n" |
| 156 | + + "'' + nlp.length + ' # ' + nlpp.length + ' # ' + test.length"; |
| 157 | + |
| 158 | + Utils.assertWithAllModes("0 # 0 # 1", script); |
| 159 | + } |
| 160 | +} |
0 commit comments