From e4477c9926fbf50336d058c1dad37b6d6cc27724 Mon Sep 17 00:00:00 2001 From: Mikhail Lobotskiy Date: Tue, 19 Dec 2023 15:10:27 +0400 Subject: [PATCH] Add `IsInit()` function --- DesktopEditor/doctrenderer/json/json.cpp | 6 ++++++ DesktopEditor/doctrenderer/json/json.h | 4 ++++ DesktopEditor/doctrenderer/test/json/main.cpp | 21 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/DesktopEditor/doctrenderer/json/json.cpp b/DesktopEditor/doctrenderer/json/json.cpp index 96cc8bef95..d4883c15cb 100644 --- a/DesktopEditor/doctrenderer/json/json.cpp +++ b/DesktopEditor/doctrenderer/json/json.cpp @@ -42,6 +42,12 @@ namespace NSJSON return m_internal->m_type == CTypedValue::vtNull; } + bool IValue::IsInit() const + { + // the same as (m_internal->m_type != CTypedValue::vtUndefined && m_internal->m_type != CTypedValue::vtNull) but a little bit faster + return m_internal->m_value.get() != nullptr; + } + bool IValue::IsBool() const { return (m_internal->m_type == CTypedValue::vtPrimitive && diff --git a/DesktopEditor/doctrenderer/json/json.h b/DesktopEditor/doctrenderer/json/json.h index e8634d635a..527bc3fb09 100644 --- a/DesktopEditor/doctrenderer/json/json.h +++ b/DesktopEditor/doctrenderer/json/json.h @@ -50,6 +50,10 @@ namespace NSJSON * Returns true if the value is null. */ bool IsNull() const; + /** + * Returns true if the value is not undefined or null. + */ + bool IsInit() const; /** * Returns true if the value is a boolean value. */ diff --git a/DesktopEditor/doctrenderer/test/json/main.cpp b/DesktopEditor/doctrenderer/test/json/main.cpp index 30fb4eb1b4..8501774af7 100644 --- a/DesktopEditor/doctrenderer/test/json/main.cpp +++ b/DesktopEditor/doctrenderer/test/json/main.cpp @@ -571,6 +571,27 @@ TEST_F(CJSONTest, wrong_usage) #endif } +TEST_F(CJSONTest, is_init) +{ + CValue val; + EXPECT_FALSE(val.IsInit()); + val = CValue::CreateNull(); + EXPECT_FALSE(val.IsInit()); + val = 42; + EXPECT_TRUE(val.IsInit()); + CValue val2 = val; + val2 = CValue::CreateUndefined(); + EXPECT_TRUE(val.IsInit()); + EXPECT_FALSE(val2.IsInit()); + CValueRef ref = val; + ref = CValue::CreateUndefined(); + EXPECT_FALSE(val.IsInit()); + EXPECT_FALSE(ref.IsInit()); + ref = 3; + EXPECT_TRUE(val.IsInit()); + EXPECT_TRUE(ref.IsInit()); +} + // ----------- toJS() tests ----------- TEST_F(CJSONTest, toJS_undefined)