| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Comment preservation |
| 4 |
# |
| 5 |
|
| 6 |
use strict; |
| 7 |
use warnings; |
| 8 |
|
| 9 |
use Test::More tests => 17; |
| 10 |
|
| 11 |
use Config::IniFiles; |
| 12 |
|
| 13 |
use lib "./t/lib"; |
| 14 |
|
| 15 |
use Config::IniFiles::TestPaths; |
| 16 |
use Config::IniFiles::Slurp qw( slurp ); |
| 17 |
|
| 18 |
my $ors = $\ || "\n"; |
| 19 |
my $irs = $/ || "\n"; |
| 20 |
my ( $ini, $value ); |
| 21 |
|
| 22 |
# Load ini file and write as new file |
| 23 |
$ini = Config::IniFiles->new( -file => t_file("test.ini") ); |
| 24 |
$ini->SetFileName( t_file("test03.ini") ); |
| 25 |
$ini->SetWriteMode("0666"); |
| 26 |
t_unlink("test03.ini"); |
| 27 |
$ini->RewriteConfig; |
| 28 |
$ini->ReadConfig; |
| 29 |
|
| 30 |
# TEST |
| 31 |
ok( $ini, "ini is still initialised" ); |
| 32 |
|
| 33 |
sub t_slurp |
| 34 |
{ |
| 35 |
return slurp( t_file(@_) ); |
| 36 |
} |
| 37 |
|
| 38 |
# Section comments preserved |
| 39 |
my $contents = t_slurp("test03.ini"); |
| 40 |
|
| 41 |
# TEST |
| 42 |
ok( scalar( $contents =~ /\# This is a section comment[$ors]\[test1\]/ ), |
| 43 |
"Found section comment." ); |
| 44 |
|
| 45 |
# Parameter comments preserved |
| 46 |
# TEST |
| 47 |
ok( |
| 48 |
scalar( $contents =~ /\# This is a parm comment[$ors]five=value5/ ), |
| 49 |
"Contains Parm comment.", |
| 50 |
); |
| 51 |
|
| 52 |
# Setting Section Comment |
| 53 |
$ini->setval( 'foo', 'bar', 'qux' ); |
| 54 |
|
| 55 |
# TEST |
| 56 |
ok( |
| 57 |
$ini->SetSectionComment( |
| 58 |
'foo', |
| 59 |
'This is a section comment', |
| 60 |
'This comment takes two lines!' |
| 61 |
), |
| 62 |
"SetSectionComment returns a true value.", |
| 63 |
); |
| 64 |
|
| 65 |
# Getting Section Comment |
| 66 |
my @comment = $ini->GetSectionComment('foo'); |
| 67 |
|
| 68 |
# TEST |
| 69 |
is_deeply( |
| 70 |
\@comment, |
| 71 |
[ '# This is a section comment', '# This comment takes two lines!', ], |
| 72 |
"Section comments are OK.", |
| 73 |
); |
| 74 |
|
| 75 |
# This is a test for: https://rt.cpan.org/Ticket/Display.html?id=8612 |
| 76 |
# TEST |
| 77 |
is( |
| 78 |
scalar( $ini->GetSectionComment('foo') ), |
| 79 |
"# This is a section comment$irs# This comment takes two lines!", |
| 80 |
"GetSectionComment in scalar context returns a joined one.", |
| 81 |
); |
| 82 |
|
| 83 |
# Deleting Section Comment |
| 84 |
$ini->DeleteSectionComment('foo'); |
| 85 |
|
| 86 |
# Should not exist! |
| 87 |
# TEST |
| 88 |
ok( |
| 89 |
!defined( $ini->GetSectionComment('foo') ), |
| 90 |
"foo section comment does not exist", |
| 91 |
); |
| 92 |
|
| 93 |
# Setting Parameter Comment |
| 94 |
# TEST |
| 95 |
ok( |
| 96 |
$ini->SetParameterComment( |
| 97 |
'foo', 'bar', |
| 98 |
'This is a parameter comment', 'This comment takes two lines!' |
| 99 |
), |
| 100 |
"SetParameterCount was successful", |
| 101 |
); |
| 102 |
|
| 103 |
# Getting Parameter Comment |
| 104 |
@comment = $ini->GetParameterComment( 'foo', 'bar' ); |
| 105 |
|
| 106 |
# TEST |
| 107 |
is_deeply( |
| 108 |
\@comment, |
| 109 |
[ '# This is a parameter comment', '# This comment takes two lines!' ], |
| 110 |
"GetParameterComment returns the correct result.", |
| 111 |
); |
| 112 |
|
| 113 |
# TEST |
| 114 |
is( |
| 115 |
scalar( $ini->GetParameterComment( 'foo', 'bar' ) ), |
| 116 |
"# This is a parameter comment$irs# This comment takes two lines!", |
| 117 |
"GetParameterComment returns comments separated by newlines", |
| 118 |
); |
| 119 |
|
| 120 |
# Deleting Parameter Comment |
| 121 |
$ini->DeleteParameterComment( 'foo', 'bar' ); |
| 122 |
|
| 123 |
# Should not exist! |
| 124 |
# TEST |
| 125 |
ok( |
| 126 |
!defined( $ini->GetSectionComment( 'foo', 'bar' ) ), |
| 127 |
"GetSectionComment for foo/bar should not exist" |
| 128 |
); |
| 129 |
|
| 130 |
# Reading a section comment from the file |
| 131 |
@comment = $ini->GetSectionComment('test1'); |
| 132 |
|
| 133 |
# TEST |
| 134 |
is_deeply( |
| 135 |
\@comment, |
| 136 |
['# This is a section comment'], |
| 137 |
"A single section comment for test1", |
| 138 |
); |
| 139 |
|
| 140 |
# Reading a parameter comment from the file |
| 141 |
@comment = $ini->GetParameterComment( 'test2', 'five' ); |
| 142 |
|
| 143 |
# TEST |
| 144 |
is_deeply( |
| 145 |
\@comment, |
| 146 |
['# This is a parm comment'], |
| 147 |
"Reading a parameter comment from the file", |
| 148 |
); |
| 149 |
|
| 150 |
# Reading a comment that starts with ';' |
| 151 |
@comment = $ini->GetSectionComment('MixedCaseSect'); |
| 152 |
|
| 153 |
# TEST |
| 154 |
is_deeply( |
| 155 |
\@comment, |
| 156 |
['; This is a semi-colon comment'], |
| 157 |
"Singled Section Comment for MixedCaseSect. Reading a comment that starts with ;", |
| 158 |
); |
| 159 |
|
| 160 |
# Test 13 |
| 161 |
# Loading from a file with alternate comment characters |
| 162 |
# and also test continuation characters (in one file) |
| 163 |
$ini = Config::IniFiles->new( |
| 164 |
-file => t_file("cmt.ini"), |
| 165 |
-commentchar => '@', |
| 166 |
-allowcontinue => 1 |
| 167 |
); |
| 168 |
|
| 169 |
# TEST |
| 170 |
ok( $ini, "cmt.ini instance was properly initialised." ); |
| 171 |
|
| 172 |
$value = $ini->GetParameterComment( 'Library', 'addmultf_lib' ); |
| 173 |
|
| 174 |
# TEST |
| 175 |
ok( |
| 176 |
scalar( |
| 177 |
$value =~ |
| 178 |
/\@#\@CF Automatically created by 'config_project' at Thu Mar 21 08:46:54 2002/ |
| 179 |
), |
| 180 |
"Contains Parameter Comment starting with an at-sign.", |
| 181 |
); |
| 182 |
|
| 183 |
# Test 15 |
| 184 |
$value = $ini->val( 'turbo_library', 'TurboLibPaths' ); |
| 185 |
|
| 186 |
# TEST |
| 187 |
ok( |
| 188 |
scalar( |
| 189 |
$value =~ |
| 190 |
m{\$WORKAREA/resources/c11_test_flow/vhdl_rtl\s+\$WORKAREA/resources/cstarlib_reg_1v5/vhdl_rtl} |
| 191 |
), |
| 192 |
"value is OK." |
| 193 |
); |
| 194 |
|
| 195 |
# Clean up when we're done |
| 196 |
t_unlink("test03.ini"); |
| 197 |
|