Permalink
Browse files

Add a new test to showcase the relative brevity of using the new Parr…

…ot_api_pmc_new_call_object and Parrot_api_pmc_setup_signature
  • Loading branch information...
Whiteknight committed May 18, 2012
1 parent d2853b9 commit 7be52c6ad1dae843390c4338f6f5bc7254b5885a
Showing with 90 additions and 5 deletions.
  1. +90 −5 t/src/embed/pmc.t
View
@@ -24,13 +24,14 @@ Tests PMC API support.
=cut
-plan tests => 8;
+plan tests => 9;
c_output_is( <<'CODE', <<'OUTPUT', "get/set_keyed_int" );
#include "parrot/api.h"
#include <stdio.h>
+
int main(int argc, char* argv[])
{
char * c_outstr = NULL;
@@ -265,7 +266,7 @@ int main(int argc, char* argv[])
GET_INIT_STRUCT(initargs);
Parrot_api_make_interpreter(NULL, 0, initargs, &interpmc);
- Parrot_api_string_import_ascii(interpmc, "I love Microsoft!", &s_teststr);
+ Parrot_api_string_import_ascii(interpmc, "I love Vanilla!", &s_teststr);
Parrot_api_pmc_box_string(interpmc, s_teststr, &p_pmc);
Parrot_api_string_import_ascii(interpmc, "replace", &s_teststr);
@@ -280,11 +281,11 @@ int main(int argc, char* argv[])
Parrot_api_pmc_set_string(interpmc, p_signature, s_signstring);
Parrot_api_pmc_set_keyed_int(interpmc, p_signature, 0, p_pmc);
- Parrot_api_string_import_ascii(interpmc, "Microsoft", &s_toreplace);
+ Parrot_api_string_import_ascii(interpmc, "Vanilla", &s_toreplace);
Parrot_api_pmc_box_string(interpmc, s_toreplace, &p_toreplace);
Parrot_api_pmc_set_keyed_int(interpmc, p_signature, 1, p_toreplace);
- Parrot_api_string_import_ascii(interpmc, "the Open Source community", &s_replacestring);
+ Parrot_api_string_import_ascii(interpmc, "Chocolate", &s_replacestring);
Parrot_api_pmc_box_string(interpmc, s_replacestring, &p_replacestring);
Parrot_api_pmc_set_keyed_int(interpmc, p_signature, 2, p_replacestring);
@@ -299,7 +300,91 @@ int main(int argc, char* argv[])
}
CODE
-I love the Open Source community!
+I love Chocolate!
+OUTPUT
+
+c_output_is( <<'CODE', <<'OUTPUT', "Parrot_api_pmc_new_call_object and _setup_signature" );
+
+#include <parrot/api.h>
+#include <stdio.h>
+
+static void
+print_parrot_string(Parrot_PMC interp, ARGMOD(FILE *vector), Parrot_String str,
+ int newline)
+{
+ char * msg_raw;
+ if (!str)
+ return;
+ Parrot_api_string_export_ascii(interp, str, &msg_raw);
+ if (msg_raw) {
+ fprintf(vector, "%s%s", msg_raw, newline ? "\n" : "");
+ Parrot_api_string_free_exported_ascii(interp, msg_raw);
+ }
+}
+
+static void
+show_last_error_and_exit(Parrot_PMC interp)
+{
+ Parrot_String errmsg, backtrace;
+ Parrot_Int exit_code, is_error;
+ Parrot_PMC exception;
+
+ if (!Parrot_api_get_result(interp, &is_error, &exception, &exit_code, &errmsg)) {
+ Parrot_api_destroy_interpreter(interp);
+ exit(EXIT_FAILURE);
+ }
+ if (is_error) {
+ if (!Parrot_api_get_exception_backtrace(interp, exception, &backtrace)) {
+ Parrot_api_destroy_interpreter(interp);
+ exit(EXIT_FAILURE);
+ }
+ print_parrot_string(interp, stderr, errmsg, 1);
+ print_parrot_string(interp, stderr, backtrace, 0);
+ }
+
+ Parrot_api_destroy_interpreter(interp);
+ exit(exit_code);
+}
+
+int main(int argc, char* argv[])
+{
+ Parrot_Init_Args *initargs = NULL;
+ Parrot_PMC interpmc = NULL;
+ Parrot_PMC p_pmc = NULL, p_method = NULL, p_signature = NULL;
+ Parrot_String s_teststr = NULL, s_outstr = NULL, s_method = NULL;
+ Parrot_String s_toreplace = NULL, s_replacestring = NULL;
+ char * c_out = NULL;
+
+ GET_INIT_STRUCT(initargs);
+ Parrot_api_make_interpreter(NULL, 0, initargs, &interpmc);
+
+ Parrot_api_string_import_ascii(interpmc, "I love Vanilla!", &s_teststr);
+ Parrot_api_pmc_box_string(interpmc, s_teststr, &p_pmc);
+
+ Parrot_api_string_import_ascii(interpmc, "replace", &s_teststr);
+ Parrot_api_pmc_find_method(interpmc, p_pmc, s_teststr, &p_method);
+ if(p_method != NULL) {
+ Parrot_api_pmc_new_call_object(interpmc, &p_signature);
+
+ Parrot_api_string_import_ascii(interpmc, "Vanilla", &s_toreplace);
+ Parrot_api_string_import_ascii(interpmc, "Strawberry", &s_replacestring);
+
+ if (!Parrot_api_pmc_setup_signature(interpmc, p_signature, "PiSS->", p_pmc, s_toreplace, s_replacestring)) {
+ show_last_error_and_exit(interpmc);
+ }
+
+ Parrot_api_pmc_invoke(interpmc, p_method, p_signature);
+ Parrot_api_pmc_get_string(interpmc, p_pmc, &s_outstr);
+ Parrot_api_string_export_ascii(interpmc, s_outstr, &c_out);
+ printf("%s\n", c_out);
+ }
+ else printf("error\n");
+
+ return 0;
+}
+
+CODE
+I love Strawberry!
OUTPUT

0 comments on commit 7be52c6

Please sign in to comment.