exiftool-vendored.exe 12.80.0 → 12.82.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (253) hide show
  1. package/README.md +6 -0
  2. package/bin/exiftool.exe +0 -0
  3. package/bin/exiftool_files/Changes +44 -0
  4. package/bin/exiftool_files/Licenses_Strawberry_Perl.zip +0 -0
  5. package/bin/exiftool_files/README +3 -2
  6. package/bin/exiftool_files/exiftool.pl +36 -14
  7. package/bin/exiftool_files/lib/Archive/Zip/Archive.pm +399 -65
  8. package/bin/exiftool_files/lib/Archive/Zip/DirectoryMember.pm +1 -1
  9. package/bin/exiftool_files/lib/Archive/Zip/FileMember.pm +1 -1
  10. package/bin/exiftool_files/lib/Archive/Zip/Member.pm +499 -195
  11. package/bin/exiftool_files/lib/Archive/Zip/NewFileMember.pm +1 -1
  12. package/bin/exiftool_files/lib/Archive/Zip/StringMember.pm +2 -2
  13. package/bin/exiftool_files/lib/Archive/Zip/ZipFileMember.pm +79 -20
  14. package/bin/exiftool_files/lib/Archive/Zip.pm +179 -29
  15. package/bin/exiftool_files/lib/AutoLoader.pm +453 -0
  16. package/bin/exiftool_files/lib/B/Deparse.pm +209 -137
  17. package/bin/exiftool_files/lib/B.pm +1 -1
  18. package/bin/exiftool_files/lib/Benchmark.pm +1123 -0
  19. package/bin/exiftool_files/lib/Class/Struct.pm +2 -2
  20. package/bin/exiftool_files/lib/Compress/Raw/Bzip2.pm +14 -9
  21. package/bin/exiftool_files/lib/Compress/Raw/Lzma.pm +982 -0
  22. package/bin/exiftool_files/lib/Compress/Raw/Zlib.pm +91 -86
  23. package/bin/exiftool_files/lib/Compress/Zlib.pm +105 -100
  24. package/bin/exiftool_files/lib/Config.pm +9 -9
  25. package/bin/exiftool_files/lib/Config_heavy.pl +36 -33
  26. package/bin/exiftool_files/lib/CryptX.pm +2 -82
  27. package/bin/exiftool_files/lib/Data/Dumper.pm +2 -2
  28. package/bin/exiftool_files/lib/Digest/MD5.pm +12 -9
  29. package/bin/exiftool_files/lib/Digest/Perl/MD5.pm +1 -191
  30. package/bin/exiftool_files/lib/Digest/base.pm +26 -20
  31. package/bin/exiftool_files/lib/DynaLoader.pm +7 -4
  32. package/bin/exiftool_files/lib/Encode.pm +3 -3
  33. package/bin/exiftool_files/lib/Errno.pm +13 -13
  34. package/bin/exiftool_files/lib/Exporter/Heavy.pm +2 -2
  35. package/bin/exiftool_files/lib/Exporter.pm +1 -1
  36. package/bin/exiftool_files/lib/ExtUtils/Command/MM.pm +323 -0
  37. package/bin/exiftool_files/lib/ExtUtils/Command.pm +382 -0
  38. package/bin/exiftool_files/lib/File/Find.pm +1 -1
  39. package/bin/exiftool_files/lib/File/Glob.pm +1 -8
  40. package/bin/exiftool_files/lib/File/GlobMapper.pm +2 -2
  41. package/bin/exiftool_files/lib/File/HomeDir/Darwin/Carbon.pm +2 -40
  42. package/bin/exiftool_files/lib/File/HomeDir/Darwin/Cocoa.pm +2 -34
  43. package/bin/exiftool_files/lib/File/HomeDir/Darwin.pm +2 -28
  44. package/bin/exiftool_files/lib/File/HomeDir/Driver.pm +2 -35
  45. package/bin/exiftool_files/lib/File/HomeDir/FreeDesktop.pm +2 -62
  46. package/bin/exiftool_files/lib/File/HomeDir/MacOS9.pm +2 -53
  47. package/bin/exiftool_files/lib/File/HomeDir/Test.pm +2 -43
  48. package/bin/exiftool_files/lib/File/HomeDir/Unix.pm +2 -53
  49. package/bin/exiftool_files/lib/File/HomeDir/Windows.pm +2 -69
  50. package/bin/exiftool_files/lib/File/HomeDir.pm +5 -416
  51. package/bin/exiftool_files/lib/File/Path.pm +3 -3
  52. package/bin/exiftool_files/lib/File/Spec/Win32.pm +2 -2
  53. package/bin/exiftool_files/lib/File/Temp.pm +70 -35
  54. package/bin/exiftool_files/lib/File/Which.pm +1 -240
  55. package/bin/exiftool_files/lib/File/stat.pm +3 -2
  56. package/bin/exiftool_files/lib/IO/Compress/Adapter/Bzip2.pm +16 -17
  57. package/bin/exiftool_files/lib/IO/Compress/Adapter/Deflate.pm +19 -20
  58. package/bin/exiftool_files/lib/IO/Compress/Base/Common.pm +5 -5
  59. package/bin/exiftool_files/lib/IO/Compress/Base.pm +35 -26
  60. package/bin/exiftool_files/lib/IO/Compress/Brotli.pm +159 -0
  61. package/bin/exiftool_files/lib/IO/Compress/Bzip2.pm +50 -25
  62. package/bin/exiftool_files/lib/IO/Compress/Gzip/Constants.pm +6 -6
  63. package/bin/exiftool_files/lib/IO/Compress/Gzip.pm +58 -32
  64. package/bin/exiftool_files/lib/IO/Compress/RawDeflate.pm +63 -38
  65. package/bin/exiftool_files/lib/IO/Compress/Zlib/Extra.pm +20 -20
  66. package/bin/exiftool_files/lib/IO/Dir.pm +1 -1
  67. package/bin/exiftool_files/lib/IO/File.pm +1 -1
  68. package/bin/exiftool_files/lib/IO/Handle.pm +1 -21
  69. package/bin/exiftool_files/lib/IO/Pipe.pm +1 -1
  70. package/bin/exiftool_files/lib/IO/Seekable.pm +1 -1
  71. package/bin/exiftool_files/lib/IO/Select.pm +16 -2
  72. package/bin/exiftool_files/lib/IO/Socket/INET.pm +14 -9
  73. package/bin/exiftool_files/lib/IO/Socket/UNIX.pm +17 -1
  74. package/bin/exiftool_files/lib/IO/Socket.pm +474 -126
  75. package/bin/exiftool_files/lib/IO/String.pm +425 -0
  76. package/bin/exiftool_files/lib/IO/Uncompress/Adapter/Inflate.pm +13 -14
  77. package/bin/exiftool_files/lib/IO/Uncompress/Base.pm +142 -132
  78. package/bin/exiftool_files/lib/IO/Uncompress/Brotli.pm +119 -0
  79. package/bin/exiftool_files/lib/IO/Uncompress/Gunzip.pm +43 -37
  80. package/bin/exiftool_files/lib/IO/Uncompress/RawInflate.pm +49 -43
  81. package/bin/exiftool_files/lib/IO.pm +2 -2
  82. package/bin/exiftool_files/lib/Image/ExifTool/BuildTagLookup.pm +44 -31
  83. package/bin/exiftool_files/lib/Image/ExifTool/CanonVRD.pm +2 -2
  84. package/bin/exiftool_files/lib/Image/ExifTool/FujiFilm.pm +20 -7
  85. package/bin/exiftool_files/lib/Image/ExifTool/GM.pm +543 -0
  86. package/bin/exiftool_files/lib/Image/ExifTool/Geolocation.pm +332 -149
  87. package/bin/exiftool_files/lib/Image/ExifTool/Geotag.pm +9 -4
  88. package/bin/exiftool_files/lib/Image/ExifTool/M2TS.pm +32 -4
  89. package/bin/exiftool_files/lib/Image/ExifTool/MakerNotes.pm +2 -2
  90. package/bin/exiftool_files/lib/Image/ExifTool/Microsoft.pm +1 -1
  91. package/bin/exiftool_files/lib/Image/ExifTool/Nikon.pm +331 -22
  92. package/bin/exiftool_files/lib/Image/ExifTool/NikonCustom.pm +55 -1
  93. package/bin/exiftool_files/lib/Image/ExifTool/Olympus.pm +1 -0
  94. package/bin/exiftool_files/lib/Image/ExifTool/OpenEXR.pm +21 -3
  95. package/bin/exiftool_files/lib/Image/ExifTool/PNG.pm +3 -3
  96. package/bin/exiftool_files/lib/Image/ExifTool/QuickTime.pm +40 -24
  97. package/bin/exiftool_files/lib/Image/ExifTool/QuickTimeStream.pl +61 -30
  98. package/bin/exiftool_files/lib/Image/ExifTool/README +2 -0
  99. package/bin/exiftool_files/lib/Image/ExifTool/Sony.pm +1 -1
  100. package/bin/exiftool_files/lib/Image/ExifTool/TagLookup.pm +4815 -4775
  101. package/bin/exiftool_files/lib/Image/ExifTool/TagNames.pod +931 -617
  102. package/bin/exiftool_files/lib/Image/ExifTool/WriteQuickTime.pl +30 -8
  103. package/bin/exiftool_files/lib/Image/ExifTool/Writer.pl +10 -4
  104. package/bin/exiftool_files/lib/Image/ExifTool/XMP.pm +4 -2
  105. package/bin/exiftool_files/lib/Image/ExifTool.pm +77 -41
  106. package/bin/exiftool_files/lib/Image/ExifTool.pod +24 -11
  107. package/bin/exiftool_files/lib/List/Util.pm +97 -8
  108. package/bin/exiftool_files/lib/MIME/Base64.pm +5 -5
  109. package/bin/exiftool_files/lib/MIME/Charset/_Compat.pm +106 -0
  110. package/bin/exiftool_files/lib/MIME/Charset.pm +1303 -0
  111. package/bin/exiftool_files/lib/Math/BigFloat.pm +444 -27
  112. package/bin/exiftool_files/lib/Math/BigInt/Calc.pm +296 -313
  113. package/bin/exiftool_files/lib/Math/BigInt/FastCalc.pm +1 -1
  114. package/bin/exiftool_files/lib/Math/BigInt/GMP.pm +2 -115
  115. package/bin/exiftool_files/lib/Math/BigInt/LTM.pm +2 -24
  116. package/bin/exiftool_files/lib/Math/BigInt/Lib.pm +61 -32
  117. package/bin/exiftool_files/lib/Math/BigInt.pm +292 -107
  118. package/bin/exiftool_files/lib/POSIX.pm +1 -1
  119. package/bin/exiftool_files/lib/PerlIO/scalar.pm +41 -0
  120. package/bin/exiftool_files/lib/PerlIO.pm +397 -0
  121. package/bin/exiftool_files/lib/Portable/CPAN.pm +94 -94
  122. package/bin/exiftool_files/lib/Portable/Config.pm +94 -94
  123. package/bin/exiftool_files/lib/Portable/FileSpec.pm +180 -180
  124. package/bin/exiftool_files/lib/Portable/HomeDir.pm +110 -110
  125. package/bin/exiftool_files/lib/Portable/LoadYaml.pm +430 -430
  126. package/bin/exiftool_files/lib/Portable/minicpan.pm +55 -55
  127. package/bin/exiftool_files/lib/Portable.pm +246 -320
  128. package/bin/exiftool_files/lib/Scalar/Util.pm +9 -4
  129. package/bin/exiftool_files/lib/Socket.pm +16 -12
  130. package/bin/exiftool_files/lib/Storable.pm +1444 -1441
  131. package/bin/exiftool_files/lib/TAP/Base.pm +133 -0
  132. package/bin/exiftool_files/lib/TAP/Formatter/Base.pm +467 -0
  133. package/bin/exiftool_files/lib/TAP/Formatter/Color.pm +116 -0
  134. package/bin/exiftool_files/lib/TAP/Formatter/Console/ParallelSession.pm +201 -0
  135. package/bin/exiftool_files/lib/TAP/Formatter/Console/Session.pm +205 -0
  136. package/bin/exiftool_files/lib/TAP/Formatter/Console.pm +100 -0
  137. package/bin/exiftool_files/lib/TAP/Formatter/File/Session.pm +95 -0
  138. package/bin/exiftool_files/lib/TAP/Formatter/File.pm +56 -0
  139. package/bin/exiftool_files/lib/TAP/Formatter/Session.pm +220 -0
  140. package/bin/exiftool_files/lib/TAP/Harness/Beyond.pod +426 -0
  141. package/bin/exiftool_files/lib/TAP/Harness/Env.pm +215 -0
  142. package/bin/exiftool_files/lib/TAP/Harness.pm +1054 -0
  143. package/bin/exiftool_files/lib/TAP/Object.pm +155 -0
  144. package/bin/exiftool_files/lib/TAP/Parser/Aggregator.pm +414 -0
  145. package/bin/exiftool_files/lib/TAP/Parser/Grammar.pm +584 -0
  146. package/bin/exiftool_files/lib/TAP/Parser/Iterator/Array.pm +100 -0
  147. package/bin/exiftool_files/lib/TAP/Parser/Iterator/Process.pm +378 -0
  148. package/bin/exiftool_files/lib/TAP/Parser/Iterator/Stream.pm +116 -0
  149. package/bin/exiftool_files/lib/TAP/Parser/Iterator.pm +162 -0
  150. package/bin/exiftool_files/lib/TAP/Parser/IteratorFactory.pm +339 -0
  151. package/bin/exiftool_files/lib/TAP/Parser/Multiplexer.pm +194 -0
  152. package/bin/exiftool_files/lib/TAP/Parser/Result/Bailout.pm +62 -0
  153. package/bin/exiftool_files/lib/TAP/Parser/Result/Comment.pm +60 -0
  154. package/bin/exiftool_files/lib/TAP/Parser/Result/Plan.pm +119 -0
  155. package/bin/exiftool_files/lib/TAP/Parser/Result/Pragma.pm +62 -0
  156. package/bin/exiftool_files/lib/TAP/Parser/Result/Test.pm +271 -0
  157. package/bin/exiftool_files/lib/TAP/Parser/Result/Unknown.pm +48 -0
  158. package/bin/exiftool_files/lib/TAP/Parser/Result/Version.pm +62 -0
  159. package/bin/exiftool_files/lib/TAP/Parser/Result/YAML.pm +61 -0
  160. package/bin/exiftool_files/lib/TAP/Parser/Result.pm +297 -0
  161. package/bin/exiftool_files/lib/TAP/Parser/ResultFactory.pm +183 -0
  162. package/bin/exiftool_files/lib/TAP/Parser/Scheduler/Job.pm +127 -0
  163. package/bin/exiftool_files/lib/TAP/Parser/Scheduler/Spinner.pm +61 -0
  164. package/bin/exiftool_files/lib/TAP/Parser/Scheduler.pm +448 -0
  165. package/bin/exiftool_files/lib/TAP/Parser/Source.pm +381 -0
  166. package/bin/exiftool_files/lib/TAP/Parser/SourceHandler/Executable.pm +184 -0
  167. package/bin/exiftool_files/lib/TAP/Parser/SourceHandler/File.pm +136 -0
  168. package/bin/exiftool_files/lib/TAP/Parser/SourceHandler/Handle.pm +124 -0
  169. package/bin/exiftool_files/lib/TAP/Parser/SourceHandler/Perl.pm +370 -0
  170. package/bin/exiftool_files/lib/TAP/Parser/SourceHandler/RawTAP.pm +130 -0
  171. package/bin/exiftool_files/lib/TAP/Parser/SourceHandler.pm +191 -0
  172. package/bin/exiftool_files/lib/TAP/Parser/YAMLish/Reader.pm +332 -0
  173. package/bin/exiftool_files/lib/TAP/Parser/YAMLish/Writer.pm +254 -0
  174. package/bin/exiftool_files/lib/TAP/Parser.pm +1931 -0
  175. package/bin/exiftool_files/lib/Test/Builder/Formatter.pm +107 -0
  176. package/bin/exiftool_files/lib/Test/Builder/IO/Scalar.pm +659 -0
  177. package/bin/exiftool_files/lib/Test/Builder/Module.pm +182 -0
  178. package/bin/exiftool_files/lib/Test/Builder/Tester/Color.pm +51 -0
  179. package/bin/exiftool_files/lib/Test/Builder/Tester.pm +675 -0
  180. package/bin/exiftool_files/lib/Test/Builder/TodoDiag.pm +68 -0
  181. package/bin/exiftool_files/lib/Test/Builder.pm +2653 -0
  182. package/bin/exiftool_files/lib/Test/Harness.pm +618 -0
  183. package/bin/exiftool_files/lib/Test/More.pm +1997 -0
  184. package/bin/exiftool_files/lib/Test/Simple.pm +220 -0
  185. package/bin/exiftool_files/lib/Test/Tester/Capture.pm +241 -0
  186. package/bin/exiftool_files/lib/Test/Tester/CaptureRunner.pm +79 -0
  187. package/bin/exiftool_files/lib/Test/Tester/Delegate.pm +45 -0
  188. package/bin/exiftool_files/lib/Test/Tester.pm +695 -0
  189. package/bin/exiftool_files/lib/Test/Tutorial.pod +618 -0
  190. package/bin/exiftool_files/lib/Test/use/ok.pm +64 -0
  191. package/bin/exiftool_files/lib/Text/ParseWords.pm +303 -0
  192. package/bin/exiftool_files/lib/Tie/StdHandle.pm +2 -2
  193. package/bin/exiftool_files/lib/Time/HiRes.pm +73 -68
  194. package/bin/exiftool_files/lib/Time/Local.pm +82 -35
  195. package/bin/exiftool_files/lib/Time/Piece.pm +19 -4
  196. package/bin/exiftool_files/lib/Time/Seconds.pm +1 -1
  197. package/bin/exiftool_files/lib/UNIVERSAL.pm +203 -0
  198. package/bin/exiftool_files/lib/Unicode/GCString.pm +60 -0
  199. package/bin/exiftool_files/lib/Unicode/LineBreak/Constants.pm +68 -0
  200. package/bin/exiftool_files/lib/Unicode/LineBreak.pm +248 -0
  201. package/bin/exiftool_files/lib/Win32/API/Struct.pm +1 -177
  202. package/bin/exiftool_files/lib/Win32/API/Type.pm +1 -100
  203. package/bin/exiftool_files/lib/Win32/API.pm +1 -830
  204. package/bin/exiftool_files/lib/Win32/FindFile.pm +2 -123
  205. package/bin/exiftool_files/lib/Win32.pm +213 -89
  206. package/bin/exiftool_files/lib/Win32API/File.pm +1 -1
  207. package/bin/exiftool_files/lib/auto/B/B.xs.dll +0 -0
  208. package/bin/exiftool_files/lib/auto/Compress/Raw/Bzip2/Bzip2.xs.dll +0 -0
  209. package/bin/exiftool_files/lib/auto/Compress/Raw/Lzma/Lzma.xs.dll +0 -0
  210. package/bin/exiftool_files/lib/auto/Compress/Raw/Lzma/autosplit.ix +3 -0
  211. package/bin/exiftool_files/lib/auto/Compress/Raw/Zlib/Zlib.xs.dll +0 -0
  212. package/bin/exiftool_files/lib/auto/CryptX/CryptX.xs.dll +0 -0
  213. package/bin/exiftool_files/lib/auto/Cwd/Cwd.xs.dll +0 -0
  214. package/bin/exiftool_files/lib/auto/Data/Dumper/Dumper.xs.dll +0 -0
  215. package/bin/exiftool_files/lib/auto/Digest/MD5/MD5.xs.dll +0 -0
  216. package/bin/exiftool_files/lib/auto/Digest/SHA/SHA.xs.dll +0 -0
  217. package/bin/exiftool_files/lib/auto/Encode/Encode.xs.dll +0 -0
  218. package/bin/exiftool_files/lib/auto/Fcntl/Fcntl.xs.dll +0 -0
  219. package/bin/exiftool_files/lib/auto/File/Glob/Glob.xs.dll +0 -0
  220. package/bin/exiftool_files/lib/auto/IO/Compress/Brotli/Brotli.xs.dll +0 -0
  221. package/bin/exiftool_files/lib/auto/IO/IO.xs.dll +0 -0
  222. package/bin/exiftool_files/lib/auto/List/Util/Util.xs.dll +0 -0
  223. package/bin/exiftool_files/lib/auto/MIME/Base64/Base64.xs.dll +0 -0
  224. package/bin/exiftool_files/lib/auto/Math/BigInt/FastCalc/FastCalc.xs.dll +0 -0
  225. package/bin/exiftool_files/lib/auto/Math/BigInt/GMP/GMP.xs.dll +0 -0
  226. package/bin/exiftool_files/lib/auto/POSIX/POSIX.xs.dll +0 -0
  227. package/bin/exiftool_files/lib/auto/PerlIO/scalar/scalar.xs.dll +0 -0
  228. package/bin/exiftool_files/lib/auto/Socket/Socket.xs.dll +0 -0
  229. package/bin/exiftool_files/lib/auto/Storable/Storable.xs.dll +0 -0
  230. package/bin/exiftool_files/lib/auto/Time/HiRes/HiRes.xs.dll +0 -0
  231. package/bin/exiftool_files/lib/auto/Time/Piece/Piece.xs.dll +0 -0
  232. package/bin/exiftool_files/lib/auto/Unicode/LineBreak/LineBreak.xs.dll +0 -0
  233. package/bin/exiftool_files/lib/auto/Win32/API/API.xs.dll +0 -0
  234. package/bin/exiftool_files/lib/auto/Win32/FindFile/FindFile.xs.dll +0 -0
  235. package/bin/exiftool_files/lib/auto/Win32/Win32.xs.dll +0 -0
  236. package/bin/exiftool_files/lib/auto/Win32API/File/File.xs.dll +0 -0
  237. package/bin/exiftool_files/lib/auto/mro/mro.xs.dll +0 -0
  238. package/bin/exiftool_files/lib/auto/re/re.xs.dll +0 -0
  239. package/bin/exiftool_files/lib/feature.pm +49 -17
  240. package/bin/exiftool_files/lib/mro.pm +4 -20
  241. package/bin/exiftool_files/lib/overload.pm +15 -15
  242. package/bin/exiftool_files/lib/parent.pm +10 -2
  243. package/bin/exiftool_files/lib/re.pm +91 -33
  244. package/bin/exiftool_files/lib/warnings.pm +17 -6
  245. package/bin/exiftool_files/libgcc_s_seh-1.dll +0 -0
  246. package/bin/exiftool_files/liblzma-5__.dll +0 -0
  247. package/bin/exiftool_files/libstdc++-6.dll +0 -0
  248. package/bin/exiftool_files/libwinpthread-1.dll +0 -0
  249. package/bin/exiftool_files/perl.exe +0 -0
  250. package/bin/exiftool_files/perl532.dll +0 -0
  251. package/package.json +7 -5
  252. package/bin/exiftool_files/libgcc_s_dw2-1.dll +0 -0
  253. package/bin/exiftool_files/perl530.dll +0 -0
