JSON/jf_test_6 [ Unittest ]

[ Top ] [ Unittest ]

NAME

jf_test_6

DESCRIPTION

Sixth 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:

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

 49 module jf_test_6_mod
 50 
 51     use json_module
 52     use, intrinsic :: iso_fortran_env , only: error_unit, output_unit, wp => real64
 53 
 54     implicit none
 55 
 56     character(len=*),parameter :: dir = '../files/inputs/'        !working directory
 57 
 58 contains
 59 
 60     subroutine test_6(error_cnt)
 61 
 62 !    This example tries to read an invalid json file.
 63 
 64     implicit none
 65 
 66     integer,intent(out) :: error_cnt
 67 
 68     type(json_file) :: json
 69     integer :: i
 70 
 71     character(len=*),dimension(2),parameter :: files = ['invalid.json ',&
 72                                                         'invalid2.json']
 73 
 74     error_cnt = 0
 75     call json_initialize()
 76     if (json_failed()) then
 77         call json_print_error_message(error_unit)
 78         error_cnt = error_cnt + 1
 79     end if
 80 
 81     write(error_unit,'(A)') ''
 82     write(error_unit,'(A)') '================================='
 83     write(error_unit,'(A)') '   EXAMPLE 6 : invalid JSON files'
 84     write(error_unit,'(A)') '================================='
 85     write(error_unit,'(A)') ''
 86 
 87     do i=1,2
 88 
 89         ! parse the json file:
 90         write(error_unit,'(A)') ''
 91         write(error_unit,'(A)') 'load file: '//trim(files(i))
 92         write(error_unit,'(A)') ''
 93         call json%load_file(filename = dir//trim(files(i)))
 94         if (json_failed()) then
 95             call json_print_error_message(error_unit)
 96         else
 97             write(error_unit,'(A)') 'An error should have been raised!'
 98             error_cnt = error_cnt + 1
 99         end if
100         ! clean up
101         call json%destroy()
102         if (json_failed()) then
103             call json_print_error_message(error_unit)
104             error_cnt = error_cnt + 1
105         end if
106     end do
107 
108     end subroutine test_6
109 
110 end module jf_test_6_mod
111 
112 program jf_test_6
113     use jf_test_6_mod , only: test_6
114     implicit none
115     integer :: n_errors
116     n_errors = 0
117     call test_6(n_errors)
118     if (n_errors /= 0) stop 1
119 end program jf_test_6