Skip to content

Commit 25780a9

Browse files
committed
Merge pull request #111 from zbeekman/issue-85-unit-zero
fixes 85 allow output to 0 (stderr)
2 parents 63215db + 78621e4 commit 25780a9

File tree

5 files changed

+53
-20
lines changed

5 files changed

+53
-20
lines changed

.travis.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ env:
99
- DEPENDS="gfortran-4.9"
1010
- CHECK_README_PROGS="yes"
1111
matrix:
12-
# CMake build with unit tests, no documentation, no coverage analysis
13-
# Allow to fail for now until tests are fixed
14-
- BUILD_SCRIPT="mkdir cmake-build && cd cmake-build && cmake -DSKIP_DOC_GEN:BOOL=TRUE -DENABLE_UNICODE:BOOL=TRUE .. && make -j 4 check"
12+
# CMake build with unit tests, no documentation, with coverage analysis
13+
# No unicode so that coverage combined with the build script will cover unicode
14+
# and non-unicode code paths
15+
- BUILD_SCRIPT="mkdir cmake-build && cd cmake-build && cmake -DSKIP_DOC_GEN:BOOL=TRUE -DCMAKE_BUILD_TYPE=COVERAGE .. && make -j 4 check"
1516
SPECIFIC_DEPENDS="cmake nodejs"
1617
JLINT="yes"
1718
DOCS="no"
1819
FoBiS="no"
20+
CODE_COVERAGE="yes"
1921

2022
# build with build.sh, make documentation, run unit tests and perform coverage analysis
2123
- BUILD_SCRIPT="./build.sh --coverage --enable-unicode"

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ cmake_minimum_required ( VERSION 2.8.8 FATAL_ERROR )
1515
set ( CMAKE_CONFIGURATION_TYPES "Debug" "Release" "MinSizeRel" "RelWithDebInfo" )
1616
set ( CMAKE_BUILD_TYPE "Release"
1717
CACHE STRING "Select which configuration to build." )
18-
set_property ( CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${CMAKE_CONFIGURATION_TYPES} )
1918

2019
enable_language ( Fortran )
2120
include ( "cmake/pickFortranCompilerFlags.cmake" )
2221

22+
set_property ( CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${CMAKE_CONFIGURATION_TYPES} )
23+
2324
# Check for in-source builds and error out if found
2425
# Provides an advanced option to allow in source builds
2526
include ( "cmake/checkOutOfSource.cmake" )

