20
20
21
21
import com .google .common .annotations .GwtCompatible ;
22
22
import com .google .common .annotations .GwtIncompatible ;
23
+ import com .google .common .annotations .J2ktIncompatible ;
23
24
import com .google .common .collect .ImmutableMap ;
25
+ import java .nio .CharBuffer ;
26
+ import java .util .ArrayList ;
24
27
import java .util .Arrays ;
28
+ import java .util .HashMap ;
25
29
import java .util .Map ;
26
30
import junit .framework .TestCase ;
27
31
@@ -316,6 +320,13 @@ public void testToStringOmitNullValues_oneField() {
316
320
assertEquals ("TestClass{}" , toTest );
317
321
}
318
322
323
+ @ GwtIncompatible // Class names are obfuscated in GWT
324
+ public void testToStringOmitEmptyValues_oneField () {
325
+ String toTest =
326
+ MoreObjects .toStringHelper (new TestClass ()).omitEmptyValues ().add ("field1" , "" ).toString ();
327
+ assertEquals ("TestClass{}" , toTest );
328
+ }
329
+
319
330
@ GwtIncompatible // Class names are obfuscated in GWT
320
331
public void testToStringOmitNullValues_manyFieldsFirstNull () {
321
332
String toTest =
@@ -328,6 +339,18 @@ public void testToStringOmitNullValues_manyFieldsFirstNull() {
328
339
assertEquals ("TestClass{field2=Googley, field3=World}" , toTest );
329
340
}
330
341
342
+ @ GwtIncompatible // Class names are obfuscated in GWT
343
+ public void testToStringOmitEmptyValues_manyFieldsFirstEmpty () {
344
+ String toTest =
345
+ MoreObjects .toStringHelper (new TestClass ())
346
+ .omitEmptyValues ()
347
+ .add ("field1" , "" )
348
+ .add ("field2" , "Googley" )
349
+ .add ("field3" , "World" )
350
+ .toString ();
351
+ assertEquals ("TestClass{field2=Googley, field3=World}" , toTest );
352
+ }
353
+
331
354
@ GwtIncompatible // Class names are obfuscated in GWT
332
355
public void testToStringOmitNullValues_manyFieldsOmitAfterNull () {
333
356
String toTest =
@@ -340,6 +363,18 @@ public void testToStringOmitNullValues_manyFieldsOmitAfterNull() {
340
363
assertEquals ("TestClass{field2=Googley, field3=World}" , toTest );
341
364
}
342
365
366
+ @ GwtIncompatible // Class names are obfuscated in GWT
367
+ public void testToStringOmitEmptyValues_manyFieldsOmitAfterEmpty () {
368
+ String toTest =
369
+ MoreObjects .toStringHelper (new TestClass ())
370
+ .add ("field1" , "" )
371
+ .add ("field2" , "Googley" )
372
+ .add ("field3" , "World" )
373
+ .omitEmptyValues ()
374
+ .toString ();
375
+ assertEquals ("TestClass{field2=Googley, field3=World}" , toTest );
376
+ }
377
+
343
378
@ GwtIncompatible // Class names are obfuscated in GWT
344
379
public void testToStringOmitNullValues_manyFieldsLastNull () {
345
380
String toTest =
@@ -352,6 +387,25 @@ public void testToStringOmitNullValues_manyFieldsLastNull() {
352
387
assertEquals ("TestClass{field1=Hello, field2=Googley}" , toTest );
353
388
}
354
389
390
+ @ GwtIncompatible // Class names are obfuscated in GWT
391
+ public void testToStringOmitEmptyValues_manyFieldsLastEmpty () {
392
+ String toTest =
393
+ MoreObjects .toStringHelper (new TestClass ())
394
+ .omitEmptyValues ()
395
+ .add ("field1" , "Hello" )
396
+ .add ("field2" , "Googley" )
397
+ .add ("field3" , "" )
398
+ .toString ();
399
+ assertEquals ("TestClass{field1=Hello, field2=Googley}" , toTest );
400
+ }
401
+
402
+ @ GwtIncompatible // Class names are obfuscated in GWT
403
+ public void testToStringOmitNullValues_oneValue () {
404
+ String toTest =
405
+ MoreObjects .toStringHelper (new TestClass ()).omitEmptyValues ().addValue ("" ).toString ();
406
+ assertEquals ("TestClass{}" , toTest );
407
+ }
408
+
355
409
@ GwtIncompatible // Class names are obfuscated in GWT
356
410
public void testToStringOmitEmptyValues_oneValue () {
357
411
String toTest =
@@ -371,6 +425,18 @@ public void testToStringOmitNullValues_manyValuesFirstNull() {
371
425
assertEquals ("TestClass{Googley, World}" , toTest );
372
426
}
373
427
428
+ @ GwtIncompatible // Class names are obfuscated in GWT
429
+ public void testToStringOmitEmptyValues_manyValuesFirstEmpty () {
430
+ String toTest =
431
+ MoreObjects .toStringHelper (new TestClass ())
432
+ .omitEmptyValues ()
433
+ .addValue ("" )
434
+ .addValue ("Googley" )
435
+ .addValue ("World" )
436
+ .toString ();
437
+ assertEquals ("TestClass{Googley, World}" , toTest );
438
+ }
439
+
374
440
@ GwtIncompatible // Class names are obfuscated in GWT
375
441
public void testToStringOmitNullValues_manyValuesLastNull () {
376
442
String toTest =
@@ -383,6 +449,18 @@ public void testToStringOmitNullValues_manyValuesLastNull() {
383
449
assertEquals ("TestClass{Hello, Googley}" , toTest );
384
450
}
385
451
452
+ @ GwtIncompatible // Class names are obfuscated in GWT
453
+ public void testToStringOmitEmptyValues_manyValuesLastEmpty () {
454
+ String toTest =
455
+ MoreObjects .toStringHelper (new TestClass ())
456
+ .omitEmptyValues ()
457
+ .addValue ("Hello" )
458
+ .addValue ("Googley" )
459
+ .addValue ("" )
460
+ .toString ();
461
+ assertEquals ("TestClass{Hello, Googley}" , toTest );
462
+ }
463
+
386
464
@ GwtIncompatible // Class names are obfuscated in GWT
387
465
public void testToStringOmitNullValues_differentOrder () {
388
466
String expected = "TestClass{field1=Hello, field2=Googley, field3=World}" ;
@@ -404,6 +482,27 @@ public void testToStringOmitNullValues_differentOrder() {
404
482
assertEquals (expected , toTest2 );
405
483
}
406
484
485
+ @ GwtIncompatible // Class names are obfuscated in GWT
486
+ public void testToStringOmitEmptyValues_differentOrder () {
487
+ String expected = "TestClass{field1=Hello, field2=Googley, field3=World}" ;
488
+ String toTest1 =
489
+ MoreObjects .toStringHelper (new TestClass ())
490
+ .omitEmptyValues ()
491
+ .add ("field1" , "Hello" )
492
+ .add ("field2" , "Googley" )
493
+ .add ("field3" , "World" )
494
+ .toString ();
495
+ String toTest2 =
496
+ MoreObjects .toStringHelper (new TestClass ())
497
+ .add ("field1" , "Hello" )
498
+ .add ("field2" , "Googley" )
499
+ .omitEmptyValues ()
500
+ .add ("field3" , "World" )
501
+ .toString ();
502
+ assertEquals (expected , toTest1 );
503
+ assertEquals (expected , toTest2 );
504
+ }
505
+
407
506
@ GwtIncompatible // Class names are obfuscated in GWT
408
507
public void testToStringOmitNullValues_canBeCalledManyTimes () {
409
508
String toTest =
@@ -419,6 +518,64 @@ public void testToStringOmitNullValues_canBeCalledManyTimes() {
419
518
assertEquals ("TestClass{field1=Hello, field2=Googley, field3=World}" , toTest );
420
519
}
421
520
521
+ @ GwtIncompatible // Class names are obfuscated in GWT
522
+ public void testToStringOmitEmptyValues_canBeCalledManyTimes () {
523
+ String toTest =
524
+ MoreObjects .toStringHelper (new TestClass ())
525
+ .omitEmptyValues ()
526
+ .omitEmptyValues ()
527
+ .add ("field1" , "Hello" )
528
+ .omitEmptyValues ()
529
+ .add ("field2" , "Googley" )
530
+ .omitEmptyValues ()
531
+ .add ("field3" , "World" )
532
+ .toString ();
533
+ assertEquals ("TestClass{field1=Hello, field2=Googley, field3=World}" , toTest );
534
+ }
535
+
536
+ @ GwtIncompatible // Class names are obfuscated in GWT
537
+ public void testToStringOmitEmptyValues_allEmptyTypes () {
538
+ String toTest =
539
+ MoreObjects .toStringHelper (new TestClass ())
540
+ .omitEmptyValues ()
541
+ // CharSequences
542
+ .add ("field1" , "" )
543
+ .add ("field2" , new StringBuilder ())
544
+ // nio CharBuffer (implements CharSequence) is tested separately below
545
+ // Collections and Maps
546
+ .add ("field11" , Arrays .asList ("Hello" ))
547
+ .add ("field12" , new ArrayList <>())
548
+ .add ("field13" , new HashMap <>())
549
+ // Optionals
550
+ .add ("field26" , Optional .of ("World" ))
551
+ .add ("field27" , Optional .absent ())
552
+ // Arrays
553
+ .add ("field31" , new Object [] {"!!!" })
554
+ .add ("field32" , new boolean [0 ])
555
+ .add ("field33" , new byte [0 ])
556
+ .add ("field34" , new char [0 ])
557
+ .add ("field35" , new short [0 ])
558
+ .add ("field36" , new int [0 ])
559
+ .add ("field37" , new long [0 ])
560
+ .add ("field38" , new float [0 ])
561
+ .add ("field39" , new double [0 ])
562
+ .add ("field40" , new Object [0 ])
563
+ .toString ();
564
+ assertEquals ("TestClass{field11=[Hello], field26=Optional.of(World), field31=[!!!]}" , toTest );
565
+ }
566
+
567
+ @ J2ktIncompatible // J2kt CharBuffer does not implement CharSequence so not recognized as empty
568
+ @ GwtIncompatible // CharBuffer not available
569
+ public void testToStringOmitEmptyValues_charBuffer () {
570
+ String toTest =
571
+ MoreObjects .toStringHelper (new TestClass ())
572
+ .omitEmptyValues ()
573
+ .add ("field1" , "Hello" )
574
+ .add ("field2" , CharBuffer .allocate (0 ))
575
+ .toString ();
576
+ assertEquals ("TestClass{field1=Hello}" , toTest );
577
+ }
578
+
422
579
public void testToStringHelperWithArrays () {
423
580
String [] strings = {"hello" , "world" };
424
581
int [] ints = {2 , 42 };
0 commit comments