Skip to content

Commit bc57256

Browse files
committed
added a new test
Fixes #622
1 parent df3c6c4 commit bc57256

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ jobs:
144144
fpm test jf_test_50 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
145145
fpm test jf_test_51 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
146146
fpm test jf_test_52 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
147+
fpm test jf_test_53 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
147148
148149
- name: Compile_with_cmake
149150
# CMake build with unit tests, no documentation, with coverage analysis

fpm.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,9 @@ main = "jf_test_51.F90"
271271
[[test]]
272272
name = "jf_test_52"
273273
source-dir = "src/tests"
274-
main = "jf_test_52.F90"
274+
main = "jf_test_52.F90"
275+
276+
[[test]]
277+
name = "jf_test_53"
278+
source-dir = "src/tests"
279+
main = "jf_test_53.F90"

src/tests/jf_test_53.f90

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
!*****************************************************************************************
2+
!>
3+
! Module for the 53rd unit test.
4+
! An example of iterating through a JSON object using get_child and get_next.
5+
6+
module jf_test_53_mod
7+
8+
use json_module, wp => json_RK, IK => json_IK, LK => json_LK, CK => json_CK
9+
use, intrinsic :: iso_fortran_env , only: error_unit, output_unit
10+
11+
implicit none
12+
13+
private
14+
public :: test_53
15+
16+
contains
17+
18+
subroutine test_53(error_cnt)
19+
20+
!! 53rd unit test.
21+
22+
integer,intent(out) :: error_cnt
23+
24+
type(json_core) :: json
25+
type(json_value),pointer :: p_root, p_child
26+
character(kind=CK,len=:),allocatable :: key
27+
integer(IK) :: count, i
28+
29+
character(kind=CK,len=*),parameter :: str = CK_'{'//&
30+
' "a": 1,'//&
31+
' "b": 2,'//&
32+
' "c": 3'//&
33+
'}'
34+
35+
character(kind=CK,len=1),dimension(3),parameter :: expected_keys = [CK_'a', CK_'b', CK_'c']
36+
37+
error_cnt = 0
38+
39+
write(error_unit,'(A)') ''
40+
write(error_unit,'(A)') '================================='
41+
write(error_unit,'(A)') ' EXAMPLE 53'
42+
write(error_unit,'(A)') '================================='
43+
write(error_unit,'(A)') ''
44+
45+
call json%initialize()
46+
call json%deserialize(p_root, str)
47+
if (json%failed()) then
48+
error_cnt = error_cnt + 1
49+
write(error_unit,'(A)') 'Error reading JSON string: '//trim(str)
50+
else
51+
call json%info(p_root,n_children=count) ! there should be 3 children
52+
if (count /= 3) then
53+
error_cnt = error_cnt + 1
54+
write(error_unit,'(A,I0)') 'Error: expected 3 children, got ', count
55+
else
56+
write(output_unit,'(A,I0)') 'Success: expected 3 children, got ', count
57+
call json%get_child(p_root, 1, p_child) ! get the first one
58+
do i = 1, count
59+
call json%info(p_child, name=key) ! get the key name
60+
write(output_unit,'(A,I0,A,A)') 'Key ', i, ': ', trim(key)
61+
if (key /= expected_keys(i)) then
62+
error_cnt = error_cnt + 1
63+
write(error_unit,'(A,I0,A,A)') 'Error: expected key ', i, ' to be ', &
64+
trim(expected_keys(i)), ' but got ', trim(key)
65+
end if
66+
! get the next one (more efficient than calling get_child again)
67+
if (i<count) call json%get_next(p_child, p_child)
68+
end do
69+
end if
70+
71+
end if
72+
73+
call json%destroy(p_root)
74+
75+
end subroutine test_53
76+
77+
end module jf_test_53_mod
78+
!*****************************************************************************************
79+
80+
!*****************************************************************************************
81+
#ifndef INTEGRATED_TESTS
82+
program jf_test_53
83+
84+
use jf_test_53_mod , only: test_53
85+
86+
implicit none
87+
integer :: n_errors
88+
89+
call test_53(n_errors)
90+
if (n_errors /= 0) stop 1
91+
92+
end program jf_test_53
93+
#endif
94+
!*****************************************************************************************
95+

visual_studio/jsonfortrantest/jsonfortrantest.vfproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
<File RelativePath="..\..\src\tests\jf_test_50.F90"/>
105105
<File RelativePath="..\..\src\tests\jf_test_51.F90"/>
106106
<File RelativePath="..\..\src\tests\jf_test_52.F90"/>
107+
<File RelativePath="..\..\src\tests\jf_test_53.F90"/>
107108
<File RelativePath=".\jsonfortrantest.f90"/>
108109
</Filter>
109110
</Files>

0 commit comments

Comments
 (0)