JSON/jf_test_7 [ Unittest ]

[ Top ] [ Unittest ]

NAME

    jf_test_7

DESCRIPTION

    Seventh unit test

USES

    json_module
    iso_fortran_env (intrinsic)

HISTORY

    Izaak Beekman : 2/18/2015 : Created (refactoried original json_example.f90 file)

LICENSE

    JSON-FORTRAN: A Fortran 2008 JSON API
    https://github.com/jacobwilliams/json-fortran

    Copyright (c) 2014, Jacob Williams
    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
    are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, this
      list of conditions and the following disclaimer in the documentation and/or
      other materials provided with the distribution.

    * The names of its contributors may not be used to endorse or promote products
      derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
    ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
    ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

SOURCE

 51 module jf_test_7_mod
 52 
 53     use json_module
 54     use, intrinsic :: iso_fortran_env , only: error_unit, output_unit, wp => real64
 55 
 56     implicit none
 57 
 58     character(len=*),parameter :: dir = '../files/'               !working directory
 59 
 60 contains
 61 
 62     subroutine test_7(error_cnt)
 63 
 64 !   Indent test
 65 
 66     implicit none
 67 
 68     integer,intent(out) :: error_cnt
 69 
 70     type(json_value),pointer :: root,a,b,c,do,e,e1,e2
 71 
 72     error_cnt = 0
 73     call json_initialize()
 74     if (json_failed()) then
 75         call json_print_error_message(error_unit)
 76         error_cnt = error_cnt + 1
 77     end if
 78 
 79     write(error_unit,'(A)') ''
 80     write(error_unit,'(A)') '================================='
 81     write(error_unit,'(A)') '   EXAMPLE 7 : indent test'
 82     write(error_unit,'(A)') '================================='
 83     write(error_unit,'(A)') ''
 84 
 85 !-----------------------
 86 ! jsonlint indention is
 87 !-----------------------
 88 !{
 89 !    "a": {
 90 !        "ints": [
 91 !            1,
 92 !            2,
 93 !            3
 94 !        ],
 95 !        "chars": [
 96 !            "a",
 97 !            "b",
 98 !            "c"
 99 !        ]
100 !    },
101 !    "b": {
102 !        "c": {
103 !            "val1": 1066
104 !        }
105 !    },
106 !    "d": {
107 !        "val2": 1815
108 !    },
109 !    "array": [
110 !        {
111 !            "int1": 1
112 !        },
113 !        {
114 !            "int1": 1,
115 !            "int2": 2
116 !        }
117 !    ]
118 !}
119 
120     !create a json structure:
121     call json_create_object(root,'root')
122     if (json_failed()) then
123         call json_print_error_message(error_unit)
124         error_cnt = error_cnt + 1
125     end if
126     call json_create_object(a,'a')
127     if (json_failed()) then
128         call json_print_error_message(error_unit)
129         error_cnt = error_cnt + 1
130     end if
131     call json_add(a,'ints', [1,2,3])
132     if (json_failed()) then
133         call json_print_error_message(error_unit)
134         error_cnt = error_cnt + 1
135     end if
136     call json_create_object(b,'b')
137     if (json_failed()) then
138         call json_print_error_message(error_unit)
139         error_cnt = error_cnt + 1
140     end if
141     call json_add(a,'chars', ['a','b','c'])
142     if (json_failed()) then
143         call json_print_error_message(error_unit)
144         error_cnt = error_cnt + 1
145     end if
146     call json_create_object(c,'c')
147     if (json_failed()) then
148         call json_print_error_message(error_unit)
149         error_cnt = error_cnt + 1
150     end if
151     call json_add(c,'val1', 1066)
152     if (json_failed()) then
153         call json_print_error_message(error_unit)
154         error_cnt = error_cnt + 1
155     end if
156     call json_create_object(do,'d')
157     if (json_failed()) then
158         call json_print_error_message(error_unit)
159         error_cnt = error_cnt + 1
160     end if
161     call json_add(do,'val2', 1815)
162     if (json_failed()) then
163         call json_print_error_message(error_unit)
164         error_cnt = error_cnt + 1
165     end if
166 
167     call json_create_array(e,'array')   !objects in an array
168     if (json_failed()) then
169         call json_print_error_message(error_unit)
170         error_cnt = error_cnt + 1
171     end if
172     call json_create_object(e1,'')
173     if (json_failed()) then
174         call json_print_error_message(error_unit)
175         error_cnt = error_cnt + 1
176     end if
177     call json_add(e1,'int1', 1)
178     if (json_failed()) then
179         call json_print_error_message(error_unit)
180         error_cnt = error_cnt + 1
181     end if
182     call json_create_object(e2,'')
183     if (json_failed()) then
184         call json_print_error_message(error_unit)
185         error_cnt = error_cnt + 1
186     end if
187     call json_add(e2,'int1', 1)
188     if (json_failed()) then
189         call json_print_error_message(error_unit)
190         error_cnt = error_cnt + 1
191     end if
192     call json_add(e2,'int2', 2)
193     if (json_failed()) then
194         call json_print_error_message(error_unit)
195         error_cnt = error_cnt + 1
196     end if
197     call json_add(e,e1)
198     if (json_failed()) then
199         call json_print_error_message(error_unit)
200         error_cnt = error_cnt + 1
201     end if
202     call json_add(e,e2)
203     if (json_failed()) then
204         call json_print_error_message(error_unit)
205         error_cnt = error_cnt + 1
206     end if
207 
208     call json_add(root,a)
209     if (json_failed()) then
210         call json_print_error_message(error_unit)
211         error_cnt = error_cnt + 1
212     end if
213     call json_add(root,b)
214     if (json_failed()) then
215         call json_print_error_message(error_unit)
216         error_cnt = error_cnt + 1
217     end if
218     call json_add(b,c)
219     if (json_failed()) then
220         call json_print_error_message(error_unit)
221         error_cnt = error_cnt + 1
222     end if
223     call json_add(root,do)
224     if (json_failed()) then
225         call json_print_error_message(error_unit)
226         error_cnt = error_cnt + 1
227     end if
228     call json_add(root,e)
229     if (json_failed()) then
230         call json_print_error_message(error_unit)
231         error_cnt = error_cnt + 1
232     end if
233 
234     nullify(a)  !don't need these anymore
235     nullify(b)
236     nullify(c)
237     nullify(do)
238     nullify(e)
239     nullify(e1)
240     nullify(e2)
241 
242     call json_print(root,output_unit)  !print to the console
243     if (json_failed()) then
244         call json_print_error_message(error_unit)
245         error_cnt = error_cnt + 1
246     end if
247 
248     call json_destroy(root)  !cleanup
249     if (json_failed()) then
250         call json_print_error_message(error_unit)
251         error_cnt = error_cnt + 1
252     end if
253 
254     end subroutine test_7
255 
256 end module jf_test_7_mod
257 
258 program jf_test_7
259     use jf_test_7_mod , only: test_7
260     implicit none
261     integer :: n_errors
262     n_errors = 0
263     call test_7(n_errors)
264     if (n_errors /= 0) stop 1
265 end program jf_test_7