@@ -19,58 +19,88 @@ subroutine test_29(error_cnt)
19
19
20
20
integer ,intent (out ) :: error_cnt
21
21
22
- type (json_value),pointer :: p
23
- type (json_core) :: json
24
- logical (LK) :: has_duplicate
25
- character (kind= CK,len= :),allocatable :: name
26
- character (kind= CK,len= :),allocatable :: path
27
-
28
- character (kind= CK,len=* ),parameter :: json_str = &
29
- ' {"vars": {"a":1, "b":2, "a":3, "a":4, "c":5} }'
30
-
31
22
error_cnt = 0
32
- call json% initialize()
33
23
34
24
write (error_unit,' (A)' ) ' '
35
25
write (error_unit,' (A)' ) ' ================================='
36
26
write (error_unit,' (A)' ) ' TEST 29'
37
27
write (error_unit,' (A)' ) ' ================================='
38
28
write (error_unit,' (A)' ) ' '
39
29
40
- write (error_unit,' (A)' ) ' '
41
- write (error_unit,' (A)' ) ' JSON string: ' // json_str
30
+ call test(CK_' {"vars":{"a":1,"b":2,"a":3,"a":4,"c":5}}' ,.true. ,CK_' a' ,CK_' vars.a' )
31
+ call test(CK_' {"vars":{"a":1,"a":3}}' ,.true. ,CK_' a' ,CK_' vars.a' )
32
+ call test(CK_' {"vars":{"aaa":1,"b":2,"aaa":3,"a":4,"c":5}}' ,.true. ,CK_' aaa' ,CK_' vars.aaa' )
33
+ call test(CK_' {"vars":{"aaaa":1,"aaaa":3}}' ,.true. ,CK_' aaaa' ,CK_' vars.aaaa' )
34
+ call test(CK_' {"a":1,"b":2,"a":3,"a":4,"c":5}' ,.true. ,CK_' a' ,CK_' a' )
35
+ call test(CK_' {"c":5}' ,.false. ,CK_' ' ,CK_' ' )
36
+ call test(CK_' {"vars":{"c":5},"array":[1,2]}' ,.false. ,CK_' ' ,CK_' ' )
37
+ call test(CK_' {}' ,.false. ,CK_' ' ,CK_' ' )
38
+
39
+ contains
40
+
41
+ subroutine test (json_str ,correct_has_duplicate ,correct_name ,correct_path )
42
+
43
+ implicit none
44
+
45
+ character (kind= CK,len=* ),intent (in ) :: json_str ! ! JSON string to check
46
+ logical (LK),intent (in ) :: correct_has_duplicate ! ! expected result
47
+ character (kind= CK,len=* ),intent (in ) :: correct_name ! ! expected result
48
+ character (kind= CK,len=* ),intent (in ) :: correct_path ! ! expected result
49
+
50
+ type (json_value),pointer :: p
51
+ type (json_core) :: json
52
+ logical (LK) :: has_duplicate
53
+ character (kind= CK,len= :),allocatable :: name
54
+ character (kind= CK,len= :),allocatable :: path
42
55
43
- call json% parse(p,json_str)
44
- if (json% failed()) then
45
- call json% print_error_message(error_unit)
46
- error_cnt = error_cnt + 1
47
- else
56
+ call json% initialize(no_whitespace= .true. )
48
57
49
58
write (error_unit,' (A)' ) ' '
50
- call json% check_for_duplicate_keys(p,has_duplicate,name,path)
59
+ write (error_unit,' (A)' ) ' JSON string: ' // json_str
60
+
61
+ call json% parse(p,json_str)
51
62
if (json% failed()) then
52
63
call json% print_error_message(error_unit)
53
64
error_cnt = error_cnt + 1
54
65
else
55
- if (has_duplicate) then
56
- write (output_unit,' (A)' ) ' Duplicate key found:'
57
- write (output_unit,' (A)' ) ' name: ' // trim (name)
58
- write (output_unit,' (A)' ) ' path: ' // trim (path)
59
- if (name /= CK_' a' .or. path /= CK_' vars.a' ) then
60
- write (error_unit,' (A)' ) ' Error: incorrect duplicate key name or path'
66
+
67
+ write (error_unit,' (A)' ) ' '
68
+
69
+ ! just test all options:
70
+ call json% check_for_duplicate_keys(p,has_duplicate,name= name)
71
+ call json% check_for_duplicate_keys(p,has_duplicate,path= path)
72
+ call json% check_for_duplicate_keys(p,has_duplicate)
73
+ call json% check_for_duplicate_keys(p,has_duplicate,name= name,path= path)
74
+ if (json% failed()) then
75
+ call json% print_error_message(error_unit)
76
+ error_cnt = error_cnt + 1
77
+ else
78
+ if (correct_has_duplicate .neqv. has_duplicate) then
79
+ write (error_unit,' (A)' ) ' Test failed.'
61
80
error_cnt = error_cnt + 1
62
81
else
63
- write (output_unit,' (A)' ) ' Test passed'
82
+ if (has_duplicate) then
83
+ write (output_unit,' (A)' ) ' Duplicate key found:'
84
+ write (output_unit,' (A)' ) ' name: ' // trim (name)
85
+ write (output_unit,' (A)' ) ' path: ' // trim (path)
86
+ if (name/= correct_name .or. path/= correct_path) then
87
+ write (error_unit,' (A)' ) ' Error: incorrect duplicate key name or path'
88
+ error_cnt = error_cnt + 1
89
+ else
90
+ write (output_unit,' (A)' ) ' Test passed: correct duplicate found'
91
+ end if
92
+ else
93
+ write (output_unit,' (A)' ) ' Test passed: no duplicates present'
94
+ end if
64
95
end if
65
- else
66
- write (error_unit,' (A)' ) ' Test failed. Duplicate keys not found'
67
- error_cnt = error_cnt + 1
68
96
end if
97
+
69
98
end if
70
99
71
- end if
100
+ call json% destroy(p)
101
+ call json% destroy()
72
102
73
- call json % destroy(p)
103
+ end subroutine test
74
104
75
105
end subroutine test_29
76
106
0 commit comments