| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # This script attempts to reproduce: |
| 4 | # https://github.com/shlomif/perl-Config-IniFiles/issues/14 |
| 5 | |
| 6 | # " Could you please document whether Unicode is supported in the config |
| 7 | # values? " |
| 8 | |
| 9 | # Written by Shlomi Fish. |
| 10 | # This file is licensed under the MIT/Expat License. |
| 11 | |
| 12 | use strict; |
| 13 | use warnings; |
| 14 | use autodie; |
| 15 | use utf8; |
| 16 | |
| 17 | use Test::More tests => 12; |
| 18 | |
| 19 | use lib "./t/lib"; |
| 20 | use Config::IniFiles; |
| 21 | use File::Spec; |
| 22 | use Config::IniFiles::Slurp qw( slurp utf8_slurp utf8_spew ); |
| 23 | |
| 24 | use File::Temp qw(tempdir); |
| 25 | |
| 26 | { |
| 27 | my $dir_name = tempdir( CLEANUP => 1 ); |
| 28 | my $filename = File::Spec->catfile( $dir_name, "utf8-test.ini" ); |
| 29 | { |
| 30 | utf8_spew( $filename, <<'EOF' ); |
| 31 | [section] |
| 32 | |
| 33 | mykey = tén |
| 34 | |
| 35 | EOF |
| 36 | } |
| 37 | |
| 38 | { |
| 39 | open my $in, '<:encoding(utf8)', $filename |
| 40 | or die "Cannot open '$filename' for slurping - $!"; |
| 41 | |
| 42 | my $ini = Config::IniFiles->new( -file => $in, ); |
| 43 | close($in); |
| 44 | |
| 45 | # TEST |
| 46 | ok( ($ini), "Ini was initialised" ); |
| 47 | |
| 48 | # TEST |
| 49 | is( scalar(@Config::IniFiles::errors), 0, "There are no errors." ); |
| 50 | |
| 51 | # TEST |
| 52 | ok( $ini->SectionExists('section'), "section exists" ); |
| 53 | |
| 54 | # TEST |
| 55 | ok( $ini->exists( 'section', 'mykey' ), "section catches parameter" ); |
| 56 | |
| 57 | # TEST |
| 58 | is( $ini->val( 'section', 'mykey' ), qq#tén#, "->val() parameter" ); |
| 59 | |
| 60 | $ini->setval( 'section', 'mykey', 'meö שלום' ); |
| 61 | |
| 62 | # TEST |
| 63 | is( |
| 64 | $ini->val( 'section', 'mykey' ), |
| 65 | 'meö שלום', |
| 66 | "setval( ) ; ->val() parameter" |
| 67 | ); |
| 68 | |
| 69 | # TEST |
| 70 | ok( scalar( $ini->WriteConfig( $filename, ) ), "WriteConfig" ); |
| 71 | } |
| 72 | |
| 73 | { |
| 74 | open my $in, '<:encoding(utf8)', $filename |
| 75 | or die "Cannot open '$filename' for slurping - $!"; |
| 76 | |
| 77 | my $ini = Config::IniFiles->new( -file => $in, ); |
| 78 | close($in); |
| 79 | |
| 80 | # TEST |
| 81 | ok( ($ini), "Ini was initialised" ); |
| 82 | |
| 83 | # TEST |
| 84 | is( scalar(@Config::IniFiles::errors), 0, "There are no errors." ); |
| 85 | |
| 86 | # TEST |
| 87 | ok( $ini->SectionExists('section'), "section exists" ); |
| 88 | |
| 89 | # TEST |
| 90 | ok( $ini->exists( 'section', 'mykey' ), "section catches parameter" ); |
| 91 | |
| 92 | # TEST |
| 93 | is( |
| 94 | $ini->val( 'section', 'mykey' ), |
| 95 | 'meö שלום', |
| 96 | "->val() parameter" |
| 97 | ); |
| 98 | } |
| 99 | } |