From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mail-a.sr.ht; dkim=pass header.d=maniero.me header.i=@maniero.me Received: from butterfly.birch.relay.mailchannels.net (butterfly.birch.relay.mailchannels.net [23.83.209.27]) by mail-a.sr.ht (Postfix) with ESMTPS id 73CF220098 for <~johnnyrichard/olang-devel@lists.sr.ht>; Mon, 19 Feb 2024 20:47:39 +0000 (UTC) X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id B01D5544429 for <~johnnyrichard/olang-devel@lists.sr.ht>; Mon, 19 Feb 2024 20:47:37 +0000 (UTC) Received: from fr-int-smtpout5.hostinger.io (unknown [127.0.0.6]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id 00798543F4C for <~johnnyrichard/olang-devel@lists.sr.ht>; Mon, 19 Feb 2024 20:47:36 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1708375657; a=rsa-sha256; cv=none; b=52aiE2TO67/Bpyn31PznhljHoWbXbQXauwX+43jdsUFW1Pk7IU3VBsVmjdsw+nE1WGnGCN 6+t3ApcVoJHu7yvZSspgxjFqysftauMNw+++w9VxgHsXXeRzFw9/x4zDVzTDz3TehX/4QP k/SW0hUtx/+71QtTqn0Ift9U8oyiWgFioalCb5GTRECudOwcLaWrwH/ElqgnFhFixmZg7I 70nOT/4wzJNg0hCo9LLDB7SBbfuI4JBx1yuOTBl3HypW1a08uer0lbJ6l8YReY2bbjvZWa Zd8tHvQokM96lYwPeMInqBHLZlI8pSJbQ9EK8Z4zoj9aaj+oTHWcQPCzmggpIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1708375657; 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:dkim-signature; bh=K1pVScq9ITBpwCSgCH4Tpc2ufQnBWSNtoa6gSt5hBe0=; b=wyd1D2+kMF6xHQYPZQqnQKCILrXjzAWOQ9g7DVZq3ilVxFw+7Pbt/BcQu1EKG0T67PIJ4F WIm+2IGTNM3OUMsezVZqO+oYhEqiTFTOMb6Ug3BmZQbC9iFE4X7tasWPidAxGjs+1LOevF swAFZ6hMnCBtkFcaMjtm9r6bMknX7UN2rn2LkM1PsVk3lMBJxUyjrpMW8SQj/iuENRTikF 4keKZmrOrJWjGHQrSJ764o1TI3IE6PDW6wN+Xn6pi+xS+d/LdDPsTqF6UpqdY+mfCGCg9O sW8u+PklIfuTktKw3QgI6jjanSKXZ/anY/rCKQVDhlLGtvH6IoZ3yJPvoIFusg== ARC-Authentication-Results: i=1; rspamd-55b4bfd7cb-6tclx; auth=pass smtp.auth=hostingeremail smtp.mailfrom=carlos@maniero.me X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me X-MC-Relay: Neutral X-MailChannels-SenderId: hostingeremail|x-authuser|carlos@maniero.me X-MailChannels-Auth-Id: hostingeremail X-Suffer-Sponge: 1cdcf07a3cbcfbb2_1708375657535_3886833221 X-MC-Loop-Signature: 1708375657535:4052885672 X-MC-Ingress-Time: 1708375657535 Received: from fr-int-smtpout5.hostinger.io ([UNAVAILABLE]. [89.116.146.168]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.126.181.103 (trex/6.9.2); Mon, 19 Feb 2024 20:47:37 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1708375655; 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=K1pVScq9ITBpwCSgCH4Tpc2ufQnBWSNtoa6gSt5hBe0=; b=dqyVIggvCw16iFt4AQyIGOE749bDfC9nVCKPS3TyYJSL6BmNUUzEgxzE2tm64ZT6llLLcy dxzY+Go7r3W5bl3pqkq0lJXtw4MZ5ymlXFNKGh32FrOhMNGQGNSWcmDXR+A+fjsIVA9Ez6 /wJ9jZ3LYrakxXzHZJCRnUQROpjOr9dzEF0o71EMElwDm8z/IUJSwpuC3hcNm0VS9+wHrj hZ9mmU3PysjekYnMX1qQj3oH5mA/6iHemHjChwiroSGG47Qq+oI+J1HrmuFp1xHh7xsTn4 3J4D6pdAdwsH4/eCODSznQjXSVWbwV0xyzpt9BlwqnYGaU2lIYlQkciu8+ov1w== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero , Johnny Richard Subject: [PATCH olang v5 4/4] lexer: test: add integration tests for --dump-tokens Date: Mon, 19 Feb 2024 17:42:52 -0300 Message-Id: <20240219204252.1642408-1-carlos@maniero.me> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240219211240.25871-1-johnny@johnnyrichard.com> References: <20240219211240.25871-1-johnny@johnnyrichard.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-Analysis: v=2.4 cv=FdIxxo+6 c=1 sm=1 tr=0 ts=65d3be66 a=5+VMC1FZ3J4mVPAKpPmAqg==:117 a=5+VMC1FZ3J4mVPAKpPmAqg==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=BXDaF_L80NYA:10 a=jziKOLAnAAAA:8 a=mrBPhR1UejvE6VmIkLwA:9 a=iEmw0ldr59FXw1_HsSWy:22 a=1ljl-wuu_96014yfT5Vk:22 X-CM-Envelope: MS4xfHHmUmaZBaw6pnllb9wljfR17zWetSnlHOlS45w8PTeqN1qihWxyv1uDts17t0wkMcrddioysiI/IUqgedUfPAN8Hm/UReaLnN3y8jVRNplO7DV6ejdp 6s044Aybd74lFWQz+EtvnVPxRNRTWt9NUb2D0T/MwX8VrhWcd1g4pghZJ03IW4BGa4cosnWkPLDsABmMytlYdMlkW2i2InfUy6fMEJzHtPII0xHNnJEapabt 1uq70hNM204457yJduNXarleUUiBQBBumQ9xeH7z6bGAIzKSkQpzBrgvUhZqPC8W X-AuthUser: carlos@maniero.me X-TUID: T/pl4etig5/8 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 | 15 +++++++++++ 4 files changed, 58 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..cb1a7df 100644 --- a/tests/integration/cli_test.c +++ b/tests/integration/cli_test.c @@ -23,6 +23,21 @@ 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" + "../../examples/main_exit.0:4:1: \n"); return MUNIT_OK; } -- 2.43.2