File: json.cmake

package info (click to toggle)
cmake 4.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 152,344 kB
  • sloc: ansic: 403,894; cpp: 303,807; sh: 4,097; python: 3,582; yacc: 3,106; lex: 1,279; f90: 538; asm: 471; lisp: 375; cs: 270; java: 266; fortran: 239; objc: 215; perl: 213; xml: 198; makefile: 108; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22
file content (69 lines) | stat: -rw-r--r-- 2,490 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Read the JSON `filename` into `outvar`.
function(read_json filename outvar)
  file(READ "${filename}" ${outvar})
  return(PROPAGATE ${outvar})
endfunction()

# Utility for error messages.
function(add_error error)
  string(APPEND RunCMake_TEST_FAILED " ${error}\n")
  string(APPEND ERROR_MESSAGE " ${error}\n")
  return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()

# Utility for JSON-specific error messages.
function(json_error file error)
  add_error("Error in JSON file ${file}:\n${error}")
  return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()

# Check if the JSON string `json` has `key` when it's not
# UNEXPECTED. If so, return it in `key`.
function(json_has_key file json key)
  cmake_parse_arguments(ARG "UNEXPECTED" "" "" ${ARGN})
  unset(missingKey)
  string(JSON ${key} ERROR_VARIABLE missingKey GET "${json}" ${key})
  if (NOT ARG_UNEXPECTED AND NOT missingKey MATCHES NOTFOUND)
    json_error("${file}" "Missing key \'${key}\':\n${json}")
  elseif (ARG_UNEXPECTED AND missingKey MATCHES NOTFOUND)
    json_error("${file}" "\nUnexpected key \'${key}\':\n${json}")
  endif()
  return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE ${key})
endfunction()

# Check if the JSON string `json` does not have `key`.
function(json_missing_key file json key)
  string(JSON data ERROR_VARIABLE missingKey GET "${json}" ${key})
  if (missingKey MATCHES NOTFOUND)
    json_error("${file}" "Has unexpected ${key}.")
  endif()
  return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()

# Check if the JSON string `json` has `key` and its value matches `expected`.
function(json_assert_key file json key expected)
  string(JSON data ERROR_VARIABLE missingKey GET "${json}" ${key})
  if (NOT missingKey MATCHES NOTFOUND)
    json_error("${file}" "Missing ${key}.")
  endif()
  if (NOT ${data} MATCHES ${expected})
    json_error(
      "${file}"
      "Unexpected data in custom content file:\nGot ${data}, Expected ${expected}."
    )
  endif()
  return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()

# Check if the two given JSON files are equal.
function(json_equals expected_file actual_file)
  read_json("${expected_file}" expected_contents)
  read_json("${actual_file}" actual_contents)
  string(JSON equal EQUAL ${expected_contents} ${actual_contents})
  if (NOT equal)
    add_error(
      "JSON ${expected_file} does not equal ${actual_file}."
    )
  endif()
  return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()