Make BB_Mimic work with Strings

Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/d30a279f
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/d30a279f
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/d30a279f

Branch: refs/heads/master
Commit: d30a279ffeda9ff9d7e163ac0b3f3194f2b8e074
Parents: 2ba218e
Author: Nick Wellnhofer <wellnho...@aevum.de>
Authored: Tue Nov 10 12:54:54 2015 +0100
Committer: Nick Wellnhofer <wellnho...@aevum.de>
Committed: Thu Nov 12 14:08:17 2015 +0100

----------------------------------------------------------------------
 runtime/core/Clownfish/ByteBuf.c          | 14 ++++++++++++--
 runtime/core/Clownfish/Test/TestByteBuf.c |  8 +++++++-
 2 files changed, 19 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d30a279f/runtime/core/Clownfish/ByteBuf.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/ByteBuf.c b/runtime/core/Clownfish/ByteBuf.c
index 625da29..16a8292 100644
--- a/runtime/core/Clownfish/ByteBuf.c
+++ b/runtime/core/Clownfish/ByteBuf.c
@@ -26,6 +26,7 @@
 #include "Clownfish/ByteBuf.h"
 #include "Clownfish/Blob.h"
 #include "Clownfish/Err.h"
+#include "Clownfish/String.h"
 #include "Clownfish/Util/Memory.h"
 
 static void
@@ -144,8 +145,17 @@ BB_Mimic_Bytes_IMP(ByteBuf *self, const void *bytes, 
size_t size) {
 
 void
 BB_Mimic_IMP(ByteBuf *self, Obj *other) {
-    ByteBuf *twin = (ByteBuf*)CERTIFY(other, BYTEBUF);
-    SI_mimic_bytes(self, twin->buf, twin->size);
+    if (Obj_is_a(other, BYTEBUF)) {
+        ByteBuf *twin = (ByteBuf*)other;
+        SI_mimic_bytes(self, twin->buf, twin->size);
+    }
+    else if (Obj_is_a(other, STRING)) {
+        String *string = (String*)other;
+        SI_mimic_bytes(self, Str_Get_Ptr8(string), Str_Get_Size(string));
+    }
+    else {
+        THROW(ERR, "ByteBuf can't mimic %o", Obj_get_class_name(other));
+    }
 }
 
 static CFISH_INLINE void

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d30a279f/runtime/core/Clownfish/Test/TestByteBuf.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Test/TestByteBuf.c 
b/runtime/core/Clownfish/Test/TestByteBuf.c
index 1fe41b8..d7eb045 100644
--- a/runtime/core/Clownfish/Test/TestByteBuf.c
+++ b/runtime/core/Clownfish/Test/TestByteBuf.c
@@ -27,6 +27,7 @@
 #include "Clownfish/TestHarness/TestUtils.h"
 #include "Clownfish/Blob.h"
 #include "Clownfish/Class.h"
+#include "Clownfish/String.h"
 
 TestByteBuf*
 TestBB_new() {
@@ -118,6 +119,11 @@ test_Mimic(TestBatchRunner *runner) {
     BB_Mimic(b, (Obj*)a);
     TEST_TRUE(runner, BB_Equals(a, (Obj*)b), "Mimic");
 
+    String *string = Str_newf("baz");
+    BB_Mimic(b, (Obj*)string);
+    DECREF(string);
+    TEST_TRUE(runner, BB_Equals_Bytes(b, "baz", 3), "Mimic String");
+
     DECREF(a);
     DECREF(b);
 }
@@ -142,7 +148,7 @@ test_Cat(TestBatchRunner *runner) {
 
 void
 TestBB_Run_IMP(TestByteBuf *self, TestBatchRunner *runner) {
-    TestBatchRunner_Plan(runner, (TestBatch*)self, 19);
+    TestBatchRunner_Plan(runner, (TestBatch*)self, 20);
     test_Equals(runner);
     test_Grow(runner);
     test_Clone(runner);

Reply via email to