Skip to content

Commit 29438aa

Browse files
committed
GH#79 : Got old+new tests to pass.
See #79 . "Specifying percentage for "all" breaks if both FORTDIR and LOCFORTDIR have fortunes #79"
1 parent a0e777b commit 29438aa

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

fortune-mod/fortune/fortune.c

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,7 @@ static int form_file_list(char **files, int file_cnt)
969969
for (i = 0; i < file_cnt; i++)
970970
{
971971
percent = NO_PROB;
972+
sp = NULL;
972973
if (!isdigit(files[i][0]))
973974
{
974975
sp = files[i];
@@ -1111,22 +1112,42 @@ static int form_file_list(char **files, int file_cnt)
11111112
lang = p;
11121113
}
11131114
/* default */
1115+
FILEDESC *system_dir_list = NULL, *local_list = NULL;
1116+
FILEDESC *system_dir_tail = NULL, *local_tail = NULL;
11141117
if (!ret)
11151118
{
1116-
ret = add_file(
1117-
percent, fullpathname, NULL, &File_list, &File_tail, NULL);
1119+
ret = add_file(100, fullpathname, NULL, &system_dir_list,
1120+
&system_dir_tail, NULL);
11181121
}
1119-
if (!ret &&
1120-
strncmp(fullpathname, locpathname, sizeof(fullpathname)))
1122+
if (strncmp(fullpathname, locpathname, sizeof(fullpathname)))
11211123
{
1122-
ret = add_file(
1123-
percent, locpathname, NULL, &File_list, &File_tail, NULL);
1124+
add_file(
1125+
100, locpathname, NULL, &local_list, &local_tail, NULL);
11241126
}
1125-
if (strncmp(fullpathname, locpathname, sizeof(fullpathname)) &&
1126-
strcmp(sp, "all") == 0)
1127+
if (!local_list)
11271128
{
1128-
add_file(
1129-
percent, locpathname, NULL, &File_list, &File_tail, NULL);
1129+
system_dir_tail->percent = percent;
1130+
File_tail = system_dir_tail;
1131+
File_list = system_dir_list;
1132+
}
1133+
else if (strncmp(fullpathname, locpathname, sizeof(fullpathname)) &&
1134+
strcmp(sp, "all") == 0)
1135+
{
1136+
FILEDESC *parent_node = new_fp();
1137+
parent_node->percent = percent;
1138+
parent_node->child = system_dir_list;
1139+
system_dir_list->next = local_list;
1140+
if (local_list)
1141+
{
1142+
local_list->prev = system_dir_list;
1143+
}
1144+
system_dir_list->parent = parent_node;
1145+
if (local_list)
1146+
{
1147+
local_list->parent = parent_node;
1148+
}
1149+
File_tail = File_list = parent_node;
1150+
ret = 1;
11301151
}
11311152
if (!ret)
11321153
{

fortune-mod/tests/t/test-fortune-percent.t

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use lib "$FindBin::Bin/lib";
99
use FortTestInst ();
1010

1111
use Path::Tiny qw/ cwd path tempdir tempfile /;
12-
use Test::More tests => 4;
12+
use Test::More tests => 7;
1313
use Test::Trap
1414
qw( trap $trap :flow:stderr(systemsafe):stdout(systemsafe):warn );
1515

@@ -81,9 +81,13 @@ EOF
8181
system(@cmd);
8282
};
8383

84-
TODO:
8584
{
86-
local $TODO = "Not fixed yet.";
85+
# TEST
86+
like(
87+
$trap->stderr(),
88+
qr/fortune: no place to put residual probability/ms,
89+
"percent overflow: https://github.com/shlomif/fortune-mod/issues/79 [all percent when local+system dirs have fortunes]"
90+
);
8791

8892
# TEST
8993
unlike(
@@ -93,4 +97,22 @@ EOF
9397
);
9498
}
9599
}
100+
101+
{
102+
my @cmd = ( $inst_dir->child( 'games', 'fortune' ), "art" );
103+
104+
print "Running [@cmd]\n";
105+
trap
106+
{
107+
system(@cmd);
108+
};
109+
110+
{
111+
# TEST
112+
like( $trap->stdout(), qr/\S/ms, "basic test", );
113+
114+
# TEST
115+
like( $trap->stderr(), qr/\A\r?\n?\z/ms, "basic test: stderr", );
116+
}
117+
}
96118
}

0 commit comments

Comments
 (0)