From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mail-a.sr.ht; dkim=pass header.d=johnnyrichard.com header.i=@johnnyrichard.com Received: from out-177.mta1.migadu.com (out-177.mta1.migadu.com [95.215.58.177]) by mail-a.sr.ht (Postfix) with ESMTPS id 5303220105 for <~johnnyrichard/olang-devel@lists.sr.ht>; Mon, 19 Feb 2024 20:07:07 +0000 (UTC) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1708373227; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vpJwX/GwV0gjHCSz1bjCK+df4y/tZLwMQsiRXsdLgVs=; b=aqkYR51/sS2Cs1oQkvgJOpDrXa5M4psLD7jFKLFPdFPTpyv1P6xvdz5zpI/iLbs4+UNIIZ dkV2HgtiBJacHNZVDAGQ9mILYyG+qk1DtTbkjkgvLtaibhBcAXxKSiPn8VdT1jLtgZdJGH Z/Rq0G4xcpkksxNOOQhpteeMLpRxdApQej7m8fOb7o4RCdYwbN7RUfW8O+rNA8bzhudqgw wQAFDl4fAYahZBo2Cm/buVNj90k7t0YdQvZ6etryc4JnqLgO8G3IJxnmDt1yI78Pr1hZ3c LdXF+hBlnEK9XOajhGf1hC5HWQqyt+/dhegw/kOslsBnLTLG6qIg+SL//SEtPg== From: Johnny Richard To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero , Johnny Richard Subject: [PATCH olang v4 4/4] lexer: test: add integration tests for --dump-tokens Date: Mon, 19 Feb 2024 22:04:11 +0100 Message-ID: <20240219210541.25624-5-johnny@johnnyrichard.com> In-Reply-To: <20240219210541.25624-1-johnny@johnnyrichard.com> References: <20240219210541.25624-1-johnny@johnnyrichard.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-TUID: 9yXO9JWTdUXt From: Carlos Maniero We want to test the 0c compiler in a black box way. This test explores `pipe` in order to handle input/output data, and it tests the --dump-tokens against the examples/main_exit.0. Signed-off-by: Johnny Richard --- src/0c.c | 1 - tests/integration/cli_runner.c | 47 ++++++++++++++++++++++++++++++---- tests/integration/cli_runner.h | 1 + tests/integration/cli_test.c | 14 ++++++++++ 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/0c.c b/src/0c.c index e84559d..978b770 100644 --- a/src/0c.c +++ b/src/0c.c @@ -75,7 +75,6 @@ main(int argc, char **argv) string_view_t file_content = read_entire_file(opts.file_path); - // TODO: missing integration test for lexer tokenizing lexer_t lexer = { 0 }; lexer_init(&lexer, file_content); diff --git a/tests/integration/cli_runner.c b/tests/integration/cli_runner.c index 0531bcc..7e4fe9a 100644 --- a/tests/integration/cli_runner.c +++ b/tests/integration/cli_runner.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #define OLANG_COMPILER_PATH "../../0c" @@ -62,16 +63,52 @@ create_tmp_file_name(char *file_name) } cli_result_t -cli_runner_compiler_dump_tokens(char *src) +cli_runner_compiler(char *src, char *args[]) { assert_compiler_exists(); - cli_result_t result; + cli_result_t result = { 0 }; create_tmp_file_name(result.program_path); - char command[1024]; - sprintf(command, "%s %s --dump-tokens", OLANG_COMPILER_PATH, src); + int fd_link[2]; + + if (pipe(fd_link) == -1) { + perror("pipe error."); + exit(1); + } + + pid_t pid = fork(); + + if (pid == -1) { + perror("fork error."); + exit(1); + } + + if (pid == 0) { + dup2(fd_link[1], STDOUT_FILENO); + close(fd_link[0]); + close(fd_link[1]); + + execv(OLANG_COMPILER_PATH, args); + perror("execl error."); + exit(127); + } else { + close(fd_link[1]); + if (read(fd_link[0], result.compiler_output, sizeof(result.compiler_output)) == -1) { + perror("read error."); + exit(1); + } + int status; + waitpid(pid, &status, 0); + result.exit_code = WEXITSTATUS(status); + } - result.exit_code = system(command); return result; } + +cli_result_t +cli_runner_compiler_dump_tokens(char *src) +{ + char *program_args[] = { "0c", "--dump-tokens", src, NULL }; + return cli_runner_compiler(src, program_args); +} diff --git a/tests/integration/cli_runner.h b/tests/integration/cli_runner.h index 8f4d69a..7ce4e7b 100644 --- a/tests/integration/cli_runner.h +++ b/tests/integration/cli_runner.h @@ -20,6 +20,7 @@ typedef struct cli_result_t { int exit_code; char program_path[255]; + char compiler_output[1024]; } cli_result_t; cli_result_t diff --git a/tests/integration/cli_test.c b/tests/integration/cli_test.c index ce2ed91..1fd70c7 100644 --- a/tests/integration/cli_test.c +++ b/tests/integration/cli_test.c @@ -23,6 +23,20 @@ test_cli_hello_file(const MunitParameter params[], void *user_data_or_fixture) { cli_result_t compilation_result = cli_runner_compiler_dump_tokens("../../examples/main_exit.0"); munit_assert_int(compilation_result.exit_code, ==, 0); + munit_assert_string_equal(compilation_result.compiler_output, + "../../examples/main_exit.0:1:1: \n" + "../../examples/main_exit.0:1:4: \n" + "../../examples/main_exit.0:1:8: <(>\n" + "../../examples/main_exit.0:1:9: <)>\n" + "../../examples/main_exit.0:1:10: <:>\n" + "../../examples/main_exit.0:1:12: \n" + "../../examples/main_exit.0:1:16: <{>\n" + "../../examples/main_exit.0:1:17: \n" + "../../examples/main_exit.0:2:3: \n" + "../../examples/main_exit.0:2:10: \n" + "../../examples/main_exit.0:2:11: \n" + "../../examples/main_exit.0:3:1: <}>\n" + "../../examples/main_exit.0:3:2: \n"); return MUNIT_OK; } -- 2.43.2