| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Test\Unit; |
| 4 |
|
| 5 |
use MediaWiki\Extension\Notifications\DiscussionParser; |
| 6 |
use MediaWikiUnitTestCase; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class DiffParserTest extends MediaWikiUnitTestCase { |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
public function testGetChangeSet( $message, array $expect, $leftText, $rightText ) { |
| 18 |
$changeSet = DiscussionParser::getMachineReadableDiff( $leftText, $rightText ); |
| 19 |
unset( $changeSet['_info'] ); |
| 20 |
$this->assertEquals( $expect, $changeSet, $message ); |
| 21 |
} |
| 22 |
|
| 23 |
public static function provider_getChangeSet() { |
| 24 |
return [ |
| 25 |
|
| 26 |
[ |
| 27 |
'Duplicate content must generate no changes', |
| 28 |
|
| 29 |
[], |
| 30 |
|
| 31 |
"a\nb\nc", |
| 32 |
|
| 33 |
"a\nb\nc", |
| 34 |
], |
| 35 |
|
| 36 |
[ |
| 37 |
'Removing blank lines must generate no changes', |
| 38 |
|
| 39 |
[], |
| 40 |
|
| 41 |
"a\n\nb\n\nc", |
| 42 |
|
| 43 |
"a\nb\nc\n", |
| 44 |
], |
| 45 |
|
| 46 |
[ |
| 47 |
'Must generate a single add change with only lines added', |
| 48 |
|
| 49 |
[ self::mockAction( 'add', "foo\nbar", 1 ) ], |
| 50 |
|
| 51 |
"something", |
| 52 |
|
| 53 |
"foo\nbar\nsomething", |
| 54 |
], |
| 55 |
|
| 56 |
[ |
| 57 |
'Must generate a single subtract change with only lines subtracted', |
| 58 |
|
| 59 |
[ self::mockAction( 'subtract', "Zomg\nHiHiHi", 2 ) ], |
| 60 |
|
| 61 |
"dummy\nZomg\nHiHiHi", |
| 62 |
|
| 63 |
"dummy", |
| 64 |
], |
| 65 |
|
| 66 |
[ |
| 67 |
'Adding content separated by no change must generate multiple changes', |
| 68 |
|
| 69 |
[ |
| 70 |
self::mockAction( 'add', 'b1', 3 ), |
| 71 |
self::mockAction( 'add', "d1\nd2", 5, 6 ), |
| 72 |
], |
| 73 |
|
| 74 |
"a\nb\nc\nd\ne\nf", |
| 75 |
|
| 76 |
"a\nb\nb1\nc\nd\nd1\nd2\ne\nf", |
| 77 |
], |
| 78 |
|
| 79 |
[ |
| 80 |
'Additon with empty line should be one action', |
| 81 |
|
| 82 |
[ self::mockAction( 'add', "User\n\nSignature", 1 ) ], |
| 83 |
|
| 84 |
"", |
| 85 |
|
| 86 |
"User\n\nSignature", |
| 87 |
], |
| 88 |
|
| 89 |
[ |
| 90 |
'Extra blank lines on the edges must be trimmed', |
| 91 |
|
| 92 |
[ self::mockAction( 'add', "Zomg\nHiHiHi", 1 ) ], |
| 93 |
|
| 94 |
"", |
| 95 |
|
| 96 |
"\nZomg\nHiHiHi\n", |
| 97 |
], |
| 98 |
|
| 99 |
[ |
| 100 |
'Extra blank lines inside the content must not be trimmed', |
| 101 |
|
| 102 |
[ self::mockAction( 'add', "\nZomg\nHiHiHi\n", 2 ) ], |
| 103 |
|
| 104 |
"foo\nbar", |
| 105 |
|
| 106 |
"foo\n\nZomg\nHiHiHi\n\nbar", |
| 107 |
], |
| 108 |
|
| 109 |
[ |
| 110 |
'A blank line replaced with content must be an add', |
| 111 |
|
| 112 |
[ self::mockAction( 'add', 'cowbell', 1 ) ], |
| 113 |
|
| 114 |
"", |
| 115 |
|
| 116 |
"cowbell", |
| 117 |
], |
| 118 |
|
| 119 |
[ |
| 120 |
'A blanked out line must be a subtraction', |
| 121 |
|
| 122 |
[ self::mockAction( 'subtract', 'cowbell', 1 ) ], |
| 123 |
|
| 124 |
"cowbell", |
| 125 |
|
| 126 |
"", |
| 127 |
], |
| 128 |
|
| 129 |
[ |
| 130 |
'A line with its content replaced must be a change', |
| 131 |
|
| 132 |
[ self::mockChange( 'Its all about the journey', 'dummy', 1 ) ], |
| 133 |
|
| 134 |
"Its all about the journey", |
| 135 |
|
| 136 |
"dummy", |
| 137 |
], |
| 138 |
|
| 139 |
[ |
| 140 |
'Changing lines and adding more must result in two changes', |
| 141 |
|
| 142 |
[ |
| 143 |
self::mockChange( 'Must be in a hurry to finish this thing', 'Must be in a hurry', 1 ), |
| 144 |
self::mockAction( 'add', 'Finish this thing', 2 ), |
| 145 |
], |
| 146 |
|
| 147 |
"Must be in a hurry to finish this thing", |
| 148 |
|
| 149 |
"Must be in a hurry\nFinish this thing", |
| 150 |
], |
| 151 |
|
| 152 |
[ |
| 153 |
'Changing multiple lines and adding more must result in two changes', |
| 154 |
|
| 155 |
[ |
| 156 |
self::mockChange( "Must not be\nin much of a hurry", "Must be\nin a hurry", 2 ), |
| 157 |
self::mockAction( 'subtract', "to finish\nthis thing", 4 ), |
| 158 |
], |
| 159 |
|
| 160 |
"abc\nMust not be\nin much of a hurry\nto finish\nthis thing", |
| 161 |
|
| 162 |
"abc\nMust be\nin a hurry", |
| 163 |
], |
| 164 |
|
| 165 |
[ |
| 166 |
'Must generate multiple add, change, and subtract actions', |
| 167 |
|
| 168 |
[ |
| 169 |
self::mockChange( "abc\nSome", "Other\nThings", 1 ), |
| 170 |
self::mockAction( 'subtract', "Stuff", 3 ), |
| 171 |
self::mockChange( "And\nThen", "There\nWas", 6, 5 ), |
| 172 |
self::mockAction( 'add', 'Fencing', 8, 7 ), |
| 173 |
], |
| 174 |
|
| 175 |
"abc\nSome\nStuff\ndef\nghi\nAnd\nThen\njkl\nmno", |
| 176 |
|
| 177 |
"\nOther\nThings\ndef\nghi\nThere\nWas\nFencing\njkl\nmno", |
| 178 |
], |
| 179 |
]; |
| 180 |
} |
| 181 |
|
| 182 |
protected static function mockAction( $action, $content, $left, $right = null ) { |
| 183 |
return [ |
| 184 |
'action' => $action, |
| 185 |
'content' => $content, |
| 186 |
'left-pos' => $left, |
| 187 |
'right-pos' => $right ?? $left, |
| 188 |
]; |
| 189 |
} |
| 190 |
|
| 191 |
public static function mockChange( $oldContent, $newContent, $left, $right = null ) { |
| 192 |
return [ |
| 193 |
'action' => 'change', |
| 194 |
'old_content' => $oldContent, |
| 195 |
'new_content' => $newContent, |
| 196 |
'left-pos' => $left, |
| 197 |
'right-pos' => $right ?? $left, |
| 198 |
]; |
| 199 |
} |
| 200 |
} |