Skip to content

Commit

Permalink
Merge pull request #302 from gosub-browser/improve-c-api-test
Browse files Browse the repository at this point in the history
improve the C API test
  • Loading branch information
Kiyoshika authored Dec 2, 2023
2 parents a605d33 + 533f875 commit 7497294
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 120 deletions.
16 changes: 11 additions & 5 deletions crates/gosub-bindings/include/nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@
#include <stdlib.h>

#include "nodes/text.h"
#include "properties.h"

struct node_t *render_tree_node_init();
void render_tree_node_free(struct node_t **node);

enum node_type_e { NODE_TYPE_ROOT = 0u, NODE_TYPE_TEXT };

struct position_t {
double x;
double y;
};

struct node_t {
enum node_type_e type;
struct position_t position;
struct rectangle_t margin;
struct rectangle_t padding;
union data {
bool root; // NODE_TYPE_ROOT
struct node_text_t text; // NODE_TYPE_TEXT
Expand All @@ -30,6 +28,14 @@ struct node_t {
struct node_t *render_tree_node_init();
double render_tree_node_get_x(const struct node_t *node);
double render_tree_node_get_y(const struct node_t *node);
double render_tree_node_get_margin_top(const struct node_t *node);
double render_tree_node_get_margin_left(const struct node_t *node);
double render_tree_node_get_margin_right(const struct node_t *node);
double render_tree_node_get_margin_bottom(const struct node_t *node);
double render_tree_node_get_padding_top(const struct node_t *node);
double render_tree_node_get_padding_left(const struct node_t *node);
double render_tree_node_get_padding_right(const struct node_t *node);
double render_tree_node_get_padding_bottom(const struct node_t *node);
void render_tree_node_free_data(struct node_t *node);
void render_tree_node_free(struct node_t **node);

Expand Down
8 changes: 4 additions & 4 deletions crates/gosub-bindings/include/nodes/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ struct node_text_t {

void render_tree_node_text_free_data(struct node_text_t *text);

const char *render_tree_node_text_value(const struct node_t *node);
const char *render_tree_node_text_font(const struct node_t *node);
double render_tree_node_text_font_size(const struct node_t *node);
bool render_tree_node_text_bold(const struct node_t *node);
const char *render_tree_node_text_get_value(const struct node_t *node);
const char *render_tree_node_text_get_font(const struct node_t *node);
double render_tree_node_text_get_font_size(const struct node_t *node);
bool render_tree_node_text_get_bold(const struct node_t *node);

#endif
16 changes: 16 additions & 0 deletions crates/gosub-bindings/include/properties.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef GOSUB_API_PROPERTIES_H
#define GOSUB_API_PROPERTIES_H

struct position_t {
double x;
double y;
};

struct rectangle_t {
double top;
double left;
double right;
double bottom;
};

#endif
2 changes: 1 addition & 1 deletion crates/gosub-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub unsafe extern "C" fn gosub_render_tree_next_node(
pub unsafe extern "C" fn gosub_render_tree_get_node_data(node: *const Node, c_node: *mut CNode) {
// Change this to a match when we have more types
if let NodeType::Text(text_node) = &(*node).node_type {
(*c_node) = CNode::new_text(&(*node).position, text_node);
*c_node = CNode::new_text(&*node, text_node);
}
}

Expand Down
32 changes: 32 additions & 0 deletions crates/gosub-bindings/src/nodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,38 @@ double render_tree_node_get_y(const struct node_t *node) {
return node->position.y;
}

double render_tree_node_get_margin_top(const struct node_t *node) {
return node->margin.top;
}

double render_tree_node_get_margin_left(const struct node_t *node) {
return node->margin.left;
}

double render_tree_node_get_margin_right(const struct node_t *node) {
return node->margin.right;
}

double render_tree_node_get_margin_bottom(const struct node_t *node) {
return node->margin.bottom;
}

double render_tree_node_get_padding_top(const struct node_t *node) {
return node->padding.top;
}

double render_tree_node_get_padding_left(const struct node_t *node) {
return node->padding.left;
}

double render_tree_node_get_padding_right(const struct node_t *node) {
return node->padding.right;
}

double render_tree_node_get_padding_bottom(const struct node_t *node) {
return node->padding.bottom;
}

void render_tree_node_free(struct node_t **node) {
free(*node);
*node = NULL;
Expand Down
8 changes: 4 additions & 4 deletions crates/gosub-bindings/src/nodes/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ void render_tree_node_text_free_data(struct node_text_t *text) {
text->font = NULL;
}

const char *render_tree_node_text_value(const struct node_t *node) {
const char *render_tree_node_text_get_value(const struct node_t *node) {
if (!node)
return NULL;

return (const char *)node->data.text.value;
}

const char *render_tree_node_text_font(const struct node_t *node) {
const char *render_tree_node_text_get_font(const struct node_t *node) {
if (!node)
return NULL;

return (const char *)node->data.text.font;
}

double render_tree_node_text_font_size(const struct node_t *node) {
double render_tree_node_text_get_font_size(const struct node_t *node) {
if (!node)
return 0.0;

return node->data.text.font_size;
}

bool render_tree_node_text_bold(const struct node_t *node) {
bool render_tree_node_text_get_bold(const struct node_t *node) {
if (!node)
return false;

Expand Down
13 changes: 10 additions & 3 deletions crates/gosub-bindings/src/wrapper/node.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use gosub_engine::render_tree::{text::TextNode, Position};
use gosub_engine::render_tree::properties::{Position, Rectangle};
use gosub_engine::render_tree::{text::TextNode, Node};

use crate::wrapper::{text::CTextNode, CNodeType};

Expand All @@ -12,6 +13,8 @@ pub enum CNodeData {
pub struct CNode {
pub tag: CNodeType,
pub position: Position,
pub margin: Rectangle,
pub padding: Rectangle,
pub data: CNodeData,
}

Expand All @@ -20,6 +23,8 @@ impl Default for CNode {
Self {
tag: CNodeType::Root,
position: Position::new(),
margin: Rectangle::new(),
padding: Rectangle::new(),
data: CNodeData::Root(true),
}
}
Expand All @@ -30,10 +35,12 @@ impl CNode {
Self::default()
}

pub fn new_text(position: &Position, text_node: &TextNode) -> Self {
pub fn new_text(node: &Node, text_node: &TextNode) -> Self {
Self {
tag: CNodeType::Text,
position: (*position).clone(),
position: node.position.clone(),
margin: node.margin.clone(),
padding: node.padding.clone(),
data: CNodeData::Text(CTextNode::from(text_node)),
}
}
Expand Down
102 changes: 56 additions & 46 deletions crates/gosub-bindings/tests/render_tree_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,80 +25,90 @@ int main() {

const double tol = 0.00001;

// TODO: it'll be good at some point in the future to have the
// margins to compute the expected_y position instead of manually
// doing math. This will make the tests more robust if we change
// margins/etc. in the engine.
double y = 0.00;

// <h1>
node = render_tree_next(&render_tree);
y += render_tree_node_get_margin_top(node);
assert(node->type == NODE_TYPE_TEXT);
assert(strcmp(node->data.text.value, "this is heading 1") == 0);
assert(strcmp(node->data.text.font, "Times New Roman") == 0);
assert(fabs(node->data.text.font_size - 37.0) < 0.00001);
assert(node->data.text.is_bold == true);
assert(fabs(node->position.x - 0.00) < tol);
assert(fabs(node->position.y - 10.72) < tol);
assert(strcmp(render_tree_node_text_get_value(node), "this is heading 1") == 0);
assert(strcmp(render_tree_node_text_get_font(node), "Times New Roman") == 0);
assert(fabs(render_tree_node_text_get_font_size(node) - 37.0) < tol);
assert(render_tree_node_text_get_bold(node) == true);
assert(fabs(render_tree_node_get_x(node) - 0.00) < tol);
assert(fabs(render_tree_node_get_y(node) - y) < tol);
y += (render_tree_node_text_get_font_size(node) + render_tree_node_get_margin_bottom(node));

// <h2>
node = render_tree_next(&render_tree);
y += render_tree_node_get_margin_top(node);
assert(node->type == NODE_TYPE_TEXT);
assert(strcmp(node->data.text.value, "this is heading 2") == 0);
assert(strcmp(node->data.text.font, "Times New Roman") == 0);
assert(fabs(node->data.text.font_size - 27.5) < 0.00001);
assert(node->data.text.is_bold == true);
assert(fabs(node->position.x - 0.00) < tol);
assert(fabs(node->position.y - 68.4) < tol);
assert(strcmp(render_tree_node_text_get_value(node), "this is heading 2") == 0);
assert(strcmp(render_tree_node_text_get_font(node), "Times New Roman") == 0);
assert(fabs(render_tree_node_text_get_font_size(node) - 27.5) < tol);
assert(render_tree_node_text_get_bold(node) == true);
assert(fabs(render_tree_node_get_x(node) - 0.00) < tol);
assert(fabs(render_tree_node_get_y(node) - y) < tol);
y += (render_tree_node_text_get_font_size(node) + render_tree_node_get_margin_bottom(node));

// <h3>
node = render_tree_next(&render_tree);
y += render_tree_node_get_margin_top(node);
assert(node->type == NODE_TYPE_TEXT);
assert(strcmp(node->data.text.value, "this is heading 3") == 0);
assert(strcmp(node->data.text.font, "Times New Roman") == 0);
assert(fabs(node->data.text.font_size - 21.5) < 0.00001);
assert(node->data.text.is_bold == true);
assert(fabs(node->position.x - 0.00) < tol);
assert(fabs(node->position.y - 115.22) < tol);
assert(strcmp(render_tree_node_text_get_value(node), "this is heading 3") == 0);
assert(strcmp(render_tree_node_text_get_font(node), "Times New Roman") == 0);
assert(fabs(render_tree_node_text_get_font_size(node) - 21.5) < tol);
assert(render_tree_node_text_get_bold(node) == true);
assert(fabs(render_tree_node_get_x(node) - 0.00) < tol);
assert(fabs(render_tree_node_get_y(node) - y) < tol);
y += (render_tree_node_text_get_font_size(node) + render_tree_node_get_margin_bottom(node));

// <h4>
node = render_tree_next(&render_tree);
y += render_tree_node_get_margin_top(node);
assert(node->type == NODE_TYPE_TEXT);
assert(strcmp(node->data.text.value, "this is heading 4") == 0);
assert(strcmp(node->data.text.font, "Times New Roman") == 0);
assert(fabs(node->data.text.font_size - 18.5) < 0.00001);
assert(node->data.text.is_bold == true);
assert(fabs(node->position.x - 0.00) < tol);
assert(fabs(node->position.y - 156.72) < tol);
assert(strcmp(render_tree_node_text_get_value(node), "this is heading 4") == 0);
assert(strcmp(render_tree_node_text_get_font(node), "Times New Roman") == 0);
assert(fabs(render_tree_node_text_get_font_size(node) - 18.5) < tol);
assert(render_tree_node_text_get_bold(node) == true);
assert(fabs(render_tree_node_get_x(node) - 0.00) < tol);
assert(fabs(render_tree_node_get_y(node) - y) < tol);
y += (render_tree_node_text_get_font_size(node) + render_tree_node_get_margin_bottom(node));

// <h5>
node = render_tree_next(&render_tree);
y += render_tree_node_get_margin_top(node);
assert(node->type == NODE_TYPE_TEXT);
assert(strcmp(node->data.text.value, "this is heading 5") == 0);
assert(strcmp(node->data.text.font, "Times New Roman") == 0);
assert(fabs(node->data.text.font_size - 15.5) < 0.00001);
assert(node->data.text.is_bold == true);
assert(fabs(node->position.x - 0.00) < tol);
assert(fabs(node->position.y - 196.949) < tol);
assert(strcmp(render_tree_node_text_get_value(node), "this is heading 5") == 0);
assert(strcmp(render_tree_node_text_get_font(node), "Times New Roman") == 0);
assert(fabs(render_tree_node_text_get_font_size(node) - 15.5) < tol);
assert(render_tree_node_text_get_bold(node) == true);
assert(fabs(render_tree_node_get_x(node) - 0.00) < tol);
assert(fabs(render_tree_node_get_y(node) - y) < tol);
y += (render_tree_node_text_get_font_size(node) + render_tree_node_get_margin_bottom(node));

// <h6>
node = render_tree_next(&render_tree);
y += render_tree_node_get_margin_top(node);
assert(node->type == NODE_TYPE_TEXT);
assert(strcmp(node->data.text.value, "this is heading 6") == 0);
assert(strcmp(node->data.text.font, "Times New Roman") == 0);
assert(fabs(node->data.text.font_size - 12.0) < 0.00001);
assert(node->data.text.is_bold == true);
assert(fabs(node->position.x - 0.00) < tol);
assert(fabs(node->position.y - 236.027) < tol);
assert(strcmp(render_tree_node_text_get_value(node), "this is heading 6") == 0);
assert(strcmp(render_tree_node_text_get_font(node), "Times New Roman") == 0);
assert(fabs(render_tree_node_text_get_font_size(node) - 12.0) < tol);
assert(render_tree_node_text_get_bold(node) == true);
assert(fabs(render_tree_node_get_x(node) - 0.00) < tol);
assert(fabs(render_tree_node_get_y(node) - y) < tol);
y += (render_tree_node_text_get_font_size(node) + render_tree_node_get_margin_bottom(node));

// <p>
node = render_tree_next(&render_tree);
y += render_tree_node_get_margin_top(node);
assert(node->type == NODE_TYPE_TEXT);
assert(strcmp(node->data.text.value, "this is a paragraph") == 0);
assert(strcmp(node->data.text.font, "Times New Roman") == 0);
assert(fabs(node->data.text.font_size - 18.5) < 0.00001);
assert(node->data.text.is_bold == false);
assert(fabs(node->position.x - 0.00) < tol);
assert(fabs(node->position.y - 268.516) < tol);
assert(strcmp(render_tree_node_text_get_value(node), "this is a paragraph") == 0);
assert(strcmp(render_tree_node_text_get_font(node), "Times New Roman") == 0);
assert(fabs(render_tree_node_text_get_font_size(node) - 18.5) < tol);
assert(render_tree_node_text_get_bold(node) == false);
assert(fabs(render_tree_node_get_x(node) - 0.00) < tol);
assert(fabs(render_tree_node_get_y(node) - y) < tol);

// end of iterator, last node is free'd
node = render_tree_next(&render_tree);
Expand Down
Loading

0 comments on commit 7497294

Please sign in to comment.