chiark / gitweb /
Begin incorporating toml-test tests into perl tests
authorJeff Ober <jober@ziprecruiter.com>
Thu, 16 Jan 2020 21:44:50 +0000 (16:44 -0500)
committerJeff Ober <jober@ziprecruiter.com>
Thu, 16 Jan 2020 21:44:50 +0000 (16:44 -0500)
t/parsing/array.t [new file with mode: 0644]
t/parsing/bool.t [new file with mode: 0644]
t/parsing/comments.t [new file with mode: 0644]
t/parsing/datetime.t [new file with mode: 0644]
t/parsing/misc.t [new file with mode: 0644]
t/parsing/strings.t [new file with mode: 0644]

diff --git a/t/parsing/array.t b/t/parsing/array.t
new file mode 100644 (file)
index 0000000..7b3664d
--- /dev/null
@@ -0,0 +1,34 @@
+use Test2::V0;
+use TOML::Tiny;
+
+is from_toml('thevoid=[[[]]]'), {thevoid => [[[]]]}, 'empty';
+
+is from_toml('ints=[1,2,3]'), {ints => [1, 2, 3]}, 'no spaces';
+
+is from_toml('ints=[1, 2, 3] # comment'), {ints => [1, 2, 3]}, 'comments after array';
+
+is from_toml(q{
+
+ints=[
+  1,      # comments
+  2, 3
+]
+
+}), {ints => [1, 2, 3]}, 'multi-line, w/ comments';
+
+is from_toml('ints=[1, 2, 3, ]'), {ints => [1, 2, 3]}, 'trailing comma';
+
+is from_toml('nested=[["a"], ["b", ["c"]]]'), {nested => [['a'], ['b', ['c']]]}, 'nested';
+
+is from_toml(q{
+title = [
+  "Client: \"XXXX\", Job: XXXX",
+  "Code: XXXX"
+]
+}), {title => ['Client: "XXXX", Job: XXXX', 'Code: XXXX']}, 'with string containing comma';;
+
+is from_toml(q{title = [ " \", ",]}), {title => [' ", ']}, 'with string containing escaped quote, then comma';
+
+is from_toml(q|foo = [ { bar="\"{{baz}}\""} ]|), {foo => [{bar => '"{{baz}}"'}]}, 'escaped quotes in string in table array table';
+
+done_testing;
diff --git a/t/parsing/bool.t b/t/parsing/bool.t
new file mode 100644 (file)
index 0000000..ddf0687
--- /dev/null
@@ -0,0 +1,9 @@
+use Test2::V0;
+use TOML::Tiny;
+
+is from_toml('x=true'), {x => $TOML::Tiny::Parser::TRUE}, 'true';
+is from_toml('x=false'), {x => $TOML::Tiny::Parser::FALSE}, 'false';
+is from_toml('x=true', inflate_boolean => sub{ $_[0] eq 'true' ? 'T' : 'F' }), {x => 'T'}, 'inflate_boolean(true)';
+is from_toml('x=false', inflate_boolean => sub{ $_[0] eq 'true' ? 'T' : 'F' }), {x => 'F'}, 'inflate_boolean(false)';
+
+done_testing;
diff --git a/t/parsing/comments.t b/t/parsing/comments.t
new file mode 100644 (file)
index 0000000..c58fe54
--- /dev/null
@@ -0,0 +1,47 @@
+use Test2::V0;
+use TOML::Tiny;
+
+is from_toml(q{
+# Top comment.
+  # Top comment.
+# Top comment.
+
+# [no-extraneous-groups-please]
+
+[group] # Comment
+answer = 42 # Comment
+# no-extraneous-keys-please = 999
+# Inbetween comment.
+more = [ # Comment
+  # What about multiple # comments?
+  # Can you handle it?
+  #
+          # Evil.
+# Evil.
+  42, 42, # Comments within arrays are fun.
+  # What about multiple # comments?
+  # Can you handle it?
+  #
+          # Evil.
+# Evil.
+# ] Did I fool you?
+] # Hopefully not.
+}), {
+  group => {
+    answer => 42,
+    more   => [42, 42],
+  }
+}, 'comments everywhere';
+
+is from_toml(q{
+# full line comment
+foo='bar' #comment
+}),
+  {foo => 'bar'}, 'comment with eol at eof';
+
+is from_toml(q{
+# full line comment
+foo='bar' #comment}),
+  {foo => 'bar'}, 'comment at eof';
+
+done_testing;
diff --git a/t/parsing/datetime.t b/t/parsing/datetime.t
new file mode 100644 (file)
index 0000000..8975d26
--- /dev/null
@@ -0,0 +1,18 @@
+use Test2::V0;
+use TOML::Tiny;
+use DateTime::Format::RFC3339;
+
+my %args = (
+  inflate_datetime => sub{
+    my $str = shift;
+    my $dt  = DateTime::Format::RFC3339->parse_datetime($str);
+    $dt->set_time_zone('UTC');
+    ''.$dt;
+  },
+);
+
+is from_toml(q{x=1987-07-05T17:45:00Z}, %args),          {x => '1987-07-05T17:45:00Z'},           'utc';
+is from_toml(q{x=1977-06-28T07:32:00-05:00}, %args),     {x => '1977-06-28T12:32:00Z'},           'offset';
+is from_toml(q{x=1977-12-21T10:32:00.555+07:00}, %args), {x => '1977-12-21T03:32:00.555000000Z'}, 'milliseconds';
+
+done_testing;
diff --git a/t/parsing/misc.t b/t/parsing/misc.t
new file mode 100644 (file)
index 0000000..f70ad6d
--- /dev/null
@@ -0,0 +1,6 @@
+use Test2::V0;
+use TOML::Tiny;
+
+is from_toml(''), {}, 'empty string';
+
+done_testing;
diff --git a/t/parsing/strings.t b/t/parsing/strings.t
new file mode 100644 (file)
index 0000000..58d2e51
--- /dev/null
@@ -0,0 +1,31 @@
+use Test2::V0;
+use TOML::Tiny;
+
+is from_toml(q{x="how now brown bureaucrat"}), {x => "how now brown bureaucrat"}, 'basic string';
+is from_toml(q{x=""}), {x => ''}, 'empty string';
+is from_toml(q{x="\\\\n"}), {x => q{\n}}, 'escaped escape';
+
+is from_toml(q{backspace   = "This string has a \b backspace character."}),             {backspace   => "This string has a \b backspace character."}, 'backspace';
+is from_toml(q{tab         = "This string has a \t tab character."}),                   {tab         => "This string has a \t tab character."}, 'tab';
+is from_toml(q{newline     = "This string has a \n new line character."}),              {newline     => "This string has a \n new line character."}, 'newline';
+is from_toml(q{formfeed    = "This string has a \f form feed character."}),             {formfeed    => "This string has a \f form feed character."}, 'formfeed';
+is from_toml(q{carriage    = "This string has a \r carriage return character."}),       {carriage    => "This string has a \r carriage return character."}, 'carriage';
+is from_toml(q{quote       = "This string has a \" quote character."}),                 {quote       => "This string has a \" quote character."}, 'quote';
+is from_toml(q{backslash   = "This string has a \\\\ backslash character."}),           {backslash   => "This string has a \\ backslash character."}, 'backslash';
+
+is from_toml(q{notunicode1 = "This string does not have a unicode \\\\u escape."}),     {notunicode1 => "This string does not have a unicode \\u escape."}, 'not unicode 1';
+is from_toml(q{notunicode2 = "This string does not have a unicode \u005Cu escape."}),   {notunicode2 => "This string does not have a unicode \\u escape."}, 'not unicode 2';
+is from_toml(q{notunicode3 = "This string does not have a unicode \\\\u0075 escape."}), {notunicode3 => "This string does not have a unicode \\u0075 escape."}, 'not unicode 3';
+is from_toml(q{notunicode4 = "This string does not have a unicode \\\u0075 escape."}),  {notunicode4 => "This string does not have a unicode \\u0075 escape."}, 'not unicode 4';
+
+is from_toml(q{nl_mid = "val\nue"}),     {nl_mid => "val\nue"}, 'newline in middle of string';
+is from_toml(q{nl_end = """value\n"""}), {nl_end => "value\n"}, 'newline at end of string';
+
+is from_toml(q{lit_nl_end = '''value\n'''}), {lit_nl_end => 'value\n'}, 'literal with \n at end';
+is from_toml(q{lit_nl_mid = 'val\nue'}),     {lit_nl_mid => 'val\nue'}, 'literal with \n in middle';
+is from_toml(q{lit_nl_uni = 'val\ue'}),      {lit_nl_uni => 'val\ue'},  'literal with \u in middle';
+
+is from_toml(q{x="hash in # string"}), {x => 'hash in # string'}, 'hash inside string is not recognized as comment';
+is from_toml(q{x="hash in # string" # comment after}), {x => 'hash in # string'}, 'hash in string with comment after string parsed correctly';
+
+done_testing;