<commit_before>#include "test_save_statement.h"
#include "lpg_statement.h"
#include "test.h"
#include "lpg_allocate.h"
#include <string.h>
#include <stdlib.h>
#include "lpg_assert.h"
#include "lpg_for.h"
#include "lpg_stream_writer.h"
#include "lpg_save_expression.h"

static success_indicator save_statement(stream_writer to,
                                        statement const *value)
{
    switch (value->type)
    {
    case statement_assign:
        LPG_TRY(save_expression(to, value->assign.left));
        LPG_TRY(stream_writer_write_string(to, " = "));
        LPG_TRY(save_expression(to, value->assign.right));
        return stream_writer_write_string(to, "\n");

    case statement_return:
    case statement_expression:
        abort();
    }
    UNREACHABLE();
}

static void check_statement_rendering(statement tree, char const *expected)
{
    memory_writer buffer = {NULL, 0, 0};
    REQUIRE(save_statement(memory_writer_erase(&buffer), &tree) == success);
    REQUIRE(memory_writer_equals(buffer, expected));
    memory_writer_free(&buffer);
    statement_free(&tree);
}

void test_save_statement(void)
{
    {
        check_statement_rendering(
            statement_from_assign(assign_create(
                expression_allocate(
                    expression_from_identifier(unicode_string_from_c_str("a"))),
                expression_allocate(
                    expression_from_integer_literal(integer_create(0, 123))))),
            "a = 123\n");
    }
}<commit_msg>format return and expression statement<commit_after>#include "test_save_statement.h"
#include "lpg_statement.h"
#include "test.h"
#include "lpg_allocate.h"
#include <string.h>
#include <stdlib.h>
#include "lpg_assert.h"
#include "lpg_for.h"
#include "lpg_stream_writer.h"
#include "lpg_save_expression.h"

static success_indicator save_statement(stream_writer to,
                                        statement const *value)
{
    switch (value->type)
    {
    case statement_assign:
        LPG_TRY(save_expression(to, value->assign.left));
        LPG_TRY(stream_writer_write_string(to, " = "));
        LPG_TRY(save_expression(to, value->assign.right));
        return stream_writer_write_string(to, "\n");

    case statement_return:
        LPG_TRY(stream_writer_write_string(to, "return "));
        LPG_TRY(save_expression(to, value->return_));
        return stream_writer_write_string(to, "\n");

    case statement_expression:
        LPG_TRY(save_expression(to, value->expression));
        return stream_writer_write_string(to, "\n");
    }
    UNREACHABLE();
}

static void check_statement_rendering(statement tree, char const *expected)
{
    memory_writer buffer = {NULL, 0, 0};
    REQUIRE(save_statement(memory_writer_erase(&buffer), &tree) == success);
    REQUIRE(memory_writer_equals(buffer, expected));
    memory_writer_free(&buffer);
    statement_free(&tree);
}

void test_save_statement(void)
{
    {
        check_statement_rendering(
            statement_from_assign(assign_create(
                expression_allocate(
                    expression_from_identifier(unicode_string_from_c_str("a"))),
                expression_allocate(
                    expression_from_integer_literal(integer_create(0, 123))))),
            "a = 123\n");
    }
    {
        check_statement_rendering(
            statement_from_return(expression_allocate(
                expression_from_identifier(unicode_string_from_c_str("a")))),
            "return a\n");
    }
    {
        check_statement_rendering(
            statement_from_expression(expression_allocate(
                expression_from_identifier(unicode_string_from_c_str("a")))),
            "a\n");
    }
}
{'fraction_non_alphanumeric': 0.11195445920303605, 'fraction_numerical': 0.004879371103280022, 'mean_word_length': 3.178935447338618, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4982901', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1117, 'n_words': 174}