{
    "content": [
        {
            "type": "text",
            "text": "# Test::Builder (perldoc)\n\n## NAME\n\nTest::Builder - Backend for building test libraries\n\n## SYNOPSIS\n\npackage My::Test::Module;\nuse base 'Test::Builder::Module';\nmy $CLASS = PACKAGE;\nsub ok {\nmy($test, $name) = @;\nmy $tb = $CLASS->builder;\n$tb->ok($test, $name);\n}\n\n## DESCRIPTION\n\nTest::Simple and Test::More have proven to be popular testing modules, but they're not always\nflexible enough. Test::Builder provides a building block upon which to write your own test\nlibraries *which can work together*.\n\n## Sections\n\n- **NAME**\n- **SYNOPSIS**\n- **DESCRIPTION** (8 subsections)\n- **EXIT CODES**\n- **THREADS**\n- **MEMORY**\n- **EXAMPLES**\n- **SEE ALSO**\n- **AUTHORS**\n- **MAINTAINERS**\n- **COPYRIGHT**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "Test::Builder",
        "section": "",
        "mode": "perldoc",
        "summary": "Test::Builder - Backend for building test libraries",
        "synopsis": "package My::Test::Module;\nuse base 'Test::Builder::Module';\nmy $CLASS = PACKAGE;\nsub ok {\nmy($test, $name) = @;\nmy $tb = $CLASS->builder;\n$tb->ok($test, $name);\n}",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [
            "CPAN can provide the best examples. Test::Simple, Test::More, Test::Exception and",
            "Test::Differences all use Test::Builder."
        ],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 12,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 4,
                "subsections": [
                    {
                        "name": "Construction",
                        "lines": 46
                    },
                    {
                        "name": "Setting up tests",
                        "lines": 75
                    },
                    {
                        "name": "Running tests",
                        "lines": 55
                    },
                    {
                        "name": "Other Testing Methods",
                        "lines": 24
                    },
                    {
                        "name": "Test building utility methods",
                        "lines": 34
                    },
                    {
                        "name": "Test style",
                        "lines": 57
                    },
                    {
                        "name": "Output",
                        "lines": 84
                    },
                    {
                        "name": "Test Status and Info",
                        "lines": 174
                    }
                ]
            },
            {
                "name": "EXIT CODES",
                "lines": 14,
                "subsections": []
            },
            {
                "name": "THREADS",
                "lines": 19,
                "subsections": []
            },
            {
                "name": "MEMORY",
                "lines": 10,
                "subsections": []
            },
            {
                "name": "EXAMPLES",
                "lines": 3,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 9,
                "subsections": []
            },
            {
                "name": "AUTHORS",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "MAINTAINERS",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "COPYRIGHT",
                "lines": 7,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "Test::Builder - Backend for building test libraries\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "package My::Test::Module;\nuse base 'Test::Builder::Module';\n\nmy $CLASS = PACKAGE;\n\nsub ok {\nmy($test, $name) = @;\nmy $tb = $CLASS->builder;\n\n$tb->ok($test, $name);\n}\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "Test::Simple and Test::More have proven to be popular testing modules, but they're not always\nflexible enough. Test::Builder provides a building block upon which to write your own test\nlibraries *which can work together*.\n",
                "subsections": [
                    {
                        "name": "Construction",
                        "content": "new\nmy $Test = Test::Builder->new;\n\nReturns a Test::Builder object representing the current state of the test.\n\nSince you only run one test per program \"new\" always returns the same Test::Builder object.\nNo matter how many times you call \"new()\", you're getting the same object. This is called a\nsingleton. This is done so that multiple modules share such global information as the test\ncounter and where test output is going.\n\nIf you want a completely new Test::Builder object different from the singleton, use\n\"create\".\n\ncreate\nmy $Test = Test::Builder->create;\n\nOk, so there can be more than one Test::Builder object and this is how you get it. You might\nuse this instead of \"new()\" if you're testing a Test::Builder based module, but otherwise\nyou probably want \"new\".\n\nNOTE: the implementation is not complete. \"level\", for example, is still shared by all\nTest::Builder objects, even ones created using this method. Also, the method name may change\nin the future.\n\nsubtest\n$builder->subtest($name, \\&subtests, @args);\n\nSee documentation of \"subtest\" in Test::More.\n\n\"subtest\" also, and optionally, accepts arguments which will be passed to the subtests\nreference.\n\nname\ndiag $builder->name;\n\nReturns the name of the current builder. Top level builders default to $0 (the name of the\nexecutable). Child builders are named via the \"child\" method. If no name is supplied, will\nbe named \"Child of $parent->name\".\n\nreset\n$Test->reset;\n\nReinitializes the Test::Builder singleton to its original state. Mostly useful for tests run\nin persistent environments where the same test might be run multiple times in the same\nprocess.\n"
                    },
                    {
                        "name": "Setting up tests",
                        "content": "These methods are for setting up tests and declaring how many there are. You usually only want\nto call one of these methods.\n\nplan\n$Test->plan('noplan');\n$Test->plan( skipall => $reason );\n$Test->plan( tests => $numtests );\n\nA convenient way to set up your tests. Call this and Test::Builder will print the\nappropriate headers and take the appropriate actions.\n\nIf you call \"plan()\", don't call any of the other methods below.\n\nexpectedtests\nmy $max = $Test->expectedtests;\n$Test->expectedtests($max);\n\nGets/sets the number of tests we expect this test to run and prints out the appropriate\nheaders.\n\nnoplan\n$Test->noplan;\n\nDeclares that this test will run an indeterminate number of tests.\n\ndonetesting\n$Test->donetesting();\n$Test->donetesting($numtests);\n\nDeclares that you are done testing, no more tests will be run after this point.\n\nIf a plan has not yet been output, it will do so.\n\n$numtests is the number of tests you planned to run. If a numbered plan was already\ndeclared, and if this contradicts, a failing test will be run to reflect the planning\nmistake. If \"noplan\" was declared, this will override.\n\nIf \"donetesting()\" is called twice, the second call will issue a failing test.\n\nIf $numtests is omitted, the number of tests run will be used, like noplan.\n\n\"donetesting()\" is, in effect, used when you'd want to use \"noplan\", but safer. You'd use\nit like so:\n\n$Test->ok($a == $b);\n$Test->donetesting();\n\nOr to plan a variable number of tests:\n\nfor my $test (@tests) {\n$Test->ok($test);\n}\n$Test->donetesting(scalar @tests);\n\nhasplan\n$plan = $Test->hasplan\n\nFind out whether a plan has been defined. $plan is either \"undef\" (no plan has been set),\n\"noplan\" (indeterminate # of tests) or an integer (the number of expected tests).\n\nskipall\n$Test->skipall;\n$Test->skipall($reason);\n\nSkips all the tests, using the given $reason. Exits immediately with 0.\n\nexportedto\nmy $pack = $Test->exportedto;\n$Test->exportedto($pack);\n\nTells Test::Builder what package you exported your functions to.\n\nThis method isn't terribly useful since modules which share the same Test::Builder object\nmight get exported to different packages and only the last one will be honored.\n"
                    },
                    {
                        "name": "Running tests",
                        "content": "These actually run the tests, analogous to the functions in Test::More.\n\nThey all return true if the test passed, false if the test failed.\n\n$name is always optional.\n\nok\n$Test->ok($test, $name);\n\nYour basic test. Pass if $test is true, fail if $test is false. Just like Test::Simple's\n\"ok()\".\n\niseq\n$Test->iseq($got, $expected, $name);\n\nLike Test::More's \"is()\". Checks if \"$got eq $expected\". This is the string version.\n\n\"undef\" only ever matches another \"undef\".\n\nisnum\n$Test->isnum($got, $expected, $name);\n\nLike Test::More's \"is()\". Checks if \"$got == $expected\". This is the numeric version.\n\n\"undef\" only ever matches another \"undef\".\n\nisnteq\n$Test->isnteq($got, $dontexpect, $name);\n\nLike Test::More's \"isnt()\". Checks if \"$got ne $dontexpect\". This is the string version.\n\nisntnum\n$Test->isntnum($got, $dontexpect, $name);\n\nLike Test::More's \"isnt()\". Checks if \"$got ne $dontexpect\". This is the numeric version.\n\nlike\n$Test->like($thing, qr/$regex/, $name);\n$Test->like($thing, '/$regex/', $name);\n\nLike Test::More's \"like()\". Checks if $thing matches the given $regex.\n\nunlike\n$Test->unlike($thing, qr/$regex/, $name);\n$Test->unlike($thing, '/$regex/', $name);\n\nLike Test::More's \"unlike()\". Checks if $thing does not match the given $regex.\n\ncmpok\n$Test->cmpok($thing, $type, $that, $name);\n\nWorks just like Test::More's \"cmpok()\".\n\n$Test->cmpok($bignum, '!=', $otherbignum);\n"
                    },
                    {
                        "name": "Other Testing Methods",
                        "content": "These are methods which are used in the course of writing a test but are not themselves tests.\n\nBAILOUT\n$Test->BAILOUT($reason);\n\nIndicates to the Test::Harness that things are going so badly all testing should terminate.\nThis includes running any additional test scripts.\n\nIt will exit with 255.\n\nskip\n$Test->skip;\n$Test->skip($why);\n\nSkips the current test, reporting $why.\n\ntodoskip\n$Test->todoskip;\n$Test->todoskip($why);\n\nLike \"skip()\", only it will declare the test as failing and TODO. Similar to\n\nprint \"not ok $tnum # TODO $why\\n\";\n"
                    },
                    {
                        "name": "Test building utility methods",
                        "content": "These methods are useful when writing your own test methods.\n\nmayberegex\n$Test->mayberegex(qr/$regex/);\n$Test->mayberegex('/$regex/');\n\nThis method used to be useful back when Test::Builder worked on Perls before 5.6 which\ndidn't have qr//. Now its pretty useless.\n\nConvenience method for building testing functions that take regular expressions as\narguments.\n\nTakes a quoted regular expression produced by \"qr//\", or a string representing a regular\nexpression.\n\nReturns a Perl value which may be used instead of the corresponding regular expression, or\n\"undef\" if its argument is not recognized.\n\nFor example, a version of \"like()\", sans the useful diagnostic messages, could be written\nas:\n\nsub laconiclike {\nmy ($self, $thing, $regex, $name) = @;\nmy $usableregex = $self->mayberegex($regex);\ndie \"expecting regex, found '$regex'\\n\"\nunless $usableregex;\n$self->ok($thing =~ m/$usableregex/, $name);\n}\n\nisfh\nmy $isfh = $Test->isfh($thing);\n\nDetermines if the given $thing can be used as a filehandle.\n"
                    },
                    {
                        "name": "Test style",
                        "content": "level\n$Test->level($howhigh);\n\nHow far up the call stack should $Test look when reporting where the test failed.\n\nDefaults to 1.\n\nSetting $Test::Builder::Level overrides. This is typically useful localized:\n\nsub myok {\nmy $test = shift;\n\nlocal $Test::Builder::Level = $Test::Builder::Level + 1;\n$TB->ok($test);\n}\n\nTo be polite to other functions wrapping your own you usually want to increment $Level\nrather than set it to a constant.\n\nusenumbers\n$Test->usenumbers($onoroff);\n\nWhether or not the test should output numbers. That is, this if true:\n\nok 1\nok 2\nok 3\n\nor this if false\n\nok\nok\nok\n\nMost useful when you can't depend on the test output order, such as when threads or forking\nis involved.\n\nDefaults to on.\n\nnodiag\n$Test->nodiag($nodiag);\n\nIf set true no diagnostics will be printed. This includes calls to \"diag()\".\n\nnoending\n$Test->noending($noending);\n\nNormally, Test::Builder does some extra diagnostics when the test ends. It also changes the\nexit code as described below.\n\nIf this is true, none of that will be done.\n\nnoheader\n$Test->noheader($noheader);\n\nIf set to true, no \"1..N\" header will be printed.\n"
                    },
                    {
                        "name": "Output",
                        "content": "Controlling where the test output goes.\n\nIt's ok for your test to change where STDOUT and STDERR point to, Test::Builder's default output\nsettings will not be affected.\n\ndiag\n$Test->diag(@msgs);\n\nPrints out the given @msgs. Like \"print\", arguments are simply appended together.\n\nNormally, it uses the \"failureoutput()\" handle, but if this is for a TODO test, the\n\"todooutput()\" handle is used.\n\nOutput will be indented and marked with a # so as not to interfere with test output. A\nnewline will be put on the end if there isn't one already.\n\nWe encourage using this rather than calling print directly.\n\nReturns false. Why? Because \"diag()\" is often used in conjunction with a failing test (\"ok()\n|| diag()\") it \"passes through\" the failure.\n\nreturn ok(...) || diag(...);\n\nnote\n$Test->note(@msgs);\n\nLike \"diag()\", but it prints to the \"output()\" handle so it will not normally be seen by the\nuser except in verbose mode.\n\nexplain\nmy @dump = $Test->explain(@msgs);\n\nWill dump the contents of any references in a human readable format. Handy for things\nlike...\n\nisdeeply($have, $want) || diag explain $have;\n\nor\n\nisdeeply($have, $want) || note explain $have;\n\noutput\nfailureoutput\ntodooutput\nmy $filehandle = $Test->output;\n$Test->output($filehandle);\n$Test->output($filename);\n$Test->output(\\$scalar);\n\nThese methods control where Test::Builder will print its output. They take either an open\n$filehandle, a $filename to open and write to or a $scalar reference to append to. It will\nalways return a $filehandle.\n\noutput is where normal \"ok/not ok\" test output goes.\n\nDefaults to STDOUT.\n\nfailureoutput is where diagnostic output on test failures and \"diag()\" goes. It is normally\nnot read by Test::Harness and instead is displayed to the user.\n\nDefaults to STDERR.\n\n\"todooutput\" is used instead of \"failureoutput()\" for the diagnostics of a failing TODO\ntest. These will not be seen by the user.\n\nDefaults to STDOUT.\n\nresetoutputs\n$tb->resetoutputs;\n\nResets all the output filehandles back to their defaults.\n\ncarp\n$tb->carp(@message);\n\nWarns with @message but the message will appear to come from the point where the original\ntest function was called (\"$tb->caller\").\n\ncroak\n$tb->croak(@message);\n\nDies with @message but the message will appear to come from the point where the original\ntest function was called (\"$tb->caller\").\n"
                    },
                    {
                        "name": "Test Status and Info",
                        "content": "nologresults\nThis will turn off result long-term storage. Calling this method will make \"details\" and\n\"summary\" useless. You may want to use this if you are running enough tests to fill up all\navailable memory.\n\nTest::Builder->new->nologresults();\n\nThere is no way to turn it back on.\n\ncurrenttest\nmy $currtest = $Test->currenttest;\n$Test->currenttest($num);\n\nGets/sets the current test number we're on. You usually shouldn't have to set this.\n\nIf set forward, the details of the missing tests are filled in as 'unknown'. if set\nbackward, the details of the intervening tests are deleted. You can erase history if you\nreally want to.\n\nispassing\nmy $ok = $builder->ispassing;\n\nIndicates if the test suite is currently passing.\n\nMore formally, it will be false if anything has happened which makes it impossible for the\ntest suite to pass. True otherwise.\n\nFor example, if no tests have run \"ispassing()\" will be true because even though a suite\nwith no tests is a failure you can add a passing test to it and start passing.\n\nDon't think about it too much.\n\nsummary\nmy @tests = $Test->summary;\n\nA simple summary of the tests so far. True for pass, false for fail. This is a logical\npass/fail, so todos are passes.\n\nOf course, test #1 is $tests[0], etc...\n\ndetails\nmy @tests = $Test->details;\n\nLike \"summary()\", but with a lot more detail.\n\n$tests[$testnum - 1] =\n{ 'ok'       => is the test considered a pass?\nactualok  => did it literally say 'ok'?\nname       => name of the test (if any)\ntype       => type of test (if any, see below).\nreason     => reason for the above (if any)\n};\n\n'ok' is true if Test::Harness will consider the test to be a pass.\n\n'actualok' is a reflection of whether or not the test literally printed 'ok' or 'not ok'.\nThis is for examining the result of 'todo' tests.\n\n'name' is the name of the test.\n\n'type' indicates if it was a special test. Normal tests have a type of ''. Type can be one\nof the following:\n\nskip        see skip()\ntodo        see todo()\ntodoskip   see todoskip()\nunknown     see below\n\nSometimes the Test::Builder test counter is incremented without it printing any test output,\nfor example, when \"currenttest()\" is changed. In these cases, Test::Builder doesn't know\nthe result of the test, so its type is 'unknown'. These details for these tests are filled\nin. They are considered ok, but the name and actualok is left \"undef\".\n\nFor example \"not ok 23 - hole count # TODO insufficient donuts\" would result in this\nstructure:\n\n$tests[22] =    # 23 - 1, since arrays start from 0.\n{ ok        => 1,   # logically, the test passed since its todo\nactualok => 0,   # in absolute terms, it failed\nname      => 'hole count',\ntype      => 'todo',\nreason    => 'insufficient donuts'\n};\n\ntodo\nmy $todoreason = $Test->todo;\nmy $todoreason = $Test->todo($pack);\n\nIf the current tests are considered \"TODO\" it will return the reason, if any. This reason\ncan come from a $TODO variable or the last call to \"todostart()\".\n\nSince a TODO test does not need a reason, this function can return an empty string even when\ninside a TODO block. Use \"$Test->intodo\" to determine if you are currently inside a TODO\nblock.\n\n\"todo()\" is about finding the right package to look for $TODO in. It's pretty good at\nguessing the right package to look at. It first looks for the caller based on \"$Level + 1\",\nsince \"todo()\" is usually called inside a test function. As a last resort it will use\n\"exportedto()\".\n\nSometimes there is some confusion about where \"todo()\" should be looking for the $TODO\nvariable. If you want to be sure, tell it explicitly what $pack to use.\n\nfindTODO\nmy $todoreason = $Test->findTODO();\nmy $todoreason = $Test->findTODO($pack);\n\nLike \"todo()\" but only returns the value of $TODO ignoring \"todostart()\".\n\nCan also be used to set $TODO to a new value while returning the old value:\n\nmy $oldreason = $Test->findTODO($pack, 1, $newreason);\n\nintodo\nmy $intodo = $Test->intodo;\n\nReturns true if the test is currently inside a TODO block.\n\ntodostart\n$Test->todostart();\n$Test->todostart($message);\n\nThis method allows you declare all subsequent tests as TODO tests, up until the \"todoend\"\nmethod has been called.\n\nThe \"TODO:\" and $TODO syntax is generally pretty good about figuring out whether or not\nwe're in a TODO test. However, often we find that this is not possible to determine (such as\nwhen we want to use $TODO but the tests are being executed in other packages which can't be\ninferred beforehand).\n\nNote that you can use this to nest \"todo\" tests\n\n$Test->todostart('working on this');\n# lots of code\n$Test->todostart('working on that');\n# more code\n$Test->todoend;\n$Test->todoend;\n\nThis is generally not recommended, but large testing systems often have weird internal\nneeds.\n\nWe've tried to make this also work with the TODO: syntax, but it's not guaranteed and its\nuse is also discouraged:\n\nTODO: {\nlocal $TODO = 'We have work to do!';\n$Test->todostart('working on this');\n# lots of code\n$Test->todostart('working on that');\n# more code\n$Test->todoend;\n$Test->todoend;\n}\n\nPick one style or another of \"TODO\" to be on the safe side.\n\n\"todoend\"\n$Test->todoend;\n\nStops running tests as \"TODO\" tests. This method is fatal if called without a preceding\n\"todostart\" method call.\n\ncaller\nmy $package = $Test->caller;\nmy($pack, $file, $line) = $Test->caller;\nmy($pack, $file, $line) = $Test->caller($height);\n\nLike the normal \"caller()\", except it reports according to your \"level()\".\n\n$height will be added to the \"level()\".\n\nIf \"caller()\" winds up off the top of the stack it report the highest context.\n"
                    }
                ]
            },
            "EXIT CODES": {
                "content": "If all your tests passed, Test::Builder will exit with zero (which is normal). If anything\nfailed it will exit with how many failed. If you run less (or more) tests than you planned, the\nmissing (or extras) will be considered failures. If no tests were ever run Test::Builder will\nthrow a warning and exit with 255. If the test died, even after having successfully completed\nall its tests, it will still be considered a failure and will exit with 255.\n\nSo the exit codes are...\n\n0                   all tests successful\n255                 test died or all passed but wrong # of tests run\nany other number    how many failed (including missing or extras)\n\nIf you fail more than 254 tests, it will be reported as 254.\n",
                "subsections": []
            },
            "THREADS": {
                "content": "In perl 5.8.1 and later, Test::Builder is thread-safe. The test number is shared by all threads.\nThis means if one thread sets the test number using \"currenttest()\" they will all be effected.\n\nWhile versions earlier than 5.8.1 had threads they contain too many bugs to support.\n\nTest::Builder is only thread-aware if threads.pm is loaded *before* Test::Builder.\n\nYou can directly disable thread support with one of the following:\n\n$ENV{T2NOIPC} = 1\n\nor\n\nno Test2::IPC;\n\nor\n\nTest2::API::test2ipcdisable()\n",
                "subsections": []
            },
            "MEMORY": {
                "content": "An informative hash, accessible via \"details()\", is stored for each test you perform. So memory\nusage will scale linearly with each test run. Although this is not a problem for most test\nsuites, it can become an issue if you do large (hundred thousands to million) combinatorics\ntests in the same run.\n\nIn such cases, you are advised to either split the test file into smaller ones, or use a reverse\napproach, doing \"normal\" (code) compares and triggering \"fail()\" should anything go unexpected.\n\nFuture versions of Test::Builder will have a way to turn history off.\n",
                "subsections": []
            },
            "EXAMPLES": {
                "content": "CPAN can provide the best examples. Test::Simple, Test::More, Test::Exception and\nTest::Differences all use Test::Builder.\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "INTERNALS\nTest2, Test2::API\n\nLEGACY\nTest::Simple, Test::More\n\nEXTERNAL\nTest::Harness\n",
                "subsections": []
            },
            "AUTHORS": {
                "content": "Original code by chromatic, maintained by Michael G Schwern <schwern@pobox.com>\n",
                "subsections": []
            },
            "MAINTAINERS": {
                "content": "Chad Granum <exodist@cpan.org>\n",
                "subsections": []
            },
            "COPYRIGHT": {
                "content": "Copyright 2002-2008 by chromatic <chromatic@wgz.org> and Michael G Schwern <schwern@pobox.com>.\n\nThis program is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n\nSee http://www.perl.com/perl/misc/Artistic.html\n",
                "subsections": []
            }
        }
    }
}