| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | use strict; |
| 4 | use warnings; |
| 5 | |
| 6 | use Test::More tests => 9; |
| 7 | |
| 8 | use Config::IniFiles; |
| 9 | use File::Spec; |
| 10 | |
| 11 | use lib "./t/lib"; |
| 12 | use Config::IniFiles::TestPaths; |
| 13 | |
| 14 | use File::Temp qw(tempdir); |
| 15 | |
| 16 | # Test of handle_trailing_comment enabled |
| 17 | { |
| 18 | my $ini = Config::IniFiles->new( |
| 19 | -file => t_file("end-of-line-comment.ini"), |
| 20 | -handle_trailing_comment => 1 |
| 21 | ); |
| 22 | |
| 23 | # TEST |
| 24 | is( $ini->val( "section1", "param1" ), |
| 25 | "value1", |
| 26 | "Comments after ';' should be omitted when tailing comment enabled" ); |
| 27 | |
| 28 | # TEST |
| 29 | is( $ini->GetParameterTrailingComment( "section1", "param1" ), |
| 30 | "comment1", "Test GetParameterTrailingComment()" ); |
| 31 | |
| 32 | # Test write back |
| 33 | my $dirname = tempdir( CLEANUP => 1 ); |
| 34 | my $filename = |
| 35 | File::Spec->catfile( $dirname, "end-trailing-comment-writeback.ini" ); |
| 36 | |
| 37 | # TEST |
| 38 | ok( $ini->WriteConfig($filename), "Write trailing comments back" ); |
| 39 | |
| 40 | open my $fh, '<', $filename; |
| 41 | my $works = 0; |
| 42 | while ( my $line = <$fh> ) |
| 43 | { |
| 44 | $works = 1 if ( $line =~ /param1\s*=\s*value1\s*[;#]\s*comment1/ ); |
| 45 | } |
| 46 | close $fh; |
| 47 | |
| 48 | # TEST |
| 49 | ok( $works, "Test trailing comment rewrite ok." ); |
| 50 | |
| 51 | # Test set() |
| 52 | # TEST |
| 53 | ok( |
| 54 | $ini->SetParameterTrailingComment( |
| 55 | "section1", "param1", "changed comment1" |
| 56 | ), |
| 57 | "Test SetParameterTrailingComment() returns." |
| 58 | ); |
| 59 | |
| 60 | # TEST |
| 61 | is( |
| 62 | $ini->GetParameterTrailingComment( "section1", "param1" ), |
| 63 | "changed comment1", |
| 64 | "Test whether SetParameterTrailingComments() works." |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | # Test of handle_trailing_comment disabled |
| 69 | { |
| 70 | my $ini = Config::IniFiles->new( |
| 71 | -file => t_file("end-of-line-comment.ini"), |
| 72 | -handle_trailing_comment => 0 |
| 73 | ); |
| 74 | |
| 75 | # TEST |
| 76 | is( $ini->val( "section1", "param1" ), |
| 77 | "value1;comment1", |
| 78 | "Comments after ';' should be kept when tailing comment disabled" ); |
| 79 | |
| 80 | # TEST |
| 81 | is( $ini->GetParameterTrailingComment( "section1", "param1" ), |
| 82 | "", "Test whether SetParameterTrailingComments() works." ); |
| 83 | } |
| 84 | |
| 85 | # Test of default handle_trailing_comment |
| 86 | { |
| 87 | # The default handle_trailing_comment param should be off |
| 88 | my $ini = |
| 89 | Config::IniFiles->new( -file => t_file("end-of-line-comment.ini") ); |
| 90 | |
| 91 | # TEST |
| 92 | is( $ini->val( "section1", "param1" ), |
| 93 | "value1;comment1", |
| 94 | "Test default trailing comment, which should be off." ); |
| 95 | } |
| 96 |