From cf89bdd804b3f416d6b3164d62d95011e8662525 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 6 Dec 2016 14:15:39 -0700 Subject: [PATCH] Update release notes, add chai type fun conversion test --- releasenotes.md | 1 + unittests/compiled_tests.cpp | 57 ++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/releasenotes.md b/releasenotes.md index 4300bd12..5f4f3eb1 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -31,6 +31,7 @@ Current Version: 6.0.0 * Support for passing r-value references to functions * Support for containing unique_ptr * Add helpers for exposing enum classes to ChaiScript +* Allow typed ChaiScript defined functions to perform conversions on call #303 #### Improvements diff --git a/unittests/compiled_tests.cpp b/unittests/compiled_tests.cpp index 39ee06e4..86227dbc 100644 --- a/unittests/compiled_tests.cpp +++ b/unittests/compiled_tests.cpp @@ -1129,3 +1129,60 @@ TEST_CASE("Use unique_ptr") } +class A +{ + public: + A() = default; + A(const A&) = default; + A(A &&) = default; + A &operator=(const A&) = default; + A &operator=(A&&) = default; + virtual ~A() = default; +}; + +class B : public A +{ + public: + B() = default; +}; + +TEST_CASE("Test typed chaiscript functions to perform conversions") +{ + chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser()); + + //------------------------------------------------------------------------- + + chai.add(chaiscript::user_type(), "A"); + + chai.add(chaiscript::user_type(), "B"); + chai.add(chaiscript::base_class()); + + chai.add(chaiscript::fun([](const B &) + { + }), "CppFunctWithBArg"); + + chai.add(chaiscript::fun([]() -> std::shared_ptr + { + return (std::shared_ptr(new B())); + }), "Create"); + + chai.eval(R"( + var inst = Create() // A* + + // it prints "A" + inst.type_name().print() + + // Ok it is casted using conversion + CppFunctWithBArg(inst) + + // Define a function with B as argument + def ChaiFuncWithBArg(B inst) + { + print("ok") + } + + // don't work + ChaiFuncWithBArg(inst) + )"); +} +