Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
driver.c
Go to the documentation of this file.
1 /*
2 ** mrbtest - Test for Embeddable Ruby
3 **
4 ** This program runs Ruby test programs in test/t directory
5 ** against the current mruby implementation.
6 */
7 
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 #include <mruby.h>
14 #include <mruby/proc.h>
15 #include <mruby/data.h>
16 #include <mruby/compile.h>
17 #include <mruby/string.h>
18 #include <mruby/variable.h>
19 
20 void
22 
23 /* Print a short remark for the user */
24 static void
25 print_hint(void)
26 {
27  printf("mrbtest - Embeddable Ruby Test\n");
28  printf("\nThis is a very early version, please test and report errors.\n");
29  printf("Thanks :)\n\n");
30 }
31 
32 static int
33 check_error(mrb_state *mrb)
34 {
35  /* Error check */
36  /* $ko_test and $kill_test should be 0 */
37  mrb_value ko_test = mrb_gv_get(mrb, mrb_intern(mrb, "$ko_test"));
38  mrb_value kill_test = mrb_gv_get(mrb, mrb_intern(mrb, "$kill_test"));
39 
40  return mrb_fixnum_p(ko_test) && mrb_fixnum(ko_test) == 0 && mrb_fixnum_p(kill_test) && mrb_fixnum(kill_test) == 0;
41 }
42 
43 static int
44 eval_test(mrb_state *mrb)
45 {
46  mrb_value return_value;
47  const char *prog = "report()";
48 
49  /* evaluate the test */
50  return_value = mrb_load_string(mrb, prog);
51  /* did an exception occur? */
52  if (mrb->exc) {
53  mrb_p(mrb, return_value);
54  mrb->exc = 0;
55  return EXIT_FAILURE;
56  }
57  else if (!check_error(mrb)) {
58  return EXIT_FAILURE;
59  }
60  return EXIT_SUCCESS;
61 }
62 
63 static void
64 t_printstr(mrb_state *mrb, mrb_value obj)
65 {
66  struct RString *str;
67  char *s;
68  int len;
69 
70  if (mrb_string_p(obj)) {
71  str = mrb_str_ptr(obj);
72  s = str->ptr;
73  len = str->len;
74  fwrite(s, len, 1, stdout);
75  }
76 }
77 
80 {
81  mrb_value argv;
82 
83  mrb_get_args(mrb, "o", &argv);
84  t_printstr(mrb, argv);
85 
86  return argv;
87 }
88 
89 int
90 main(int argc, char **argv)
91 {
92  mrb_state *mrb;
93  struct RClass *krn;
94  int ret;
95 
96  print_hint();
97 
98  /* new interpreter instance */
99  mrb = mrb_open();
100  if (mrb == NULL) {
101  fprintf(stderr, "Invalid mrb_state, exiting test driver");
102  return EXIT_FAILURE;
103  }
104 
105  if (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'v') {
106  printf("verbose mode: enable\n\n");
107  mrb_gv_set(mrb, mrb_intern(mrb, "$mrbtest_verbose"), mrb_true_value());
108  }
109 
110  krn = mrb->kernel_module;
111  mrb_define_method(mrb, krn, "__t_printstr__", mrb_t_printstr, MRB_ARGS_REQ(1));
112 
113  mrb_init_mrbtest(mrb);
114  ret = eval_test(mrb);
115  mrb_close(mrb);
116 
117  return ret;
118 }