Skip to content

Commit

Permalink
Merge pull request #126 from tedious/even_better_keywords
Browse files Browse the repository at this point in the history
Better Keyword Support and Testing
  • Loading branch information
tedivm authored Mar 9, 2023
2 parents 439459e + 363b391 commit dee8822
Show file tree
Hide file tree
Showing 10 changed files with 20,655 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/JShrink/Minifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,11 @@ protected function saveString()
*/
protected function saveRegex()
{
$this->echo($this->a . $this->b);
if ($this->a != " ") {
$this->echo($this->a);
}

$this->echo($this->b);

while (($this->a = $this->getChar()) !== false) {
if ($this->a === '/') {
Expand Down Expand Up @@ -656,10 +660,7 @@ protected function endsInKeyword() {
$testOutput = $this->output . $this->a;

foreach(static::$keywords as $keyword) {
if (str_ends_with($testOutput, $keyword)) {
return true;
}
if (str_ends_with($testOutput, $keyword . " ")) {
if (preg_match('/[^\w]'.$keyword.'[ ]?$/i', $testOutput) === 1) {
return true;
}
}
Expand Down
31 changes: 31 additions & 0 deletions tests/JShrink/Test/JShrinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ public function testRequests($testName, $input, $output)
$this->assertEquals($output, \JShrink\Minifier::minify($input), 'Running User Requested Test: ' . $testName);
}

/**
* @dataProvider librariesProvider
*/
public function testLibraries($testName, $input)
{
$this->expectNotToPerformAssertions();
\JShrink\Minifier::minify($input);
}

// /**
// * @group development
// * @dataProvider developmentProvider
Expand Down Expand Up @@ -111,6 +120,23 @@ public static function getTestFiles($group)
return $returnData;
}

public static function getTestLibraries()
{
$testDir = __DIR__ . '/../../Resources/libraries/';

$returnData = array();

$testFiles = scandir($testDir);
foreach ($testFiles as $testFile) {
if (substr($testFile, -3) !== '.js') {
continue;
}
$returnData["Libraries:" . $testFile] = [$testFile, file_get_contents($testDir . $testFile)];
}

return $returnData;
}

public static function uglifyProvider()
{
return self::getTestFiles('uglify');
Expand All @@ -130,4 +156,9 @@ public static function developmentProvider()
{
return self::getTestFiles('development');
}

public static function librariesProvider()
{
return self::getTestLibraries();
}
}
6 changes: 6 additions & 0 deletions tests/Resources/jshrink/input/regex_close.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function test (string) {
return (string || '').replace(
/([\\!"#$%&'()*+,./:;<=>?@\[\]^`{|}~])/g,
'\\$1'
)
}
1 change: 1 addition & 0 deletions tests/Resources/jshrink/input/regex_keyword_overhang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aboveMin = Math.round(aboveMin / options.step) * options.step
4 changes: 4 additions & 0 deletions tests/Resources/jshrink/output/regex_close.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function test(string){return(string||'').replace(/([\\!"#$%&'()*+,./:;<=>?@\[\]^`{|}~])/g,
'\\$1'
)
}
1 change: 1 addition & 0 deletions tests/Resources/jshrink/output/regex_keyword_overhang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aboveMin=Math.round(aboveMin / options.step)*options.step
4 changes: 2 additions & 2 deletions tests/Resources/jshrink/output/regex_keywords.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
return /'/
typeof /'/
return/'/
typeof/'/
2 changes: 1 addition & 1 deletion tests/Resources/jshrink/output/regex_spaces.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
function airplaneIsCarrierBased(model){return /^(FI-167|Swordfish|Fulmar|Firefly|F4F Wildcat|F6F-[35] Hellcat|Latécoère 298|A[567]M)$/.test(model)}
function airplaneIsCarrierBased(model){return/^(FI-167|Swordfish|Fulmar|Firefly|F4F Wildcat|F6F-[35] Hellcat|Latécoère 298|A[567]M)$/.test(model)}
console.log(airplaneIsCarrierBased('F6F-5 Hellcat'))
2 changes: 1 addition & 1 deletion tests/Resources/jshrink/output/regex_with_quote_real.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
function test(input){return /^(אחה"צ|אחרי הצהריים|בערב)$/.test(input)}
function test(input){return/^(אחה"צ|אחרי הצהריים|בערב)$/.test(input)}
Loading

0 comments on commit dee8822

Please sign in to comment.