#!/usr/bin/env perl

use warnings;
use strict;

use Mail::IMAPClient;
use Mail::IMAPClient::BodyStructure;

print "using Mail::IMAPClient $Mail::IMAPClient::VERSION\n";

sub pretty {
    my ($bs) = @_;
    return $bs->id." ".$bs->bodytype."/".$bs->bodysubtype;
}

for my $test (@{tests()}) {
    my ($raw, $want) = @$test;

    print "\n";

    my $top = Mail::IMAPClient::BodyStructure->new("(BODYSTRUCTURE $raw)");

    print "top: ",pretty($top),"\n";

    print "  ",pretty($_),"\n" for $top->bodystructure;
}

sub tests {
    return [

    [
        q{(("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL "Explanation" "8BIT" 190 3 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "Rich Text demo" "7BIT" 4437 ("Tue, 24 Dec 1991 08:14:27 -0500 (EST)" "Re: a MIME-Version misfeature" (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) ((NIL NIL "ietf-822" "dimacs.rutgers.edu")) NIL NIL NIL "<sdJn_nq0M2YtNKaFtO@thumper.bellcore.com>") (("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "8BIT" 767 16 NIL NIL NIL)(("TEXT" "RICHTEXT" ("CHARSET" "us-ascii") NIL NIL "8BIT" 887 13 NIL NIL NIL) "MIXED" ("BOUNDARY" "Alternative_Boundary_8dJn:mu0M2Yt5KaFZ8AdJn:mu0M2Yt1KaFdA") NIL NIL)("APPLICATION" "ANDREW-INSET" NIL NIL NIL "7BIT" 917 NIL NIL NIL) "ALTERNATIVE" ("BOUNDARY" "Interpart_Boundary_AdJn:mu0M2YtJKaFh9AdJn:mu0M2YtJKaFk=") NIL NIL) 100 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "Voice Mail demo" "7BIT" 562208 ("Tue, 8 Oct 91 10:25:36 EDT" "Re: multipart mail" (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) ((NIL NIL "mrc" "panda.com")) NIL NIL NIL "<9110081425.AA00616@greenbush.bellcore.com>") ("AUDIO" "BASIC" NIL NIL "Hi Mark" "BASE64" 561240 NIL NIL NIL) 7218 NIL NIL NIL)("AUDIO" "BASIC" NIL NIL "Flint phone" "BASE64" 36234 NIL NIL NIL)("IMAGE" "PBM" NIL NIL "MTR's photo" "BASE64" 1816 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "Star Trek Party" "7BIT" 182716 ("Thu, 19 Sep 91 12:41:43 EDT" "No Subject" (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) ((NIL NIL "abel" "thumper.bellcore.com")(NIL NIL "bianchi" "thumper.bellcore.com")(NIL NIL "braun" "thumper.bellcore.com")(NIL NIL "cameron" "thumper.bellcore.com")(NIL NIL "carmen" "thumper.bellcore.com")(NIL NIL "jfp" "thumper.bellcore.com")(NIL NIL "jxr" "thumper.bellcore.com")(NIL NIL "kraut" "thumper.bellcore.com")(NIL NIL "lamb" "thumper.bellcore.com")(NIL NIL "lowery" "thumper.bellcore.com")(NIL NIL "lynn" "thumper.bellcore.com")(NIL NIL "mlittman" "thumper.bellcore.com")(NIL NIL "nancyg" "thumper.bellcore.com")(NIL NIL "sau" "thumper.bellcore.com")(NIL NIL "shoshi" "thumper.bellcore.com")(NIL NIL "slr" "thumper.bellcore.com")(NIL NIL "stornett" "flash.bellcore.com")(NIL NIL "tkl" "thumper.bellcore.com")) ((NIL NIL "nsb" "thumper.bellcore.com")(NIL NIL "trina" "flash.bellcore.com")) NIL NIL "<9109191641.AA12840@greenbush.bellcore.com>") ((("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "8BIT" 731 16 NIL NIL NIL)("AUDIO" "X-SUN" NIL NIL "He's dead, Jim" "BASE64" 31426 NIL NIL NIL) "MIXED" ("BOUNDARY" "Where_No_One_Has_Gone_Before") NIL NIL)(("IMAGE" "GIF" NIL NIL "Kirk/Spock/McCoy" "BASE64" 25962 NIL NIL NIL)("IMAGE" "GIF" NIL NIL "Star Trek Next Generation" "BASE64" 18638 NIL NIL NIL)("APPLICATION" "X-BE2" ("VERSION" "12") NIL NIL "7BIT" 46125 NIL NIL NIL)("APPLICATION" "ATOMICMAIL" ("VERSION" "1.12") NIL NIL "7BIT" 9203 NIL NIL NIL) "MIXED" ("BOUNDARY" "Where_No_Man_Has_Gone_Before") NIL NIL)("AUDIO" "X-SUN" NIL NIL "Distress calls" "BASE64" 47754 NIL NIL NIL) "MIXED" ("BOUNDARY" "Outermost_Trek") NIL NIL) 4474 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "Digitizer test" "7BIT" 87577 ("Fri, 24 May 91 10:40:25 EDT" "A cheap digitizer test" (("Stephen A Uhler" NIL "sau" "sleepy.bellcore.com")) (("Stephen A Uhler" NIL "sau" "sleepy.bellcore.com")) (("Stephen A Uhler" NIL "sau" "sleepy.bellcore.com")) ((NIL NIL "nsb" "sleepy.bellcore.com")) ((NIL NIL "sau" "sleepy.bellcore.com")) NIL NIL "<9105241440.AA08935@sleepy.bellcore.com>") (("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "8BIT" 21 0 NIL NIL NIL)("IMAGE" "PGM" NIL NIL "Bellcore mug" "BASE64" 85502 NIL NIL NIL)("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "8BIT" 267 8 NIL NIL NIL) "MIXED" ("BOUNDARY" "mail.sleepy.sau.144.8891") NIL NIL) 1151 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "More Imagery" "7BIT" 75661 ("Fri, 7 Jun 91 09:09:05 EDT" "meta-mail" (("Stephen A Uhler" NIL "sau" "sleepy.bellcore.com")) (("Stephen A Uhler" NIL "sau" "sleepy.bellcore.com")) (("Stephen A Uhler" NIL "sau" "sleepy.bellcore.com")) ((NIL NIL "nsb" "sleepy.bellcore.com")) NIL NIL NIL "<9106071309.AA00574@sleepy.bellcore.com>") (("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "8BIT" 1246 26 NIL NIL NIL)("IMAGE" "PBM" NIL NIL "Mail architecture slide" "BASE64" 72816 NIL NIL NIL) "MIXED" ("BOUNDARY" "mail.sleepy.sau.158.532") NIL NIL) 999 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "PostScript demo" "7BIT" 398861 ("Mon, 7 Oct 91 12:13:55 EDT" "An image that went gif->ppm->ps" (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) ((NIL NIL "mrc" "cac.washington.edu")) NIL NIL NIL "<9110071613.AA10867@greenbush.bellcore.com>") ("APPLICATION" "POSTSCRIPT" NIL NIL "Captain Picard" "7BIT" 398008 NIL NIL NIL) 6455 NIL NIL NIL)("IMAGE" "GIF" NIL NIL "Quoted-Printable test" "QUOTED-PRINTABLE" 77180 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "q-p vs. base64 test" "7BIT" 103620 ("Sat, 26 Oct 91 09:35:10 EDT" "Audio in q-p" (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) ((NIL NIL "mrc" "akbar.cac.washington.edu")) NIL NIL NIL "<9110261335.AA01130@greenbush.bellcore.com>") (("AUDIO" "BASIC" NIL NIL "I'm sorry, Dave (q-p)" "QUOTED-PRINTABLE" 62081 NIL NIL NIL)("AUDIO" "BASIC" NIL NIL "I'm sorry, Dave (BASE64)" "BASE64" 40578 NIL NIL NIL) "MIXED" ("BOUNDARY" "hal_9000") NIL NIL) 1352 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "Multiple encapsulation" "7BIT" 317104 ("Thu, 24 Oct 1991 17:32:56 -0700 (PDT)" "Here's some more" (("Mark Crispin" NIL "MRC" "CAC.Washington.EDU")) (("Mark Crispin" NIL "mrc" "Tomobiki-Cho.CAC.Washington.EDU")) (("Mark Crispin" NIL "MRC" "CAC.Washington.EDU")) (("Mark Crispin" NIL "MRC" "CAC.Washington.EDU")) NIL NIL NIL "<MailManager.688350776.11603.mrc@Tomobiki-Cho.CAC.Washington.EDU>") (("APPLICATION" "POSTSCRIPT" NIL NIL "The Simpsons!!" "BASE64" 52984 NIL NIL NIL)("BINARY" "UNKNOWN" ("NAME" "Alices_PDP-10") NIL "Alice's PDP-10 w/ TECO & DDT" "BASE64" 18404 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "Going deeper" "7BIT" 244680 ("Thu, 24 Oct 1991 17:08:20 -0700 (PDT)" "A Multipart message" (("Nathaniel S. Borenstein" NIL "nsb" "thumper.bellcore.com")) (("Nathaniel S. Borenstein" NIL "nsb" "thumper.bellcore.com")) (("Nathaniel S. Borenstein" NIL "nsb" "thumper.bellcore.com")) ((NIL NIL "nsb" "thumper.bellcore.com")) NIL NIL NIL "<1067124673.10745.0.camel@localhost>") (("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "8BIT" 319 7 NIL NIL NIL)(("IMAGE" "GIF" NIL NIL "Bunny" "BASE64" 3330 NIL NIL NIL)("AUDIO" "BASIC" NIL NIL "TV Theme songs" "BASE64" 159174 NIL NIL NIL) "PARALLEL" ("BOUNDARY" "seconddivider") NIL NIL)("APPLICATION" "ATOMICMAIL" NIL NIL NIL "7BIT" 4924 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "Yet another level deeper..." "7BIT" 76044 ("Thu, 24 Oct 1991 17:09:10 -0700 (PDT)" "Monster!" (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) (("Nathaniel Borenstein" NIL "nsb" "thumper.bellcore.com")) NIL NIL NIL NIL "<1067124673.10745.1.camel@localhost>") ("AUDIO" "X-SUN" NIL NIL "I'm Twying..." "BASE64" 75756 NIL NIL NIL) 981 NIL NIL NIL) "MIXED" ("BOUNDARY" "foobarbazola") NIL NIL) 3309 NIL NIL NIL) "MIXED" ("BOUNDARY" "16819560-2078917053-688350843:#11603") NIL NIL) 4255 NIL NIL NIL) "MIXED" ("BOUNDARY" "owatagusiam") NIL NIL))},
    ],

    [
        q{(("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "8BIT" 617 16 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "I'll be whatever I wanna do. --Fry" "7BIT" 582 ("23 Oct 2003 22:25:56 -0700" "plain jane message" (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) ((NIL NIL "bob" "domain.dom")) NIL NIL NIL "<1066973156.4264.42.camel@localhost>") ("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "8BIT" 311 9 NIL NIL NIL) 18 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "Would you kindly shut your noise-hole? --Bender" "7BIT" 1460 ("23 Oct 2003 23:15:11 -0700" "messages inside messages inside..." (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) ((NIL NIL "bob" "domain.dom")) NIL NIL NIL "<1066976111.4263.74.camel@localhost>") (("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "8BIT" 193 3 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "At the risk of sounding negative, no. --Leela" "7BIT" 697 ("23 Oct 2003 23:09:05 -0700" "the original message" (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) ((NIL NIL "bob" "domain.dom")) NIL NIL NIL "<1066975745.4263.70.camel@localhost>") (("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "8BIT" 78 3 NIL NIL NIL)("APPLICATION" "X-GZIP" ("NAME" "foo.gz") NIL NIL "BASE64" 58 NIL ("ATTACHMENT" ("FILENAME" "foo.gz")) NIL) "MIXED" ("BOUNDARY" "=-XFYecI7w+0shpolXq8bb") NIL NIL) 25 NIL NIL NIL) "MIXED" ("BOUNDARY" "=-9Brg7LoMERBrIDtMRose") NIL NIL) 49 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "Dirt doesn't need luck! --Professor" "7BIT" 817 ("23 Oct 2003 22:40:49 -0700" "this message JUST contains an attachment" (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) ((NIL NIL "bob" "domain.dom")) NIL NIL NIL "<1066974048.4264.62.camel@localhost>") ("APPLICATION" "X-GZIP" ("NAME" "blah.gz") NIL "Attachment has identical content to above foo.gz" "BASE64" 396 NIL ("ATTACHMENT" ("FILENAME" "blah.gz")) NIL) 17 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "Hold still, I don't have good depth perception! --Leela" "7BIT" 1045 ("23 Oct 2003 23:09:16 -0700" "Attachment filename vs. name" (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) ((NIL NIL "bob" "domain.dom")) NIL NIL NIL "<1066975756.4263.70.camel@localhost>") (("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "8BIT" 377 6 NIL NIL NIL)("APPLICATION" "X-GZIP" ("NAME" "blah2.gz") NIL "filename is blah1.gz, name is blah2.gz" "BASE64" 58 NIL ("ATTACHMENT" ("FILENAME" "blah1.gz")) NIL) "MIXED" ("BOUNDARY" "=-1066975756jd02") NIL NIL) 29 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "Hello little man.  I WILL DESTROY YOU! --Moro" "7BIT" 1149 ("23 Oct 2003 23:09:21 -0700" "No filename?  No problem!" (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) ((NIL NIL "bob" "domain.dom")) NIL NIL NIL "<1066975761.4263.70.camel@localhost>") (("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "8BIT" 517 10 NIL NIL NIL)("APPLICATION" "X-GZIP" NIL NIL "I'm getting sick of witty things to say" "BASE64" 58 NIL NIL NIL) "MIXED" ("BOUNDARY" "=-1066975756jd03") NIL NIL) 33 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "Friends! Help! A guinea pig tricked me! --Zoidberg" "7BIT" 896 ("23 Oct 2003 22:40:45 -0700" "html and text, both inline" (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) ((NIL NIL "bob" "domain.dom")) NIL NIL NIL "<1066974044.4264.62.camel@localhost>") (("TEXT" "HTML" ("CHARSET" "utf-8") NIL NIL "8BIT" 327 11 NIL NIL NIL)("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "8BIT" 61 2 NIL NIL NIL) "MIXED" ("BOUNDARY" "=-ZCKMfHzvHMyK1iBu4kff") NIL NIL) 33 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "Smeesh! --Amy" "7BIT" 642 ("23 Oct 2003 22:41:29 -0700" "text and text, both inline" (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) ((NIL NIL "bob" "domain.dom")) NIL NIL NIL "<1066974089.4265.64.camel@localhost>") (("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "8BIT" 62 2 NIL NIL NIL)("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "8BIT" 68 2 NIL NIL NIL) "MIXED" ("BOUNDARY" "=-pNc4wtlOIxs8RcX7H/AK") NIL NIL) 24 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "That's not a cigar. Uh... and it's not mine. --Hermes" "7BIT" 1515 ("23 Oct 2003 22:39:17 -0700" "HTML and...  HTML?" (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) ((NIL NIL "bob" "domain.dom")) NIL NIL NIL "<1066973957.4263.59.camel@localhost>") (("TEXT" "HTML" ("CHARSET" "utf-8") NIL NIL "8BIT" 824 22 NIL NIL NIL)("TEXT" "HTML" ("NAME" "htmlfile.html" "CHARSET" "UTF-8") NIL NIL "8BIT" 118 6 NIL ("ATTACHMENT" ("FILENAME" "htmlfile.html")) NIL) "MIXED" ("BOUNDARY" "=-zxh/IezwzZITiphpcbJZ") NIL NIL) 49 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "The spirit is willing, but the flesh is spongy, and	bruised. --Zapp" "7BIT" 6685 ("23 Oct 2003 22:23:16 -0700" "smiley!" (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) ((NIL NIL "bob" "domain.dom")) NIL NIL NIL "<1066972996.4264.39.camel@localhost>") ((((("TEXT" "PLAIN" NIL NIL NIL "QUOTED-PRINTABLE" 1606 42 NIL NIL NIL)("TEXT" "HTML" ("CHARSET" "utf-8") NIL NIL "QUOTED-PRINTABLE" 2173 54 NIL NIL NIL) "ALTERNATIVE" ("BOUNDARY" "=-dHujWM/Xizz57x/JOmDF") NIL NIL)("IMAGE" "PNG" ("NAME" "smiley-3.png") "<1066971953.4232.15.camel@localhost>" NIL "BASE64" 1122 NIL ("ATTACHMENT" ("FILENAME" "smiley-3.png")) NIL) "RELATED" ("TYPE" "multipart/alternative" "BOUNDARY" "=-GpwozF9CQ7NdF+fd+vMG") NIL NIL)("IMAGE" "GIF" ("NAME" "dot.gif") NIL NIL "BASE64" 96 NIL ("ATTACHMENT" ("FILENAME" "dot.gif")) NIL) "MIXED" ("BOUNDARY" "=-CgV5jm9HAY9VbUlAuneA") NIL NIL)("APPLICATION" "PGP-SIGNATURE" ("NAME" "signature.asc") NIL "This is a digitally signed message part" "7BIT" 196 NIL NIL NIL) "SIGNED" ("MICALG" "pgp-sha1" "PROTOCOL" "application/pgp-signature" "BOUNDARY" "=-vH3FQO9a8icUn1ROCoAi") NIL NIL) 177 NIL NIL NIL)("MESSAGE" "RFC822" NIL NIL "Kittens give Morbo gas. --Morbo" "7BIT" 3113 ("23 Oct 2003 22:32:37 -0700" "the PROPER way to do alternative/related" (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) (("Ryan Finnie" NIL "rfinnie" "domain.dom")) ((NIL NIL "bob" "domain.dom")) NIL NIL NIL "<1066973557.4265.51.camel@localhost>") (("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "8BIT" 863 22 NIL NIL NIL)(("TEXT" "HTML" ("CHARSET" "utf-8") NIL NIL "8BIT" 1283 22 NIL NIL NIL)("IMAGE" "GIF" NIL "<1066973340.4232.46.camel@localhost>" NIL "BASE64" 116 NIL NIL NIL) "RELATED" ("BOUNDARY" "=-bFkxH1S3HVGcxi+o/5jG") NIL NIL) "ALTERNATIVE" ("TYPE" "multipart/alternative" "BOUNDARY" "=-tyGlQ9JvB5uvPWzozI+y") NIL NIL) 79 NIL NIL NIL) "MIXED" ("BOUNDARY" "=-qYxqvD9rbH0PNeExagh1") NIL NIL))},
    ],

    ];

}