cmake/pickFortranCompilerFlags.cmake

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@ if ( NOT Fortran_FLAGS_INIT )
2323
set ( CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -check all" )
2424
endif ()
2525
elseif ( "${CMAKE_Fortran_COMPILER_ID}" MATCHES "GNU" )
26-
set ( ENABLE_CODE_COVERAGE FALSE CACHE BOOL
27-
"Compile with code coverage output enabled using gcov. May not work on Mac.")
28-
if ( ENABLE_CODE_COVERAGE )
29-
set ( CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fprofile-arcs -ftest-coverage" )
30-
endif ()
26+
# add a coverage build configuration
27+
set ( CMAKE_CONFIGURATION_TYPES ${CMAKE_CONFIGURATION_TYPES} "Coverage" )
28+
set ( CMAKE_Fortran_FLAGS_COVERAGE "-fprofile-arcs -ftest-coverage -O0" CACHE STRING
29+
"Fortran compiler flags for coverage configuration" )
3130
if ( ENABLE_BACK_TRACE )
3231
set ( CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fbacktrace" )
3332
endif ()

src/json_module.F90

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,9 @@ end subroutine array_callback_func
11461146
integer(IK),parameter :: chunk_size = 100 !allocate chunks of this size
11471147
integer(IK) :: ipos = 1 !next character to read
11481148

1149+
!unit number to cause stuff to be output to strings rather than files
1150+
!See 9.5.6.12 in the F2003/08 standard
1151+
integer(IK),parameter :: unit2str = -1
11491152
contains
11501153
!*****************************************************************************************
11511154

@@ -1386,15 +1389,15 @@ subroutine json_file_print_1(me, iunit)
13861389
implicit none
13871390

13881391
class(json_file),intent(inout) :: me
1389-
integer(IK),intent(in) :: iunit !must be non-zero
1392+
integer(IK),intent(in) :: iunit !must not be -1
13901393

13911394
integer(IK) :: i
13921395
character(kind=CK,len=:),allocatable :: dummy
13931396

1394-
if (iunit/=0) then
1397+
if (iunit/=unit2str) then
13951398
i = iunit
13961399
else
1397-
call throw_exception('Error in json_file_print_1: iunit must be nonzero.')
1400+
call throw_exception('Error in json_file_print_1: iunit must not be -1.')
13981401
return
13991402
end if
14001403

@@ -4217,7 +4220,7 @@ subroutine json_value_to_string(me,str)
42174220
character(kind=CK,len=:),intent(out),allocatable :: str
42184221

42194222
str = ''
4220-
call json_value_print(me, iunit=0, str=str, indent=1, colon=.true.)
4223+
call json_value_print(me, iunit=unit2str, str=str, indent=1, colon=.true.)
42214224

42224225
end subroutine json_value_to_string
42234226
!*****************************************************************************************
@@ -4232,7 +4235,7 @@ end subroutine json_value_to_string
42324235
! Print the JSON structure to a file.
42334236
!
42344237
! INPUT
4235-
! * iunit is the nonzero file unit (the file must already have been opened).
4238+
! * iunit is the file unit (the file must already have been opened, can't be -1).
42364239
!
42374240
! AUTHOR
42384241
! Jacob Williams, 6/20/2014
@@ -4244,13 +4247,13 @@ subroutine json_print_1(me,iunit)
42444247
implicit none
42454248

42464249
type(json_value),pointer,intent(in) :: me
4247-
integer(IK),intent(in) :: iunit !must be non-zero
4250+
integer(IK),intent(in) :: iunit !must not be -1
42484251
character(kind=CK,len=:),allocatable :: dummy
42494252

4250-
if (iunit/=0) then
4253+
if (iunit/=unit2str) then
42514254
call json_value_print(me,iunit,str=dummy, indent=1, colon=.true.)
42524255
else
4253-
call throw_exception('Error in json_print: iunit must be nonzero.')
4256+
call throw_exception('Error in json_print: iunit must not be -1.')
42544257
end if
42554258

42564259
end subroutine json_print_1
@@ -4321,7 +4324,7 @@ recursive subroutine json_value_print(me,iunit,str,indent,need_comma,colon,is_ar
43214324
logical(LK),intent(in),optional :: need_comma !if it needs a comma after it
43224325
logical(LK),intent(in),optional :: colon !if the colon was just written
43234326
character(kind=CK,len=:),intent(inout),allocatable :: str
4324-
!if iunit==0, then the structure is
4327+
!if iunit==unit2str (-1) then the structure is
43254328
! printed to this string rather than
43264329
! a file. This mode is used by
43274330
! json_value_to_string.
@@ -4337,7 +4340,7 @@ recursive subroutine json_value_print(me,iunit,str,indent,need_comma,colon,is_ar
43374340
if (.not. exception_thrown) then
43384341

43394342
!whether to write a string or a file (one or the other):
4340-
write_string = (iunit==0)
4343+
write_string = (iunit==unit2str)
43414344
write_file = .not. write_string
43424345

43434346
!if the comma will be printed after the value

src/tests/jf_test_7.f90

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ subroutine test_7(error_cnt)
6363

6464
integer,intent(out) :: error_cnt
6565

66-
type(json_value),pointer :: root,a,b,c,d,e,e1,e2
66+
type(json_value),pointer :: root,a,b,c,d,e,e1,e2,escaped_string
6767

6868
error_cnt = 0
6969
call json_initialize()
@@ -111,6 +111,7 @@ subroutine test_7(error_cnt)
111111
! "int2": 2
112112
! }
113113
! ]
114+
! "escaped string": "\\\/\b\f\n\r\t"
114115
!}
115116

116117
!create a json structure:
@@ -196,6 +197,22 @@ subroutine test_7(error_cnt)
196197
error_cnt = error_cnt + 1
197198
end if
198199
call json_add(e,e2)
200+
if (json_failed()) then
201+
call json_print_error_message(error_unit)
202+
error_cnt = error_cnt + 1
203+
end if
204+
call json_create_object(escaped_string,'escaped string')
205+
if (json_failed()) then
206+
call json_print_error_message(error_unit)
207+
error_cnt = error_cnt + 1
208+
end if
209+
call json_add(escaped_string,'escaped string',&
210+
'\/'//&
211+
achar(8)//&
212+
achar(12)//&
213+
achar(10)//&
214+
achar(13)//&
215+
achar(9))
199216
if (json_failed()) then
200217
call json_print_error_message(error_unit)
201218
error_cnt = error_cnt + 1
@@ -226,6 +243,11 @@ subroutine test_7(error_cnt)
226243
call json_print_error_message(error_unit)
227244
error_cnt = error_cnt + 1
228245
end if
246+
call json_add(root,escaped_string)
247+
if (json_failed()) then
248+
call json_print_error_message(error_unit)
249+
error_cnt = error_cnt + 1
250+
end if
229251

230252
nullify(a) !don't need these anymore
231253
nullify(b)
@@ -234,12 +256,18 @@ subroutine test_7(error_cnt)
234256
nullify(e)
235257
nullify(e1)
236258
nullify(e2)
259+
nullify(escaped_string)
237260

238261
call json_print(root,output_unit) !print to the console
239262
if (json_failed()) then
240263
call json_print_error_message(error_unit)
241264
error_cnt = error_cnt + 1
242265
end if
266+
call json_print(root,error_unit) !print to stderr
267+
if (json_failed()) then
268+
call json_print_error_message(error_unit)
269+
error_cnt = error_cnt + 1
270+
end if
243271

244272
call json_destroy(root) !cleanup
245273
if (json_failed()) then

0 commit comments

Comments
 (0)