Sha256: 20e8938422a5d538c102c7c190e65160ff129f2bf594b9bafb37951ec40b05f0

Contents?: true

Size: 1.97 KB

Versions: 6

Compression:

Stored size: 1.97 KB

Contents

/*
 * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
 *
 * Jansson is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include <jansson.h>
#include <string.h>
#include <stdlib.h>
#include "util.h"

struct my_source {
    const char *buf;
    size_t off;
    size_t cap;
};

static const char my_str[] = "[\"A\", {\"B\": \"C\", \"e\": false}, 1, null, \"foo\"]";

static size_t greedy_reader(void *buf, size_t buflen, void *arg)
{
    struct my_source *s = arg;
    if (buflen > s->cap - s->off)
        buflen = s->cap - s->off;
    if (buflen > 0) {
        memcpy(buf, s->buf + s->off, buflen);
        s->off += buflen;
        return buflen;
    } else {
        return 0;
    }
}

static void run_tests()
{
    struct my_source s;
    json_t *json;
    json_error_t error;

    s.off = 0;
    s.cap = strlen(my_str);
    s.buf = my_str;

    json = json_load_callback(greedy_reader, &s, 0, &error);

    if (!json)
        fail("json_load_callback failed on a valid callback");
    json_decref(json);

    s.off = 0;
    s.cap = strlen(my_str) - 1;
    s.buf = my_str;

    json = json_load_callback(greedy_reader, &s, 0, &error);
    if (json) {
        json_decref(json);
        fail("json_load_callback should have failed on an incomplete stream, but it didn't");
    }
    if (strcmp(error.source, "<callback>") != 0) {
        fail("json_load_callback returned an invalid error source");
    }
    if (strcmp(error.text, "']' expected near end of file") != 0) {
        fail("json_load_callback returned an invalid error message for an unclosed top-level array");
    }

    json = json_load_callback(NULL, NULL, 0, &error);
    if (json) {
        json_decref(json);
        fail("json_load_callback should have failed on NULL load callback, but it didn't");
    }
    if (strcmp(error.text, "wrong arguments") != 0) {
        fail("json_load_callback returned an invalid error message for a NULL load callback");
    }
}

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ctags.rb-1.1.4 ext/vendor/jansson-2.9/test/suites/api/test_load_callback.c
ctags.rb-1.1.3 ext/vendor/jansson-2.9/test/suites/api/test_load_callback.c
ctags.rb-1.0.19 ext/vendor/jansson-2.5/test/suites/api/test_load_callback.c
ctags.rb-1.0.18 ext/vendor/jansson-2.5/test/suites/api/test_load_callback.c
ctags.rb-1.0.15 ext/vendor/jansson-2.5/test/suites/api/test_load_callback.c
ctags.rb-1.0.6 ext/vendor/jansson-2.5/test/suites/api/test_load_callback.c