Make real number encoding and decoding work under all locales
[jansson.git] / test / suites / api / test_load.c
1 /*
2  * Copyright (c) 2009-2011 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
8 #include <jansson.h>
9 #include <string.h>
10 #include "util.h"
11
12 static void file_not_found()
13 {
14     json_t *json;
15     json_error_t error;
16
17     json = json_load_file("/path/to/nonexistent/file.json", 0, &error);
18     if(json)
19         fail("json_load_file returned non-NULL for a nonexistent file");
20     if(error.line != -1)
21         fail("json_load_file returned an invalid line number");
22     if(strcmp(error.text, "unable to open /path/to/nonexistent/file.json: No such file or directory") != 0)
23         fail("json_load_file returned an invalid error message");
24 }
25
26 static void reject_duplicates()
27 {
28     json_error_t error;
29
30     if(json_loads("{\"foo\": 1, \"foo\": 2}", JSON_REJECT_DUPLICATES, &error))
31         fail("json_loads did not detect a duplicate key");
32     check_error("duplicate object key near '\"foo\"'", "<string>", 1, 16, 16);
33 }
34
35 static void disable_eof_check()
36 {
37     json_error_t error;
38     json_t *json;
39
40     const char *text = "{\"foo\": 1} garbage";
41
42     if(json_loads(text, 0, &error))
43         fail("json_loads did not detect garbage after JSON text");
44     check_error("end of file expected near 'garbage'", "<string>", 1, 18, 18);
45
46     json = json_loads(text, JSON_DISABLE_EOF_CHECK, &error);
47     if(!json)
48         fail("json_loads failed with JSON_DISABLE_EOF_CHECK");
49
50     json_decref(json);
51 }
52
53 static void run_tests()
54 {
55     file_not_found();
56     reject_duplicates();
57     disable_eof_check();
58 }