• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16// test1: normal trap.
17function test1()
18{
19  const validator = {
20    set(target, prop, value) {
21      if (prop === 'age' && (typeof value !== 'number' || value < 0)) {
22        throw new TypeError('Age must be a positive number');
23      }
24      target[prop] = value;
25      return true;
26    }
27  };
28  const user = new Proxy({}, validator);
29  try {
30    user.age  = -5; // TypeError
31  } catch(error) {
32    assert_equal(error instanceof TypeError, true);
33  }
34}
35test1();
36
37// test2: trap is undefined, set property.
38function test2()
39{
40  const target = { data: 'original' };
41  const proxy = new Proxy(target, {});
42
43  proxy.data  = 'new value';
44  assert_equal(target.data, "new value");
45}
46test2();
47
48// test3: trap returned false.
49function test3()
50{
51  const strictProxy = new Proxy({}, {
52    set(target, prop, value) {
53      target[prop] = value;
54      return false;
55    }
56  });
57  try {
58    strictProxy.test  = 1; // TypeError
59  } catch(error) {
60    assert_equal(error instanceof TypeError, true);
61  }
62}
63test3();
64
65// test4: trap is undefined, defineProperty.
66function test4()
67{
68  let v1 = {};
69  let v2 = new Proxy({}, v1);
70  v2.get = {};
71  assert_equal(v2.get instanceof Object, true);
72}
73test4();
74
75// test5: CheckSetTrapResult, check data.
76function test5()
77{
78  const target = {};
79  Object.defineProperty(target, 'id', {
80    value: 1,
81    writable: false,
82    configurable: false
83  });
84  const proxy = new Proxy(target, {
85    set(target, prop, value) {
86      return true;
87    }
88  });
89  try {
90    proxy.id = 2;
91  } catch (error) {
92    assert_equal(error instanceof TypeError, true); // true
93  }
94}
95test5();
96
97// test6: CheckSetTrapResult, check accessor.
98function test6()
99{
100  const target = {};
101  Object.defineProperty(target, 'id', {
102    get() {
103    },
104    configurable: false
105  });
106  const proxy = new Proxy(target, {
107    set(target, prop, value) {
108      return true;
109    }
110  });
111  try {
112    proxy.id = 2;
113  } catch (error) {
114    assert_equal(error instanceof TypeError, true); // true
115  }
116}
117test6();
118
119// test7: CheckSetTrapResult, check accessor, configurable is true.
120function test7()
121{
122  const target = {};
123  Object.defineProperty(target, 'id', {
124    get() {
125    },
126    configurable: true,
127  });
128  const proxy = new Proxy(target, {
129    set(target, prop, value) {
130      return true;
131    }
132  });
133  try {
134    proxy.id = 2;
135  } catch (error) {
136    assert_equal(error instanceof TypeError, false); // false
137  }
138}
139test7();
140
141// test8: CheckSetTrapResult, check internal accessor.
142function test8()
143{
144  const target = function f0(){};
145  Object.defineProperty(target, 'length', {
146    value: 0,
147    configurable: false,
148    writable: false,
149  });
150  const proxy = new Proxy(target, {
151    set(target, prop, value) {
152      return true;
153    }
154  });
155  try {
156    proxy.length = 1;
157  } catch (error) {
158    assert_equal(error instanceof TypeError, true); // true
159  }
160}
161test8();
162
163// test9: CheckSetTrapResult, check internal accessor, value is same.
164function test9()
165{
166  const target = function f0(){};
167  Object.defineProperty(target, 'length', {
168    value: true,
169    configurable: false,
170    writable: false,
171  });
172  const proxy = new Proxy(target, {
173    set(target, prop, value) {
174      return true;
175    }
176  });
177  try {
178    proxy.length = true;
179  } catch (error) {
180    print(error instanceof TypeError, false); // false
181  }
182}
183test9();
184
185// test10: CheckSetTrapResult, check internal accessor, configurable is true.
186function test10()
187{
188  const target = function f0(){};
189  Object.defineProperty(target, 'length', {
190    value: true,
191    configurable: true,
192  });
193  const proxy = new Proxy(target, {
194    set(target, prop, value) {
195      return true;
196    }
197  });
198  try {
199    proxy.length = 1;
200  } catch (error) {
201    assert_equal(error instanceof TypeError, false); // false
202  }
203}
204test10();
205
206test_end();