package Ġc afe . ad riel . broker Ċ Ċ import Ġc afe . ad riel . broker . util . Test Coroutine Scope Rule Ċ import Ġio . mock k . co Every Ċ import Ġio . mock k . co Verify Ċ import Ġio . mock k . mock k Ċ import Ġio . mock k . slot Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġstri kt . api . expect That Ċ import Ġstri kt . assertions . is A Ċ import Ġstri kt . assertions . isEqualTo Ċ import Ġstri kt . assertions . isNull Ċ Ċ class ĠBroker Test Ġ{ ĊĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġval Ġtest Scope Rule Ġ= ĠTest Coroutine Scope Rule () ĊĊĠĠĠ Ġval Ġbroker Ġ= ĠBroker ( test Scope Rule . dispatcher ) ĊĊĠĠĠ Ġval Ġlistener Success Ġ= Ġmock k < suspend Ġ( Test Event ) Ġ-> ĠUnit >( relax ed Ġ= Ġtrue ) ĊĠĠĠ Ġval Ġlistener Throw Error Ġ= Ġmock k < suspend Ġ( Test Event ) Ġ-> ĠUnit >( relax ed Ġ= Ġtrue ) ĊĠĠĠ Ġval Ġlistener Catch Error Ġ= Ġmock k < suspend Ġ( Broker Exception Event ) Ġ-> ĠUnit >( relax ed Ġ= Ġtrue ) ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun Ġsetup () Ġ{ ĊĠĠĠĠĠĠĠ Ġco Every Ġ{ Ġlistener Throw Error ( any ()) Ġ} Ġthrows ĠIllegalStateException (" Test Ġerror ") ĊĊĠĠĠĠĠĠĠ Ġbroker . unsubscribe ( this ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġpublish Ġevent Ġwith Ġsubscriber Ġthen Ġdeliver Ġit `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġevent Ġ= ĠTest Event (" It Ġworks !") ĊĠĠĠĠĠĠĠ Ġbroker . subscribe ( this , Ġtest Scope Rule , Ġtest Scope Rule . dispatcher , Ġon Event Ġ= Ġlistener Success ) ĊĠĠĠĠĠĠĠ Ġbroker . publish ( event ) ĊĊĠĠĠĠĠĠĠ Ġco Verify Ġ{ Ġlistener Success ( event ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġpublish Ġevent Ġwithout Ġsubscriber Ġthen Ġdo Ġnot Ġdeliver Ġit `() Ġ{ ĊĠĠĠĠĠĠĠ Ġbroker . subscribe ( this , Ġtest Scope Rule , Ġtest Scope Rule . dispatcher , Ġon Event Ġ= Ġlistener Success ) ĊĠĠĠĠĠĠĠ Ġbroker . publish ( Un subscribed Event ) ĊĊĠĠĠĠĠĠĠ Ġco Verify ( inverse Ġ= Ġtrue ) Ġ{ Ġlistener Success ( any ()) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġsubscriber Ġthrows Ġexception Ġthen Ġpublish Ġexception Ġevent `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġexception Event Slot Ġ= Ġslot < Broker Exception Event >() ĊĠĠĠĠĠĠĠ Ġval Ġevent Ġ= ĠTest Event (" Ops !") ĊĠĠĠĠĠĠĠ Ġbroker . subscribe ( this , Ġtest Scope Rule , Ġtest Scope Rule . dispatcher , Ġon Event Ġ= Ġlistener Throw Error ) ĊĠĠĠĠĠĠĠ Ġbroker . subscribe ( this , Ġtest Scope Rule , Ġtest Scope Rule . dispatcher , Ġon Event Ġ= Ġlistener Catch Error ) ĊĠĠĠĠĠĠĠ Ġbroker . publish ( event ) ĊĊĠĠĠĠĠĠĠ Ġco Verify Ġ{ Ġlistener Throw Error ( event ) Ġ} ĊĠĠĠĠĠĠĠ Ġco Verify Ġ{ Ġlistener Catch Error ( capture ( exception Event Slot )) Ġ} ĊĊĠĠĠĠĠĠĠ Ġexpect That ( exception Event Slot . captured ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġget ( Broker Exception Event :: subscriber ) Ġis EqualTo Ġthis @ Broker Test ĊĠĠĠĠĠĠĠĠĠĠĠ Ġget ( Broker Exception Event :: event ) Ġis EqualTo Ġevent ĊĠĠĠĠĠĠĠĠĠĠĠ Ġget ( Broker Exception Event :: error ). is A < IllegalStateException >() ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġpublish Ġretained Ġevent Ġthen Ġevent Ġshould Ġbe Ġretained `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġevent Ġ= ĠTest Event (" It Ġworks !") ĊĠĠĠĠĠĠĠ Ġbroker . subscribe ( this , Ġtest Scope Rule , Ġtest Scope Rule . dispatcher , Ġon Event Ġ= Ġlistener Success ) ĊĠĠĠĠĠĠĠ Ġbroker . publish ( event , Ġretain Ġ= Ġtrue ) ĊĊĠĠĠĠĠĠĠ Ġexpect That ( broker . get Ret ained < Test Event >()) Ġis EqualTo Ġevent ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġpublish Ġretained Ġevent Ġthen Ġsubscribers Ġshould Ġreceive Ġit `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġevent Ġ= ĠTest Event (" It Ġworks !") ĊĠĠĠĠĠĠĠ Ġbroker . subscribe ( this , Ġtest Scope Rule , Ġtest Scope Rule . dispatcher , Ġemit Ret ained Ġ= Ġfalse , Ġon Event Ġ= Ġlistener Success ) ĊĠĠĠĠĠĠĠ Ġbroker . publish ( event , Ġretain Ġ= Ġtrue ) ĊĠĠĠĠĠĠĠ Ġbroker . subscribe ( this , Ġtest Scope Rule , Ġtest Scope Rule . dispatcher , Ġemit Ret ained Ġ= Ġtrue , Ġon Event Ġ= Ġlistener Success ) ĊĊĠĠĠĠĠĠĠ Ġco Verify ( exact ly Ġ= Ġ 2 ) Ġ{ Ġlistener Success ( event ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġremove Ġretained Ġevent Ġthen Ġretained Ġevent Ġshould Ġbe Ġnull `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġevent Ġ= ĠTest Event (" It Ġworks !") ĊĠĠĠĠĠĠĠ Ġbroker . publish ( event , Ġretain Ġ= Ġtrue ) ĊĠĠĠĠĠĠĠ Ġbroker . remove Ret ained < Test Event >() ĊĊĠĠĠĠĠĠĠ Ġexpect That ( broker . get Ret ained < Test Event > ()). isNull () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġremove Ġnot Ġretained Ġevent Ġthen Ġnothing Ġshould Ġhappen `() Ġ{ ĊĠĠĠĠĠĠĠ Ġbroker . remove Ret ained < Un subscribed Event >() ĊĊĠĠĠĠĠĠĠ Ġexpect That ( broker . get Ret ained < Un subscribed Event > ()). isNull () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġdata Ġclass ĠTest Event ( val Ġmessage : ĠString ) ĊĠĠĠ Ġobject ĠUn subscribed Event Ċ } Ċ