JSON/jf_test_8 [ 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
49 module jf_test_8_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 contains 57 58 subroutine test_8(error_cnt) 59 60 ! read a JSON structure from a string 61 62 implicit none 63 64 integer,intent(out) :: error_cnt 65 66 type(json_value),pointer :: p 67 68 character(len=*),parameter :: newline = achar(10) 69 70 character(len=*),parameter :: str = '{ "label": "foo",'//newline//' "value": "bar" }' 71 72 character(len=*),parameter :: str2 = '{ "label": "foo",'//newline//& 73 ' "value": "bar",'//newline//& 74 ' "empty_array": [],'//newline//& 75 ' "empty_object": {}' //newline//& 76 '}' 77 78 character(len=*),parameter :: str_invalid = '{ "label": "foo",'//newline//' "value : "bar" }' 79 80 error_cnt = 0 81 call json_initialize() 82 if (json_failed()) then 83 call json_print_error_message(error_unit) 84 error_cnt = error_cnt + 1 85 end if 86 87 write(error_unit,'(A)') '' 88 write(error_unit,'(A)') '=================================' 89 write(error_unit,'(A)') ' EXAMPLE 8 : read JSON from string' 90 write(error_unit,'(A)') '=================================' 91 write(error_unit,'(A)') '' 92 93 write(error_unit,'(A)') '**************' 94 write(error_unit,'(A)') ' Valid test 1:' 95 write(error_unit,'(A)') '**************' 96 write(error_unit,'(A)') '' 97 call json_parse(str=str, p=p) ! read it from str 98 if (json_failed()) then 99 call json_print_error_message(error_unit) 100 error_cnt = error_cnt + 1 101 end if 102 write(output_unit,'(A)') '{ "part a" : ' 103 call json_print(p,output_unit) ! print to console 104 write(output_unit,'(A)') ',' 105 if (json_failed()) then 106 call json_print_error_message(error_unit) 107 error_cnt = error_cnt + 1 108 end if 109 call json_destroy(p) ! cleanup 110 if (json_failed()) then 111 call json_print_error_message(error_unit) 112 error_cnt = error_cnt + 1 113 end if 114 write(error_unit,'(A)') '' 115 116 write(error_unit,'(A)') '**************' 117 write(error_unit,'(A)') ' Valid test 2:' 118 write(error_unit,'(A)') '**************' 119 write(error_unit,'(A)') '' 120 call json_parse(str=str2, p=p) ! read it from str 121 if (json_failed()) then 122 call json_print_error_message(error_unit) 123 error_cnt = error_cnt + 1 124 end if 125 write(output_unit,'(A)') '"part b" : ' 126 call json_print(p,output_unit) ! print to console 127 write(output_unit,'(A)') ',' 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 write(output_unit,'(A)') '"part c" : ' 151 call json_print(p,output_unit) ! print to console 152 write(output_unit,'(A)') '}' 153 if (json_failed()) then 154 call json_print_error_message(error_unit) 155 error_cnt = error_cnt + 1 156 end if 157 call json_destroy(p) ! cleanup 158 if (json_failed()) then 159 call json_print_error_message(error_unit) 160 error_cnt = error_cnt + 1 161 end if 162 write(error_unit,'(A)') '' 163 164 end subroutine test_8 165 166 end module jf_test_8_mod 167 168 program jf_test_8 169 use jf_test_8_mod , only: test_8 170 implicit none 171 integer :: n_errors 172 n_errors = 0 173 call test_8(n_errors) 174 if (n_errors /= 0) stop 1 175 end program jf_test_8