@@ -115,6 +115,14 @@ Deno.test('V4A.apply - bare triple star marker is rejected', () => {
115115 assertThrows ( ( ) => V4A . apply ( 'a\nb' , '@@\n a\n-b\n+B\n***' ) )
116116} )
117117
118+ Deno . test ( 'V4A.apply - bare triple star mid-patch with trailing content throws' , ( ) => {
119+ assertThrows (
120+ ( ) => V4A . apply ( 'a\nb\nc' , '@@\n a\n-b\n+B\n***\n c' ) ,
121+ SyntaxError ,
122+ 'unexpected line'
123+ )
124+ } )
125+
118126Deno . test ( 'V4A.apply - blank context line matches' , ( ) => {
119127 const applyResult = V4A . apply ( 'a\n\nb' , '@@\n a\n \n-b\n+B' )
120128 assertEquals ( applyResult . text , 'a\n\nB' )
@@ -177,10 +185,18 @@ Deno.test('V4A.apply - create preserves trailing blank lines', () => {
177185 assertEquals ( V4A . apply ( '' , '+a\n+\n+' , 'create' ) . text , 'a\n\n' )
178186} )
179187
188+ Deno . test ( 'V4A.apply - create rejects bare anchor line' , ( ) => {
189+ assertThrows ( ( ) => V4A . apply ( '' , '@@\n+x' , 'create' ) , SyntaxError , "expected '+' prefix" )
190+ } )
191+
180192Deno . test ( 'V4A.apply - create rejects non plus prefix line' , ( ) => {
181193 assertThrows ( ( ) => V4A . apply ( '' , '+ok\n-bad' , 'create' ) )
182194} )
183195
196+ Deno . test ( 'V4A.apply - create rejects space-prefixed context line' , ( ) => {
197+ assertThrows ( ( ) => V4A . apply ( '' , '+ok\n bad' , 'create' ) , SyntaxError , "expected '+' prefix" )
198+ } )
199+
184200Deno . test ( 'V4A.apply - curly apostrophe anchor normalizes' , ( ) => {
185201 const applyResult = V4A . apply ( 'it\u2019s here' , "@@\n-it's here\n+it is here" )
186202 assertEquals ( applyResult . text , 'it is here' )
@@ -210,6 +226,12 @@ Deno.test('V4A.apply - delete every line via end of file marker', () => {
210226 assertEquals ( applyResult . text , '' )
211227} )
212228
229+ Deno . test ( 'V4A.apply - delete mode ignores malformed diff text' , ( ) => {
230+ const applyResult = V4A . apply ( 'a\nb' , 'GARBAGE\n???not a patch' , 'delete' )
231+ assertEquals ( applyResult . text , '' )
232+ assertEquals ( applyResult . diff . length , 2 )
233+ } )
234+
213235Deno . test ( 'V4A.apply - delete mode normalizes CRLF source' , ( ) => {
214236 const applyResult = V4A . apply ( 'a\r\nb\r\nc' , 'ignored' , 'delete' )
215237 assertEquals ( applyResult . diff . length , 3 )
@@ -385,6 +407,26 @@ Deno.test('V4A.apply - non breaking space anchor normalizes', () => {
385407 assertEquals ( applyResult . text , 'ab' )
386408} )
387409
410+ Deno . test ( 'V4A.apply - non-string source throws TypeError' , ( ) => {
411+ assertThrows (
412+ ( ) => V4A . apply ( 123 as unknown as string , '@@\n-x\n+y' ) ,
413+ TypeError
414+ )
415+ } )
416+
417+ Deno . test ( 'V4A.apply - nonexistent anchor is ignored when context still matches' , ( ) => {
418+ const applyResult = V4A . apply ( 'a\nb\nc' , '@@ NONEXISTENT\n a\n-b\n+B\n c' )
419+ assertEquals ( applyResult . text , 'a\nB\nc' )
420+ } )
421+
422+ Deno . test ( 'V4A.apply - nonexistent anchor with unmatched context throws' , ( ) => {
423+ assertThrows (
424+ ( ) => V4A . apply ( 'a\nb\nc' , '@@ NONEXISTENT\n zzz\n-b\n+B' ) ,
425+ SyntaxError ,
426+ 'unmatched context'
427+ )
428+ } )
429+
388430Deno . test ( 'V4A.apply - null diff is identity' , ( ) => {
389431 const applyResult = V4A . apply ( 'x' , null as unknown as string , 'update' )
390432 assertEquals ( applyResult . text , 'x' )
@@ -395,6 +437,13 @@ Deno.test('V4A.apply - null source coerces then add preserves empty line', () =>
395437 assertEquals ( applyResult . text , 'HEAD\n' )
396438} )
397439
440+ Deno . test ( 'V4A.apply - object source throws TypeError' , ( ) => {
441+ assertThrows (
442+ ( ) => V4A . apply ( { } as unknown as string , '@@\n-x\n+y' ) ,
443+ TypeError
444+ )
445+ } )
446+
398447Deno . test ( 'V4A.apply - prefix anchor fallback on longer line' , ( ) => {
399448 const applyResult = V4A . apply (
400449 'const foobar = 1' ,
@@ -462,6 +511,18 @@ Deno.test('V4A.apply - two change groups split chunks with correct line numbers'
462511 )
463512} )
464513
514+ Deno . test ( 'V4A.apply - undefined source and undefined diff yield empty single equal line' , ( ) => {
515+ const applyResult = V4A . apply (
516+ undefined as unknown as string ,
517+ undefined as unknown as string ,
518+ 'update'
519+ )
520+ assertEquals ( applyResult . text , '' )
521+ assertEquals ( applyResult . source , '' )
522+ assertEquals ( applyResult . diff . length , 1 )
523+ assertEquals ( applyResult . diff [ 0 ] ! . type , 'equal' )
524+ } )
525+
465526Deno . test ( 'V4A.apply - unequal delete add counts one delete two add' , ( ) => {
466527 const applyResult = V4A . apply ( 'x\nz' , '@@\n-x\n+M\n+N\n z' )
467528 assertEquals ( applyResult . text , 'M\nN\nz' )
0 commit comments