-
Notifications
You must be signed in to change notification settings - Fork 864
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a new, experimental JavaScript feature.
- Loading branch information
1 parent
743d21c
commit f7b16e9
Showing
3 changed files
with
187 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
testsrc/org/mozilla/javascript/tests/es2022/NativeObjectTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
/** Test for the Object.hasOwn */ | ||
package org.mozilla.javascript.tests.es2022; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mozilla.javascript.Context; | ||
import org.mozilla.javascript.ScriptableObject; | ||
|
||
public class NativeObjectTest { | ||
|
||
private Context cx; | ||
private ScriptableObject scope; | ||
|
||
@Before | ||
public void setUp() { | ||
cx = Context.enter(); | ||
cx.setLanguageVersion(Context.VERSION_ES6); | ||
scope = cx.initStandardObjects(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
Context.exit(); | ||
} | ||
|
||
@Test | ||
public void testHasStringOwn() { | ||
Object result = | ||
cx.evaluateString( | ||
scope, | ||
"let result = Object.hasOwn({ test: '123' }, 'test');\n" | ||
+ "'result = ' + result", | ||
"test", | ||
1, | ||
null); | ||
|
||
assertEquals("result = true", result); | ||
} | ||
|
||
@Test | ||
public void testHasUndefinedOwn() { | ||
Object result = | ||
cx.evaluateString( | ||
scope, | ||
"let result = Object.hasOwn({ test: undefined }, 'test');\n" | ||
+ "'result = ' + result;", | ||
"test", | ||
1, | ||
null); | ||
|
||
assertEquals("result = true", result); | ||
} | ||
|
||
@Test | ||
public void testHasNullOwn() { | ||
Object result = | ||
cx.evaluateString( | ||
scope, | ||
"let result = Object.hasOwn({ test: null }, 'test');\n" | ||
+ "'result = ' + result;", | ||
"test", | ||
1, | ||
null); | ||
|
||
assertEquals("result = true", result); | ||
} | ||
|
||
@Test | ||
public void testHasArrayPropertyOwn() { | ||
Object result = | ||
cx.evaluateString( | ||
scope, | ||
"let dessert = [\"cake\", \"coffee\", \"chocolate\"];\n" | ||
+ "let result = Object.hasOwn(dessert, 2);\n" | ||
+ "'result = ' + result;", | ||
"test", | ||
1, | ||
null); | ||
|
||
assertEquals("result = true", result); | ||
} | ||
|
||
@Test | ||
public void testHasNoOwn() { | ||
Object result = | ||
cx.evaluateString( | ||
scope, | ||
"let result = Object.hasOwn({ cake: 123 }, 'test');\n" | ||
+ "'result = ' + result", | ||
"test", | ||
1, | ||
null); | ||
|
||
assertEquals("result = false", result); | ||
} | ||
|
||
@Test | ||
public void testCreateHasOwn() { | ||
Object result = | ||
cx.evaluateString( | ||
scope, | ||
"var foo = Object.create(null);\n" | ||
+ "foo.prop = 'test';\n" | ||
+ "var result = Object.hasOwn(foo, 'prop');\n" | ||
+ "'result = ' + result;", | ||
"test", | ||
1, | ||
null); | ||
|
||
assertEquals("result = true", result); | ||
} | ||
|
||
@Test | ||
public void testCreateNoHasOwn() { | ||
Object result = | ||
cx.evaluateString( | ||
scope, | ||
"var result = Object.hasOwn(Object.create({ q: 321 }), 'q');\n" | ||
+ "'result = ' + result; ", | ||
"test", | ||
1, | ||
null); | ||
|
||
assertEquals("result = false", result); | ||
} | ||
|
||
@Test | ||
public void testCalledTest() { | ||
Object result = | ||
cx.evaluateString( | ||
scope, | ||
"var called = false;\n" | ||
+ "try {\n" | ||
+ " Object.hasOwn(null, { toString() { called = true } });\n" | ||
+ "} catch (e) {}\n" | ||
+ "'called = ' + called;", | ||
"test", | ||
1, | ||
null); | ||
|
||
assertEquals("called = false", result); | ||
} | ||
} |