Repository navigation
Expand file tree
/
Copy pathJsFunctionsScannerTest.php
More file actions
67 lines (53 loc) · 1.93 KB
/
Copy pathJsFunctionsScannerTest.php
File metadata and controls
67 lines (53 loc) · 1.93 KB
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
<?php
namespace WP_CLI\I18n\Tests;
use Gettext\Translations;
use WP_CLI\I18n\JsCodeExtractor;
use WP_CLI\Tests\TestCase;
class JsFunctionsScannerTest extends TestCase {
/**
* Helper to extract translations from JavaScript code.
*
* @param string $code JavaScript code.
* @param array<mixed> $options Extraction options.
* @return Translations
*/
private function extract( $code, array $options = [] ) {
$translations = new Translations();
JsCodeExtractor::fromString( $code, $translations, $options + [ 'file' => 'test.js' ] );
return $translations;
}
public function test_does_not_extract_anything_without_functions() {
$translations = $this->extract(
"__( 'Hello', 'foo-plugin' );",
[ 'functions' => [] ]
);
$this->assertCount( 0, $translations );
}
public function test_ignores_comments_if_comment_extraction_is_disabled() {
$translations = $this->extract(
"// translators: A comment.\n__( 'Hello', 'foo-plugin' );",
[ 'extractComments' => false ]
);
$translation = $translations->find( null, 'Hello' );
$this->assertNotFalse( $translation );
$this->assertSame( [], $translation->getExtractedComments() );
}
public function test_extracts_all_comments_with_empty_prefix() {
$translations = $this->extract(
"// Any comment.\n__( 'Hello', 'foo-plugin' );",
[ 'extractComments' => '' ]
);
$translation = $translations->find( null, 'Hello' );
$this->assertNotFalse( $translation );
$this->assertSame( [ 'Any comment.' ], $translation->getExtractedComments() );
}
public function test_extracts_translator_comments_with_prefix() {
$translations = $this->extract(
"// translators: A comment.\n__( 'Hello', 'foo-plugin' );",
[ 'extractComments' => [ 'translators', 'Translators' ] ]
);
$translation = $translations->find( null, 'Hello' );
$this->assertNotFalse( $translation );
$this->assertSame( [ 'translators: A comment.' ], $translation->getExtractedComments() );
}
}