Skip to content

Commit

Permalink
feat(context): add from_json for jsc
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Gorez <gorez.tony@gmail.com>
  • Loading branch information
tony-go committed Mar 21, 2024
1 parent c322bce commit 3bcc598
Show file tree
Hide file tree
Showing 63 changed files with 36,193 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cmake/FindGoogleTest.cmake
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
if(NOT GoogleTest_FOUND)
set(BUILD_GMOCK OFF CACHE BOOL "disable googlemock")
include(GoogleTest)
set(BUILD_GMOCK ON CACHE BOOL "enable googlemock")
set(INSTALL_GTEST OFF CACHE BOOL "disable installation")
add_subdirectory("${PROJECT_SOURCE_DIR}/vendor/googletest")
include(GoogleTest)
set(GoogleTest_FOUND ON)
endif()

Expand Down
1 change: 1 addition & 0 deletions src/engine/include/includejs/engine_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class INCLUDEJS_ENGINE_EXPORT Context {
auto from(double value) const -> Value;
auto from(bool value) const -> Value;
auto from(std::nullptr_t value) const -> Value;
auto from_json(const std::string &json_string) const -> Value;
auto global() const -> Value;

private:
Expand Down
11 changes: 11 additions & 0 deletions src/engine/javascript_core/engine_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ auto Context::from(std::nullptr_t) const -> Value {
return {this->internal->context, JSValueMakeNull(this->internal->context)};
}

auto Context::from_json(const std::string &json_string) const -> Value {
JSStringRef json_c_str = JSStringCreateWithUTF8CString(json_string.c_str());
JSValueRef result =
JSValueMakeFromJSONString(this->internal->context, json_c_str);
JSStringRelease(json_c_str);
if (result == nullptr) {
throw std::invalid_argument("Invalid JSON string");
}
return {this->internal->context, result};
}

auto Context::global() const -> Value {
return {this->internal->context, this->internal->global};
}
Expand Down
5 changes: 3 additions & 2 deletions test/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ add_executable(includejs_engine_unit
engine_value_function_test.cc
engine_value_undefined_test.cc
engine_value_type_test.cc
engine_value_null_test.cc)
engine_value_null_test.cc
engine_context_from_json_test.cc)

includejs_add_compile_options(includejs_engine_unit)

target_link_libraries(includejs_engine_unit
PRIVATE GTest::gtest GTest::gtest_main)
PRIVATE GTest::gtest GTest::gtest_main GTest::gmock)
target_link_libraries(includejs_engine_unit
PRIVATE includejs::engine)
gtest_discover_tests(includejs_engine_unit)
Expand Down
34 changes: 34 additions & 0 deletions test/engine/engine_context_from_json_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <includejs/engine.h>

TEST(IncludeJS_Engine, from_json_object) {
includejs::Engine engine;

auto json = engine.context().from_json("{ \"key\": null }");

EXPECT_TRUE(json.is_object());
auto json_object = json.to_map();
EXPECT_EQ(json_object.size(), 1);
EXPECT_TRUE(json_object.at("key").is_null());
}

TEST(IncludeJS_Engine, from_json_array) {
includejs::Engine engine;

auto json = engine.context().from_json("[2]");

EXPECT_TRUE(json.is_array());
auto json_array = json.to_vector();
EXPECT_EQ(json_array.size(), 1);
EXPECT_TRUE(json_array.at(0).is_number());
}

TEST(IncludeJS_Engine, from_json_failure) {
includejs::Engine engine;

EXPECT_THAT([&] { engine.context().from_json("<div>"); },
testing::ThrowsMessage<std::invalid_argument>(
testing::HasSubstr("Invalid JSON string")));
}
1 change: 0 additions & 1 deletion vendor/googletest.mask

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

209 changes: 209 additions & 0 deletions vendor/googletest/googlemock/CMakeLists.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions vendor/googletest/googlemock/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions vendor/googletest/googlemock/cmake/gmock.pc.in

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions vendor/googletest/googlemock/cmake/gmock_main.pc.in

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions vendor/googletest/googlemock/docs/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3bcc598

Please sign in to comment.