aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorChad Austin <chad@imvu.com>2014-05-07 18:30:30 -0700
committerBruce Mitchener <bruce.mitchener@gmail.com>2014-05-21 22:58:23 +0700
commit1f7d9ea73de5fccbedf642b68ff600a5b0285061 (patch)
tree01c309e4398f055c191c7cd60db436e2e09dbd3f /tests
parent1bad0e2ae1038b9ae6ba362c73ca9d3fc8f17e11 (diff)
Support deriving from abstract classes with constructors
Diffstat (limited to 'tests')
-rw-r--r--tests/embind/embind.test.js20
-rw-r--r--tests/embind/embind_test.cpp33
2 files changed, 52 insertions, 1 deletions
diff --git a/tests/embind/embind.test.js b/tests/embind/embind.test.js
index 8307677d..6f0c6860 100644
--- a/tests/embind/embind.test.js
+++ b/tests/embind/embind.test.js
@@ -1570,6 +1570,7 @@ module({
test("properties set in constructor are externally visible", function() {
var HasProperty = cm.AbstractClass.extend("HasProperty", {
initialize: function(x) {
+ this.__parent.initialize.call(this);
this.property = x;
},
abstractMethod: function() {
@@ -1595,6 +1596,7 @@ module({
test("properties set in constructor are visible in overridden methods", function() {
var HasProperty = cm.AbstractClass.extend("HasProperty", {
initialize: function(x) {
+ this.__parent.initialize.call(this);
this.x = x;
},
abstractMethod: function() {
@@ -1747,6 +1749,24 @@ module({
});
assert.equal('Pure virtual function abstractMethod must be implemented in JavaScript', error.message);
});
+
+ test("can extend from C++ class with constructor arguments", function() {
+ var parent = cm.AbstractClassWithConstructor;
+ var C = parent.extend("C", {
+ initialize: function(x) {
+ this.__parent.initialize.call(this, x);
+ },
+ abstractMethod: function() {
+ return this.concreteMethod();
+ }
+ });
+
+ var impl = new C("hi");
+ var rv = cm.callAbstractMethod2(impl);
+ impl.delete();
+
+ assert.equal("hi", rv);
+ });
});
BaseFixture.extend("registration order", function() {
diff --git a/tests/embind/embind_test.cpp b/tests/embind/embind_test.cpp
index 1bfd0ef7..d652d528 100644
--- a/tests/embind/embind_test.cpp
+++ b/tests/embind/embind_test.cpp
@@ -1127,7 +1127,6 @@ class ConcreteClass : public AbstractClass {
return "from concrete";
}
-
void differentArguments(int i, double d, unsigned char f, double q, std::string s) {
}
@@ -1157,6 +1156,31 @@ void callDifferentArguments(AbstractClass& ac, int i, double d, unsigned char f,
return ac.differentArguments(i, d, f, q, s);
}
+struct AbstractClassWithConstructor {
+ explicit AbstractClassWithConstructor(std::string s)
+ : s(s)
+ {}
+
+ virtual std::string abstractMethod() = 0;
+ std::string concreteMethod() {
+ return s;
+ }
+
+ std::string s;
+};
+
+struct AbstractClassWithConstructorWrapper : public wrapper<AbstractClassWithConstructor> {
+ EMSCRIPTEN_WRAPPER(AbstractClassWithConstructorWrapper);
+
+ virtual std::string abstractMethod() override {
+ return call<std::string>("abstractMethod");
+ }
+};
+
+std::string callAbstractMethod2(AbstractClassWithConstructor& ac) {
+ return ac.abstractMethod();
+}
+
EMSCRIPTEN_BINDINGS(interface_tests) {
class_<AbstractClass>("AbstractClass")
.smart_ptr<std::shared_ptr<AbstractClass>>("shared_ptr<AbstractClass>")
@@ -1177,6 +1201,13 @@ EMSCRIPTEN_BINDINGS(interface_tests) {
function("callOptionalMethod", &callOptionalMethod);
function("callReturnsSharedPtrMethod", &callReturnsSharedPtrMethod);
function("callDifferentArguments", &callDifferentArguments);
+
+ class_<AbstractClassWithConstructor>("AbstractClassWithConstructor")
+ .allow_subclass<AbstractClassWithConstructorWrapper>("AbstractClassWithConstructorWrapper", constructor<std::string>())
+ .function("abstractMethod", &AbstractClassWithConstructor::abstractMethod, pure_virtual())
+ .function("concreteMethod", &AbstractClassWithConstructor::concreteMethod)
+ ;
+ function("callAbstractMethod2", &callAbstractMethod2);
}
template<typename T, size_t sizeOfArray>