| 1 |
program test_state_io |
| 2 |
use fgof_state, only : & |
| 3 |
FGOF_STATE_ERR_VERSION, & |
| 4 |
FGOF_STATE_OK, & |
| 5 |
clear_state_options, & |
| 6 |
load_state_text, & |
| 7 |
remove_state_document, & |
| 8 |
save_state_text |
| 9 |
use fgof_state_types, only : state_document, state_options, state_text_result |
| 10 |
implicit none |
| 11 |
|
| 12 |
type(state_options) :: options |
| 13 |
type(state_document) :: document |
| 14 |
type(state_text_result) :: load_result |
| 15 |
character(len=:), allocatable :: base_dir |
| 16 |
|
| 17 |
base_dir = unique_root("state-io") |
| 18 |
|
| 19 |
options = clear_state_options() |
| 20 |
options%root_dir = base_dir |
| 21 |
options%namespace = "demo-app" |
| 22 |
options%scope = "workspace" |
| 23 |
|
| 24 |
document = save_state_text("settings.json", "hello world", options, 3) |
| 25 |
if (document%error_code /= FGOF_STATE_OK) error stop "save_state_text should succeed for valid options" |
| 26 |
if (.not. document%present) error stop "saved documents should be marked present" |
| 27 |
if (document%version /= 3) error stop "save_state_text should preserve the requested version" |
| 28 |
|
| 29 |
load_result = load_state_text("settings.json", options) |
| 30 |
if (load_result%error_code /= FGOF_STATE_OK) error stop "load_state_text should succeed for saved documents" |
| 31 |
if (.not. load_result%found) error stop "load_state_text should mark saved documents as found" |
| 32 |
if (load_result%version_checked) error stop "load_state_text without expected_version should not report a version check" |
| 33 |
if (load_result%text /= "hello world") error stop "load_state_text should return the saved text" |
| 34 |
if (load_result%document%version /= 3) error stop "load_state_text should return the stored version" |
| 35 |
|
| 36 |
load_result = load_state_text("settings.json", options, expected_version=3) |
| 37 |
if (load_result%error_code /= FGOF_STATE_OK) error stop "matching expected_version should succeed" |
| 38 |
if (.not. load_result%version_checked) error stop "matching expected_version should report that a version check happened" |
| 39 |
if (.not. load_result%version_matched) error stop "matching expected_version should report a version match" |
| 40 |
|
| 41 |
load_result = load_state_text("settings.json", options, expected_version=2) |
| 42 |
if (load_result%error_code /= FGOF_STATE_ERR_VERSION) error stop "mismatched expected_version should report version error" |
| 43 |
if (.not. load_result%version_checked) error stop "mismatched expected_version should still report that a version check happened" |
| 44 |
if (load_result%version_matched) error stop "mismatched expected_version should report version mismatch" |
| 45 |
if (load_result%document%version /= 3) error stop "version mismatch should still surface the stored version" |
| 46 |
if (load_result%text /= "") error stop "version mismatch should not surface payload text yet" |
| 47 |
|
| 48 |
document = remove_state_document("settings.json", options) |
| 49 |
if (document%error_code /= FGOF_STATE_OK) error stop "remove_state_document should succeed for saved documents" |
| 50 |
if (document%present) error stop "removed documents should be marked absent" |
| 51 |
|
| 52 |
load_result = load_state_text("settings.json", options) |
| 53 |
if (load_result%found) error stop "removed documents should no longer load as found" |
| 54 |
|
| 55 |
contains |
| 56 |
|
| 57 |
function unique_root(label) result(path) |
| 58 |
character(len=*), intent(in) :: label |
| 59 |
character(len=:), allocatable :: path |
| 60 |
integer :: count |
| 61 |
character(len=32) :: count_text |
| 62 |
|
| 63 |
call system_clock(count) |
| 64 |
write(count_text, "(i0)") count |
| 65 |
path = "build/state-tests/" // label // "-" // trim(count_text) |
| 66 |
end function unique_root |
| 67 |
|
| 68 |
end program test_state_io |
| 69 |
|