App-mdee
view release on metacpan or search on metacpan
t/01_mdee.t view on Meta::CPAN
# Helper: count output lines from fold
my $fold_lines = sub {
my ($input) = @_;
my ($fh, $tmp) = tempfile(SUFFIX => '.md', UNLINK => 1);
print $fh $input;
close $fh;
my $out = `$mdee --no-nup --no-table --fold --width=40 --mode=light $tmp 2>&1`;
return split /\n/, $out;
};
# 1. (traditional) should fold
ok($fold_lines->("1. $long\n") > 1, '1. list item is folded');
# 1) (CommonMark paren) should fold
ok($fold_lines->("1) $long\n") > 1, '1) list item is folded');
# #. (Pandoc auto-number) should fold
ok($fold_lines->(" #. $long\n") > 1, '#. list item is folded');
# #) should fold
ok($fold_lines->(" #) $long\n") > 1, '#) list item is folded');
};
# Test: md module table formatting (actual execution)
subtest 'md module table execution' => sub {
# Run with table formatting enabled
my $out = `$mdee --no-nup --no-fold --table $test_md 2>&1`;
is($?, 0, 'mdee with table exits successfully');
# Table should be formatted with aligned columns
# The separator line |---|---|---| should have consistent dashes
use Encode 'decode_utf8';
like(decode_utf8($out), qr/â+â¼â+â¼â+/, 'table separator is formatted with box-drawing');
# Check that ANSI sequences are present
like($out, qr/\e\[/, 'output contains ANSI escape sequences');
# --no-rule: ASCII separators instead of box-drawing
my $norule = decode_utf8(`$mdee --no-nup --no-fold --table --no-rule $test_md 2>&1`);
is($?, 0, 'mdee with --no-rule exits successfully');
like($norule, qr/[-]+\|[-]+/, '--no-rule produces ASCII separator');
unlike($norule, qr/[ââ¼â¤ââ]/, '--no-rule does not produce box-drawing chars');
};
# Test: combined execution (fold + table)
subtest 'combined execution' => sub {
my $out = `$mdee --no-nup --fold --table --width=60 $test_md 2>&1`;
is($?, 0, 'mdee with fold+table exits successfully');
like($out, qr/\e\[/, 'output contains ANSI escape sequences');
# Both table formatting and text should be present
like($out, qr/greple.*Pattern matching/s, 'table content is present');
};
# Test: show option (verify actual output behavior)
subtest 'show option' => sub {
# Helper: check if text has ANSI color directly applied
# Markers and content may be colored separately (emphasis_mark),
# so match ANSI before either the marker or the content.
sub has_ansi_around {
my ($out, $text) = @_;
return $out =~ /\e\[[0-9;]*m\Q$text\E/;
}
sub has_bold_coloring {
my ($out) = @_;
# Markers (**) are colored with emphasis_mark, content with bold
return $out =~ /\e\[[0-9;]*m\*\*.*bold text.*\*\*/;
}
sub has_italic_coloring {
my ($out) = @_;
return $out =~ /\e\[[0-9;]*m_.*italic text.*_/;
}
# Default: bold should be colored (--no-theme to test with markers visible)
my $default = `$mdee -f --no-theme $test_md 2>&1`;
ok(has_bold_coloring($default), 'default has bold formatting');
# --show bold=0: bold should NOT be colored
my $no_bold = `$mdee -f --no-theme --show bold=0 $test_md 2>&1`;
ok(!has_bold_coloring($no_bold), '--show bold=0 disables bold');
# --show italic=0: italic should NOT be colored
my $no_italic = `$mdee -f --no-theme --show italic=0 $test_md 2>&1`;
ok(!has_italic_coloring($no_italic), '--show italic=0 disables italic');
# --show all= disables all formatting
my $all_off = `$mdee -f --no-theme '--show=all=' $test_md 2>&1`;
ok(!has_bold_coloring($all_off), '--show all= disables bold');
# --show all= --show bold: only bold colored
my $only_bold = `$mdee -f --no-theme '--show=all=' --show=bold $test_md 2>&1`;
ok(has_bold_coloring($only_bold), '--show all= --show bold enables bold');
ok(!has_italic_coloring($only_bold), '--show all= --show bold disables italic');
# unknown field should error
my $unknown = `$mdee --dryrun --show unknown $test_md 2>&1`;
like($unknown, qr/unknown field/, '--show unknown produces error');
};
# Test: config file defaults
subtest 'config file defaults' => sub {
use File::Temp qw(tempdir);
my $tmpdir = tempdir(CLEANUP => 1);
my $config_dir = "$tmpdir/mdee";
mkdir $config_dir;
# Test default[style]
{
open my $fh, '>', "$config_dir/config.sh" or die;
print $fh "default[style]='pager'\n";
close $fh;
my $out = `XDG_CONFIG_HOME=$tmpdir $mdee --dryrun --mode=light $test_md 2>&1`;
like($out, qr/run_pager/, 'default[style]=pager adds pager');
unlike($out, qr/run_nup/, 'default[style]=pager removes nup');
}
# Test default[width]
{
open my $fh, '>', "$config_dir/config.sh" or die;
print $fh "default[width]=40\n";
close $fh;
my $out = `XDG_CONFIG_HOME=$tmpdir $mdee -ddn --mode=light $test_md 2>&1`;
like($out, qr/foldwidth=40\b/, 'default[width]=40 sets fold width');
}
# Test default[base_color]
{
open my $fh, '>', "$config_dir/config.sh" or die;
print $fh "default[base_color]='Crimson'\n";
close $fh;
my $out = `XDG_CONFIG_HOME=$tmpdir $mdee -dd --dryrun --mode=light $test_md 2>&1`;
like($out, qr/base_color=.*Crimson/, 'default[base_color]=Crimson is passed via config');
}
# Test command-line overrides config defaults
{
open my $fh, '>', "$config_dir/config.sh" or die;
print $fh "default[style]='pager'\ndefault[width]=40\n";
close $fh;
my $out = `COLUMNS=200 XDG_CONFIG_HOME=$tmpdir $mdee --dryrun --mode=light -s nup -w 120 $test_md 2>&1`;
like($out, qr/run_nup/, '-s nup overrides default[style]=pager');
}
# Test custom theme defined in config.sh
{
open my $fh, '>', "$config_dir/config.sh" or die;
print $fh <<'CONF';
theme_light[base]='<DarkCyan>=y25'
theme_dark[base]='<DarkCyan>=y80'
CONF
close $fh;
my $out = `XDG_CONFIG_HOME=$tmpdir $mdee -d --dryrun --mode=light $test_md 2>&1`;
like($out, qr/DarkCyan/, 'custom theme from config.sh is loaded');
( run in 0.867 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )