forked from GlotPress/GlotPress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_format_json.php
169 lines (141 loc) · 5.21 KB
/
test_format_json.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
class GP_Test_Format_JSON extends GP_UnitTestCase {
/**
* @var GP_Translation_Set
*/
protected $translation_set;
/**
* @var GP_Locale
*/
protected $locale;
/**
* @var string
*/
protected $format = 'json';
public function setUp(): void {
parent::setUp();
$this->translation_set = $this->factory->translation_set->create_with_project_and_locale( array(), array( 'name' => 'foo_project' ) );
$this->locale = new GP_Locale( array(
'slug' => $this->translation_set->locale,
'nplurals' => 2,
'plural_expression' => 'n != 1',
) );
}
public function test_format_name() {
$this->assertSame( 'JSON (.json)', GP::$formats[ $this->format ]->name );
}
public function test_format_extension() {
$this->assertSame( 'json', GP::$formats[ $this->format ]->extension );
}
public function test_print_exported_file_can_be_decoded() {
$entries = array(
new Translation_Entry( array( 'singular' => 'foo', 'translations' => array( 'foox' ) ) ),
);
$json = GP::$formats[ $this->format ]->print_exported_file( $this->translation_set->project, $this->locale, $this->translation_set, $entries );
$this->assertNotNull( json_decode( $json, true ) );
}
public function test_print_exported_file_has_valid_format() {
$entries = array(
new Translation_Entry( array( 'singular' => 'foo', 'translations' => array( 'bar' ) ) ),
new Translation_Entry( array( 'singular' => 'bar', 'translations' => array( 'baz' ) ) ),
);
$json = GP::$formats[ $this->format ]->print_exported_file( $this->translation_set->project, $this->locale, $this->translation_set, $entries );
$actual = json_decode( $json, true );
$this->assertEquals( array(
'foo' => array( 'bar' ),
'bar' => array( 'baz' ),
), $actual );
}
public function test_read_originals_from_file_non_existent_file() {
$this->assertFalse( GP::$formats[ $this->format ]->read_originals_from_file( GP_DIR_TESTDATA . '/foo.json' ) );
}
public function test_read_originals_from_file_invalid_file() {
$this->assertFalse( GP::$formats[ $this->format ]->read_originals_from_file( GP_DIR_TESTDATA . '/invalid.json' ) );
}
public function test_read_originals_from_file() {
$expected = $this->data_example_originals();
/* @var Translations $actual */
$actual = GP::$formats[ $this->format ]->read_originals_from_file( GP_DIR_TESTDATA . '/originals.json' );
$this->assertSame( 5, count( $actual->entries ) );
$this->assertEquals( $expected, $actual );
}
public function test_read_translations_from_file_non_existent_file() {
$this->assertFalse( GP::$formats[ $this->format ]->read_translations_from_file( GP_DIR_TESTDATA . '/foo.json' ) );
}
public function test_read_translations_from_file_invalid_file() {
$this->assertFalse( GP::$formats[ $this->format ]->read_translations_from_file( GP_DIR_TESTDATA . '/invalid.json' ) );
}
public function test_read_translations_from_file() {
$expected = $this->data_example_translations();
/* @var Translations $actual */
$actual = GP::$formats[ $this->format ]->read_translations_from_file( GP_DIR_TESTDATA . '/translation.json' );
$this->assertCount( 5, $actual->entries );
$this->assertEquals( $expected, $actual );
}
/**
* Returns the expected data for the parsed example-untranslated.json file.
*/
public function data_example_originals() {
$translations = new Translations();
$translations->add_entry( new Translation_Entry( array(
'singular' => 'This file is too big. Files must be less than %d KB in size.',
) ) );
$translations->add_entry( new Translation_Entry( array(
'singular' => '%d Theme Update',
) ) );
$translations->add_entry( new Translation_Entry( array(
'singular' => 'Medium',
'context' => 'password strength',
) ) );
$translations->add_entry( new Translation_Entry( array(
'singular' => 'Category',
'context' => 'taxonomy singular name',
) ) );
$translations->add_entry( new Translation_Entry( array(
'singular' => 'Pages',
'context' => 'post type general name',
) ) );
return $translations;
}
/**
* Returns the expected data for the parsed example-untranslated.json file.
*/
public function data_example_translations() {
$translations = new Translations();
$translations->add_entry( new Translation_Entry( array(
'singular' => 'This file is too big. Files must be less than %d KB in size.',
'translations' => array(
'Diese Datei ist zu gross. Dateien müssen kleiner als %d KB sein.',
),
) ) );
$translations->add_entry( new Translation_Entry( array(
'singular' => '%d Theme Update',
'translations' => array(
'%d Theme-Aktualisierung',
'%d Theme-Aktualisierungen',
)
) ) );
$translations->add_entry( new Translation_Entry( array(
'singular' => 'Medium',
'context' => 'password strength',
'translations' => array(
'Medium',
)
) ) );
$translations->add_entry( new Translation_Entry( array(
'singular' => 'Category',
'context' => 'taxonomy singular name',
'translations' => array(
'Kategorie',
)
) ) );
$translations->add_entry( new Translation_Entry( array(
'singular' => 'Pages',
'context' => 'post type general name',
'translations' => array(
'Seiten',
)
) ) );
return $translations;
}
}