@@ -0,0 +1,2653 @@
1
+ package Test::Builder;
2
+
3
+ use 5.006;
4
+ use strict;
5
+ use warnings;
6
+
7
+ our $VERSION = '1.302183';
8
+
9
+ BEGIN {
10
+ if( $] < 5.008 ) {
11
+ require Test::Builder::IO::Scalar;
12
+ }
13
+ }
14
+
15
+ use Scalar::Util qw/blessed reftype weaken/;
16
+
17
+ use Test2::Util qw/USE_THREADS try get_tid/;
18
+ use Test2::API qw/context release/;
19
+ # Make Test::Builder thread-safe for ithreads.
20
+ BEGIN {
21
+ warn "Test::Builder was loaded after Test2 initialization, this is not recommended."
22
+ if Test2::API::test2_init_done() || Test2::API::test2_load_done();
23
+
24
+ if (USE_THREADS && ! Test2::API::test2_ipc_disabled()) {
25
+ require Test2::IPC;
26
+ require Test2::IPC::Driver::Files;
27
+ Test2::IPC::Driver::Files->import;
28
+ Test2::API::test2_ipc_enable_polling();
29
+ Test2::API::test2_no_wait(1);
30
+ }
31
+ }
32
+
33
+ use Test2::Event::Subtest;
34
+ use Test2::Hub::Subtest;
35
+
36
+ use Test::Builder::Formatter;
37
+ use Test::Builder::TodoDiag;
38
+
39
+ our $Level = 1;
40
+ our $Test = $ENV{TB_NO_EARLY_INIT} ? undef : Test::Builder->new;
41
+
42
+ sub _add_ts_hooks {
43
+ my $self = shift;
44
+
45
+ my $hub = $self->{Stack}->top;
46
+
47
+ # Take a reference to the hash key, we do this to avoid closing over $self
48
+ # which is the singleton. We use a reference because the value could change
49
+ # in rare cases.
50
+ my $epkgr = \$self->{Exported_To};
51
+
52
+ #$hub->add_context_aquire(sub {$_[0]->{level} += $Level - 1});
53
+
54
+ $hub->pre_filter(
55
+ sub {
56
+ my ($active_hub, $e) = @_;
57
+
58
+ my $epkg = $$epkgr;
59
+ my $cpkg = $e->{trace} ? $e->{trace}->{frame}->[0] : undef;
60
+
61
+ no strict 'refs';
62
+ no warnings 'once';
63
+ my $todo;
64
+ $todo = ${"$cpkg\::TODO"} if $cpkg;
65
+ $todo = ${"$epkg\::TODO"} if $epkg && !$todo;
66
+
67
+ return $e unless defined($todo);
68
+ return $e unless length($todo);
69
+
70
+ # Turn a diag into a todo diag
71
+ return Test::Builder::TodoDiag->new(%$e) if ref($e) eq 'Test2::Event::Diag';
72
+
73
+ $e->set_todo($todo) if $e->can('set_todo');
74
+ $e->add_amnesty({tag => 'TODO', details => $todo});
75
+
76
+ # Set todo on ok's
77
+ if ($e->isa('Test2::Event::Ok')) {
78
+ $e->set_effective_pass(1);
79
+
80
+ if (my $result = $e->get_meta(__PACKAGE__)) {
81
+ $result->{reason} ||= $todo;
82
+ $result->{type} ||= 'todo';
83
+ $result->{ok} = 1;
84
+ }
85
+ }
86
+
87
+ return $e;
88
+ },
89
+
90
+ inherit => 1,
91
+
92
+ intercept_inherit => {
93
+ clean => sub {
94
+ my %params = @_;
95
+
96
+ my $state = $params{state};
97
+ my $trace = $params{trace};
98
+
99
+ my $epkg = $$epkgr;
100
+ my $cpkg = $trace->{frame}->[0];
101
+
102
+ no strict 'refs';
103
+ no warnings 'once';
104
+
105
+ $state->{+__PACKAGE__} = {};
106
+ $state->{+__PACKAGE__}->{"$cpkg\::TODO"} = ${"$cpkg\::TODO"} if $cpkg;
107
+ $state->{+__PACKAGE__}->{"$epkg\::TODO"} = ${"$epkg\::TODO"} if $epkg;
108
+
109
+ ${"$cpkg\::TODO"} = undef if $cpkg;
110
+ ${"$epkg\::TODO"} = undef if $epkg;
111
+ },
112
+ restore => sub {
113
+ my %params = @_;
114
+ my $state = $params{state};
115
+
116
+ no strict 'refs';
117
+ no warnings 'once';
118
+
119
+ for my $item (keys %{$state->{+__PACKAGE__}}) {
120
+ no strict 'refs';
121
+ no warnings 'once';
122
+
123
+ ${"$item"} = $state->{+__PACKAGE__}->{$item};
124
+ }
125
+ },
126
+ },
127
+ );
128
+ }
129
+
130
+ {
131
+ no warnings;
132
+ INIT {
133
+ use warnings;
134
+ Test2::API::test2_load() unless Test2::API::test2_in_preload();
135
+ }
136
+ }
137
+
138
+ sub new {
139
+ my($class) = shift;
140
+ unless($Test) {
141
+ $Test = $class->create(singleton => 1);
142
+
143
+ Test2::API::test2_add_callback_post_load(
144
+ sub {
145
+ $Test->{Original_Pid} = $$ if !$Test->{Original_Pid} || $Test->{Original_Pid} == 0;
146
+ $Test->reset(singleton => 1);
147
+ $Test->_add_ts_hooks;
148
+ }
149
+ );
150
+
151
+ # Non-TB tools normally expect 0 added to the level. $Level is normally 1. So
152
+ # we only want the level to change if $Level != 1.
153
+ # TB->ctx compensates for this later.
154
+ Test2::API::test2_add_callback_context_aquire(sub { $_[0]->{level} += $Level - 1 });
155
+
156
+ Test2::API::test2_add_callback_exit(sub { $Test->_ending(@_) });
157
+
158
+ Test2::API::test2_ipc()->set_no_fatal(1) if Test2::API::test2_has_ipc();
159
+ }
160
+ return $Test;
161
+ }
162
+
163
+ sub create {
164
+ my $class = shift;
165
+ my %params = @_;
166
+
167
+ my $self = bless {}, $class;
168
+ if ($params{singleton}) {
169
+ $self->{Stack} = Test2::API::test2_stack();
170
+ }
171
+ else {
172
+ $self->{Stack} = Test2::API::Stack->new;
173
+ $self->{Stack}->new_hub(
174
+ formatter => Test::Builder::Formatter->new,
175
+ ipc => Test2::API::test2_ipc(),
176
+ );
177
+
178
+ $self->reset(%params);
179
+ $self->_add_ts_hooks;
180
+ }
181
+
182
+ return $self;
183
+ }
184
+
185
+ sub ctx {
186
+ my $self = shift;
187
+ context(
188
+ # 1 for our frame, another for the -1 off of $Level in our hook at the top.
189
+ level => 2,
190
+ fudge => 1,
191
+ stack => $self->{Stack},
192
+ hub => $self->{Hub},
193
+ wrapped => 1,
194
+ @_
195
+ );
196
+ }
197
+
198
+ sub parent {
199
+ my $self = shift;
200
+ my $ctx = $self->ctx;
201
+ my $chub = $self->{Hub} || $ctx->hub;
202
+ $ctx->release;
203
+
204
+ my $meta = $chub->meta(__PACKAGE__, {});
205
+ my $parent = $meta->{parent};
206
+
207
+ return undef unless $parent;
208
+
209
+ return bless {
210
+ Original_Pid => $$,
211
+ Stack => $self->{Stack},
212
+ Hub => $parent,
213
+ }, blessed($self);
214
+ }
215
+
216
+ sub child {
217
+ my( $self, $name ) = @_;
218
+
219
+ $name ||= "Child of " . $self->name;
220
+ my $ctx = $self->ctx;
221
+
222
+ my $parent = $ctx->hub;
223
+ my $pmeta = $parent->meta(__PACKAGE__, {});
224
+ $self->croak("You already have a child named ($pmeta->{child}) running")
225
+ if $pmeta->{child};
226
+
227
+ $pmeta->{child} = $name;
228
+
229
+ # Clear $TODO for the child.
230
+ my $orig_TODO = $self->find_TODO(undef, 1, undef);
231
+
232
+ my $subevents = [];
233
+
234
+ my $hub = $ctx->stack->new_hub(
235
+ class => 'Test2::Hub::Subtest',
236
+ );
237
+
238
+ $hub->pre_filter(sub {
239
+ my ($active_hub, $e) = @_;
240
+
241
+ # Turn a diag into a todo diag
242
+ return Test::Builder::TodoDiag->new(%$e) if ref($e) eq 'Test2::Event::Diag';
243
+
244
+ return $e;
245
+ }, inherit => 1) if $orig_TODO;
246
+
247
+ $hub->listen(sub { push @$subevents => $_[1] });
248
+
249
+ $hub->set_nested( $parent->nested + 1 );
250
+
251
+ my $meta = $hub->meta(__PACKAGE__, {});
252
+ $meta->{Name} = $name;
253
+ $meta->{TODO} = $orig_TODO;
254
+ $meta->{TODO_PKG} = $ctx->trace->package;
255
+ $meta->{parent} = $parent;
256
+ $meta->{Test_Results} = [];
257
+ $meta->{subevents} = $subevents;
258
+ $meta->{subtest_id} = $hub->id;
259
+ $meta->{subtest_uuid} = $hub->uuid;
260
+ $meta->{subtest_buffered} = $parent->format ? 0 : 1;
261
+
262
+ $self->_add_ts_hooks;
263
+
264
+ $ctx->release;
265
+ return bless { Original_Pid => $$, Stack => $self->{Stack}, Hub => $hub, no_log_results => $self->{no_log_results} }, blessed($self);
266
+ }
267
+
268
+ sub finalize {
269
+ my $self = shift;
270
+ my $ok = 1;
271
+ ($ok) = @_ if @_;
272
+
273
+ my $st_ctx = $self->ctx;
274
+ my $chub = $self->{Hub} || return $st_ctx->release;
275
+
276
+ my $meta = $chub->meta(__PACKAGE__, {});
277
+ if ($meta->{child}) {
278
+ $self->croak("Can't call finalize() with child ($meta->{child}) active");
279
+ }
280
+
281
+ local $? = 0; # don't fail if $subtests happened to set $? nonzero
282
+
283
+ $self->{Stack}->pop($chub);
284
+
285
+ $self->find_TODO($meta->{TODO_PKG}, 1, $meta->{TODO});
286
+
287
+ my $parent = $self->parent;
288
+ my $ctx = $parent->ctx;
289
+ my $trace = $ctx->trace;
290
+ delete $ctx->hub->meta(__PACKAGE__, {})->{child};
291
+
292
+ $chub->finalize($trace->snapshot(hid => $chub->hid, nested => $chub->nested), 1)
293
+ if $ok
294
+ && $chub->count
295
+ && !$chub->no_ending
296
+ && !$chub->ended;
297
+
298
+ my $plan = $chub->plan || 0;
299
+ my $count = $chub->count;
300
+ my $failed = $chub->failed;
301
+ my $passed = $chub->is_passing;
302
+
303
+ my $num_extra = $plan =~ m/\D/ ? 0 : $count - $plan;
304
+ if ($count && $num_extra != 0) {
305
+ my $s = $plan == 1 ? '' : 's';
306
+ $st_ctx->diag(<<"FAIL");
307
+ Looks like you planned $plan test$s but ran $count.
308
+ FAIL
309
+ }
310
+
311
+ if ($failed) {
312
+ my $s = $failed == 1 ? '' : 's';
313
+
314
+ my $qualifier = $num_extra == 0 ? '' : ' run';
315
+
316
+ $st_ctx->diag(<<"FAIL");
317
+ Looks like you failed $failed test$s of $count$qualifier.
318
+ FAIL
319
+ }
320
+
321
+ if (!$passed && !$failed && $count && !$num_extra) {
322
+ $st_ctx->diag(<<"FAIL");
323
+ All assertions inside the subtest passed, but errors were encountered.
324
+ FAIL
325
+ }
326
+
327
+ $st_ctx->release;
328
+
329
+ unless ($chub->bailed_out) {
330
+ my $plan = $chub->plan;
331
+ if ( $plan && $plan eq 'SKIP' ) {
332
+ $parent->skip($chub->skip_reason, $meta->{Name});
333
+ }
334
+ elsif ( !$chub->count ) {
335
+ $parent->ok( 0, sprintf q[No tests run for subtest "%s"], $meta->{Name} );
336
+ }
337
+ else {
338
+ $parent->{subevents} = $meta->{subevents};
339
+ $parent->{subtest_id} = $meta->{subtest_id};
340
+ $parent->{subtest_uuid} = $meta->{subtest_uuid};
341
+ $parent->{subtest_buffered} = $meta->{subtest_buffered};
342
+ $parent->ok( $chub->is_passing, $meta->{Name} );
343
+ }
344
+ }
345
+
346
+ $ctx->release;
347
+ return $chub->is_passing;
348
+ }
349
+
350
+ sub subtest {
351
+ my $self = shift;
352
+ my ($name, $code, @args) = @_;
353
+ my $ctx = $self->ctx;
354
+ $ctx->throw("subtest()'s second argument must be a code ref")
355
+ unless $code && reftype($code) eq 'CODE';
356
+
357
+ $name ||= "Child of " . $self->name;
358
+
359
+
360
+ $_->($name,$code,@args)
361
+ for Test2::API::test2_list_pre_subtest_callbacks();
362
+
363
+ $ctx->note("Subtest: $name");
364
+
365
+ my $child = $self->child($name);
366
+
367
+ my $start_pid = $$;
368
+ my $st_ctx;
369
+ my ($ok, $err, $finished, $child_error);
370
+ T2_SUBTEST_WRAPPER: {
371
+ my $ctx = $self->ctx;
372
+ $st_ctx = $ctx->snapshot;
373
+ $ctx->release;
374
+ $ok = eval { local $Level = 1; $code->(@args); 1 };
375
+ ($err, $child_error) = ($@, $?);
376
+
377
+ # They might have done 'BEGIN { skip_all => "whatever" }'
378
+ if (!$ok && $err =~ m/Label not found for "last T2_SUBTEST_WRAPPER"/ || (blessed($err) && blessed($err) eq 'Test::Builder::Exception')) {
379
+ $ok = undef;
380
+ $err = undef;
381
+ }
382
+ else {
383
+ $finished = 1;
384
+ }
385
+ }
386
+
387
+ if ($start_pid != $$ && !$INC{'Test2/IPC.pm'}) {
388
+ warn $ok ? "Forked inside subtest, but subtest never finished!\n" : $err;
389
+ exit 255;
390
+ }
391
+
392
+ my $trace = $ctx->trace;
393
+
394
+ if (!$finished) {
395
+ if(my $bailed = $st_ctx->hub->bailed_out) {
396
+ my $chub = $child->{Hub};
397
+ $self->{Stack}->pop($chub);
398
+ $ctx->bail($bailed->reason);
399
+ }
400
+ my $code = $st_ctx->hub->exit_code;
401
+ $ok = !$code;
402
+ $err = "Subtest ended with exit code $code" if $code;
403
+ }
404
+
405
+ my $st_hub = $st_ctx->hub;
406
+ my $plan = $st_hub->plan;
407
+ my $count = $st_hub->count;
408
+
409
+ if (!$count && (!defined($plan) || "$plan" ne 'SKIP')) {
410
+ $st_ctx->plan(0) unless defined $plan;
411
+ $st_ctx->diag('No tests run!');
412
+ }
413
+
414
+ $child->finalize($st_ctx->trace);
415
+
416
+ $ctx->release;
417
+
418
+ die $err unless $ok;
419
+
420
+ $? = $child_error if defined $child_error;
421
+
422
+ return $st_hub->is_passing;
423
+ }
424
+
425
+ sub name {
426
+ my $self = shift;
427
+ my $ctx = $self->ctx;
428
+ release $ctx, $ctx->hub->meta(__PACKAGE__, {})->{Name};
429
+ }
430
+
431
+ sub reset { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
432
+ my ($self, %params) = @_;
433
+
434
+ Test2::API::test2_unset_is_end();
435
+
436
+ # We leave this a global because it has to be localized and localizing
437
+ # hash keys is just asking for pain. Also, it was documented.
438
+ $Level = 1;
439
+
440
+ $self->{no_log_results} = $ENV{TEST_NO_LOG_RESULTS} ? 1 : 0
441
+ unless $params{singleton};
442
+
443
+ $self->{Original_Pid} = Test2::API::test2_in_preload() ? -1 : $$;
444
+
445
+ my $ctx = $self->ctx;
446
+ my $hub = $ctx->hub;
447
+ $ctx->release;
448
+ unless ($params{singleton}) {
449
+ $hub->reset_state();
450
+ $hub->_tb_reset();
451
+ }
452
+
453
+ $ctx = $self->ctx;
454
+
455
+ my $meta = $ctx->hub->meta(__PACKAGE__, {});
456
+ %$meta = (
457
+ Name => $0,
458
+ Ending => 0,
459
+ Done_Testing => undef,
460
+ Skip_All => 0,
461
+ Test_Results => [],
462
+ parent => $meta->{parent},
463
+ );
464
+
465
+ $self->{Exported_To} = undef unless $params{singleton};
466
+
467
+ $self->{Orig_Handles} ||= do {
468
+ my $format = $ctx->hub->format;
469
+ my $out;
470
+ if ($format && $format->isa('Test2::Formatter::TAP')) {
471
+ $out = $format->handles;
472
+ }
473
+ $out ? [@$out] : [];
474
+ };
475
+
476
+ $self->use_numbers(1);
477
+ $self->no_header(0) unless $params{singleton};
478
+ $self->no_ending(0) unless $params{singleton};
479
+ $self->reset_outputs;
480
+
481
+ $ctx->release;
482
+
483
+ return;
484
+ }
485
+
486
+
487
+ my %plan_cmds = (
488
+ no_plan => \&no_plan,
489
+ skip_all => \&skip_all,
490
+ tests => \&_plan_tests,
491
+ );
492
+
493
+ sub plan {
494
+ my( $self, $cmd, $arg ) = @_;
495
+
496
+ return unless $cmd;
497
+
498
+ my $ctx = $self->ctx;
499
+ my $hub = $ctx->hub;
500
+
501
+ $ctx->throw("You tried to plan twice") if $hub->plan;
502
+
503
+ local $Level = $Level + 1;
504
+
505
+ if( my $method = $plan_cmds{$cmd} ) {
506
+ local $Level = $Level + 1;
507
+ $self->$method($arg);
508
+ }
509
+ else {
510
+ my @args = grep { defined } ( $cmd, $arg );
511
+ $ctx->throw("plan() doesn't understand @args");
512
+ }
513
+
514
+ release $ctx, 1;
515
+ }
516
+
517
+
518
+ sub _plan_tests {
519
+ my($self, $arg) = @_;
520
+
521
+ my $ctx = $self->ctx;
522
+
523
+ if($arg) {
524
+ local $Level = $Level + 1;
525
+ $self->expected_tests($arg);
526
+ }
527
+ elsif( !defined $arg ) {
528
+ $ctx->throw("Got an undefined number of tests");
529
+ }
530
+ else {
531
+ $ctx->throw("You said to run 0 tests");
532
+ }
533
+
534
+ $ctx->release;
535
+ }
536
+
537
+
538
+ sub expected_tests {
539
+ my $self = shift;
540
+ my($max) = @_;
541
+
542
+ my $ctx = $self->ctx;
543
+
544
+ if(@_) {
545
+ $self->croak("Number of tests must be a positive integer. You gave it '$max'")
546
+ unless $max =~ /^\+?\d+$/;
547
+
548
+ $ctx->plan($max);
549
+ }
550
+
551
+ my $hub = $ctx->hub;
552
+
553
+ $ctx->release;
554
+
555
+ my $plan = $hub->plan;
556
+ return 0 unless $plan;
557
+ return 0 if $plan =~ m/\D/;
558
+ return $plan;
559
+ }
560
+
561
+
562
+ sub no_plan {
563
+ my($self, $arg) = @_;
564
+
565
+ my $ctx = $self->ctx;
566
+
567
+ if (defined $ctx->hub->plan) {
568
+ warn "Plan already set, no_plan() is a no-op, this will change to a hard failure in the future.";
569
+ $ctx->release;
570
+ return;
571
+ }
572
+
573
+ $ctx->alert("no_plan takes no arguments") if $arg;
574
+
575
+ $ctx->hub->plan('NO PLAN');
576
+
577
+ release $ctx, 1;
578
+ }
579
+
580
+
581
+ sub done_testing {
582
+ my($self, $num_tests) = @_;
583
+
584
+ my $ctx = $self->ctx;
585
+
586
+ my $meta = $ctx->hub->meta(__PACKAGE__, {});
587
+
588
+ if ($meta->{Done_Testing}) {
589
+ my ($file, $line) = @{$meta->{Done_Testing}}[1,2];
590
+ local $ctx->hub->{ended}; # OMG This is awful.
591
+ $self->ok(0, "done_testing() was already called at $file line $line");
592
+ $ctx->release;
593
+ return;
594
+ }
595
+ $meta->{Done_Testing} = [$ctx->trace->call];
596
+
597
+ my $plan = $ctx->hub->plan;
598
+ my $count = $ctx->hub->count;
599
+
600
+ # If done_testing() specified the number of tests, shut off no_plan
601
+ if( defined $num_tests ) {
602
+ $ctx->plan($num_tests) if !$plan || $plan eq 'NO PLAN';
603
+ }
604
+ elsif ($count && defined $num_tests && $count != $num_tests) {
605
+ $self->ok(0, "planned to run @{[ $self->expected_tests ]} but done_testing() expects $num_tests");
606
+ }
607
+ else {
608
+ $num_tests = $self->current_test;
609
+ }
610
+
611
+ if( $self->expected_tests && $num_tests != $self->expected_tests ) {
612
+ $self->ok(0, "planned to run @{[ $self->expected_tests ]} ".
613
+ "but done_testing() expects $num_tests");
614
+ }
615
+
616
+ $ctx->plan($num_tests) if $ctx->hub->plan && $ctx->hub->plan eq 'NO PLAN';
617
+
618
+ $ctx->hub->finalize($ctx->trace, 1);
619
+
620
+ release $ctx, 1;
621
+ }
622
+
623
+
624
+ sub has_plan {
625
+ my $self = shift;
626
+
627
+ my $ctx = $self->ctx;
628
+ my $plan = $ctx->hub->plan;
629
+ $ctx->release;
630
+
631
+ return( $plan ) if $plan && $plan !~ m/\D/;
632
+ return('no_plan') if $plan && $plan eq 'NO PLAN';
633
+ return(undef);
634
+ }
635
+
636
+
637
+ sub skip_all {
638
+ my( $self, $reason ) = @_;
639
+
640
+ my $ctx = $self->ctx;
641
+
642
+ $ctx->hub->meta(__PACKAGE__, {})->{Skip_All} = $reason || 1;
643
+
644
+ # Work around old perl bug
645
+ if ($] < 5.020000) {
646
+ my $begin = 0;
647
+ my $level = 0;
648
+ while (my @call = caller($level++)) {
649
+ last unless @call && $call[0];
650
+ next unless $call[3] =~ m/::BEGIN$/;
651
+ $begin++;
652
+ last;
653
+ }
654
+ # HACK!
655
+ die 'Label not found for "last T2_SUBTEST_WRAPPER"' if $begin && $ctx->hub->meta(__PACKAGE__, {})->{parent};
656
+ }
657
+
658
+ $ctx->plan(0, SKIP => $reason);
659
+ }
660
+
661
+
662
+ sub exported_to {
663
+ my( $self, $pack ) = @_;
664
+
665
+ if( defined $pack ) {
666
+ $self->{Exported_To} = $pack;
667
+ }
668
+ return $self->{Exported_To};
669
+ }
670
+
671
+
672
+ sub ok {
673
+ my( $self, $test, $name ) = @_;
674
+
675
+ my $ctx = $self->ctx;
676
+
677
+ # $test might contain an object which we don't want to accidentally
678
+ # store, so we turn it into a boolean.
679
+ $test = $test ? 1 : 0;
680
+
681
+ # In case $name is a string overloaded object, force it to stringify.
682
+ no warnings qw/uninitialized numeric/;
683
+ $name = "$name" if defined $name;
684
+
685
+ # Profiling showed that the regex here was a huge time waster, doing the
686
+ # numeric addition first cuts our profile time from ~300ms to ~50ms
687
+ $self->diag(<<" ERR") if 0 + $name && $name =~ /^[\d\s]+$/;
688
+ You named your test '$name'. You shouldn't use numbers for your test names.
689
+ Very confusing.
690
+ ERR
691
+ use warnings qw/uninitialized numeric/;
692
+
693
+ my $trace = $ctx->{trace};
694
+ my $hub = $ctx->{hub};
695
+
696
+ my $result = {
697
+ ok => $test,
698
+ actual_ok => $test,
699
+ reason => '',
700
+ type => '',
701
+ (name => defined($name) ? $name : ''),
702
+ };
703
+
704
+ $hub->{_meta}->{+__PACKAGE__}->{Test_Results}[ $hub->{count} ] = $result unless $self->{no_log_results};
705
+
706
+ my $orig_name = $name;
707
+
708
+ my @attrs;
709
+ my $subevents = delete $self->{subevents};
710
+ my $subtest_id = delete $self->{subtest_id};
711
+ my $subtest_uuid = delete $self->{subtest_uuid};
712
+ my $subtest_buffered = delete $self->{subtest_buffered};
713
+ my $epkg = 'Test2::Event::Ok';
714
+ if ($subevents) {
715
+ $epkg = 'Test2::Event::Subtest';
716
+ push @attrs => (subevents => $subevents, subtest_id => $subtest_id, subtest_uuid => $subtest_uuid, buffered => $subtest_buffered);
717
+ }
718
+
719
+ my $e = bless {
720
+ trace => bless( {%$trace}, 'Test2::EventFacet::Trace'),
721
+ pass => $test,
722
+ name => $name,
723
+ _meta => {'Test::Builder' => $result},
724
+ effective_pass => $test,
725
+ @attrs,
726
+ }, $epkg;
727
+ $hub->send($e);
728
+
729
+ $self->_ok_debug($trace, $orig_name) unless($test);
730
+
731
+ $ctx->release;
732
+ return $test;
733
+ }
734
+
735
+ sub _ok_debug {
736
+ my $self = shift;
737
+ my ($trace, $orig_name) = @_;
738
+
739
+ my $is_todo = $self->in_todo;
740
+
741
+ my $msg = $is_todo ? "Failed (TODO)" : "Failed";
742
+
743
+ my (undef, $file, $line) = $trace->call;
744
+ if (defined $orig_name) {
745
+ $self->diag(qq[ $msg test '$orig_name'\n at $file line $line.\n]);
746
+ }
747
+ else {
748
+ $self->diag(qq[ $msg test at $file line $line.\n]);
749
+ }
750
+ }
751
+
752
+ sub _diag_fh {
753
+ my $self = shift;
754
+ local $Level = $Level + 1;
755
+ return $self->in_todo ? $self->todo_output : $self->failure_output;
756
+ }
757
+
758
+ sub _unoverload {
759
+ my ($self, $type, $thing) = @_;
760
+
761
+ return unless ref $$thing;
762
+ return unless blessed($$thing) || scalar $self->_try(sub{ $$thing->isa('UNIVERSAL') });
763
+ {
764
+ local ($!, $@);
765
+ require overload;
766
+ }
767
+ my $string_meth = overload::Method( $$thing, $type ) || return;
768
+ $$thing = $$thing->$string_meth();
769
+ }
770
+
771
+ sub _unoverload_str {
772
+ my $self = shift;
773
+
774
+ $self->_unoverload( q[""], $_ ) for @_;
775
+ }
776
+
777
+ sub _unoverload_num {
778
+ my $self = shift;
779
+
780
+ $self->_unoverload( '0+', $_ ) for @_;
781
+
782
+ for my $val (@_) {
783
+ next unless $self->_is_dualvar($$val);
784
+ $$val = $$val + 0;
785
+ }
786
+ }
787
+
788
+ # This is a hack to detect a dualvar such as $!
789
+ sub _is_dualvar {
790
+ my( $self, $val ) = @_;
791
+
792
+ # Objects are not dualvars.
793
+ return 0 if ref $val;
794
+
795
+ no warnings 'numeric';
796
+ my $numval = $val + 0;
797
+ return ($numval != 0 and $numval ne $val ? 1 : 0);
798
+ }
799
+
800
+
801
+ sub is_eq {
802
+ my( $self, $got, $expect, $name ) = @_;
803
+
804
+ my $ctx = $self->ctx;
805
+
806
+ local $Level = $Level + 1;
807
+
808
+ if( !defined $got || !defined $expect ) {
809
+ # undef only matches undef and nothing else
810
+ my $test = !defined $got && !defined $expect;
811
+
812
+ $self->ok( $test, $name );
813
+ $self->_is_diag( $got, 'eq', $expect ) unless $test;
814
+ $ctx->release;
815
+ return $test;
816
+ }
817
+
818
+ release $ctx, $self->cmp_ok( $got, 'eq', $expect, $name );
819
+ }
820
+
821
+
822
+ sub is_num {
823
+ my( $self, $got, $expect, $name ) = @_;
824
+ my $ctx = $self->ctx;
825
+ local $Level = $Level + 1;
826
+
827
+ if( !defined $got || !defined $expect ) {
828
+ # undef only matches undef and nothing else
829
+ my $test = !defined $got && !defined $expect;
830
+
831
+ $self->ok( $test, $name );
832
+ $self->_is_diag( $got, '==', $expect ) unless $test;
833
+ $ctx->release;
834
+ return $test;
835
+ }
836
+
837
+ release $ctx, $self->cmp_ok( $got, '==', $expect, $name );
838
+ }
839
+
840
+
841
+ sub _diag_fmt {
842
+ my( $self, $type, $val ) = @_;
843
+
844
+ if( defined $$val ) {
845
+ if( $type eq 'eq' or $type eq 'ne' ) {
846
+ # quote and force string context
847
+ $$val = "'$$val'";
848
+ }
849
+ else {
850
+ # force numeric context
851
+ $self->_unoverload_num($val);
852
+ }
853
+ }
854
+ else {
855
+ $$val = 'undef';
856
+ }
857
+
858
+ return;
859
+ }
860
+
861
+
862
+ sub _is_diag {
863
+ my( $self, $got, $type, $expect ) = @_;
864
+
865
+ $self->_diag_fmt( $type, $_ ) for \$got, \$expect;
866
+
867
+ local $Level = $Level + 1;
868
+ return $self->diag(<<"DIAGNOSTIC");
869
+ got: $got
870
+ expected: $expect
871
+ DIAGNOSTIC
872
+
873
+ }
874
+
875
+ sub _isnt_diag {
876
+ my( $self, $got, $type ) = @_;
877
+
878
+ $self->_diag_fmt( $type, \$got );
879
+
880
+ local $Level = $Level + 1;
881
+ return $self->diag(<<"DIAGNOSTIC");
882
+ got: $got
883
+ expected: anything else
884
+ DIAGNOSTIC
885
+ }
886
+
887
+
888
+ sub isnt_eq {
889
+ my( $self, $got, $dont_expect, $name ) = @_;
890
+ my $ctx = $self->ctx;
891
+ local $Level = $Level + 1;
892
+
893
+ if( !defined $got || !defined $dont_expect ) {
894
+ # undef only matches undef and nothing else
895
+ my $test = defined $got || defined $dont_expect;
896
+
897
+ $self->ok( $test, $name );
898
+ $self->_isnt_diag( $got, 'ne' ) unless $test;
899
+ $ctx->release;
900
+ return $test;
901
+ }
902
+
903
+ release $ctx, $self->cmp_ok( $got, 'ne', $dont_expect, $name );
904
+ }
905
+
906
+ sub isnt_num {
907
+ my( $self, $got, $dont_expect, $name ) = @_;
908
+ my $ctx = $self->ctx;
909
+ local $Level = $Level + 1;
910
+
911
+ if( !defined $got || !defined $dont_expect ) {
912
+ # undef only matches undef and nothing else
913
+ my $test = defined $got || defined $dont_expect;
914
+
915
+ $self->ok( $test, $name );
916
+ $self->_isnt_diag( $got, '!=' ) unless $test;
917
+ $ctx->release;
918
+ return $test;
919
+ }
920
+
921
+ release $ctx, $self->cmp_ok( $got, '!=', $dont_expect, $name );
922
+ }
923
+
924
+
925
+ sub like {
926
+ my( $self, $thing, $regex, $name ) = @_;
927
+ my $ctx = $self->ctx;
928
+
929
+ local $Level = $Level + 1;
930
+
931
+ release $ctx, $self->_regex_ok( $thing, $regex, '=~', $name );
932
+ }
933
+
934
+ sub unlike {
935
+ my( $self, $thing, $regex, $name ) = @_;
936
+ my $ctx = $self->ctx;
937
+
938
+ local $Level = $Level + 1;
939
+
940
+ release $ctx, $self->_regex_ok( $thing, $regex, '!~', $name );
941
+ }
942
+
943
+
944
+ my %numeric_cmps = map { ( $_, 1 ) } ( "<", "<=", ">", ">=", "==", "!=", "<=>" );
945
+
946
+ # Bad, these are not comparison operators. Should we include more?
947
+ my %cmp_ok_bl = map { ( $_, 1 ) } ( "=", "+=", ".=", "x=", "^=", "|=", "||=", "&&=", "...");
948
+
949
+ sub cmp_ok {
950
+ my( $self, $got, $type, $expect, $name ) = @_;
951
+ my $ctx = $self->ctx;
952
+
953
+ if ($cmp_ok_bl{$type}) {
954
+ $ctx->throw("$type is not a valid comparison operator in cmp_ok()");
955
+ }
956
+
957
+ my ($test, $succ);
958
+ my $error;
959
+ {
960
+ ## no critic (BuiltinFunctions::ProhibitStringyEval)
961
+
962
+ local( $@, $!, $SIG{__DIE__} ); # isolate eval
963
+
964
+ my($pack, $file, $line) = $ctx->trace->call();
965
+ my $warning_bits = $ctx->trace->warning_bits;
966
+ # convert this to a code string so the BEGIN doesn't have to close
967
+ # over it, which can lead to issues with Devel::Cover
968
+ my $bits_code = defined $warning_bits ? qq["\Q$warning_bits\E"] : 'undef';
969
+
970
+ # This is so that warnings come out at the caller's level
971
+ $succ = eval qq[
972
+ BEGIN {\${^WARNING_BITS} = $bits_code};
973
+ #line $line "(eval in cmp_ok) $file"
974
+ \$test = (\$got $type \$expect);
975
+ 1;
976
+ ];
977
+ $error = $@;
978
+ }
979
+ local $Level = $Level + 1;
980
+ my $ok = $self->ok( $test, $name );
981
+
982
+ # Treat overloaded objects as numbers if we're asked to do a
983
+ # numeric comparison.
984
+ my $unoverload
985
+ = $numeric_cmps{$type}
986
+ ? '_unoverload_num'
987
+ : '_unoverload_str';
988
+
989
+ $self->diag(<<"END") unless $succ;
990
+ An error occurred while using $type:
991
+ ------------------------------------
992
+ $error
993
+ ------------------------------------
994
+ END
995
+
996
+ unless($ok) {
997
+ $self->$unoverload( \$got, \$expect );
998
+
999
+ if( $type =~ /^(eq|==)$/ ) {
1000
+ $self->_is_diag( $got, $type, $expect );
1001
+ }
1002
+ elsif( $type =~ /^(ne|!=)$/ ) {
1003
+ no warnings;
1004
+ my $eq = ($got eq $expect || $got == $expect)
1005
+ && (
1006
+ (defined($got) xor defined($expect))
1007
+ || (length($got) != length($expect))
1008
+ );
1009
+ use warnings;
1010
+
1011
+ if ($eq) {
1012
+ $self->_cmp_diag( $got, $type, $expect );
1013
+ }
1014
+ else {
1015
+ $self->_isnt_diag( $got, $type );
1016
+ }
1017
+ }
1018
+ else {
1019
+ $self->_cmp_diag( $got, $type, $expect );
1020
+ }
1021
+ }
1022
+ return release $ctx, $ok;
1023
+ }
1024
+
1025
+ sub _cmp_diag {
1026
+ my( $self, $got, $type, $expect ) = @_;
1027
+
1028
+ $got = defined $got ? "'$got'" : 'undef';
1029
+ $expect = defined $expect ? "'$expect'" : 'undef';
1030
+
1031
+ local $Level = $Level + 1;
1032
+ return $self->diag(<<"DIAGNOSTIC");
1033
+ $got
1034
+ $type
1035
+ $expect
1036
+ DIAGNOSTIC
1037
+ }
1038
+
1039
+ sub _caller_context {
1040
+ my $self = shift;
1041
+
1042
+ my( $pack, $file, $line ) = $self->caller(1);
1043
+
1044
+ my $code = '';
1045
+ $code .= "#line $line $file\n" if defined $file and defined $line;
1046
+
1047
+ return $code;
1048
+ }
1049
+
1050
+
1051
+ sub BAIL_OUT {
1052
+ my( $self, $reason ) = @_;
1053
+
1054
+ my $ctx = $self->ctx;
1055
+
1056
+ $self->{Bailed_Out} = 1;
1057
+
1058
+ $ctx->bail($reason);
1059
+ }
1060
+
1061
+
1062
+ {
1063
+ no warnings 'once';
1064
+ *BAILOUT = \&BAIL_OUT;
1065
+ }
1066
+
1067
+ sub skip {
1068
+ my( $self, $why, $name ) = @_;
1069
+ $why ||= '';
1070
+ $name = '' unless defined $name;
1071
+ $self->_unoverload_str( \$why );
1072
+
1073
+ my $ctx = $self->ctx;
1074
+
1075
+ $ctx->hub->meta(__PACKAGE__, {})->{Test_Results}[ $ctx->hub->count ] = {
1076
+ 'ok' => 1,
1077
+ actual_ok => 1,
1078
+ name => $name,
1079
+ type => 'skip',
1080
+ reason => $why,
1081
+ } unless $self->{no_log_results};
1082
+
1083
+ $name =~ s|#|\\#|g; # # in a name can confuse Test::Harness.
1084
+ $name =~ s{\n}{\n# }sg;
1085
+ $why =~ s{\n}{\n# }sg;
1086
+
1087
+ my $tctx = $ctx->snapshot;
1088
+ $tctx->skip('', $why);
1089
+
1090
+ return release $ctx, 1;
1091
+ }
1092
+
1093
+
1094
+ sub todo_skip {
1095
+ my( $self, $why ) = @_;
1096
+ $why ||= '';
1097
+
1098
+ my $ctx = $self->ctx;
1099
+
1100
+ $ctx->hub->meta(__PACKAGE__, {})->{Test_Results}[ $ctx->hub->count ] = {
1101
+ 'ok' => 1,
1102
+ actual_ok => 0,
1103
+ name => '',
1104
+ type => 'todo_skip',
1105
+ reason => $why,
1106
+ } unless $self->{no_log_results};
1107
+
1108
+ $why =~ s{\n}{\n# }sg;
1109
+ my $tctx = $ctx->snapshot;
1110
+ $tctx->send_event( 'Skip', todo => $why, todo_diag => 1, reason => $why, pass => 0);
1111
+
1112
+ return release $ctx, 1;
1113
+ }
1114
+
1115
+
1116
+ sub maybe_regex {
1117
+ my( $self, $regex ) = @_;
1118
+ my $usable_regex = undef;
1119
+
1120
+ return $usable_regex unless defined $regex;
1121
+
1122
+ my( $re, $opts );
1123
+
1124
+ # Check for qr/foo/
1125
+ if( _is_qr($regex) ) {
1126
+ $usable_regex = $regex;
1127
+ }
1128
+ # Check for '/foo/' or 'm,foo,'
1129
+ elsif(( $re, $opts ) = $regex =~ m{^ /(.*)/ (\w*) $ }sx or
1130
+ ( undef, $re, $opts ) = $regex =~ m,^ m([^\w\s]) (.+) \1 (\w*) $,sx
1131
+ )
1132
+ {
1133
+ $usable_regex = length $opts ? "(?$opts)$re" : $re;
1134
+ }
1135
+
1136
+ return $usable_regex;
1137
+ }
1138
+
1139
+ sub _is_qr {
1140
+ my $regex = shift;
1141
+
1142
+ # is_regexp() checks for regexes in a robust manner, say if they're
1143
+ # blessed.
1144
+ return re::is_regexp($regex) if defined &re::is_regexp;
1145
+ return ref $regex eq 'Regexp';
1146
+ }
1147
+
1148
+ sub _regex_ok {
1149
+ my( $self, $thing, $regex, $cmp, $name ) = @_;
1150
+
1151
+ my $ok = 0;
1152
+ my $usable_regex = $self->maybe_regex($regex);
1153
+ unless( defined $usable_regex ) {
1154
+ local $Level = $Level + 1;
1155
+ $ok = $self->ok( 0, $name );
1156
+ $self->diag(" '$regex' doesn't look much like a regex to me.");
1157
+ return $ok;
1158
+ }
1159
+
1160
+ {
1161
+ my $test;
1162
+ my $context = $self->_caller_context;
1163
+
1164
+ {
1165
+ ## no critic (BuiltinFunctions::ProhibitStringyEval)
1166
+
1167
+ local( $@, $!, $SIG{__DIE__} ); # isolate eval
1168
+
1169
+ # No point in issuing an uninit warning, they'll see it in the diagnostics
1170
+ no warnings 'uninitialized';
1171
+
1172
+ $test = eval $context . q{$test = $thing =~ /$usable_regex/ ? 1 : 0};
1173
+ }
1174
+
1175
+ $test = !$test if $cmp eq '!~';
1176
+
1177
+ local $Level = $Level + 1;
1178
+ $ok = $self->ok( $test, $name );
1179
+ }
1180
+
1181
+ unless($ok) {
1182
+ $thing = defined $thing ? "'$thing'" : 'undef';
1183
+ my $match = $cmp eq '=~' ? "doesn't match" : "matches";
1184
+
1185
+ local $Level = $Level + 1;
1186
+ $self->diag( sprintf <<'DIAGNOSTIC', $thing, $match, $regex );
1187
+ %s
1188
+ %13s '%s'
1189
+ DIAGNOSTIC
1190
+
1191
+ }
1192
+
1193
+ return $ok;
1194
+ }
1195
+
1196
+
1197
+ sub is_fh {
1198
+ my $self = shift;
1199
+ my $maybe_fh = shift;
1200
+ return 0 unless defined $maybe_fh;
1201
+
1202
+ return 1 if ref $maybe_fh eq 'GLOB'; # its a glob ref
1203
+ return 1 if ref \$maybe_fh eq 'GLOB'; # its a glob
1204
+
1205
+ return eval { $maybe_fh->isa("IO::Handle") } ||
1206
+ eval { tied($maybe_fh)->can('TIEHANDLE') };
1207
+ }
1208
+
1209
+
1210
+ sub level {
1211
+ my( $self, $level ) = @_;
1212
+
1213
+ if( defined $level ) {
1214
+ $Level = $level;
1215
+ }
1216
+ return $Level;
1217
+ }
1218
+
1219
+
1220
+ sub use_numbers {
1221
+ my( $self, $use_nums ) = @_;
1222
+
1223
+ my $ctx = $self->ctx;
1224
+ my $format = $ctx->hub->format;
1225
+ unless ($format && $format->can('no_numbers') && $format->can('set_no_numbers')) {
1226
+ warn "The current formatter does not support 'use_numbers'" if $format;
1227
+ return release $ctx, 0;
1228
+ }
1229
+
1230
+ $format->set_no_numbers(!$use_nums) if defined $use_nums;
1231
+
1232
+ return release $ctx, $format->no_numbers ? 0 : 1;
1233
+ }
1234
+
1235
+ BEGIN {
1236
+ for my $method (qw(no_header no_diag)) {
1237
+ my $set = "set_$method";
1238
+ my $code = sub {
1239
+ my( $self, $no ) = @_;
1240
+
1241
+ my $ctx = $self->ctx;
1242
+ my $format = $ctx->hub->format;
1243
+ unless ($format && $format->can($set)) {
1244
+ warn "The current formatter does not support '$method'" if $format;
1245
+ $ctx->release;
1246
+ return
1247
+ }
1248
+
1249
+ $format->$set($no) if defined $no;
1250
+
1251
+ return release $ctx, $format->$method ? 1 : 0;
1252
+ };
1253
+
1254
+ no strict 'refs'; ## no critic
1255
+ *$method = $code;
1256
+ }
1257
+ }
1258
+
1259
+ sub no_ending {
1260
+ my( $self, $no ) = @_;
1261
+
1262
+ my $ctx = $self->ctx;
1263
+
1264
+ $ctx->hub->set_no_ending($no) if defined $no;
1265
+
1266
+ return release $ctx, $ctx->hub->no_ending;
1267
+ }
1268
+
1269
+ sub diag {
1270
+ my $self = shift;
1271
+ return unless @_;
1272
+
1273
+ my $text = join '' => map {defined($_) ? $_ : 'undef'} @_;
1274
+
1275
+ if (Test2::API::test2_in_preload()) {
1276
+ chomp($text);
1277
+ $text =~ s/^/# /msg;
1278
+ print STDERR $text, "\n";
1279
+ return 0;
1280
+ }
1281
+
1282
+ my $ctx = $self->ctx;
1283
+ $ctx->diag($text);
1284
+ $ctx->release;
1285
+ return 0;
1286
+ }
1287
+
1288
+
1289
+ sub note {
1290
+ my $self = shift;
1291
+ return unless @_;
1292
+
1293
+ my $text = join '' => map {defined($_) ? $_ : 'undef'} @_;
1294
+
1295
+ if (Test2::API::test2_in_preload()) {
1296
+ chomp($text);
1297
+ $text =~ s/^/# /msg;
1298
+ print STDOUT $text, "\n";
1299
+ return 0;
1300
+ }
1301
+
1302
+ my $ctx = $self->ctx;
1303
+ $ctx->note($text);
1304
+ $ctx->release;
1305
+ return 0;
1306
+ }
1307
+
1308
+
1309
+ sub explain {
1310
+ my $self = shift;
1311
+
1312
+ local ($@, $!);
1313
+ require Data::Dumper;
1314
+
1315
+ return map {
1316
+ ref $_
1317
+ ? do {
1318
+ my $dumper = Data::Dumper->new( [$_] );
1319
+ $dumper->Indent(1)->Terse(1);
1320
+ $dumper->Sortkeys(1) if $dumper->can("Sortkeys");
1321
+ $dumper->Dump;
1322
+ }
1323
+ : $_
1324
+ } @_;
1325
+ }
1326
+
1327
+
1328
+ sub output {
1329
+ my( $self, $fh ) = @_;
1330
+
1331
+ my $ctx = $self->ctx;
1332
+ my $format = $ctx->hub->format;
1333
+ $ctx->release;
1334
+ return unless $format && $format->isa('Test2::Formatter::TAP');
1335
+
1336
+ $format->handles->[Test2::Formatter::TAP::OUT_STD()] = $self->_new_fh($fh)
1337
+ if defined $fh;
1338
+
1339
+ return $format->handles->[Test2::Formatter::TAP::OUT_STD()];
1340
+ }
1341
+
1342
+ sub failure_output {
1343
+ my( $self, $fh ) = @_;
1344
+
1345
+ my $ctx = $self->ctx;
1346
+ my $format = $ctx->hub->format;
1347
+ $ctx->release;
1348
+ return unless $format && $format->isa('Test2::Formatter::TAP');
1349
+
1350
+ $format->handles->[Test2::Formatter::TAP::OUT_ERR()] = $self->_new_fh($fh)
1351
+ if defined $fh;
1352
+
1353
+ return $format->handles->[Test2::Formatter::TAP::OUT_ERR()];
1354
+ }
1355
+
1356
+ sub todo_output {
1357
+ my( $self, $fh ) = @_;
1358
+
1359
+ my $ctx = $self->ctx;
1360
+ my $format = $ctx->hub->format;
1361
+ $ctx->release;
1362
+ return unless $format && $format->isa('Test::Builder::Formatter');
1363
+
1364
+ $format->handles->[Test::Builder::Formatter::OUT_TODO()] = $self->_new_fh($fh)
1365
+ if defined $fh;
1366
+
1367
+ return $format->handles->[Test::Builder::Formatter::OUT_TODO()];
1368
+ }
1369
+
1370
+ sub _new_fh {
1371
+ my $self = shift;
1372
+ my($file_or_fh) = shift;
1373
+
1374
+ my $fh;
1375
+ if( $self->is_fh($file_or_fh) ) {
1376
+ $fh = $file_or_fh;
1377
+ }
1378
+ elsif( ref $file_or_fh eq 'SCALAR' ) {
1379
+ # Scalar refs as filehandles was added in 5.8.
1380
+ if( $] >= 5.008 ) {
1381
+ open $fh, ">>", $file_or_fh
1382
+ or $self->croak("Can't open scalar ref $file_or_fh: $!");
1383
+ }
1384
+ # Emulate scalar ref filehandles with a tie.
1385
+ else {
1386
+ $fh = Test::Builder::IO::Scalar->new($file_or_fh)
1387
+ or $self->croak("Can't tie scalar ref $file_or_fh");
1388
+ }
1389
+ }
1390
+ else {
1391
+ open $fh, ">", $file_or_fh
1392
+ or $self->croak("Can't open test output log $file_or_fh: $!");
1393
+ _autoflush($fh);
1394
+ }
1395
+
1396
+ return $fh;
1397
+ }
1398
+
1399
+ sub _autoflush {
1400
+ my($fh) = shift;
1401
+ my $old_fh = select $fh;
1402
+ $| = 1;
1403
+ select $old_fh;
1404
+
1405
+ return;
1406
+ }
1407
+
1408
+
1409
+ sub reset_outputs {
1410
+ my $self = shift;
1411
+
1412
+ my $ctx = $self->ctx;
1413
+ my $format = $ctx->hub->format;
1414
+ $ctx->release;
1415
+ return unless $format && $format->isa('Test2::Formatter::TAP');
1416
+ $format->set_handles([@{$self->{Orig_Handles}}]) if $self->{Orig_Handles};
1417
+
1418
+ return;
1419
+ }
1420
+
1421
+
1422
+ sub carp {
1423
+ my $self = shift;
1424
+ my $ctx = $self->ctx;
1425
+ $ctx->alert(join "", @_);
1426
+ $ctx->release;
1427
+ }
1428
+
1429
+ sub croak {
1430
+ my $self = shift;
1431
+ my $ctx = $self->ctx;
1432
+ $ctx->throw(join "", @_);
1433
+ $ctx->release;
1434
+ }
1435
+
1436
+
1437
+ sub current_test {
1438
+ my( $self, $num ) = @_;
1439
+
1440
+ my $ctx = $self->ctx;
1441
+ my $hub = $ctx->hub;
1442
+
1443
+ if( defined $num ) {
1444
+ $hub->set_count($num);
1445
+
1446
+ unless ($self->{no_log_results}) {
1447
+ # If the test counter is being pushed forward fill in the details.
1448
+ my $test_results = $ctx->hub->meta(__PACKAGE__, {})->{Test_Results};
1449
+ if ($num > @$test_results) {
1450
+ my $start = @$test_results ? @$test_results : 0;
1451
+ for ($start .. $num - 1) {
1452
+ $test_results->[$_] = {
1453
+ 'ok' => 1,
1454
+ actual_ok => undef,
1455
+ reason => 'incrementing test number',
1456
+ type => 'unknown',
1457
+ name => undef
1458
+ };
1459
+ }
1460
+ }
1461
+ # If backward, wipe history. Its their funeral.
1462
+ elsif ($num < @$test_results) {
1463
+ $#{$test_results} = $num - 1;
1464
+ }
1465
+ }
1466
+ }
1467
+ return release $ctx, $hub->count;
1468
+ }
1469
+
1470
+
1471
+ sub is_passing {
1472
+ my $self = shift;
1473
+
1474
+ my $ctx = $self->ctx;
1475
+ my $hub = $ctx->hub;
1476
+
1477
+ if( @_ ) {
1478
+ my ($bool) = @_;
1479
+ $hub->set_failed(0) if $bool;
1480
+ $hub->is_passing($bool);
1481
+ }
1482
+
1483
+ return release $ctx, $hub->is_passing;
1484
+ }
1485
+
1486
+
1487
+ sub summary {
1488
+ my($self) = shift;
1489
+
1490
+ return if $self->{no_log_results};
1491
+
1492
+ my $ctx = $self->ctx;
1493
+ my $data = $ctx->hub->meta(__PACKAGE__, {})->{Test_Results};
1494
+ $ctx->release;
1495
+ return map { $_ ? $_->{'ok'} : () } @$data;
1496
+ }
1497
+
1498
+
1499
+ sub details {
1500
+ my $self = shift;
1501
+
1502
+ return if $self->{no_log_results};
1503
+
1504
+ my $ctx = $self->ctx;
1505
+ my $data = $ctx->hub->meta(__PACKAGE__, {})->{Test_Results};
1506
+ $ctx->release;
1507
+ return @$data;
1508
+ }
1509
+
1510
+
1511
+ sub find_TODO {
1512
+ my( $self, $pack, $set, $new_value ) = @_;
1513
+
1514
+ my $ctx = $self->ctx;
1515
+
1516
+ $pack ||= $ctx->trace->package || $self->exported_to;
1517
+ $ctx->release;
1518
+
1519
+ return unless $pack;
1520
+
1521
+ no strict 'refs'; ## no critic
1522
+ no warnings 'once';
1523
+ my $old_value = ${ $pack . '::TODO' };
1524
+ $set and ${ $pack . '::TODO' } = $new_value;
1525
+ return $old_value;
1526
+ }
1527
+
1528
+ sub todo {
1529
+ my( $self, $pack ) = @_;
1530
+
1531
+ local $Level = $Level + 1;
1532
+ my $ctx = $self->ctx;
1533
+ $ctx->release;
1534
+
1535
+ my $meta = $ctx->hub->meta(__PACKAGE__, {todo => []})->{todo};
1536
+ return $meta->[-1]->[1] if $meta && @$meta;
1537
+
1538
+ $pack ||= $ctx->trace->package;
1539
+
1540
+ return unless $pack;
1541
+
1542
+ no strict 'refs'; ## no critic
1543
+ no warnings 'once';
1544
+ return ${ $pack . '::TODO' };
1545
+ }
1546
+
1547
+ sub in_todo {
1548
+ my $self = shift;
1549
+
1550
+ local $Level = $Level + 1;
1551
+ my $ctx = $self->ctx;
1552
+ $ctx->release;
1553
+
1554
+ my $meta = $ctx->hub->meta(__PACKAGE__, {todo => []})->{todo};
1555
+ return 1 if $meta && @$meta;
1556
+
1557
+ my $pack = $ctx->trace->package || return 0;
1558
+
1559
+ no strict 'refs'; ## no critic
1560
+ no warnings 'once';
1561
+ my $todo = ${ $pack . '::TODO' };
1562
+
1563
+ return 0 unless defined $todo;
1564
+ return 0 if "$todo" eq '';
1565
+ return 1;
1566
+ }
1567
+
1568
+ sub todo_start {
1569
+ my $self = shift;
1570
+ my $message = @_ ? shift : '';
1571
+
1572
+ my $ctx = $self->ctx;
1573
+
1574
+ my $hub = $ctx->hub;
1575
+ my $filter = $hub->pre_filter(sub {
1576
+ my ($active_hub, $e) = @_;
1577
+
1578
+ # Turn a diag into a todo diag
1579
+ return Test::Builder::TodoDiag->new(%$e) if ref($e) eq 'Test2::Event::Diag';
1580
+
1581
+ # Set todo on ok's
1582
+ if ($hub == $active_hub && $e->isa('Test2::Event::Ok')) {
1583
+ $e->set_todo($message);
1584
+ $e->set_effective_pass(1);
1585
+
1586
+ if (my $result = $e->get_meta(__PACKAGE__)) {
1587
+ $result->{reason} ||= $message;
1588
+ $result->{type} ||= 'todo';
1589
+ $result->{ok} = 1;
1590
+ }
1591
+ }
1592
+
1593
+ return $e;
1594
+ }, inherit => 1);
1595
+
1596
+ push @{$ctx->hub->meta(__PACKAGE__, {todo => []})->{todo}} => [$filter, $message];
1597
+
1598
+ $ctx->release;
1599
+
1600
+ return;
1601
+ }
1602
+
1603
+ sub todo_end {
1604
+ my $self = shift;
1605
+
1606
+ my $ctx = $self->ctx;
1607
+
1608
+ my $set = pop @{$ctx->hub->meta(__PACKAGE__, {todo => []})->{todo}};
1609
+
1610
+ $ctx->throw('todo_end() called without todo_start()') unless $set;
1611
+
1612
+ $ctx->hub->pre_unfilter($set->[0]);
1613
+
1614
+ $ctx->release;
1615
+
1616
+ return;
1617
+ }
1618
+
1619
+
1620
+ sub caller { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
1621
+ my( $self ) = @_;
1622
+
1623
+ my $ctx = $self->ctx;
1624
+
1625
+ my $trace = $ctx->trace;
1626
+ $ctx->release;
1627
+ return wantarray ? $trace->call : $trace->package;
1628
+ }
1629
+
1630
+
1631
+ sub _try {
1632
+ my( $self, $code, %opts ) = @_;
1633
+
1634
+ my $error;
1635
+ my $return;
1636
+ {
1637
+ local $!; # eval can mess up $!
1638
+ local $@; # don't set $@ in the test
1639
+ local $SIG{__DIE__}; # don't trip an outside DIE handler.
1640
+ $return = eval { $code->() };
1641
+ $error = $@;
1642
+ }
1643
+
1644
+ die $error if $error and $opts{die_on_fail};
1645
+
1646
+ return wantarray ? ( $return, $error ) : $return;
1647
+ }
1648
+
1649
+ sub _ending {
1650
+ my $self = shift;
1651
+ my ($ctx, $real_exit_code, $new) = @_;
1652
+
1653
+ unless ($ctx) {
1654
+ my $octx = $self->ctx;
1655
+ $ctx = $octx->snapshot;
1656
+ $octx->release;
1657
+ }
1658
+
1659
+ return if $ctx->hub->no_ending;
1660
+ return if $ctx->hub->meta(__PACKAGE__, {})->{Ending}++;
1661
+
1662
+ # Don't bother with an ending if this is a forked copy. Only the parent
1663
+ # should do the ending.
1664
+ return unless $self->{Original_Pid} == $$;
1665
+
1666
+ my $hub = $ctx->hub;
1667
+ return if $hub->bailed_out;
1668
+
1669
+ my $plan = $hub->plan;
1670
+ my $count = $hub->count;
1671
+ my $failed = $hub->failed;
1672
+ my $passed = $hub->is_passing;
1673
+ return unless $plan || $count || $failed;
1674
+
1675
+ # Ran tests but never declared a plan or hit done_testing
1676
+ if( !$hub->plan and $hub->count ) {
1677
+ $self->diag("Tests were run but no plan was declared and done_testing() was not seen.");
1678
+
1679
+ if($real_exit_code) {
1680
+ $self->diag(<<"FAIL");
1681
+ Looks like your test exited with $real_exit_code just after $count.
1682
+ FAIL
1683
+ $$new ||= $real_exit_code;
1684
+ return;
1685
+ }
1686
+
1687
+ # But if the tests ran, handle exit code.
1688
+ if($failed > 0) {
1689
+ my $exit_code = $failed <= 254 ? $failed : 254;
1690
+ $$new ||= $exit_code;
1691
+ return;
1692
+ }
1693
+
1694
+ $$new ||= 254;
1695
+ return;
1696
+ }
1697
+
1698
+ if ($real_exit_code && !$count) {
1699
+ $self->diag("Looks like your test exited with $real_exit_code before it could output anything.");
1700
+ $$new ||= $real_exit_code;
1701
+ return;
1702
+ }
1703
+
1704
+ return if $plan && "$plan" eq 'SKIP';
1705
+
1706
+ if (!$count) {
1707
+ $self->diag('No tests run!');
1708
+ $$new ||= 255;
1709
+ return;
1710
+ }
1711
+
1712
+ if ($real_exit_code) {
1713
+ $self->diag(<<"FAIL");
1714
+ Looks like your test exited with $real_exit_code just after $count.
1715
+ FAIL
1716
+ $$new ||= $real_exit_code;
1717
+ return;
1718
+ }
1719
+
1720
+ if ($plan eq 'NO PLAN') {
1721
+ $ctx->plan( $count );
1722
+ $plan = $hub->plan;
1723
+ }
1724
+
1725
+ # Figure out if we passed or failed and print helpful messages.
1726
+ my $num_extra = $count - $plan;
1727
+
1728
+ if ($num_extra != 0) {
1729
+ my $s = $plan == 1 ? '' : 's';
1730
+ $self->diag(<<"FAIL");
1731
+ Looks like you planned $plan test$s but ran $count.
1732
+ FAIL
1733
+ }
1734
+
1735
+ if ($failed) {
1736
+ my $s = $failed == 1 ? '' : 's';
1737
+
1738
+ my $qualifier = $num_extra == 0 ? '' : ' run';
1739
+
1740
+ $self->diag(<<"FAIL");
1741
+ Looks like you failed $failed test$s of $count$qualifier.
1742
+ FAIL
1743
+ }
1744
+
1745
+ if (!$passed && !$failed && $count && !$num_extra) {
1746
+ $ctx->diag(<<"FAIL");
1747
+ All assertions passed, but errors were encountered.
1748
+ FAIL
1749
+ }
1750
+
1751
+ my $exit_code = 0;
1752
+ if ($failed) {
1753
+ $exit_code = $failed <= 254 ? $failed : 254;
1754
+ }
1755
+ elsif ($num_extra != 0) {
1756
+ $exit_code = 255;
1757
+ }
1758
+ elsif (!$passed) {
1759
+ $exit_code = 255;
1760
+ }
1761
+
1762
+ $$new ||= $exit_code;
1763
+ return;
1764
+ }
1765
+
1766
+ # Some things used this even though it was private... I am looking at you
1767
+ # Test::Builder::Prefix...
1768
+ sub _print_comment {
1769
+ my( $self, $fh, @msgs ) = @_;
1770
+
1771
+ return if $self->no_diag;
1772
+ return unless @msgs;
1773
+
1774
+ # Prevent printing headers when compiling (i.e. -c)
1775
+ return if $^C;
1776
+
1777
+ # Smash args together like print does.
1778
+ # Convert undef to 'undef' so its readable.
1779
+ my $msg = join '', map { defined($_) ? $_ : 'undef' } @msgs;
1780
+
1781
+ # Escape the beginning, _print will take care of the rest.
1782
+ $msg =~ s/^/# /;
1783
+
1784
+ local( $\, $", $, ) = ( undef, ' ', '' );
1785
+ print $fh $msg;
1786
+
1787
+ return 0;
1788
+ }
1789
+
1790
+ # This is used by Test::SharedFork to turn on IPC after the fact. Not
1791
+ # documenting because I do not want it used. The method name is borrowed from
1792
+ # Test::Builder 2
1793
+ # Once Test2 stuff goes stable this method will be removed and Test::SharedFork
1794
+ # will be made smarter.
1795
+ sub coordinate_forks {
1796
+ my $self = shift;
1797
+
1798
+ {
1799
+ local ($@, $!);
1800
+ require Test2::IPC;
1801
+ }
1802
+ Test2::IPC->import;
1803
+ Test2::API::test2_ipc_enable_polling();
1804
+ Test2::API::test2_load();
1805
+ my $ipc = Test2::IPC::apply_ipc($self->{Stack});
1806
+ $ipc->set_no_fatal(1);
1807
+ Test2::API::test2_no_wait(1);
1808
+ }
1809
+
1810
+ sub no_log_results { $_[0]->{no_log_results} = 1 }
1811
+
1812
+ 1;
1813
+
1814
+ __END__
1815
+
1816
+ =head1 NAME
1817
+
1818
+ Test::Builder - Backend for building test libraries
1819
+
1820
+ =head1 SYNOPSIS
1821
+
1822
+ package My::Test::Module;
1823
+ use base 'Test::Builder::Module';
1824
+
1825
+ my $CLASS = __PACKAGE__;
1826
+
1827
+ sub ok {
1828
+ my($test, $name) = @_;
1829
+ my $tb = $CLASS->builder;
1830
+
1831
+ $tb->ok($test, $name);
1832
+ }
1833
+
1834
+
1835
+ =head1 DESCRIPTION
1836
+
1837
+ L<Test::Simple> and L<Test::More> have proven to be popular testing modules,
1838
+ but they're not always flexible enough. Test::Builder provides a
1839
+ building block upon which to write your own test libraries I<which can
1840
+ work together>.
1841
+
1842
+ =head2 Construction
1843
+
1844
+ =over 4
1845
+
1846
+ =item B<new>
1847
+
1848
+ my $Test = Test::Builder->new;
1849
+
1850
+ Returns a Test::Builder object representing the current state of the
1851
+ test.
1852
+
1853
+ Since you only run one test per program C<new> always returns the same
1854
+ Test::Builder object. No matter how many times you call C<new()>, you're
1855
+ getting the same object. This is called a singleton. This is done so that
1856
+ multiple modules share such global information as the test counter and
1857
+ where test output is going.
1858
+
1859
+ If you want a completely new Test::Builder object different from the
1860
+ singleton, use C<create>.
1861
+
1862
+ =item B<create>
1863
+
1864
+ my $Test = Test::Builder->create;
1865
+
1866
+ Ok, so there can be more than one Test::Builder object and this is how
1867
+ you get it. You might use this instead of C<new()> if you're testing
1868
+ a Test::Builder based module, but otherwise you probably want C<new>.
1869
+
1870
+ B<NOTE>: the implementation is not complete. C<level>, for example, is still
1871
+ shared by B<all> Test::Builder objects, even ones created using this method.
1872
+ Also, the method name may change in the future.
1873
+
1874
+ =item B<subtest>
1875
+
1876
+ $builder->subtest($name, \&subtests, @args);
1877
+
1878
+ See documentation of C<subtest> in Test::More.
1879
+
1880
+ C<subtest> also, and optionally, accepts arguments which will be passed to the
1881
+ subtests reference.
1882
+
1883
+ =item B<name>
1884
+
1885
+ diag $builder->name;
1886
+
1887
+ Returns the name of the current builder. Top level builders default to C<$0>
1888
+ (the name of the executable). Child builders are named via the C<child>
1889
+ method. If no name is supplied, will be named "Child of $parent->name".
1890
+
1891
+ =item B<reset>
1892
+
1893
+ $Test->reset;
1894
+
1895
+ Reinitializes the Test::Builder singleton to its original state.
1896
+ Mostly useful for tests run in persistent environments where the same
1897
+ test might be run multiple times in the same process.
1898
+
1899
+ =back
1900
+
1901
+ =head2 Setting up tests
1902
+
1903
+ These methods are for setting up tests and declaring how many there
1904
+ are. You usually only want to call one of these methods.
1905
+
1906
+ =over 4
1907
+
1908
+ =item B<plan>
1909
+
1910
+ $Test->plan('no_plan');
1911
+ $Test->plan( skip_all => $reason );
1912
+ $Test->plan( tests => $num_tests );
1913
+
1914
+ A convenient way to set up your tests. Call this and Test::Builder
1915
+ will print the appropriate headers and take the appropriate actions.
1916
+
1917
+ If you call C<plan()>, don't call any of the other methods below.
1918
+
1919
+ =item B<expected_tests>
1920
+
1921
+ my $max = $Test->expected_tests;
1922
+ $Test->expected_tests($max);
1923
+
1924
+ Gets/sets the number of tests we expect this test to run and prints out
1925
+ the appropriate headers.
1926
+
1927
+
1928
+ =item B<no_plan>
1929
+
1930
+ $Test->no_plan;
1931
+
1932
+ Declares that this test will run an indeterminate number of tests.
1933
+
1934
+
1935
+ =item B<done_testing>
1936
+
1937
+ $Test->done_testing();
1938
+ $Test->done_testing($num_tests);
1939
+
1940
+ Declares that you are done testing, no more tests will be run after this point.
1941
+
1942
+ If a plan has not yet been output, it will do so.
1943
+
1944
+ $num_tests is the number of tests you planned to run. If a numbered
1945
+ plan was already declared, and if this contradicts, a failing test
1946
+ will be run to reflect the planning mistake. If C<no_plan> was declared,
1947
+ this will override.
1948
+
1949
+ If C<done_testing()> is called twice, the second call will issue a
1950
+ failing test.
1951
+
1952
+ If C<$num_tests> is omitted, the number of tests run will be used, like
1953
+ no_plan.
1954
+
1955
+ C<done_testing()> is, in effect, used when you'd want to use C<no_plan>, but
1956
+ safer. You'd use it like so:
1957
+
1958
+ $Test->ok($a == $b);
1959
+ $Test->done_testing();
1960
+
1961
+ Or to plan a variable number of tests:
1962
+
1963
+ for my $test (@tests) {
1964
+ $Test->ok($test);
1965
+ }
1966
+ $Test->done_testing(scalar @tests);
1967
+
1968
+
1969
+ =item B<has_plan>
1970
+
1971
+ $plan = $Test->has_plan
1972
+
1973
+ Find out whether a plan has been defined. C<$plan> is either C<undef> (no plan
1974
+ has been set), C<no_plan> (indeterminate # of tests) or an integer (the number
1975
+ of expected tests).
1976
+
1977
+ =item B<skip_all>
1978
+
1979
+ $Test->skip_all;
1980
+ $Test->skip_all($reason);
1981
+
1982
+ Skips all the tests, using the given C<$reason>. Exits immediately with 0.
1983
+
1984
+ =item B<exported_to>
1985
+
1986
+ my $pack = $Test->exported_to;
1987
+ $Test->exported_to($pack);
1988
+
1989
+ Tells Test::Builder what package you exported your functions to.
1990
+
1991
+ This method isn't terribly useful since modules which share the same
1992
+ Test::Builder object might get exported to different packages and only
1993
+ the last one will be honored.
1994
+
1995
+ =back
1996
+
1997
+ =head2 Running tests
1998
+
1999
+ These actually run the tests, analogous to the functions in Test::More.
2000
+
2001
+ They all return true if the test passed, false if the test failed.
2002
+
2003
+ C<$name> is always optional.
2004
+
2005
+ =over 4
2006
+
2007
+ =item B<ok>
2008
+
2009
+ $Test->ok($test, $name);
2010
+
2011
+ Your basic test. Pass if C<$test> is true, fail if $test is false. Just
2012
+ like Test::Simple's C<ok()>.
2013
+
2014
+ =item B<is_eq>
2015
+
2016
+ $Test->is_eq($got, $expected, $name);
2017
+
2018
+ Like Test::More's C<is()>. Checks if C<$got eq $expected>. This is the
2019
+ string version.
2020
+
2021
+ C<undef> only ever matches another C<undef>.
2022
+
2023
+ =item B<is_num>
2024
+
2025
+ $Test->is_num($got, $expected, $name);
2026
+
2027
+ Like Test::More's C<is()>. Checks if C<$got == $expected>. This is the
2028
+ numeric version.
2029
+
2030
+ C<undef> only ever matches another C<undef>.
2031
+
2032
+ =item B<isnt_eq>
2033
+
2034
+ $Test->isnt_eq($got, $dont_expect, $name);
2035
+
2036
+ Like L<Test::More>'s C<isnt()>. Checks if C<$got ne $dont_expect>. This is
2037
+ the string version.
2038
+
2039
+ =item B<isnt_num>
2040
+
2041
+ $Test->isnt_num($got, $dont_expect, $name);
2042
+
2043
+ Like L<Test::More>'s C<isnt()>. Checks if C<$got ne $dont_expect>. This is
2044
+ the numeric version.
2045
+
2046
+ =item B<like>
2047
+
2048
+ $Test->like($thing, qr/$regex/, $name);
2049
+ $Test->like($thing, '/$regex/', $name);
2050
+
2051
+ Like L<Test::More>'s C<like()>. Checks if $thing matches the given C<$regex>.
2052
+
2053
+ =item B<unlike>
2054
+
2055
+ $Test->unlike($thing, qr/$regex/, $name);
2056
+ $Test->unlike($thing, '/$regex/', $name);
2057
+
2058
+ Like L<Test::More>'s C<unlike()>. Checks if $thing B<does not match> the
2059
+ given C<$regex>.
2060
+
2061
+ =item B<cmp_ok>
2062
+
2063
+ $Test->cmp_ok($thing, $type, $that, $name);
2064
+
2065
+ Works just like L<Test::More>'s C<cmp_ok()>.
2066
+
2067
+ $Test->cmp_ok($big_num, '!=', $other_big_num);
2068
+
2069
+ =back
2070
+
2071
+ =head2 Other Testing Methods
2072
+
2073
+ These are methods which are used in the course of writing a test but are not themselves tests.
2074
+
2075
+ =over 4
2076
+
2077
+ =item B<BAIL_OUT>
2078
+
2079
+ $Test->BAIL_OUT($reason);
2080
+
2081
+ Indicates to the L<Test::Harness> that things are going so badly all
2082
+ testing should terminate. This includes running any additional test
2083
+ scripts.
2084
+
2085
+ It will exit with 255.
2086
+
2087
+ =for deprecated
2088
+ BAIL_OUT() used to be BAILOUT()
2089
+
2090
+ =item B<skip>
2091
+
2092
+ $Test->skip;
2093
+ $Test->skip($why);
2094
+
2095
+ Skips the current test, reporting C<$why>.
2096
+
2097
+ =item B<todo_skip>
2098
+
2099
+ $Test->todo_skip;
2100
+ $Test->todo_skip($why);
2101
+
2102
+ Like C<skip()>, only it will declare the test as failing and TODO. Similar
2103
+ to
2104
+
2105
+ print "not ok $tnum # TODO $why\n";
2106
+
2107
+ =begin _unimplemented
2108
+
2109
+ =item B<skip_rest>
2110
+
2111
+ $Test->skip_rest;
2112
+ $Test->skip_rest($reason);
2113
+
2114
+ Like C<skip()>, only it skips all the rest of the tests you plan to run
2115
+ and terminates the test.
2116
+
2117
+ If you're running under C<no_plan>, it skips once and terminates the
2118
+ test.
2119
+
2120
+ =end _unimplemented
2121
+
2122
+ =back
2123
+
2124
+
2125
+ =head2 Test building utility methods
2126
+
2127
+ These methods are useful when writing your own test methods.
2128
+
2129
+ =over 4
2130
+
2131
+ =item B<maybe_regex>
2132
+
2133
+ $Test->maybe_regex(qr/$regex/);
2134
+ $Test->maybe_regex('/$regex/');
2135
+
2136
+ This method used to be useful back when Test::Builder worked on Perls
2137
+ before 5.6 which didn't have qr//. Now its pretty useless.
2138
+
2139
+ Convenience method for building testing functions that take regular
2140
+ expressions as arguments.
2141
+
2142
+ Takes a quoted regular expression produced by C<qr//>, or a string
2143
+ representing a regular expression.
2144
+
2145
+ Returns a Perl value which may be used instead of the corresponding
2146
+ regular expression, or C<undef> if its argument is not recognized.
2147
+
2148
+ For example, a version of C<like()>, sans the useful diagnostic messages,
2149
+ could be written as:
2150
+
2151
+ sub laconic_like {
2152
+ my ($self, $thing, $regex, $name) = @_;
2153
+ my $usable_regex = $self->maybe_regex($regex);
2154
+ die "expecting regex, found '$regex'\n"
2155
+ unless $usable_regex;
2156
+ $self->ok($thing =~ m/$usable_regex/, $name);
2157
+ }
2158
+
2159
+
2160
+ =item B<is_fh>
2161
+
2162
+ my $is_fh = $Test->is_fh($thing);
2163
+
2164
+ Determines if the given C<$thing> can be used as a filehandle.
2165
+
2166
+ =cut
2167
+
2168
+
2169
+ =back
2170
+
2171
+
2172
+ =head2 Test style
2173
+
2174
+
2175
+ =over 4
2176
+
2177
+ =item B<level>
2178
+
2179
+ $Test->level($how_high);
2180
+
2181
+ How far up the call stack should C<$Test> look when reporting where the
2182
+ test failed.
2183
+
2184
+ Defaults to 1.
2185
+
2186
+ Setting C<$Test::Builder::Level> overrides. This is typically useful
2187
+ localized:
2188
+
2189
+ sub my_ok {
2190
+ my $test = shift;
2191
+
2192
+ local $Test::Builder::Level = $Test::Builder::Level + 1;
2193
+ $TB->ok($test);
2194
+ }
2195
+
2196
+ To be polite to other functions wrapping your own you usually want to increment C<$Level> rather than set it to a constant.
2197
+
2198
+ =item B<use_numbers>
2199
+
2200
+ $Test->use_numbers($on_or_off);
2201
+
2202
+ Whether or not the test should output numbers. That is, this if true:
2203
+
2204
+ ok 1
2205
+ ok 2
2206
+ ok 3
2207
+
2208
+ or this if false
2209
+
2210
+ ok
2211
+ ok
2212
+ ok
2213
+
2214
+ Most useful when you can't depend on the test output order, such as
2215
+ when threads or forking is involved.
2216
+
2217
+ Defaults to on.
2218
+
2219
+ =item B<no_diag>
2220
+
2221
+ $Test->no_diag($no_diag);
2222
+
2223
+ If set true no diagnostics will be printed. This includes calls to
2224
+ C<diag()>.
2225
+
2226
+ =item B<no_ending>
2227
+
2228
+ $Test->no_ending($no_ending);
2229
+
2230
+ Normally, Test::Builder does some extra diagnostics when the test
2231
+ ends. It also changes the exit code as described below.
2232
+
2233
+ If this is true, none of that will be done.
2234
+
2235
+ =item B<no_header>
2236
+
2237
+ $Test->no_header($no_header);
2238
+
2239
+ If set to true, no "1..N" header will be printed.
2240
+
2241
+ =back
2242
+
2243
+ =head2 Output
2244
+
2245
+ Controlling where the test output goes.
2246
+
2247
+ It's ok for your test to change where STDOUT and STDERR point to,
2248
+ Test::Builder's default output settings will not be affected.
2249
+
2250
+ =over 4
2251
+
2252
+ =item B<diag>
2253
+
2254
+ $Test->diag(@msgs);
2255
+
2256
+ Prints out the given C<@msgs>. Like C<print>, arguments are simply
2257
+ appended together.
2258
+
2259
+ Normally, it uses the C<failure_output()> handle, but if this is for a
2260
+ TODO test, the C<todo_output()> handle is used.
2261
+
2262
+ Output will be indented and marked with a # so as not to interfere
2263
+ with test output. A newline will be put on the end if there isn't one
2264
+ already.
2265
+
2266
+ We encourage using this rather than calling print directly.
2267
+
2268
+ Returns false. Why? Because C<diag()> is often used in conjunction with
2269
+ a failing test (C<ok() || diag()>) it "passes through" the failure.
2270
+
2271
+ return ok(...) || diag(...);
2272
+
2273
+ =for blame transfer
2274
+ Mark Fowler <mark@twoshortplanks.com>
2275
+
2276
+ =item B<note>
2277
+
2278
+ $Test->note(@msgs);
2279
+
2280
+ Like C<diag()>, but it prints to the C<output()> handle so it will not
2281
+ normally be seen by the user except in verbose mode.
2282
+
2283
+ =item B<explain>
2284
+
2285
+ my @dump = $Test->explain(@msgs);
2286
+
2287
+ Will dump the contents of any references in a human readable format.
2288
+ Handy for things like...
2289
+
2290
+ is_deeply($have, $want) || diag explain $have;
2291
+
2292
+ or
2293
+
2294
+ is_deeply($have, $want) || note explain $have;
2295
+
2296
+ =item B<output>
2297
+
2298
+ =item B<failure_output>
2299
+
2300
+ =item B<todo_output>
2301
+
2302
+ my $filehandle = $Test->output;
2303
+ $Test->output($filehandle);
2304
+ $Test->output($filename);
2305
+ $Test->output(\$scalar);
2306
+
2307
+ These methods control where Test::Builder will print its output.
2308
+ They take either an open C<$filehandle>, a C<$filename> to open and write to
2309
+ or a C<$scalar> reference to append to. It will always return a C<$filehandle>.
2310
+
2311
+ B<output> is where normal "ok/not ok" test output goes.
2312
+
2313
+ Defaults to STDOUT.
2314
+
2315
+ B<failure_output> is where diagnostic output on test failures and
2316
+ C<diag()> goes. It is normally not read by Test::Harness and instead is
2317
+ displayed to the user.
2318
+
2319
+ Defaults to STDERR.
2320
+
2321
+ C<todo_output> is used instead of C<failure_output()> for the
2322
+ diagnostics of a failing TODO test. These will not be seen by the
2323
+ user.
2324
+
2325
+ Defaults to STDOUT.
2326
+
2327
+ =item reset_outputs
2328
+
2329
+ $tb->reset_outputs;
2330
+
2331
+ Resets all the output filehandles back to their defaults.
2332
+
2333
+ =item carp
2334
+
2335
+ $tb->carp(@message);
2336
+
2337
+ Warns with C<@message> but the message will appear to come from the
2338
+ point where the original test function was called (C<< $tb->caller >>).
2339
+
2340
+ =item croak
2341
+
2342
+ $tb->croak(@message);
2343
+
2344
+ Dies with C<@message> but the message will appear to come from the
2345
+ point where the original test function was called (C<< $tb->caller >>).
2346
+
2347
+
2348
+ =back
2349
+
2350
+
2351
+ =head2 Test Status and Info
2352
+
2353
+ =over 4
2354
+
2355
+ =item B<no_log_results>
2356
+
2357
+ This will turn off result long-term storage. Calling this method will make
2358
+ C<details> and C<summary> useless. You may want to use this if you are running
2359
+ enough tests to fill up all available memory.
2360
+
2361
+ Test::Builder->new->no_log_results();
2362
+
2363
+ There is no way to turn it back on.
2364
+
2365
+ =item B<current_test>
2366
+
2367
+ my $curr_test = $Test->current_test;
2368
+ $Test->current_test($num);
2369
+
2370
+ Gets/sets the current test number we're on. You usually shouldn't
2371
+ have to set this.
2372
+
2373
+ If set forward, the details of the missing tests are filled in as 'unknown'.
2374
+ if set backward, the details of the intervening tests are deleted. You
2375
+ can erase history if you really want to.
2376
+
2377
+
2378
+ =item B<is_passing>
2379
+
2380
+ my $ok = $builder->is_passing;
2381
+
2382
+ Indicates if the test suite is currently passing.
2383
+
2384
+ More formally, it will be false if anything has happened which makes
2385
+ it impossible for the test suite to pass. True otherwise.
2386
+
2387
+ For example, if no tests have run C<is_passing()> will be true because
2388
+ even though a suite with no tests is a failure you can add a passing
2389
+ test to it and start passing.
2390
+
2391
+ Don't think about it too much.
2392
+
2393
+
2394
+ =item B<summary>
2395
+
2396
+ my @tests = $Test->summary;
2397
+
2398
+ A simple summary of the tests so far. True for pass, false for fail.
2399
+ This is a logical pass/fail, so todos are passes.
2400
+
2401
+ Of course, test #1 is $tests[0], etc...
2402
+
2403
+
2404
+ =item B<details>
2405
+
2406
+ my @tests = $Test->details;
2407
+
2408
+ Like C<summary()>, but with a lot more detail.
2409
+
2410
+ $tests[$test_num - 1] =
2411
+ { 'ok' => is the test considered a pass?
2412
+ actual_ok => did it literally say 'ok'?
2413
+ name => name of the test (if any)
2414
+ type => type of test (if any, see below).
2415
+ reason => reason for the above (if any)
2416
+ };
2417
+
2418
+ 'ok' is true if Test::Harness will consider the test to be a pass.
2419
+
2420
+ 'actual_ok' is a reflection of whether or not the test literally
2421
+ printed 'ok' or 'not ok'. This is for examining the result of 'todo'
2422
+ tests.
2423
+
2424
+ 'name' is the name of the test.
2425
+
2426
+ 'type' indicates if it was a special test. Normal tests have a type
2427
+ of ''. Type can be one of the following:
2428
+
2429
+ skip see skip()
2430
+ todo see todo()
2431
+ todo_skip see todo_skip()
2432
+ unknown see below
2433
+
2434
+ Sometimes the Test::Builder test counter is incremented without it
2435
+ printing any test output, for example, when C<current_test()> is changed.
2436
+ In these cases, Test::Builder doesn't know the result of the test, so
2437
+ its type is 'unknown'. These details for these tests are filled in.
2438
+ They are considered ok, but the name and actual_ok is left C<undef>.
2439
+
2440
+ For example "not ok 23 - hole count # TODO insufficient donuts" would
2441
+ result in this structure:
2442
+
2443
+ $tests[22] = # 23 - 1, since arrays start from 0.
2444
+ { ok => 1, # logically, the test passed since its todo
2445
+ actual_ok => 0, # in absolute terms, it failed
2446
+ name => 'hole count',
2447
+ type => 'todo',
2448
+ reason => 'insufficient donuts'
2449
+ };
2450
+
2451
+
2452
+ =item B<todo>
2453
+
2454
+ my $todo_reason = $Test->todo;
2455
+ my $todo_reason = $Test->todo($pack);
2456
+
2457
+ If the current tests are considered "TODO" it will return the reason,
2458
+ if any. This reason can come from a C<$TODO> variable or the last call
2459
+ to C<todo_start()>.
2460
+
2461
+ Since a TODO test does not need a reason, this function can return an
2462
+ empty string even when inside a TODO block. Use C<< $Test->in_todo >>
2463
+ to determine if you are currently inside a TODO block.
2464
+
2465
+ C<todo()> is about finding the right package to look for C<$TODO> in. It's
2466
+ pretty good at guessing the right package to look at. It first looks for
2467
+ the caller based on C<$Level + 1>, since C<todo()> is usually called inside
2468
+ a test function. As a last resort it will use C<exported_to()>.
2469
+
2470
+ Sometimes there is some confusion about where C<todo()> should be looking
2471
+ for the C<$TODO> variable. If you want to be sure, tell it explicitly
2472
+ what $pack to use.
2473
+
2474
+ =item B<find_TODO>
2475
+
2476
+ my $todo_reason = $Test->find_TODO();
2477
+ my $todo_reason = $Test->find_TODO($pack);
2478
+
2479
+ Like C<todo()> but only returns the value of C<$TODO> ignoring
2480
+ C<todo_start()>.
2481
+
2482
+ Can also be used to set C<$TODO> to a new value while returning the
2483
+ old value:
2484
+
2485
+ my $old_reason = $Test->find_TODO($pack, 1, $new_reason);
2486
+
2487
+ =item B<in_todo>
2488
+
2489
+ my $in_todo = $Test->in_todo;
2490
+
2491
+ Returns true if the test is currently inside a TODO block.
2492
+
2493
+ =item B<todo_start>
2494
+
2495
+ $Test->todo_start();
2496
+ $Test->todo_start($message);
2497
+
2498
+ This method allows you declare all subsequent tests as TODO tests, up until
2499
+ the C<todo_end> method has been called.
2500
+
2501
+ The C<TODO:> and C<$TODO> syntax is generally pretty good about figuring out
2502
+ whether or not we're in a TODO test. However, often we find that this is not
2503
+ possible to determine (such as when we want to use C<$TODO> but
2504
+ the tests are being executed in other packages which can't be inferred
2505
+ beforehand).
2506
+
2507
+ Note that you can use this to nest "todo" tests
2508
+
2509
+ $Test->todo_start('working on this');
2510
+ # lots of code
2511
+ $Test->todo_start('working on that');
2512
+ # more code
2513
+ $Test->todo_end;
2514
+ $Test->todo_end;
2515
+
2516
+ This is generally not recommended, but large testing systems often have weird
2517
+ internal needs.
2518
+
2519
+ We've tried to make this also work with the TODO: syntax, but it's not
2520
+ guaranteed and its use is also discouraged:
2521
+
2522
+ TODO: {
2523
+ local $TODO = 'We have work to do!';
2524
+ $Test->todo_start('working on this');
2525
+ # lots of code
2526
+ $Test->todo_start('working on that');
2527
+ # more code
2528
+ $Test->todo_end;
2529
+ $Test->todo_end;
2530
+ }
2531
+
2532
+ Pick one style or another of "TODO" to be on the safe side.
2533
+
2534
+
2535
+ =item C<todo_end>
2536
+
2537
+ $Test->todo_end;
2538
+
2539
+ Stops running tests as "TODO" tests. This method is fatal if called without a
2540
+ preceding C<todo_start> method call.
2541
+
2542
+ =item B<caller>
2543
+
2544
+ my $package = $Test->caller;
2545
+ my($pack, $file, $line) = $Test->caller;
2546
+ my($pack, $file, $line) = $Test->caller($height);
2547
+
2548
+ Like the normal C<caller()>, except it reports according to your C<level()>.
2549
+
2550
+ C<$height> will be added to the C<level()>.
2551
+
2552
+ If C<caller()> winds up off the top of the stack it report the highest context.
2553
+
2554
+ =back
2555
+
2556
+ =head1 EXIT CODES
2557
+
2558
+ If all your tests passed, Test::Builder will exit with zero (which is
2559
+ normal). If anything failed it will exit with how many failed. If
2560
+ you run less (or more) tests than you planned, the missing (or extras)
2561
+ will be considered failures. If no tests were ever run Test::Builder
2562
+ will throw a warning and exit with 255. If the test died, even after
2563
+ having successfully completed all its tests, it will still be
2564
+ considered a failure and will exit with 255.
2565
+
2566
+ So the exit codes are...
2567
+
2568
+ 0 all tests successful
2569
+ 255 test died or all passed but wrong # of tests run
2570
+ any other number how many failed (including missing or extras)
2571
+
2572
+ If you fail more than 254 tests, it will be reported as 254.
2573
+
2574
+ =head1 THREADS
2575
+
2576
+ In perl 5.8.1 and later, Test::Builder is thread-safe. The test number is
2577
+ shared by all threads. This means if one thread sets the test number using
2578
+ C<current_test()> they will all be effected.
2579
+
2580
+ While versions earlier than 5.8.1 had threads they contain too many
2581
+ bugs to support.
2582
+
2583
+ Test::Builder is only thread-aware if threads.pm is loaded I<before>
2584
+ Test::Builder.
2585
+
2586
+ You can directly disable thread support with one of the following:
2587
+
2588
+ $ENV{T2_NO_IPC} = 1
2589
+
2590
+ or
2591
+
2592
+ no Test2::IPC;
2593
+
2594
+ or
2595
+
2596
+ Test2::API::test2_ipc_disable()
2597
+
2598
+ =head1 MEMORY
2599
+
2600
+ An informative hash, accessible via C<details()>, is stored for each
2601
+ test you perform. So memory usage will scale linearly with each test
2602
+ run. Although this is not a problem for most test suites, it can
2603
+ become an issue if you do large (hundred thousands to million)
2604
+ combinatorics tests in the same run.
2605
+
2606
+ In such cases, you are advised to either split the test file into smaller
2607
+ ones, or use a reverse approach, doing "normal" (code) compares and
2608
+ triggering C<fail()> should anything go unexpected.
2609
+
2610
+ Future versions of Test::Builder will have a way to turn history off.
2611
+
2612
+
2613
+ =head1 EXAMPLES
2614
+
2615
+ CPAN can provide the best examples. L<Test::Simple>, L<Test::More>,
2616
+ L<Test::Exception> and L<Test::Differences> all use Test::Builder.
2617
+
2618
+ =head1 SEE ALSO
2619
+
2620
+ =head2 INTERNALS
2621
+
2622
+ L<Test2>, L<Test2::API>
2623
+
2624
+ =head2 LEGACY
2625
+
2626
+ L<Test::Simple>, L<Test::More>
2627
+
2628
+ =head2 EXTERNAL
2629
+
2630
+ L<Test::Harness>
2631
+
2632
+ =head1 AUTHORS
2633
+
2634
+ Original code by chromatic, maintained by Michael G Schwern
2635
+ E<lt>schwern@pobox.comE<gt>
2636
+
2637
+ =head1 MAINTAINERS
2638
+
2639
+ =over 4
2640
+
2641
+ =item Chad Granum E<lt>exodist@cpan.orgE<gt>
2642
+
2643
+ =back
2644
+
2645
+ =head1 COPYRIGHT
2646
+
2647
+ Copyright 2002-2008 by chromatic E<lt>chromatic@wgz.orgE<gt> and
2648
+ Michael G Schwern E<lt>schwern@pobox.comE<gt>.
2649
+
2650
+ This program is free software; you can redistribute it and/or
2651
+ modify it under the same terms as Perl itself.
2652
+
2653
+ See F<http://www.perl.com/perl/misc/Artistic.html>