/* Ċ Ġ* ĠFX GL Ġ- ĠJava FX ĠGame ĠLibrary . ĠThe ĠMIT ĠLicense Ġ( MIT ). Ċ Ġ* ĠCopyright Ġ( c ) ĠAl mas B Ġ( al mas lvl @ gmail . com ). Ċ Ġ* ĠSee ĠLICENSE Ġfor Ġdetails . Ċ Ġ*/ Ċ @ file : Suppress (" JAVA _ MODULE _ DO ES _ NOT _ DEPEND _ ON _ MODULE ") Ċ package Ġcom . al mas b . fx gl . core . fsm Ċ Ċ import Ġorg . hamcrest . Core Matchers .` is ` Ċ import Ġorg . hamcrest . Matcher Assert . assertThat Ċ import Ġorg . hamcrest . Matchers . contains Ċ import Ġorg . junit . jupiter . api . Assertions .* Ċ import Ġorg . junit . jupiter . api . BeforeEach Ċ import Ġorg . junit . jupiter . api . Test Ċ Ċ /** Ċ Ġ* Ċ Ġ* Ġ@ author ĠAl mas ĠB aim ag amb et ov Ġ( al mas lvl @ gmail . com ) Ċ Ġ*/ Ċ class ĠState Machine Test Ġ{ ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar ĠinitialState : ĠTest State ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġstate 1 : ĠTest State ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġstate 2 : ĠTest State ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġsub State 1 : ĠTest State ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġsub State 2 : ĠTest State ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġmachine : ĠState Machine < Test State > ĊĊĠĠĠ Ġ@ BeforeEach ĊĠĠĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠĠĠĠĠ ĠinitialState Ġ= ĠTest State ( false , Ġfalse ) ĊĠĠĠĠĠĠĠ Ġmachine Ġ= ĠState Machine ( initialState ) ĊĠĠĠĠĠĠĠ Ġstate 1 Ġ= ĠTest State ( false , Ġfalse ) ĊĠĠĠĠĠĠĠ Ġstate 2 Ġ= ĠTest State ( false , Ġfalse ) ĊĠĠĠĠĠĠĠ Ġsub State 1 Ġ= ĠTest State ( true , Ġfalse ) ĊĠĠĠĠĠĠĠ Ġsub State 2 Ġ= ĠTest State ( true , Ġfalse ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` Fails Ġwhen Ġinitial Ġstate Ġis Ġa Ġsub state `() Ġ{ ĊĠĠĠĠĠĠĠ ĠassertThrows ( IllegalArgumentException :: class . java ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠState Machine ( Test State ( true , Ġfalse )) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` Change Ġparent Ġstate Ġto Ġnew Ġstate `() Ġ{ ĊĠĠĠĠĠĠĠ ĠassertFalse ( machine . isIn Hierarchy ( state 1 )) ĊĊĠĠĠĠĠĠĠ Ġmachine . change State ( state 1 ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( machine . parent State , Ġ` is `( machine . currentState )) ĊĠĠĠĠĠĠĠ ĠassertThat ( machine . currentState , Ġ` is `( state 1 )) ĊĠĠĠĠĠĠĠ ĠassertTrue ( machine . isIn Hierarchy ( state 1 )) ĊĊĠĠĠĠĠĠĠ Ġmachine . change State ( state 2 ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( machine . parent State , Ġ` is `( machine . currentState )) ĊĠĠĠĠĠĠĠ ĠassertThat ( machine . currentState , Ġ` is `( state 2 )) ĊĠĠĠĠĠĠĠ ĠassertFalse ( machine . isIn Hierarchy ( state 1 )) ĊĠĠĠĠĠĠĠ ĠassertTrue ( machine . isIn Hierarchy ( state 2 )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` Change Ġstate Ġto Ġnew Ġsub state `() Ġ{ ĊĠĠĠĠĠĠĠ Ġmachine . change State ( sub State 1 ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( machine . parent State , Ġ` is `( initialState )) ĊĠĠĠĠĠĠĠ ĠassertThat ( machine . currentState , Ġ` is `( sub State 1 )) ĊĊĠĠĠĠĠĠĠ Ġmachine . change State ( sub State 2 ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( machine . parent State , Ġ` is `( initialState )) ĊĠĠĠĠĠĠĠ ĠassertThat ( machine . currentState , Ġ` is `( sub State 2 )) ĊĊĠĠĠĠĠĠĠ Ġ// Ġcheck Ġthat Ġstate 1 Ġis Ġalso Ġin Ġhierarchy ĊĠĠĠĠĠĠĠ ĠassertTrue ( machine . isIn Hierarchy ( sub State 1 )) ĊĠĠĠĠĠĠĠ ĠassertTrue ( machine . isIn Hierarchy ( sub State 2 )) ĊĊĠĠĠĠĠĠĠ Ġmachine . pop Sub State () ĊĊĠĠĠĠĠĠĠ ĠassertThat ( machine . parent State , Ġ` is `( initialState )) ĊĠĠĠĠĠĠĠ ĠassertThat ( machine . currentState , Ġ` is `( sub State 1 )) ĊĊĠĠĠĠĠĠĠ ĠassertTrue ( machine . isIn Hierarchy ( sub State 1 )) ĊĠĠĠĠĠĠĠ ĠassertFalse ( machine . isIn Hierarchy ( sub State 2 )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` Allow Ġsub state Ġconcurrency `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrun Result Ġ= Ġarray ListOf < Test State >() ĊĊĠĠĠĠĠĠĠ ĠassertThat ( machine . active States , Ġcontains ( initialState )) ĊĊĠĠĠĠĠĠĠ Ġmachine . change State ( sub State 1 ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( machine . active States , Ġcontains ( sub State 1 )) ĊĊĠĠĠĠĠĠĠ Ġmachine . run On Active States Ġ{ Ġrun Result Ġ+= Ġit Ġ} ĊĠĠĠĠĠĠĠ ĠassertThat ( run Result , Ġcontains ( sub State 1 )) ĊĊĠĠĠĠĠĠĠ Ġval Ġsub State 3 Ġ= ĠTest State ( true , Ġtrue ) ĊĊĠĠĠĠĠĠĠ Ġmachine . change State ( sub State 3 ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( machine . active States , Ġcontains ( sub State 1 , Ġsub State 3 )) ĊĠĠĠĠĠĠĠ Ġrun Result . clear () ĊĠĠĠĠĠĠĠ Ġmachine . run On Active States Ġ{ Ġrun Result Ġ+= Ġit Ġ} ĊĠĠĠĠĠĠĠ ĠassertThat ( run Result , Ġcontains ( sub State 1 , Ġsub State 3 )) ĊĊĠĠĠĠĠĠĠ Ġmachine . change State ( sub State 2 ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( machine . active States , Ġcontains ( sub State 2 )) ĊĊĠĠĠĠĠĠĠ Ġmachine . pop Sub State () ĊĊĠĠĠĠĠĠĠ ĠassertThat ( machine . active States , Ġcontains ( sub State 1 , Ġsub State 3 )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` Parent Ġstate Ġis Ġalso Ġactive Ġwhen Ġsub state Ġallows Ġconcurrency `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġsub State 3 Ġ= ĠTest State ( true , Ġtrue ) ĊĊĠĠĠĠĠĠĠ Ġmachine . change State ( sub State 3 ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( machine . active States , Ġcontains ( initialState , Ġsub State 3 )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` New Ġparent Ġstate Ġis Ġnot Ġset Ġif Ġsubst ates Ġare Ġpresent `() Ġ{ ĊĠĠĠĠĠĠĠ Ġmachine . change State ( sub State 1 ) ĊĊĠĠĠĠĠĠĠ Ġmachine . change State ( state 2 ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( machine . parent State , Ġ` is `( initialState )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` State Ġis Ġnot Ġpop ped Ġif Ġthere Ġare Ġnone `() Ġ{ ĊĠĠĠĠĠĠĠ ĠassertFalse ( machine . pop Sub State ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` Test Ġlifecycle Ġcallbacks `() Ġ{ ĊĠĠĠĠĠĠĠ Ġ// Ġparent Ġ-> Ġsub state ĊĠĠĠĠĠĠĠ Ġmachine . change State ( sub State 1 ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . create , Ġ` is `( 1 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . enter , Ġ` is `( 1 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . exit , Ġ` is `( 0 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . destroy , Ġ` is `( 0 )) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( initialState . exit , Ġ` is `( 1 )) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . enter ed From , Ġ` is `< State < *> >( initialState )) ĊĠĠĠĠĠĠĠ ĠassertThat ( initialState . exit ed To , Ġ` is `< State < *> >( sub State 1 )) ĊĊĊĠĠĠĠĠĠĠ Ġ// Ġsub state Ġ-> Ġsub state ĊĠĠĠĠĠĠĠ Ġmachine . change State ( sub State 2 ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . create , Ġ` is `( 1 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . enter , Ġ` is `( 1 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . exit , Ġ` is `( 1 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . destroy , Ġ` is `( 0 )) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 2 . create , Ġ` is `( 1 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 2 . enter , Ġ` is `( 1 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 2 . exit , Ġ` is `( 0 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 2 . destroy , Ġ` is `( 0 )) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . exit ed To , Ġ` is `< State < *> >( sub State 2 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 2 . enter ed From , Ġ` is `< State < *> >( sub State 1 )) ĊĊĠĠĠĠĠĠĠ Ġ// Ġsub state Ġ<- Ġsub state ĊĠĠĠĠĠĠĠ Ġmachine . pop Sub State () ĊĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . create , Ġ` is `( 1 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . enter , Ġ` is `( 2 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . exit , Ġ` is `( 1 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . destroy , Ġ` is `( 0 )) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 2 . create , Ġ` is `( 1 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 2 . enter , Ġ` is `( 1 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 2 . exit , Ġ` is `( 1 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 2 . destroy , Ġ` is `( 1 )) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 2 . exit ed To , Ġ` is `< State < *> >( sub State 1 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . enter ed From , Ġ` is `< State < *> >( sub State 2 )) ĊĊĊĠĠĠĠĠĠĠ Ġ// Ġparent Ġ<- Ġsub state ĊĠĠĠĠĠĠĠ Ġmachine . pop Sub State () ĊĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . create , Ġ` is `( 1 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . enter , Ġ` is `( 2 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . exit , Ġ` is `( 2 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . destroy , Ġ` is `( 1 )) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( sub State 1 . exit ed To , Ġ` is `< State < *> >( initialState )) ĊĠĠĠĠĠĠĠ ĠassertThat ( initialState . enter ed From , Ġ` is `< State < *> >( sub State 1 )) ĊĊĊĠĠĠĠĠĠĠ Ġ// Ġparent Ġ-> Ġparent ĊĠĠĠĠĠĠĠ Ġmachine . change State ( state 2 ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( initialState . create , Ġ` is `( 1 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( initialState . enter , Ġ` is `( 1 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( initialState . exit , Ġ` is `( 2 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( initialState . destroy , Ġ` is `( 1 )) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( initialState . exit ed To , Ġ` is `< State < *> >( state 2 )) ĊĠĠĠĠĠĠĠ ĠassertThat ( state 2 . enter ed From , Ġ` is `< State < *> >( initialState )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġclass ĠTest State ( override Ġval Ġis Sub State : ĠBoolean , Ġoverride Ġval Ġis Allow Concurrency : ĠBoolean ) Ġ: ĠState < Test State > Ġ{ ĊĠĠĠĠĠĠĠ Ġvar Ġcreate Ġ= Ġ 0 ĊĠĠĠĠĠĠĠ Ġvar Ġdestroy Ġ= Ġ 0 ĊĠĠĠĠĠĠĠ Ġvar Ġenter Ġ= Ġ 0 ĊĠĠĠĠĠĠĠ Ġvar Ġexit Ġ= Ġ 0 ĊĊĠĠĠĠĠĠĠ Ġlateinit Ġvar Ġentered From : ĠTest State ĊĠĠĠĠĠĠĠ Ġlateinit Ġvar Ġexited To : ĠTest State ĊĊĠĠĠĠĠĠĠ Ġoverride Ġfun ĠonCreate () Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcreate ++ ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġoverride Ġfun Ġon Destroy () Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġdestroy ++ ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġoverride Ġfun Ġon Entered From ( prev State : ĠTest State ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġentered From Ġ= Ġprev State ĊĠĠĠĠĠĠĠĠĠĠĠ Ġenter ++ ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġoverride Ġfun Ġon Exit ing To ( next State : ĠTest State ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġexited To Ġ= Ġnext State ĊĠĠĠĠĠĠĠĠĠĠĠ Ġexit ++ ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ }