Permalink
Browse files

fix error parsing ini without toplevel

  • Loading branch information...
1 parent c277df9 commit e8323ff36bcd6e9683ac82c50c79e7a6a1c75c47 @nobuo-danjou nobuo-danjou committed Jan 3, 2011
Showing with 72 additions and 13 deletions.
  1. +2 −0 .gitignore
  2. +5 −1 lib/Config/INI.pm
  3. +26 −1 t/01-parser.t
  4. +39 −11 t/02-writer.t
View
@@ -0,0 +1,2 @@
+blib
+Makefile
View
@@ -21,7 +21,11 @@ grammar INI {
}
class INI::Actions {
- method TOP ($/) { make { '_' => $<toplevel>[0].ast.hash, $<sections>».ast } }
+ method TOP ($/) {
+ my %hash = $<sections>».ast;
+ %hash<_> = $<toplevel>[0].ast.hash if $<toplevel>[0].ast;
+ make %hash;
+ }
method toplevel ($/) { make $<keyval>».ast.hash }
method sections ($/) { make $<header><text>.Str => $<keyval>».ast.hash }
# TODO: The .trim is useless, <!after \h> should be added to key regex,
View
@@ -1,6 +1,6 @@
use v6;
use Test;
-plan 30;
+plan 38;
use Config::INI;
@@ -110,4 +110,29 @@ is %si<section><moo>, 'kooh', '6.6 ok';
is %si{'Section Two'}{'something else'}, 'blah', '6.7 ok';
is %si{'Section Two'}<remove>, 'whitespace', '6.8 ok';
+my $seventh = Q {
+[section]
+one=two
+Foo=Bar
+this=Your Mother!
+blank=
+moo=kooh
+
+[Section Two]
+something else=blah
+ remove = whitespace
+};
+
+my %se = Config::INI::parse($seventh);
+
+ok 1, 'seventh config parsed';
+
+is %si<section><one>, 'two', '7.1 ok';
+is %si<section><Foo>, 'Bar', '7.2 ok';
+is %si<section><this>, 'Your Mother!', '7.3 ok';
+is %si<section><blank>, '', '7.4 ok';
+is %si<section><moo>, 'kooh', '7.5 ok';
+is %si{'Section Two'}{'something else'}, 'blah', '7.6 ok';
+is %si{'Section Two'}<remove>, 'whitespace', '7.7 ok';
+
done_testing; # and you thougt this would never happen!
View
@@ -2,11 +2,12 @@ use v6;
use Config::INI;
use Config::INI::Writer;
use Test;
-plan 8;
+plan 14;
ok 1, 'Modules loaded';
-my %hash =
+{
+ my %hash =
foo => 'bar',
another => 'thing',
section => {
@@ -18,18 +19,45 @@ my %hash =
};
-my $str = Config::INI::Writer::dump(%hash);
+ my $str = Config::INI::Writer::dump(%hash);
-ok 2, 'String dumped';
+ ok 2, 'String dumped';
-my %new = Config::INI::parse($str);
+ my %new = Config::INI::parse($str);
-ok 3, 'String parsed';
+ ok 3, 'String parsed';
-is %new<_><foo>, 'bar', 'content ok, 1/5';
-is %new<_><another>, 'thing', 'content ok, 2/5';
-is %new<section><one>, 'two', 'content ok, 3/5';
-is %new<section><three>, '4', 'content ok, 4/5';
-is %new<onemore><why>, 'not', 'content ok, 5/5';
+ is %new<_><foo>, 'bar', 'content ok, 1/5';
+ is %new<_><another>, 'thing', 'content ok, 2/5';
+ is %new<section><one>, 'two', 'content ok, 3/5';
+ is %new<section><three>, '4', 'content ok, 4/5';
+ is %new<onemore><why>, 'not', 'content ok, 5/5';
+}
+{
+ my %hash = {
+ section => {
+ one => 'two',
+ three => 4,
+ },
+ onemore => {
+ why => 'not',
+ }
+ };
+ my $str = Config::INI::Writer::dump(%hash);
+
+ ok 1, 'String dumped';
+
+ my %new = Config::INI::parse($str);
+
+ ok 1, 'String parsed';
+
+ ok %new.keys ~~ <section onemore>;
+
+ is %new<section><one>, 'two', 'content ok, 1/3';
+ is %new<section><three>, '4', 'content ok, 2/3';
+ is %new<onemore><why>, 'not', 'content ok, 3/3';
+}
done_testing;
+
+# vim: ft=perl6

0 comments on commit e8323ff

Please sign in to comment.