Skip to content

Commit 6fe76f7

Browse files
committed
added unit tests for recent new features.
1 parent 99479cf commit 6fe76f7

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

src/tests/jf_test_14.f90

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module jf_test_14_mod
1414
character(len=*),parameter :: dir = '../files/inputs/' !! working directory
1515
character(len=*),parameter :: filename1 = 'test1.json' !! the file to read
1616
integer :: icount = 0 !! a count of the number of "name" variables found
17+
character(len=:),allocatable :: new_name !! name to change to
1718

1819
contains
1920

@@ -29,6 +30,7 @@ subroutine test_14(error_cnt)
2930

3031
type(json_core) :: json
3132
type(json_value),pointer :: p
33+
type(json_file) :: f
3234

3335
write(error_unit,'(A)') ''
3436
write(error_unit,'(A)') '================================='
@@ -37,7 +39,9 @@ subroutine test_14(error_cnt)
3739
write(error_unit,'(A)') ''
3840

3941
error_cnt = 0
42+
4043
icount = 0 !number of name changes (should be 2)
44+
new_name = 'Fred' !change all names to this
4145

4246
call json%initialize() !initialize the module
4347

@@ -60,7 +64,7 @@ subroutine test_14(error_cnt)
6064

6165
if (error_cnt==0) then
6266
write(error_unit,'(A)') ''
63-
write(error_unit,'(A)') ' All names changed to Fred:'
67+
write(error_unit,'(A)') ' All names changed to '//new_name//':'
6468
write(error_unit,'(A)') ''
6569
call json%print(p,output_unit)
6670
write(error_unit,'(A)') ''
@@ -72,6 +76,40 @@ subroutine test_14(error_cnt)
7276
error_cnt = error_cnt + 1
7377
end if
7478

79+
! now, test traversal from a json_file:
80+
new_name = 'Bob'
81+
icount = 0
82+
call f%initialize()
83+
call f%load_file(dir//filename1) !read the file
84+
if (f%failed()) then
85+
call f%print_error_message(error_unit)
86+
error_cnt = error_cnt + 1
87+
end if
88+
call f%traverse(rename) !traverse all nodes in the structure
89+
if (f%failed()) then
90+
call f%print_error_message(error_unit)
91+
error_cnt = error_cnt + 1
92+
end if
93+
94+
if (icount/=2) then
95+
write(error_unit,'(A)') 'Error: should be 2 "name" variables in this file: '//filename1
96+
error_cnt = error_cnt + 1
97+
end if
98+
99+
if (error_cnt==0) then
100+
write(error_unit,'(A)') ''
101+
write(error_unit,'(A)') ' All names changed to '//new_name//':'
102+
write(error_unit,'(A)') ''
103+
call f%print_file(output_unit)
104+
write(error_unit,'(A)') ''
105+
end if
106+
107+
call f%destroy() ! clean up
108+
if (f%failed()) then
109+
call f%print_error_message(error_unit)
110+
error_cnt = error_cnt + 1
111+
end if
112+
75113
end subroutine test_14
76114

77115
subroutine rename(json,p,finished) !! change all "name" variable values to "Fred"
@@ -91,9 +129,9 @@ subroutine rename(json,p,finished) !! change all "name" variable values to "Fre
91129

92130
!it must be a string named "name":
93131
if (var_type==json_string .and. str=='name') then
94-
call json%get(p,'@',str) ! get original name
95-
call json%update(p,'@','Fred',found) ! change it
96-
write(error_unit,'(A)') str//' name changed'
132+
call json%get(p,'@',str) ! get original name
133+
call json%update(p,'@',new_name,found) ! change it
134+
write(error_unit,'(A)') str//' name changed to '//new_name
97135
icount = icount + 1
98136
end if
99137

src/tests/jf_test_3.f90

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ subroutine test_3(error_cnt)
3131
integer :: i
3232
character(kind=json_CK,len=10) :: str
3333
real(wp),dimension(:),allocatable :: rvec
34+
type(json_core) :: core
3435

3536
write(error_unit,'(A)') ''
3637
write(error_unit,'(A)') '================================='
@@ -119,6 +120,19 @@ subroutine test_3(error_cnt)
119120
error_cnt = error_cnt + 1
120121
end if
121122

123+
write(error_unit,'(A)') ''
124+
write(error_unit,'(A)') 'json_core tests...'
125+
! test json_core manipulation:
126+
call core%initialize(trailing_spaces_significant=.true.,&
127+
case_sensitive_keys=.true.)
128+
call json%initialize(core) ! send it to the file
129+
call core%destroy()
130+
call json%get_core(core) ! get it back
131+
if (core%failed()) then
132+
call core%print_error_message(error_unit)
133+
error_cnt = error_cnt + 1
134+
end if
135+
122136
end subroutine test_3
123137

124138
end module jf_test_3_mod

0 commit comments

Comments
 (0)