subroutine test_add_to_state(this)
class(ESMF_TestMethod), intent(inout) :: this
type(StateRegistry), target :: r
type(StateRegistry), target :: r_A ! child registry
type(VirtualConnectionPt) :: cp_e1, cp_e2
type(VirtualConnectionPt) :: cp_i1, cp_i2
integer :: status
type(MultiState) :: user_states, outer_states
type(ESMF_Info) :: info
r = StateRegistry('P')
r_a = StateRegistry('child_A')
call r%add_subregistry(r_a)
cp_e1 = VirtualConnectionPt(state_intent='export', short_name='e1')
cp_e2 = VirtualConnectionPt(state_intent='export', short_name='e2')
cp_i1 = VirtualConnectionPt(state_intent='import', short_name='i1')
cp_i2 = VirtualConnectionPt(state_intent='import', short_name='i2')
call r_a%add_primary_spec(cp_e1, MockItemSpec('e1'))
call r_a%add_primary_spec(cp_i1, MockItemSpec('i1'))
call r%add_primary_spec(cp_e2, MockItemSpec('e2'))
call r%add_primary_spec(cp_i1, MockItemSpec('i1')) ! intentional duplicate with r_A
call r%add_primary_spec(cp_i2, MockItemSpec('i2'))
call r%propagate_exports(_RC)
call r%propagate_unsatisfied_imports(_RC)
user_states = MultiState()
call r%add_to_states(user_states, 'user', _RC)
! expect e2 and i2 only
call ESMF_InfoGetFromHost(user_states%exportstate, info, _RC)
@assert_that(ESMF_InfoIsPresent(info, 'e2'), is(true()))
@assert_that(ESMF_InfoIsPresent(info, 'a/e1'), is(false()))
call ESMF_InfoGetFromHost(user_states%importstate, info, _RC)
@assert_that(ESMF_InfoIsPresent(info, 'i2'), is(true()))
@assert_that(ESMF_InfoIsPresent(info, 'i1'), is(true()))
@assert_that(ESMF_InfoIsPresent(info, 'i1(1)'), is(false()))
outer_states = MultiState()
call r%add_to_states(outer_states, 'outer', _RC)
! expect e2 and i2 only
call ESMF_InfoGetFromHost(outer_states%exportstate, info, _RC)
@assert_that(ESMF_InfoIsPresent(info, 'e2'), is(true()))
@assert_that(ESMF_InfoIsPresent(info, 'child_A/e1'), is(true()))
call ESMF_InfoGetFromHost(outer_states%importstate, info, _RC)
@assert_that(ESMF_InfoIsPresent(info, 'i2'), is(true()))
@assert_that(ESMF_InfoIsPresent(info, 'i1'), is(true()))
@assert_that(ESMF_InfoIsPresent(info, 'i1(1)'), is(true()))
_UNUSED_DUMMY(this)
end subroutine test_add_to_state