Skip to content
File

Blob: src/rust/jsg-test/tests/name.rs

rust95 lines
1// Copyright (c) 2026 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5//! Tests for `Local<Name>` — the `v8::Name` supertype shared by `String` and `Symbol`.
6 
7use jsg::v8;
8use jsg::v8::Local;
9 
10// =============================================================================
11// Helpers
12// =============================================================================
13 
14fn str_to_name<'a>(lock: &mut jsg::Lock, s: &str) -> Local<'a, v8::Name> {
15 let js_str = v8::String::new_from_str(lock, s).unwrap(lock);
16 js_str.into()
17}
18 
19// =============================================================================
20// Identity hash — via Local<Name>
21// =============================================================================
22 
23#[test]
24fn name_get_identity_hash_string_is_nonzero() {
25 let harness = crate::Harness::new();
26 harness.run_in_context(|lock, _ctx| {
27 let name = str_to_name(lock, "hash-me");
28 assert_ne!(name.get_identity_hash(), 0);
29 Ok(())
30 });
31}
32 
33#[test]
34fn name_get_identity_hash_string_is_stable() {
35 let harness = crate::Harness::new();
36 harness.run_in_context(|lock, _ctx| {
37 let name = str_to_name(lock, "stable-hash");
38 assert_eq!(name.get_identity_hash(), name.get_identity_hash());
39 Ok(())
40 });
41}
42 
43#[test]
44fn name_get_identity_hash_symbol_is_nonzero() {
45 let harness = crate::Harness::new();
46 harness.run_in_context(|lock, _ctx| {
47 let sym = v8::Symbol::new(lock, None);
48 let name: Local<v8::Name> = sym.into();
49 assert_ne!(name.get_identity_hash(), 0);
50 Ok(())
51 });
52}
53 
54#[test]
55fn name_get_identity_hash_symbol_is_stable() {
56 let harness = crate::Harness::new();
57 harness.run_in_context(|lock, _ctx| {
58 let sym = v8::Symbol::new(lock, None);
59 let name: Local<v8::Name> = sym.clone().into();
60 let name2: Local<v8::Name> = sym.into();
61 assert_eq!(name.get_identity_hash(), name2.get_identity_hash());
62 Ok(())
63 });
64}
65 
66// =============================================================================
67// Upcasting: String → Name and Symbol → Name
68// =============================================================================
69 
70#[test]
71fn string_upcast_to_name_is_name() {
72 let harness = crate::Harness::new();
73 harness.run_in_context(|lock, _ctx| {
74 let s = v8::String::new_from_str(lock, "hello").unwrap(lock);
75 assert!(s.is_string());
76 assert!(s.is_name());
77 let name: Local<v8::Name> = s.into();
78 assert!(name.is_name());
79 Ok(())
80 });
81}
82 
83#[test]
84fn symbol_upcast_to_name_is_name() {
85 let harness = crate::Harness::new();
86 harness.run_in_context(|lock, _ctx| {
87 let sym = v8::Symbol::new(lock, None);
88 assert!(sym.is_symbol());
89 assert!(sym.is_name());
90 let name: Local<v8::Name> = sym.into();
91 assert!(name.is_name());
92 Ok(())
93 });
94}