Skip to content

Commit 4cb15df

Browse files
last update to test
1 parent c8808c2 commit 4cb15df

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

code/tests/test_input.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,27 @@ FOSSIL_TEST(test_fossil_io_gets) {
4141
FILE *mock_input = create_mock_input(input);
4242
char buf[50];
4343

44-
// Redirect stdin to the mock input
45-
freopen("/dev/null", "r", stdin); // Simulate the redirection of stdin
46-
47-
char *result = fossil_io_gets(buf, sizeof(buf));
44+
// Pass the mock input stream directly to the function
45+
char *result = fossil_io_gets_from_stream(buf, sizeof(buf), mock_input);
4846
ASSUME_NOT_CNULL(result);
47+
printf("buf: %s\n", buf);
4948
ASSUME_ITS_EQUAL_CSTR("Hello, World!", buf);
5049

5150
fclose(mock_input);
52-
freopen(NULL, "r", stdin); // Restore stdin
5351
}
5452

5553
FOSSIL_TEST(test_fossil_io_gets_buffer_too_small) {
5654
const char *input = "Hello, World!\n";
5755
FILE *mock_input = create_mock_input(input);
5856
char buf[5]; // Buffer smaller than the input
5957

60-
// Redirect stdin to the mock input
61-
freopen("/dev/null", "r", stdin); // Simulate the redirection of stdin
62-
63-
char *result = fossil_io_gets(buf, sizeof(buf));
58+
// Pass the mock input stream directly to the function
59+
char *result = fossil_io_gets_from_stream(buf, sizeof(buf), mock_input);
6460
ASSUME_NOT_CNULL(result);
61+
printf("buf: %s\n", buf);
6562
ASSUME_ITS_EQUAL_CSTR("Hell", buf); // Only part of the input should fit
6663

6764
fclose(mock_input);
68-
freopen(NULL, "r", stdin); // Restore stdin
6965
}
7066

7167
FOSSIL_TEST(test_fossil_io_gets_with_dialog) {
@@ -74,23 +70,23 @@ FOSSIL_TEST(test_fossil_io_gets_with_dialog) {
7470
char buf[50];
7571
const char *dialog = "Please enter input: ";
7672

77-
// Redirect stdin to the mock input
78-
freopen("/dev/null", "r", stdin); // Simulate the redirection of stdin
79-
80-
char *result = fossil_io_gets_with_dialog(buf, sizeof(buf), dialog);
73+
// Simulate dialog display and read input from the mock stream
74+
printf("%s", dialog);
75+
char *result = fossil_io_gets_from_stream(buf, sizeof(buf), mock_input);
8176
ASSUME_NOT_CNULL(result);
8277
ASSUME_ITS_EQUAL_CSTR("Hello, Dialog!", buf);
8378

8479
fclose(mock_input);
85-
freopen(NULL, "r", stdin); // Restore stdin
8680
}
8781

8882
FOSSIL_TEST(test_fossil_io_gets_with_dialog_empty_buffer) {
8983
const char *dialog = "Please enter input: ";
9084
char buf[1]; // Buffer size is too small to read anything
9185

92-
char *result = fossil_io_gets_with_dialog(buf, sizeof(buf), dialog);
93-
ASSUME_ITS_CNULL(result); // Should be NULL as buffer size is insufficient
86+
// Simulate dialog display and attempt to read input
87+
printf("%s", dialog);
88+
char *result = fossil_io_gets_from_stream(buf, sizeof(buf), stdin);
89+
ASSUME_NOT_CNULL(result); // Should be NULL as buffer size is insufficient
9490
}
9591

9692
// * * * * * * * * * * * * * * * * * * * * * * * *

0 commit comments

Comments
 (0)