Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(context): add from_json for jsc #54

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 12 additions & 0 deletions src/engine/javascript_core/engine_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern "C" {
}

#include <cassert> // assert
#include <stdexcept>

namespace includejs {

Expand Down Expand Up @@ -90,6 +91,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
36 changes: 36 additions & 0 deletions test/engine/engine_context_from_json_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdexcept>

#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