Esempio n. 1
0
File: hemp.c Progetto: abw/hemp
void test_hemp() {
    Hemp hemp = hemp_new();
    ok( hemp, "created hemp object" );

    HEMP_TRY;
        HempString version = hemp_version();
        ok( version, "got hemp version: %s", version );

        /* test the global configuration */
        HempValue cfghemp = hemp_config_get(hemp, "hemp");
        ok( hemp_is_defined(cfghemp), "fetched hemp config" );

        test_hemp_config(hemp, "hemp.version",    version);
        test_hemp_config(hemp, "hemp.dir",        HEMP_DIR);
        test_hemp_config(hemp, "hemp.module_dir", HEMP_MODULE_DIR);
        test_hemp_config(hemp, "hemp.module_ext", HEMP_MODULE_EXT);

        /* add some extra configuration values from a hash */
        HempHash config = hemp_hash_new();
        hemp_hash_store_string(config, "foo",       "Hello");
        hemp_hash_store_string(config, "bar",       "World");
        hemp_hash_store_string(config, "hemp.dir",  "/some/where/else");

        /* add a nested hemp.root value to mask the global config value */
        HempHash hconf = hemp_hash_new();
        hemp_hash_store_hash(config, "hemp", hconf);
        hemp_hash_store_string(hconf, "dir", "/some/where/else");

        /* configure the hemp object from the hash and check values */
        hemp_configure(hemp, hemp_hash_val(config));
        test_hemp_config(hemp, "foo",           "Hello");
        test_hemp_config(hemp, "bar",           "World");
        test_hemp_config(hemp, "hemp.dir",      "/some/where/else");
        test_hemp_config(hemp, "hemp.version",  version);

        /* read configuration from a file */
        hemp_language(hemp, "json");  // FIXME
        hemp_configure_from(hemp, "json", "file", HEMP_TEST_DIR "/data/config1.json");
        test_hemp_config(hemp, "hemp.dir",      "/dir/from/config1");
        test_hemp_config(hemp, "hemp.animal",   "badger");
        test_hemp_config(hemp, "extra.cheese",  "please");

        hemp_hash_free(hconf);
        hemp_hash_free(config);

    HEMP_CATCH_ALL;
        fail("caught error: %s", hemp->error->message);
        HempText error = hemp_error_text(hemp->error);
        printf("%s", error->string);
        hemp_text_free(error);

    HEMP_END;
    
    hemp_free(hemp);
}
Esempio n. 2
0
File: hemp.c Progetto: abw/hemp
void test_hemp_config(
    Hemp   hemp,
    HempString name,
    HempString expect
) {
    HempValue  value = hemp_config_get(hemp, name);
    HempString string;

    if (hemp_is_string(value)) {
        string = hemp_val_str(value);
        is(string, expect, name);
    }
    else if (hemp_is_text(value)) {
        string = hemp_val_text(value)->string;
        is(string, expect, name);
    }
    else if (hemp_is_defined(value)) {
        fail("Unexpected value for %s: %s", name, hemp_type_name(value));
    }
    else {
        fail("Config item not found: %s", name);
    }
}
Esempio n. 3
0
HempModule
hemp_use_module(
    Hemp       hemp,
    HempString     type,
    HempString     name
) {
    hemp_debug_call("hemp_use_module(%s => %s)\n", type, name);

    HempValue  path = hemp_config_get(hemp, HEMP_CONFIG_MODPATH);
    HempString string;

    if (hemp_is_defined(path)) {
        string = hemp_to_string(path, hemp->context);
//      hemp_debug_msg("already got %s: %s\n", HEMP_CONFIG_MODPATH, string);
    }
    else {
        HempValue  dir  = hemp_config_get(hemp, HEMP_CONFIG_DIR);
        HempValue  mod  = hemp_config_get(hemp, HEMP_CONFIG_MODDIR);
        HempString dstr = hemp_to_string(dir, hemp->context);
        HempString mstr = hemp_to_string(mod, hemp->context);

        string = hemp_uri_path_join(dstr, mstr, 1);
//      hemp_debug_msg("constructed %s: %s\n", HEMP_CONFIG_MODPATH, string);

        /* ugly work-around so we can get the context to manage memory */
        HempText text = hemp_context_tmp_text_size(hemp->context, strlen(string) + 1);
        hemp_text_append_string(text, string);
        hemp_mem_free(string);
        string = text->string;
//        hemp_config_set(hemp, HEMP_CONFIG_MODPATH, hemp_text_val(text));
    }

    // TODO: need a way to save dotted path (hemp.module_path) back into config

    /* quick hack to get something working */
    HempString modpath = getenv("HEMP_MODULE_PATH");

    if (! modpath || ! *modpath) {
        modpath = string;
//        hemp_debug_msg("No HEMP_MODULE_PATH environment variable set\n");
//        return HEMP_FALSE;
    }

    /* TODO: sort this mess out */
    HempString tpath = hemp_uri_path_join(modpath, type, 1);
    HempString mpath = hemp_uri_path_join(tpath, name, 1);
    HempText   mtext = hemp_text_from_string(mpath);
    hemp_text_append_string(mtext, HEMP_MODULE_EXT);
    hemp_mem_free(mpath);
    hemp_mem_free(tpath);
//  hemp_debug_msg("constructed module path: %s\n", mtext->string);

    HempModule module = hemp_global_module(hemp->global, mtext->string);

    if (module->binder) {
        module->binder(module, hemp);
    }
    else if (module->error) {
        hemp_fatal(module->error);
    }
    else {
        /* should never happen - famous last word */
        hemp_fatal("No binder function for %s module", name);
    }

    hemp_text_free(mtext);
    return module;
}