@@ -363,6 +363,103 @@ public void componentizedEquals() {
363363 }
364364 }
365365
366+ @ Test
367+ public void startsWith () {
368+ DotName componentizedCom = DotName .createComponentized (null , "com" );
369+ assertTrue (componentizedCom .startsWith (componentizedCom ));
370+
371+ DotName componentizedComExample = DotName .createComponentized (componentizedCom , "example" );
372+ assertTrue (componentizedComExample .startsWith (componentizedCom ));
373+ assertTrue (componentizedComExample .startsWith (componentizedComExample ));
374+
375+ DotName componentizedComExampleFooBar = DotName .createComponentized (componentizedComExample , "FooBar" );
376+ assertTrue (componentizedComExampleFooBar .startsWith (componentizedCom ));
377+ assertTrue (componentizedComExampleFooBar .startsWith (componentizedComExample ));
378+ assertTrue (componentizedComExampleFooBar .startsWith (componentizedComExampleFooBar ));
379+
380+ DotName componentizedComEx = DotName .createComponentized (componentizedCom , "ex" );
381+ assertFalse (componentizedComExampleFooBar .startsWith (componentizedComEx ));
382+
383+ DotName componentizedComExampleFoo = DotName .createComponentized (componentizedComExample , "Foo" );
384+ assertFalse (componentizedComExampleFooBar .startsWith (componentizedComExampleFoo ));
385+
386+ DotName simpleCom = DotName .createSimple ("com" );
387+ assertTrue (simpleCom .startsWith (simpleCom ));
388+
389+ assertTrue (simpleCom .startsWith (componentizedCom ));
390+ assertTrue (componentizedCom .startsWith (simpleCom ));
391+
392+ DotName simpleComExample = DotName .createSimple ("com.example" );
393+ assertTrue (simpleComExample .startsWith (simpleCom ));
394+ assertTrue (simpleComExample .startsWith (simpleComExample ));
395+
396+ assertTrue (simpleComExample .startsWith (componentizedCom ));
397+ assertTrue (simpleComExample .startsWith (componentizedComExample ));
398+ assertTrue (componentizedComExample .startsWith (simpleCom ));
399+ assertTrue (componentizedComExample .startsWith (simpleComExample ));
400+
401+ DotName simpleComExampleFooBar = DotName .createSimple ("com.example.FooBar" );
402+ assertTrue (simpleComExampleFooBar .startsWith (simpleCom ));
403+ assertTrue (simpleComExampleFooBar .startsWith (simpleComExample ));
404+ assertTrue (simpleComExampleFooBar .startsWith (simpleComExampleFooBar ));
405+
406+ assertTrue (simpleComExampleFooBar .startsWith (componentizedCom ));
407+ assertTrue (simpleComExampleFooBar .startsWith (componentizedComExample ));
408+ assertTrue (simpleComExampleFooBar .startsWith (componentizedComExampleFooBar ));
409+ assertTrue (componentizedComExampleFooBar .startsWith (simpleCom ));
410+ assertTrue (componentizedComExampleFooBar .startsWith (simpleComExample ));
411+ assertTrue (componentizedComExampleFooBar .startsWith (simpleComExampleFooBar ));
412+
413+ DotName simpleComEx = DotName .createSimple ("com.ex" );
414+ assertFalse (simpleComExampleFooBar .startsWith (simpleComEx ));
415+ assertFalse (simpleComExampleFooBar .startsWith (componentizedComEx ));
416+ assertFalse (componentizedComExampleFooBar .startsWith (simpleComEx ));
417+
418+ DotName simpleComExampleFoo = DotName .createSimple ("com.example.Foo" );
419+ assertFalse (simpleComExampleFooBar .startsWith (simpleComExampleFoo ));
420+ assertFalse (simpleComExampleFooBar .startsWith (componentizedComExampleFoo ));
421+ assertFalse (componentizedComExampleFooBar .startsWith (simpleComExampleFoo ));
422+ }
423+
424+ @ Test
425+ public void startsWith_random () {
426+ for (int i = 0 ; i < 2_000_000 ; i ++) {
427+ DotName name1 = createRandomDotName ();
428+ DotName name2 = createRandomDotName ();
429+
430+ // `createRandomComponentised()` only flags the last component as inner
431+ if (name1 .isInner () ^ name2 .isInner ()) {
432+ continue ;
433+ }
434+
435+ assertEquals (naiveStartsWith (name1 , name2 ), name1 .startsWith (name2 ));
436+
437+ if (name1 .isComponentized ()) {
438+ DotName name1Prefix = name1 .prefix ();
439+ if (name1Prefix != null ) {
440+ assertTrue (name1 .startsWith (name1Prefix ));
441+ }
442+ } else {
443+ if (name1 .local ().indexOf ('.' ) >= 0 ) {
444+ DotName name1Prefix = DotName .createSimple (name1 .local ().substring (0 , name1 .local ().lastIndexOf ('.' )));
445+ assertTrue (name1 .startsWith (name1Prefix ));
446+ }
447+ }
448+ }
449+ }
450+
451+ private boolean naiveStartsWith (DotName this_ , DotName prefix ) {
452+ String thisStr = this_ .toString ();
453+ String prefixStr = prefix .toString ();
454+ if (thisStr .length () < prefixStr .length ()) {
455+ return false ;
456+ } else if (thisStr .length () == prefixStr .length ()) {
457+ return thisStr .equals (prefixStr );
458+ } else {
459+ return thisStr .startsWith (prefixStr ) && thisStr .charAt (prefixStr .length ()) == '.' ;
460+ }
461+ }
462+
366463 @ Test
367464 public void scalaAnonfunCurriedCase1 () {
368465 DotName a ;
0 commit comments