-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
95 lines (79 loc) · 2.67 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!doctype html>
<?php
function get_subjects($subjects) {
return array_map('trim', array_filter($subjects, function ($line) {
return trim($line) !== '' && $line{0} === '.';
}));
}
$fullPassingMatches = file(__DIR__ . '/test.pass');
$fullFailingMatches = file(__DIR__ . '/test.fail');
$passingMatches = get_subjects($fullPassingMatches);
$failingMatches = get_subjects($fullFailingMatches);
$fullSubjects = implode("\n", array_merge($fullPassingMatches, $fullFailingMatches));
$subjects = implode("\n", array_merge($passingMatches, $failingMatches));
$regexs = file(__DIR__ . '/BEM.regex');
$regexs = array_filter($regexs, function ($pattern) {
return trim($pattern) !== '' && $pattern{0} === '^';
});
$result = '';
array_walk($regexs, function ($regex) use (&$result, $passingMatches, $subjects) {
$regex = trim($regex);
$pattern = vsprintf("/%s/m", [$regex]);
$encodedPattern = htmlentities($regex);
$match = preg_match_all($pattern, $subjects, $matches);
// @TODO: Mark test as error if $match === false
$shouldMatch = array_diff($passingMatches, $matches[0]);
$shouldNotMatch = array_diff($matches[0], $passingMatches);
if ($shouldMatch === [] && $shouldNotMatch === []) {
$result .= '<p><span class="pass">Success.</span> <code>' . $encodedPattern .'</code></p>';
} else {
$result .= '<p><span class="fail">Failure.</span> <code>' . $encodedPattern .'</code></p>';
$glue = '</code></li><li><code>';
if ($shouldMatch !== []) {
$result .= '<p>The following items SHOULD have been matched but were not:</p>'
. '<ul><li><code>'.join($glue, $shouldMatch).'</code></li></ul>'
;
}
if ($shouldNotMatch !== []) {
$result .= '<p>The following items SHOULD NOT have been matched but were:</p>'
. '<ul><li><code>'.join($glue, $shouldNotMatch).'</code></li></ul>'
;
}
}
});
?>
<html lang="en">
<head>
<title></title>
<style>
summary {
border: 1px solid #CCC;
cursor: pointer;
display: inline;
padding: 0.2em;
}
.fail {
background-color: red;
color: white;
display: inline;
padding: 0.2em;
}
.pass {
background-color: limegreen;
color: white;
display: inline;
padding: 0.2em;
}
</style>
</head>
<body>
<h1>Regex for BEM</h1>
<h2>The regular expressions</h2>
<?= $result; ?>
<h2>The text matched against</h2>
<details>
<summary>Click to open</summary>
<pre><code><?= $fullSubjects; ?></code></pre>
</details>
</body>
</html>