JSON/jf_test_8 [ Unittest ]

[ Top ] [ Unittest ]

NAME

    jf_test_8

DESCRIPTION

    Eighth 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_8_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_8(error_cnt)
 63 
 64 !   read a JSON structure from a string
 65 
 66     implicit none
 67 
 68     integer,intent(out) :: error_cnt
 69 
 70     type(json_value),pointer :: p
 71 
 72     character(len=*),parameter :: newline = achar(10)
 73 
 74     character(len=*),parameter :: str = '{ "label": "foo",'//newline//' "value": "bar" }'
 75 
 76     character(len=*),parameter :: str2 = '{ "label": "foo",'//newline//&
 77                                          '  "value": "bar",'//newline//&
 78                                          '  "empty_array": [],'//newline//&
 79                                          '  "empty_object": {}' //newline//&
 80                                          '}'
 81 
 82     character(len=*),parameter :: str_invalid = '{ "label": "foo",'//newline//' "value : "bar" }'
 83 
 84     error_cnt = 0
 85     call json_initialize()
 86     if (json_failed()) then
 87         call json_print_error_message(error_unit)
 88         error_cnt = error_cnt + 1
 89     end if
 90 
 91     write(error_unit,'(A)') ''
 92     write(error_unit,'(A)') '================================='
 93     write(error_unit,'(A)') '   EXAMPLE 8 : read JSON from string'
 94     write(error_unit,'(A)') '================================='
 95     write(error_unit,'(A)') ''
 96 
 97     write(error_unit,'(A)') '**************'
 98     write(error_unit,'(A)') ' Valid test 1:'
 99     write(error_unit,'(A)') '**************'
100     write(error_unit,'(A)') ''
101     call json_parse(str=str, p=p)   ! read it from str
102     if (json_failed()) then
103         call json_print_error_message(error_unit)
104         error_cnt = error_cnt + 1
105     end if
106     call json_print(p,OUTPUT_UNIT)  ! print to console
107     if (json_failed()) then
108         call json_print_error_message(error_unit)
109         error_cnt = error_cnt + 1
110     end if
111     call json_destroy(p)            ! cleanup
112     if (json_failed()) then
113         call json_print_error_message(error_unit)
114         error_cnt = error_cnt + 1
115     end if
116     write(error_unit,'(A)') ''
117 
118     write(error_unit,'(A)') '**************'
119     write(error_unit,'(A)') ' Valid test 2:'
120     write(error_unit,'(A)') '**************'
121     write(error_unit,'(A)') ''
122     call json_parse(str=str2, p=p)   ! read it from str
123     if (json_failed()) then
124         call json_print_error_message(error_unit)
125         error_cnt = error_cnt + 1
126     end if
127     call json_print(p,OUTPUT_UNIT)  ! print to console
128     if (json_failed()) then
129         call json_print_error_message(error_unit)
130         error_cnt = error_cnt + 1
131     end if
132     call json_destroy(p)            ! cleanup
133     if (json_failed()) then
134         call json_print_error_message(error_unit)
135         error_cnt = error_cnt + 1
136     end if
137     write(error_unit,'(A)') ''
138 
139     write(error_unit,'(A)') '**************'
140     write(error_unit,'(A)') ' Invalid test:'
141     write(error_unit,'(A)') '**************'
142     write(error_unit,'(A)') ''
143     call json_parse(str=str_invalid, p=p)   ! read it from str
144     if (json_failed()) then
145         call json_print_error_message(error_unit)
146     else
147         write(error_unit,'(A)') 'This should have failed!'
148         error_cnt = error_cnt + 1
149     end if
150     call json_print(p,OUTPUT_UNIT)  ! print to console
151     if (json_failed()) then
152         call json_print_error_message(error_unit)
153         error_cnt = error_cnt + 1
154     end if
155     call json_destroy(p)            ! cleanup
156     if (json_failed()) then
157         call json_print_error_message(error_unit)
158         error_cnt = error_cnt + 1
159     end if
160     write(error_unit,'(A)') ''
161 
162     end subroutine test_8
163 
164 end module jf_test_8_mod
165 
166 program jf_test_8
167     use jf_test_8_mod , only: test_8
168     implicit none
169     integer :: n_errors
170     n_errors = 0
171     call test_8(n_errors)
172     if (n_errors /= 0) stop 1
173 end program jf_test_8