diff --git a/libkc3/buf_inspect.c b/libkc3/buf_inspect.c
index adf6e4c..961842a 100644
--- a/libkc3/buf_inspect.c
+++ b/libkc3/buf_inspect.c
@@ -263,7 +263,7 @@ sw buf_inspect_block (s_buf *buf, const s_block *block)
return buf_write_1(buf, "do end");
}
pretty_save = buf->pretty;
- pretty_indent(&buf->pretty, 2);
+ pretty_indent(&buf->pretty, PRETTY_INDENT);
if (block->short_form) {
if ((r = buf_write_1(buf, "{ ")) < 0)
return r;
@@ -391,7 +391,7 @@ sw buf_inspect_block_size (s_pretty *pretty, const s_block *block)
return buf_write_1_size(pretty, "do end");
}
pretty_save = *pretty;
- pretty_indent(pretty, 2);
+ pretty_indent(pretty, PRETTY_INDENT);
if (block->short_form) {
if ((r = buf_write_1_size(pretty, "{ ")) < 0)
return r;
@@ -669,6 +669,7 @@ sw buf_inspect_call_if_then_else (s_buf *buf, const s_call *call)
s_tag *condition;
s_tag *else_;
uw i;
+ s_pretty pretty_save;
sw r;
sw result = 0;
s_tag *then;
@@ -684,13 +685,15 @@ sw buf_inspect_call_if_then_else (s_buf *buf, const s_call *call)
return r;
result += r;
then = &list_next(call->arguments)->tag;
+ pretty_save = buf->pretty;
if (then->type == TAG_BLOCK) {
if ((r = buf_write_1(buf, " do")) < 0)
return r;
result += r;
+ pretty_indent(&buf->pretty, PRETTY_INDENT);
i = 0;
while (i < then->data.block.count) {
- if ((r = buf_write_1(buf, "\n ")) < 0)
+ if ((r = buf_write_1(buf, "\n")) < 0)
return r;
result += r;
if ((r = buf_inspect_tag(buf, then->data.block.tag + i)) < 0)
@@ -702,11 +705,14 @@ sw buf_inspect_call_if_then_else (s_buf *buf, const s_call *call)
if ((r = buf_inspect_tag(buf, then)) < 0)
return r;
result += r;
+ buf->pretty = pretty_save;
else_ = &list_next(list_next(call->arguments))->tag;
if (else_->type != TAG_VOID) {
if ((r = buf_write_1(buf, "\nelse")) < 0)
return r;
result += r;
+ pretty_save = buf->pretty;
+ pretty_indent(&buf->pretty, PRETTY_INDENT);
if (then->type == TAG_BLOCK) {
if ((r = buf_inspect_block_inner(buf, &else_->data.block)) < 0)
return r;
@@ -716,6 +722,7 @@ sw buf_inspect_call_if_then_else (s_buf *buf, const s_call *call)
return r;
result += r;
}
+ buf->pretty = pretty_save;
if ((r = buf_write_1(buf, "\nend")) < 0)
return r;
result += r;
@@ -748,7 +755,7 @@ sw buf_inspect_call_if_then_else_size (s_pretty *pretty, const s_call *call)
if ((r = buf_write_1_size(pretty, " do")) < 0)
return r;
result += r;
- pretty_indent(pretty, 2);
+ pretty_indent(pretty, PRETTY_INDENT);
i = 0;
while (i < then->data.block.count) {
if ((r = buf_write_1_size(pretty, "\n")) < 0)
@@ -760,7 +767,7 @@ sw buf_inspect_call_if_then_else_size (s_pretty *pretty, const s_call *call)
}
}
else {
- pretty_indent(pretty, 2);
+ pretty_indent(pretty, PRETTY_INDENT);
if ((r = buf_inspect_tag_size(pretty, then)) < 0)
return r;
}
@@ -2148,11 +2155,11 @@ sw buf_inspect_integer (s_buf *buf, const s_integer *x)
const mp_digit radix = 10;
s32 size = 0;
mp_int t;
+ if (MP_IS_ZERO(&x->mp_int))
+ return buf_write_u8(buf, '0');
if (mp_radix_size(&x->mp_int, radix, &size) != MP_OKAY)
return -1;
maxlen = size;
- if (MP_IS_ZERO(&x->mp_int))
- return buf_write_u8(buf, '0');
if (mp_init_copy(&t, &x->mp_int) != MP_OKAY)
return -1;
if (t.sign == MP_NEG) {
@@ -2184,15 +2191,13 @@ sw buf_inspect_integer (s_buf *buf, const s_integer *x)
sw buf_inspect_integer_size (s_pretty *pretty, const s_integer *x)
{
const mp_digit radix = 10;
- sw result = 0;
s32 size = 0;
(void) pretty;
- if (mp_radix_size(&x->mp_int, radix, &size) != MP_OKAY)
- return -1;
- result = size;
if (MP_IS_ZERO(&x->mp_int))
return 1;
- return result;
+ if (mp_radix_size(&x->mp_int, radix, &size) != MP_OKAY)
+ return -1;
+ return size - 1;
}
sw buf_inspect_list (s_buf *buf, const s_list * const *x)
@@ -3027,7 +3032,7 @@ sw buf_inspect_struct (s_buf *buf, const s_struct *s)
assert(! "buf_inspect_struct: sym_is_module(s->type->module)");
return -1;
}
- if ((r = buf_write_str(buf, &s->type->module->str)) < 0)
+ if ((r = buf_write_str_memcpy(buf, &s->type->module->str)) < 0)
return r;
result += r;
if ((r = buf_write_1(buf, "{")) < 0)
@@ -3050,7 +3055,7 @@ sw buf_inspect_struct (s_buf *buf, const s_struct *s)
return r;
}
else
- if ((r = buf_write_str(buf, &k->data.sym->str)) < 0)
+ if ((r = buf_write_str_memcpy(buf, &k->data.sym->str)) < 0)
return r;
result += r;
if ((r = buf_write_1(buf, ": ")) < 0)
@@ -3145,7 +3150,6 @@ sw buf_inspect_struct_size (s_pretty *pretty, const s_struct *s)
return r;
result += r;
}
- return r;
}
}
if ((r = buf_write_1_size(pretty, "}")) < 0)
diff --git a/libkc3/env.c b/libkc3/env.c
index f440784..c5fa1c2 100644
--- a/libkc3/env.c
+++ b/libkc3/env.c
@@ -523,6 +523,7 @@ bool env_eval_array_tag (s_env *env, const s_array *array, s_tag *dest)
dest->data.array = tmp;
return true;
}
+
bool env_eval_block (s_env *env, const s_block *block, s_tag *dest)
{
uw i = 0;
@@ -1758,42 +1759,39 @@ bool env_eval_quote_unquote (s_env *env, const s_unquote *unquote, s_tag *dest)
return true;
}
-bool env_eval_struct (s_env *env, const s_struct *s, s_tag *dest)
+bool env_eval_struct (s_env *env, const s_struct *s, s_struct *dest)
{
uw i;
- s_struct *t;
s_tag tag = {0};
const void *tag_data;
- s_tag tmp = {0};
+ s_struct tmp = {0};
const s_sym *type;
const void *value;
assert(env);
assert(s);
assert(dest);
- tmp.type = TAG_STRUCT;
- t = &tmp.data.struct_;
if (s->data) {
- if (! struct_init_copy(t, s))
+ if (! struct_init_copy(&tmp, s))
return false;
*dest = tmp;
return true;
}
- t->type = s->type;
- if (! struct_allocate(t))
+ tmp.type = s->type;
+ if (! struct_allocate(&tmp))
return false;
i = 0;
- while (i < t->type->map.count) {
+ while (i < tmp.type->map.count) {
if (s->tag) {
- if (t->type->map.value[i].type == TAG_VAR)
- type = t->type->map.value[i].data.var.type;
+ if (tmp.type->map.value[i].type == TAG_VAR)
+ type = tmp.type->map.value[i].data.var.type;
else {
- if (! tag_type(t->type->map.value + i, &type))
+ if (! tag_type(tmp.type->map.value + i, &type))
goto ko;
}
if (! env_eval_tag(env, s->tag + i, &tag))
goto ko;
- if (t->type->map.value[i].type == TAG_VAR) {
- if (! data_init_cast((s8 *) t->data + t->type->offset[i],
+ if (tmp.type->map.value[i].type == TAG_VAR) {
+ if (! data_init_cast((s8 *) tmp.data + tmp.type->offset[i],
&type, &tag))
goto ko_init;
}
@@ -1802,18 +1800,18 @@ bool env_eval_struct (s_env *env, const s_struct *s, s_tag *dest)
tag_clean(&tag);
goto ko;
}
- if (! data_init_copy(type, (s8 *) t->data + t->type->offset[i],
+ if (! data_init_copy(type, (s8 *) tmp.data + tmp.type->offset[i],
tag_data))
goto ko_init;
}
tag_clean(&tag);
}
else {
- if (! tag_type(t->type->map.value + i, &type))
+ if (! tag_type(tmp.type->map.value + i, &type))
goto ko;
- if (! tag_to_const_pointer(t->type->map.value + i, type, &value))
+ if (! tag_to_const_pointer(tmp.type->map.value + i, type, &value))
goto ko;
- if (! data_init_copy(type, (s8 *) t->data + t->type->offset[i],
+ if (! data_init_copy(type, (s8 *) tmp.data + tmp.type->offset[i],
value))
goto ko;
}
@@ -1825,15 +1823,23 @@ bool env_eval_struct (s_env *env, const s_struct *s, s_tag *dest)
err_write_1("env_eval_struct: invalid type ");
err_write_1(tag_type_to_string(tag.type));
err_write_1(" for key ");
- err_write_1(t->type->map.key[i].data.sym->str.ptr.pchar);
+ err_write_1(tmp.type->map.key[i].data.sym->str.ptr.pchar);
err_write_1(", expected ");
- err_puts(tag_type_to_string(t->type->map.value[i].type));
+ err_puts(tag_type_to_string(tmp.type->map.value[i].type));
tag_clean(&tag);
ko:
- struct_clean(t);
+ struct_clean(&tmp);
return false;
}
+bool env_eval_struct_tag (s_env *env, const s_struct *s, s_tag *dest)
+{
+ if (! env_eval_struct(env, s, &dest->data.struct_))
+ return false;
+ dest->type = TAG_STRUCT;
+ return true;
+}
+
bool env_eval_tag (s_env *env, const s_tag *tag, s_tag *dest)
{
assert(env);
@@ -1865,7 +1871,7 @@ bool env_eval_tag (s_env *env, const s_tag *tag, s_tag *dest)
case TAG_QUOTE:
return env_eval_quote(env, &tag->data.quote, dest);
case TAG_STRUCT:
- return env_eval_struct(env, &tag->data.struct_, dest);
+ return env_eval_struct_tag(env, &tag->data.struct_, dest);
case TAG_TIME:
return env_eval_time(env, &tag->data.time, dest);
case TAG_TUPLE:
diff --git a/libkc3/env.h b/libkc3/env.h
index d6a0e90..0ad1be9 100644
--- a/libkc3/env.h
+++ b/libkc3/env.h
@@ -167,7 +167,9 @@ bool env_eval_quote_unquote (s_env *env,
const s_unquote *unquote,
s_tag *dest);
bool env_eval_struct (s_env *env, const s_struct *s,
- s_tag *dest);
+ s_struct *dest);
+bool env_eval_struct_tag (s_env *env, const s_struct *s,
+ s_tag *dest);
bool env_eval_tag (s_env *env, const s_tag *tag,
s_tag *dest);
bool env_eval_time (s_env *env, const s_time *time,
diff --git a/libkc3/inspect.c b/libkc3/inspect.c
index 65afdaf..e3eddc8 100644
--- a/libkc3/inspect.c
+++ b/libkc3/inspect.c
@@ -27,7 +27,11 @@ s_str * inspect_array (const s_array *array, s_str *dest)
assert(! "inspect_array: buf_inspect_array_size error");
return NULL;
}
- buf_init_alloc(&tmp, size);
+ if (! buf_init_alloc(&tmp, size)) {
+ err_puts("inspect_array: buf_init alloc");
+ assert(! "inspect_array: buf_init_alloc");
+ return NULL;
+ }
buf_inspect_array(&tmp, array);
if (tmp.wpos != tmp.size) {
err_puts("inspect_array: tmp.wpos != tmp.size");
@@ -237,6 +241,37 @@ s_str * inspect_str (const s_str *str, s_str *dest)
return buf_to_str(&buf, dest);
}
+s_str * inspect_struct (const s_struct *s, s_str *dest)
+{
+ s_pretty pretty = {0};
+ sw size;
+ s_buf tmp;
+ size = buf_inspect_struct_size(&pretty, s);
+ if (size < 0) {
+ err_puts("inspect_struct: buf_inspect_struct_size error");
+ assert(! "inspect_struct: buf_inspect_struct_size error");
+ return NULL;
+ }
+ if (! buf_init_alloc(&tmp, size)) {
+ err_puts("inspect_struct: buf_init alloc");
+ assert(! "inspect_struct: buf_init_alloc");
+ return NULL;
+ }
+ if (buf_inspect_struct(&tmp, s) < 0) {
+ err_puts("inspect_struct: buf_inspect_struct error");
+ assert(! "inspect_struct: buf_inspect_struct error");
+ buf_clean(&tmp);
+ return NULL;
+ }
+ if (tmp.wpos != tmp.size) {
+ err_puts("inspect_struct: tmp.wpos != tmp.size");
+ assert(! "inspect_struct: tmp.wpos != tmp.size");
+ buf_clean(&tmp);
+ return NULL;
+ }
+ return buf_to_str(&tmp, dest);
+}
+
s_str * inspect_sym (const s_sym *sym, s_str *dest)
{
s_buf buf;
diff --git a/libkc3/inspect.h b/libkc3/inspect.h
index 6934c43..d2786dd 100644
--- a/libkc3/inspect.h
+++ b/libkc3/inspect.h
@@ -30,6 +30,7 @@ s_str * inspect_ident (const s_ident *ident, s_str *dest);
s_str * inspect_list (const s_list *list, s_str *dest);
s_str * inspect_ratio (const s_ratio *src, s_str *dest);
s_str * inspect_str (const s_str *str, s_str *dest);
+s_str * inspect_struct (const s_struct *s, s_str *dest);
s_str * inspect_sym (const s_sym *sym, s_str *dest);
s_str * inspect_tag (const s_tag *tag, s_str *dest);
s_str * inspect_tuple (const s_tuple *tuple, s_str *dest);
diff --git a/libkc3/pretty.h b/libkc3/pretty.h
index d701585..4a80faf 100644
--- a/libkc3/pretty.h
+++ b/libkc3/pretty.h
@@ -21,6 +21,8 @@
#include "types.h"
+#define PRETTY_INDENT 2
+
/* Operators. */
s_pretty * pretty_indent (s_pretty *pretty, sw indent);
diff --git a/libkc3/str.c b/libkc3/str.c
index dcf0f5c..0b1c275 100644
--- a/libkc3/str.c
+++ b/libkc3/str.c
@@ -42,12 +42,31 @@
{ \
s_buf buf; \
s_pretty pretty = {0}; \
+ sw r; \
sw size; \
size = buf_inspect_ ## name ## _size(&pretty, &x); \
- if (size <= 0) \
+ if (! size) \
+ return str_init_empty(str); \
+ if (size < 0) { \
+ err_puts("str_init_" # name ": buf_inspect_" # name \
+ "_size < 0"); \
+ return NULL; \
+ } \
+ if (! buf_init_alloc(&buf, size)) { \
+ err_puts("str_init_" # name ": buf_init_alloc"); \
+ return NULL; \
+ } \
+ if ((r = buf_inspect_ ## name(&buf, &x)) < 0) { \
+ err_puts("str_init_" # name ": buf_inspect_" # name " < 0"); \
+ buf_clean(&buf); \
return NULL; \
- buf_init_alloc(&buf, size); \
- if (buf_inspect_ ## name(&buf, &x) < 0) { \
+ } \
+ if (r != size) { \
+ err_write_1("str_init_" # name ": buf_inspect_" # name ": "); \
+ err_inspect_sw_decimal(&r); \
+ err_write_1(" != "); \
+ err_inspect_sw_decimal(&size); \
+ err_write_1("\n"); \
buf_clean(&buf); \
return NULL; \
} \
@@ -62,9 +81,17 @@
s_pretty pretty = {0}; \
sw size; \
size = buf_inspect_ ## type ## _decimal_size(&pretty, &x); \
- if (size <= 0) \
+ if (! size) \
+ return str_init_empty(str); \
+ if (size < 0) { \
+ err_puts("str_init_" # type ": buf_inspect_" # type \
+ "_size < 0"); \
+ return NULL; \
+ } \
+ if (! buf_init_alloc(&buf, size)) { \
+ err_puts("str_init_" # type ": buf_init_alloc"); \
return NULL; \
- buf_init_alloc(&buf, size); \
+ } \
buf_inspect_ ## type ## _decimal(&buf, &x); \
assert(buf.wpos == (uw) size); \
return buf_to_str(&buf, str); \
@@ -310,6 +337,8 @@ s_str * str_init_cast (s_str *str, const s_sym * const *type,
return str_init_u64(str, tag->data.u64);
case TAG_UW:
return str_init_uw(str, tag->data.uw);
+ case TAG_VAR:
+ return str_init_var(str, tag);
default:
break;
}
@@ -501,6 +530,41 @@ DEF_STR_INIT_INT(u32)
DEF_STR_INIT_INT(u64)
DEF_STR_INIT_INT(uw)
+s_str * str_init_var (s_str *str, const s_tag *x)
+{
+ s_buf buf;
+ s_pretty pretty = {0};
+ sw r;
+ sw size;
+ size = buf_inspect_var_size(&pretty, x);
+ if (! size)
+ return str_init_empty(str);
+ if (size < 0) {
+ err_puts("str_init_var: buf_inspect_var_size < 0");
+ return NULL;
+ }
+ if (! buf_init_alloc(&buf, size)) {
+ err_puts("str_init_var: buf_init_alloc");
+ return NULL;
+ }
+ if ((r = buf_inspect_var(&buf, x)) < 0) {
+ err_puts("str_init_var: buf_inspect_var < 0");
+ buf_clean(&buf);
+ return NULL;
+ }
+ if (r != size) {
+ err_write_1("str_init_var: buf_inspect_var: ");
+ err_inspect_sw_decimal(&r);
+ err_write_1(" != ");
+ err_inspect_sw_decimal(&size);
+ err_write_1("\n");
+ buf_clean(&buf);
+ return NULL;
+ }
+ assert(buf.wpos == (uw) size);
+ return buf_to_str(&buf, str);
+}
+
s_str * str_init_vf (s_str *str, const char *fmt, va_list ap)
{
int len;
diff --git a/libkc3/str.h b/libkc3/str.h
index 067a339..f7b05ce 100644
--- a/libkc3/str.h
+++ b/libkc3/str.h
@@ -72,6 +72,7 @@ PROTOTYPE_STR_INIT_INT(u16);
PROTOTYPE_STR_INIT_INT(u32);
PROTOTYPE_STR_INIT_INT(u64);
PROTOTYPE_STR_INIT_INT(uw);
+PROTOTYPE_STR_INIT(var, const s_tag *);
s_str * str_init_vf (s_str *str, const char *fmt, va_list ap);
/* Constructors, call str_delete after use */
diff --git a/libkc3/struct.c b/libkc3/struct.c
index 69ef986..7e70a99 100644
--- a/libkc3/struct.c
+++ b/libkc3/struct.c
@@ -13,6 +13,8 @@
#include <string.h>
#include "alloc.h"
#include "assert.h"
+#include "buf.h"
+#include "buf_parse.h"
#include "data.h"
#include "env.h"
#include "list.h"
@@ -180,13 +182,32 @@ s_struct * struct_init (s_struct *s, const s_sym *module)
return s;
}
-s_struct * struct_init_1 (s_struct *s, const s8 *p)
+s_struct * struct_init_1 (s_struct *s, const char *p)
{
+ s_buf buf;
+ uw len;
+ sw r;
+ s_struct tmp;
assert(s);
assert(p);
- (void) s;
- (void) p;
- // FIXME
+ len = strlen(p);
+ buf_init_const(&buf, len, p);
+ buf.wpos = len;
+ r = buf_parse_struct(&buf, &tmp);
+ if (r < 0 || (uw) r != len) {
+ err_puts("struct_init_1: invalid struct");
+ assert(! "struct_init_1: invalid struct");
+ if (r > 0)
+ struct_clean(&tmp);
+ return NULL;
+ }
+ if (! env_eval_struct(&g_kc3_env, &tmp, s)) {
+ err_puts("struct_init_1: env_eval_struct");
+ assert(! "struct_init_1: env_eval_struct");
+ struct_clean(&tmp);
+ return NULL;
+ }
+ struct_clean(&tmp);
return s;
}
@@ -341,7 +362,7 @@ s_struct * struct_new (const s_sym *module)
return s;
}
-s_struct * struct_new_1 (const s8 *p)
+s_struct * struct_new_1 (const char *p)
{
s_struct *s;
assert(p);
diff --git a/libkc3/struct.h b/libkc3/struct.h
index 4d747fd..7991b79 100644
--- a/libkc3/struct.h
+++ b/libkc3/struct.h
@@ -23,7 +23,7 @@
/* Stack-allocation compatible functions, call struct_clean after use. */
void struct_clean (s_struct *s);
s_struct * struct_init (s_struct *s, const s_sym *module);
-s_struct * struct_init_1 (s_struct *s, const s8 *p);
+s_struct * struct_init_1 (s_struct *s, const char *p);
s_struct * struct_init_cast (s_struct *s, const s_sym * const *type,
const s_tag *tag);
s_struct * struct_init_copy (s_struct *s, const s_struct *src);
@@ -36,7 +36,7 @@ s_struct * struct_init_with_data (s_struct *s, const s_sym *module,
/* Heap-allocation functions, call struct_delete after use. */
void struct_delete (s_struct *s);
s_struct * struct_new (const s_sym *module);
-s_struct * struct_new_1 (const s8 *p);
+s_struct * struct_new_1 (const char *p);
s_struct * struct_new_copy (const s_struct *src);
s_struct * struct_new_with_data (const s_sym *module, void *data);
diff --git a/test/ikc3/str.kc3 b/test/ikc3/str.kc3
index 2018a0b..8019691 100644
--- a/test/ikc3/str.kc3
+++ b/test/ikc3/str.kc3
@@ -50,6 +50,14 @@ quote "1 + 2 = #{1 + 2}"
"1 + 2 = #{1 + 2}"
quote "#{%{a: 1, b: 2}}"
"#{%{a: 1, b: 2}}"
+quote "#{:+}"
+"#{:+}"
+quote "#{?}"
+"#{?}"
+quote "#{0}"
+"#{0}"
+quote "#{:left}"
+"#{:left}"
quote "#{%KC3.Operator{}}"
"#{%KC3.Operator{}}"
quote "#{%KC3.Operator{sym: :-}}"
diff --git a/test/ikc3/str.out.expected b/test/ikc3/str.out.expected
index 1189555..f1c43f3 100644
--- a/test/ikc3/str.out.expected
+++ b/test/ikc3/str.out.expected
@@ -35,6 +35,14 @@
"1 + 2 = 3"
"#{%{a: 1, b: 2}}"
"%{a: 1, b: 2}"
+"#{:+}"
+"+"
+"#{?}"
+"?"
+"#{0}"
+"0"
+"#{:left}"
+"left"
"#{%KC3.Operator{}}"
"%KC3.Operator{sym: :+, symbol_value: ?, operator_precedence: 0, operator_associativity: :left}"
"#{%KC3.Operator{sym: :-, symbol_value: ?, operator_precedence: 0, operator_associativity: :left}}"
diff --git a/test/inspect_test.c b/test/inspect_test.c
index fbb4578..095995e 100644
--- a/test/inspect_test.c
+++ b/test/inspect_test.c
@@ -13,6 +13,7 @@
#include <assert.h>
#include <string.h>
#include "../libkc3/array.h"
+#include "../libkc3/buf_inspect.h"
#include "../libkc3/call.h"
#include "../libkc3/fact.h"
#include "../libkc3/ident.h"
@@ -23,6 +24,7 @@
#include "../libkc3/str.h"
#include "../libkc3/sym.h"
#include "../libkc3/tag.h"
+#include "../libkc3/struct.h"
#include "../libkc3/tuple.h"
#include "test.h"
@@ -126,7 +128,7 @@
test_context(NULL); \
} while (0)
-#define INSPECT_TEST_STR_1(test, expected) \
+#define INSPECT_TEST_STR_1(test, expected) \
do { \
s_str result; \
s_str str; \
@@ -138,6 +140,22 @@
test_context(NULL); \
} while (0)
+#define INSPECT_TEST_STRUCT(test, expected) \
+ do { \
+ s_pretty pretty = {0}; \
+ s_str result; \
+ s_struct struct_test; \
+ assert(test); \
+ test_context("inspect_struct(" # test ") -> " # expected); \
+ struct_init_1(&struct_test, (test)); \
+ TEST_EQ(buf_inspect_struct_size(&pretty, &struct_test), \
+ strlen(expected)); \
+ TEST_EQ(inspect_struct(&struct_test, &result), &result); \
+ TEST_STRNCMP(result.ptr.p, (expected), result.size); \
+ str_clean(&result); \
+ test_context(NULL); \
+ } while (0)
+
#define INSPECT_TEST_SYM(test, result) \
do { \
const s_sym *sym; \
@@ -177,6 +195,7 @@ TEST_CASE_PROTOTYPE(inspect_ident);
TEST_CASE_PROTOTYPE(inspect_list);
TEST_CASE_PROTOTYPE(inspect_ratio);
TEST_CASE_PROTOTYPE(inspect_str);
+TEST_CASE_PROTOTYPE(inspect_struct);
TEST_CASE_PROTOTYPE(inspect_sym);
TEST_CASE_PROTOTYPE(inspect_tuple);
@@ -190,6 +209,7 @@ void inspect_test (void)
TEST_CASE_RUN(inspect_list);
TEST_CASE_RUN(inspect_ratio);
TEST_CASE_RUN(inspect_str);
+ TEST_CASE_RUN(inspect_struct);
TEST_CASE_RUN(inspect_sym);
TEST_CASE_RUN(inspect_tuple);
}
@@ -395,6 +415,14 @@ TEST_CASE(inspect_str)
}
TEST_CASE_END(inspect_str)
+TEST_CASE(inspect_struct)
+{
+ INSPECT_TEST_STRUCT("%KC3.Operator{}", "%KC3.Operator{sym: :+, symbol_value: ?, operator_precedence: 0, operator_associativity: :left}");
+ INSPECT_TEST_STRUCT("%KC3.Operator{sym: :-}",
+ "%KC3.Operator{sym: :-, symbol_value: ?, operator_precedence: 0, operator_associativity: :left}");
+}
+TEST_CASE_END(inspect_struct)
+
TEST_CASE(inspect_sym)
{
INSPECT_TEST_SYM("", ":\"\"");