test_load.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2009-2012 Petri Lehtinen <petri@digip.org>
  3. *
  4. * Jansson is free software; you can redistribute it and/or modify
  5. * it under the terms of the MIT license. See LICENSE for details.
  6. */
  7. #include <jansson.h>
  8. #include <string.h>
  9. #include "util.h"
  10. static void file_not_found()
  11. {
  12. json_t *json;
  13. json_error_t error;
  14. char *pos;
  15. json = json_load_file("/path/to/nonexistent/file.json", 0, &error);
  16. if(json)
  17. fail("json_load_file returned non-NULL for a nonexistent file");
  18. if(error.line != -1)
  19. fail("json_load_file returned an invalid line number");
  20. /* The error message is locale specific, only check the beginning
  21. of the error message. */
  22. pos = strchr(error.text, ':');
  23. if(!pos)
  24. fail("json_load_file returne an invalid error message");
  25. *pos = '\0';
  26. if(strcmp(error.text, "unable to open /path/to/nonexistent/file.json") != 0)
  27. fail("json_load_file returned an invalid error message");
  28. }
  29. static void reject_duplicates()
  30. {
  31. json_error_t error;
  32. if(json_loads("{\"foo\": 1, \"foo\": 2}", JSON_REJECT_DUPLICATES, &error))
  33. fail("json_loads did not detect a duplicate key");
  34. check_error("duplicate object key near '\"foo\"'", "<string>", 1, 16, 16);
  35. }
  36. static void disable_eof_check()
  37. {
  38. json_error_t error;
  39. json_t *json;
  40. const char *text = "{\"foo\": 1} garbage";
  41. if(json_loads(text, 0, &error))
  42. fail("json_loads did not detect garbage after JSON text");
  43. check_error("end of file expected near 'garbage'", "<string>", 1, 18, 18);
  44. json = json_loads(text, JSON_DISABLE_EOF_CHECK, &error);
  45. if(!json)
  46. fail("json_loads failed with JSON_DISABLE_EOF_CHECK");
  47. json_decref(json);
  48. }
  49. static void decode_any()
  50. {
  51. json_t *json;
  52. json_error_t error;
  53. json = json_loads("\"foo\"", JSON_DECODE_ANY, &error);
  54. if (!json || !json_is_string(json))
  55. fail("json_load decoded any failed - string");
  56. json_decref(json);
  57. json = json_loads("42", JSON_DECODE_ANY, &error);
  58. if (!json || !json_is_integer(json))
  59. fail("json_load decoded any failed - integer");
  60. json_decref(json);
  61. json = json_loads("true", JSON_DECODE_ANY, &error);
  62. if (!json || !json_is_true(json))
  63. fail("json_load decoded any failed - boolean");
  64. json_decref(json);
  65. json = json_loads("null", JSON_DECODE_ANY, &error);
  66. if (!json || !json_is_null(json))
  67. fail("json_load decoded any failed - null");
  68. json_decref(json);
  69. }
  70. static void load_wrong_args()
  71. {
  72. json_t *json;
  73. json_error_t error;
  74. json = json_loads(NULL, 0, &error);
  75. if (json)
  76. fail("json_loads should return NULL if the first argument is NULL");
  77. json = json_loadb(NULL, 0, 0, &error);
  78. if (json)
  79. fail("json_loadb should return NULL if the first argument is NULL");
  80. json = json_loadf(NULL, 0, &error);
  81. if (json)
  82. fail("json_loadf should return NULL if the first argument is NULL");
  83. json = json_load_file(NULL, 0, &error);
  84. if (json)
  85. fail("json_loadf should return NULL if the first argument is NULL");
  86. }
  87. static void position()
  88. {
  89. json_t *json;
  90. size_t flags = JSON_DISABLE_EOF_CHECK;
  91. json_error_t error;
  92. json = json_loads("{\"foo\": \"bar\"}", 0, &error);
  93. if(error.position != 14)
  94. fail("json_loads returned a wrong position");
  95. json_decref(json);
  96. json = json_loads("{\"foo\": \"bar\"} baz quux", flags, &error);
  97. if(error.position != 14)
  98. fail("json_loads returned a wrong position");
  99. json_decref(json);
  100. }
  101. static void run_tests()
  102. {
  103. file_not_found();
  104. reject_duplicates();
  105. disable_eof_check();
  106. decode_any();
  107. load_wrong_args();
  108. position();
  109. }
粤ICP备19079148号