/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "ecmascript/js_symbol.h" #include "ecmascript/tests/test_helper.h" using namespace panda; using namespace panda::ecmascript; namespace panda::test { class JSSymbolTest : public BaseTestWithScope { }; HWTEST_F_L0(JSSymbolTest, SymbolCreate) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle normallSymbol = factory->NewJSSymbol(); EXPECT_TRUE(*normallSymbol != nullptr); EXPECT_TRUE(normallSymbol->GetDescription(thread).IsUndefined()); JSHandle privateSymbol = factory->NewPrivateSymbol(); EXPECT_TRUE(*privateSymbol != nullptr); EXPECT_TRUE(privateSymbol->IsPrivate()); EXPECT_TRUE(privateSymbol->GetDescription(thread).IsUndefined()); JSHandle symbolName(factory->NewFromASCII("hello world")); JSHandle privateNameSymbol = factory->NewPrivateNameSymbol(symbolName); EXPECT_TRUE(*privateNameSymbol != nullptr); EXPECT_TRUE(privateNameSymbol->IsPrivateNameSymbol()); EXPECT_FALSE(privateNameSymbol->GetDescription(thread).IsUndefined()); JSHandle wellKnowSymbol = factory->NewWellKnownSymbol(symbolName); EXPECT_TRUE(*wellKnowSymbol != nullptr); EXPECT_TRUE(wellKnowSymbol->IsWellKnownSymbol()); EXPECT_FALSE(wellKnowSymbol->GetDescription(thread).IsUndefined()); } HWTEST_F_L0(JSSymbolTest, SymbolEqual) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle helloWord(factory->NewFromASCII("hello world")); JSHandle hiWord(factory->NewFromASCII("hi world")); JSHandle helloWord1SymbolVal(factory->NewPrivateNameSymbol(helloWord)); JSHandle helloWord2SymbolVal(factory->NewPrivateNameSymbol(helloWord)); JSHandle hiWordSymbolVal(factory->NewPrivateNameSymbol(hiWord)); JSSymbol *helloWord1Symbol = JSSymbol::Cast(helloWord1SymbolVal->GetTaggedObject()); JSSymbol *helloWord2Symbol = JSSymbol::Cast(helloWord2SymbolVal->GetTaggedObject()); JSSymbol *hiWordSymbol = JSSymbol::Cast(hiWordSymbolVal->GetTaggedObject()); EXPECT_TRUE(JSSymbol::Equal(thread, *helloWord1Symbol, *helloWord2Symbol)); helloWord2Symbol->SetFlags(1); EXPECT_FALSE(JSSymbol::Equal(thread, *helloWord1Symbol, *helloWord2Symbol)); EXPECT_FALSE(JSSymbol::Equal(thread, *helloWord1Symbol, *hiWordSymbol)); } HWTEST_F_L0(JSSymbolTest, ConvertToString001) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle symbol = factory->NewPublicSymbolWithChar("bbb"); CString str = ConvertToString(thread, symbol.GetTaggedValue()); EXPECT_EQ(str, CString("bbb")); } HWTEST_F_L0(JSSymbolTest, ConvertToString002) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle symbol = factory->NewPublicSymbolWithChar(""); symbol->SetDescription(thread, JSTaggedValue::Undefined()); symbol->SetFlags(0); symbol->SetHashField(0); CString str = ConvertToString(thread, symbol.GetTaggedValue()); EXPECT_EQ(str, CString("Symbol()")); } } // namespace panda::test