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:

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