<commit_before>
find_program(JSONLINT_EXECUTABLE NAMES jsonlint.cmd jsonlint)
find_program(AJV_EXECUTABLE NAMES ajv.cmd ajv)

file(GLOB json_files
  *.json
  )

file(GLOB json_example_files
  *example.json
  )

file(GLOB json_common_schema_files
  *common-schema.json
  )

file(GLOB json_schema_files
  *schema.json
  )
list(REMOVE_ITEM json_schema_files ${json_common_schema_files})

#-----------------------------------------------------------------------------
# General json syntax validation
#-----------------------------------------------------------------------------

foreach(json_file ${json_files})
  get_filename_component(json_name ${json_file} NAME_WE)
  set(testname jsonlint_${json_name})
  add_test(NAME ${testname}
    COMMAND ${JSONLINT_EXECUTABLE} ${json_file})
  set_property(TEST ${testname} PROPERTY LABELS jsonlint)
endforeach()

#-----------------------------------------------------------------------------
# Schema validation
#-----------------------------------------------------------------------------

set(AJV_OPTIONS --all-errors)

# create argument list for all referenced common schemata
foreach(json_common_schema_file ${json_common_schema_files})
  # -r needs to be used when specifying a schema that has references to external files
  list(APPEND common_schema_references -r ${json_common_schema_file})
endforeach()

function(createSchemaValidationTest schema_name)
  get_filename_component(json_name ${schema_name} NAME_WE)
  set(testname ajv_${json_name}-schema)
  add_test(NAME ${testname}
    COMMAND ${AJV_EXECUTABLE} compile -s ${schema_name} ${common_schema_references} ${AJV_OPTIONS})
  set_property(TEST ${testname} PROPERTY LABELS ajv)
endfunction()

# validation of each common schema
foreach(json_common_schema_file ${json_common_schema_files})
  createSchemaValidationTest(${json_common_schema_file})
endforeach()

# validation of each specific (sr, seg, pm) schema file including referenced common schemata
foreach(json_schema_file ${json_schema_files})
  createSchemaValidationTest(${json_schema_file})
endforeach()

#-----------------------------------------------------------------------------
# Example of validation tests
#-----------------------------------------------------------------------------

# function to simplify creation of example validation tests
function(createExampleDataTest example_name schema)
  set(json_example_file ${CMAKE_SOURCE_DIR}/doc/${example_name}-example.json)
  set(testname ajv_${example_name}-example)
  add_test(NAME ${testname}
    # -d stands for data that needs to be validated
    COMMAND ${AJV_EXECUTABLE} -s ${schema} ${common_schema_references} -d ${json_example_file} ${AJV_OPTIONS})
  set_property(TEST ${testname} PROPERTY LABELS ajv)
endfunction()

# validation of each specific (sr, seg, pm) example file including specific schema and referenced common schemata
set(SEG_EXAMPLES seg bmmr)
set(SEG_SCHEMA ${CMAKE_SOURCE_DIR}/doc/seg-schema.json)

foreach(seg_example_name ${SEG_EXAMPLES})
  createExampleDataTest(${seg_example_name} ${SEG_SCHEMA})
endforeach()

set(PM_EXAMPLES pm)
set(PM_SCHEMA ${CMAKE_SOURCE_DIR}/doc/pm-schema.json)

foreach(pm_example_name ${PM_EXAMPLES})
  createExampleDataTest(${pm_example_name} ${PM_SCHEMA})<commit_msg>ENH: doc/cmake: Skipping adding tests if either ajv or jsonlint is not found<commit_after>
find_program(JSONLINT_EXECUTABLE NAMES jsonlint.cmd jsonlint)
find_program(AJV_EXECUTABLE NAMES ajv.cmd ajv)

file(GLOB json_files
  *.json
  )

file(GLOB json_example_files
  *example.json
  )

file(GLOB json_common_schema_files
  *common-schema.json
  )

file(GLOB json_schema_files
  *schema.json
  )
list(REMOVE_ITEM json_schema_files ${json_common_schema_files})

#-----------------------------------------------------------------------------
# General json syntax validation
#-----------------------------------------------------------------------------

foreach(json_file ${json_files})
  get_filename_component(json_name ${json_file} NAME_WE)
  set(testname jsonlint_${json_name})
  if(JSONLINT_EXECUTABLE)
    add_test(NAME ${testname}
      COMMAND ${JSONLINT_EXECUTABLE} ${json_file})
    set_property(TEST ${testname} PROPERTY LABELS jsonlint)
  else()
    message(STATUS "Skipping '${testname}' test: jsonlint executable not found")
  endif()
endforeach()

#-----------------------------------------------------------------------------
# Schema validation
#-----------------------------------------------------------------------------

set(AJV_OPTIONS --all-errors)

# create argument list for all referenced common schemata
foreach(json_common_schema_file ${json_common_schema_files})
  # -r needs to be used when specifying a schema that has references to external files
  list(APPEND common_schema_references -r ${json_common_schema_file})
endforeach()

function(createSchemaValidationTest schema_name)
  get_filename_component(json_name ${schema_name} NAME_WE)
  set(testname ajv_${json_name}-schema)
  if(AJV_EXECUTABLE)
    add_test(NAME ${testname}
      COMMAND ${AJV_EXECUTABLE} compile -s ${schema_name} ${common_schema_references} ${AJV_OPTIONS})
    set_property(TEST ${testname} PROPERTY LABELS ajv)
  else()
    message(STATUS "Skipping '${testname}' test: ajv executable not found")
  endif()
endfunction()

# validation of each common schema
foreach(json_common_schema_file ${json_common_schema_files})
  createSchemaValidationTest(${json_common_schema_file})
endforeach()

# validation of each specific (sr, seg, pm) schema file including referenced common schemata
foreach(json_schema_file ${json_schema_files})
  createSchemaValidationTest(${json_schema_file})
endforeach()

#-----------------------------------------------------------------------------
# Example of validation tests
#-----------------------------------------------------------------------------

# function to simplify creation of example validation tests
function(createExampleDataTest example_name schema)
  set(json_example_file ${CMAKE_SOURCE_DIR}/doc/${example_name}-example.json)
  set(testname ajv_${example_name}-example)
  if(AJV_EXECUTABLE)
    add_test(NAME ${testname}
      # -d stands for data that needs to be validated
      COMMAND ${AJV_EXECUTABLE} -s ${schema} ${common_schema_references} -d ${json_example_file} ${AJV_OPTIONS})
    set_property(TEST ${testname} PROPERTY LABELS ajv)
  else()
    message(STATUS "Skipping '${testname}' test: ajv executable not found")
  endif()
endfunction()

# validation of each specific (sr, seg, pm) example file including specific schema and referenced common schemata
set(SEG_EXAMPLES seg bmmr)
set(SEG_SCHEMA ${CMAKE_SOURCE_DIR}/doc/seg-schema.json)

foreach(seg_example_name ${SEG_EXAMPLES})
  createExampleDataTest(${seg_example_name} ${SEG_SCHEMA})
endforeach()

set(PM_EXAMPLES pm)
set(PM_SCHEMA ${CMAKE_SOURCE_DIR}/doc/pm-schema.json)

foreach(pm_example_name ${PM_EXAMPLES})
  createExampleDataTest(${pm_example_name} ${PM_SCHEMA})
{'fraction_non_alphanumeric': 0.21816870944484498, 'fraction_numerical': 0.0, 'mean_word_length': 3.0728126834997065, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1632504', 'n_tokens_mistral': 2061, 'n_tokens_neox': 1997, 'n_words': 456}