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