Debian Codegraph @

vendor/wikimedia/parsoid/src/ParserTests/TestMode.php

raw
1 <?php
2 declare( strict_types = 1 );
3
4 namespace Wikimedia\Parsoid\ParserTests;
5
6 /**
7 * Represents a parser test mode, that is, a certain way of executing a
8 * parser tests and evaluating the result.
9 *
10 * As a trivial example, a parser test will typically have a
11 * "wikitext" section and an "html" section. Two possible modes for
12 * evaluating the test are "wt2html" (where you programatically
13 * convert the "wikitext" section to HTML and verify that the result
14 * matches the "html" section, after normalization) and "html2wt"
15 * (where you programmatically convert the "html" section back to
16 * wikitext and verify that the result matches the "wikitext" section,
17 * after normalization).
18 */
19 class TestMode {
20
21 /** Valid test modes, as keys for efficient query/set intersection. */
22 public const TEST_MODES = [
23 'legacy' => true, // wt2html with legacy parser
24 'wt2html' => true,
25 'wt2wt' => true,
26 'html2html' => true,
27 'html2wt' => true,
28 'selser' => true,
29 ];
30
31 /**
32 * Selected test mode, typically one of the values from self::TEST_MODES.
33 * @var string
34 */
35 public $mode;
36
37 /**
38 * The "selser" test mode can operate with an explicit changetree
39 * provided in this property.
40 * @var ?array
41 */
42 public $changetree;
43
44 /**
45 * Create a new test mode
46 * @param string $mode The test mode. An external caller should use
47 * one of `self::TEST_MODES`, although ParserTestRunner uses a few
48 * additional values internally.
49 * @param ?array $changetree The specific changes to apply in selser test
50 * mode.
51 */
52 public function __construct(
53 string $mode,
54 ?array $changetree = null
55 ) {
56 $this->mode = $mode;
57 $this->changetree = $changetree;
58 }
59
60 /**
61 * Helper function: returns true if this test mode is 'legacy'; that is,
62 * is this test to run with the legacy parser.
63 * @return bool
64 */
65 public function isLegacy() {
66 return $this->mode === 'legacy';
67 }
68
69 /**
70 * Helper function: returns true if we are running this test to cache some info
71 * for use in later tests.
72 * @return bool
73 */
74 public function isCachingMode() {
75 return $this->mode === 'cache';
76 }
77
78 /**
79 * Returns a string representation of this test mode, which can also
80 * be used as an array key or for human-friendly output.
81 * @return string
82 */
83 public function __toString(): string {
84 $s = $this->mode;
85 if ( $this->changetree !== null ) {
86 $s .= ' ' . json_encode(
87 $this->changetree,
88 JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
89 );
90 }
91 return $s;
92 }
93
94 /**
95 * Helper function: filter a given set of options against the
96 * TEST_MODES. Optionally ensure that all modes are returned if
97 * none are explicitly set.
98 *
99 * @param string[] $options The user-specified test modes
100 * @param bool $ifEmptySetAll If true, ensure that the result always
101 * includes at least one set test mode by setting all available test
102 * modes if the passed $options array does not contain any.
103 * @return string[] A filtered set of test modes
104 */
105 public static function requestedTestModes( array $options, bool $ifEmptySetAll = true ) {
106 if ( $ifEmptySetAll ) {
107 $allModes = true;
108 foreach ( self::TEST_MODES as $mode => $ignore ) {
109 if ( $options[$mode] ?? false ) {
110 $allModes = false;
111 }
112 }
113 if ( $allModes ) {
114 return array_keys( self::TEST_MODES );
115 }
116 }
117 return array_keys( array_intersect_assoc(
118 $options, self::TEST_MODES
119 ) );
120 }
121 }