Пример #1
0
Файл: hash.c Проект: abw/hemp
void test_hash_methods() {
    Hemp       hemp    = hemp_new();
    HempHash  hash    = hemp_hash_new();
//  HempText  text    = hemp_text_from_string("Hello World!");
    HempValue value   = hemp_hash_val(hash);
    HempContext  context = hemp_context(hemp); 
    
    hemp_hash_store_string(hash, "message", "Hello World");
    hemp_hash_store_integer(hash, "answer", 42);
    hemp_hash_store_number(hash, "pi", 3.14159);
    
    ok( hemp_is_hash(value), "value is a hash" );
    
    HempValue length = hemp_send(value, "length", context);
    ok( hemp_is_defined(length), "got defined length" );
    ok( hemp_is_integer(length), "got an integer length" );
    eq( hemp_val_int(length), 3, "hash length is 3" );

    HempValue htext = hemp_send(value, "text", context);
    ok( hemp_is_text(htext), "got hash as text" );

    hemp_context_free(context);
    hemp_hash_free(hash);
//    hemp_text_free( hemp_val_text(htext) );
    hemp_free(hemp);
}
Пример #2
0
Файл: hemp.c Проект: 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);
}
Пример #3
0
Файл: type.c Проект: abw/hemp
void test_types() {
    Hemp hemp            = hemp_new();
    HempContext context = hemp_context(hemp);

    HempValue number = hemp_num_val(3.14159);
    ok( hemp_is_number(number), "created a number" );

    HempMemory method;
    method = hemp_object_method(number, "name");
    ok( method, "got a pointer to name method");

    HempValue value = hemp_send(number, "name", context);
    ok( hemp_is_defined(value), "got result" );
    is( hemp_val_str(value), "Number", "got number name" );

    value = hemp_send(number, "no_such_method", context);
    ok( hemp_is_missing(value), "no_such_method is missing" );

    HempText text = hemp_text_from_string("hello world!");
    value = hemp_send( hemp_text_val(text), "length", context);
    ok( hemp_is_defined(value), "got text length" );
    eq( hemp_val_int(value), 12, "length is 12" );
    hemp_text_free(text);
    
    /* try converting number to text */
    value = hemp_send(value, "text", context);
    if (hemp_is_missing(value)) {
        fail("No text method?");
        exit(1);
    }
    ok( hemp_is_defined(value), "got length as text" );
    is( hemp_val_text(value)->string, "12", "length text is 12" );

// Text value is now managed by context
//    hemp_text_free( hemp_val_text(value) );
    
    hemp_context_free(context);
    hemp_free(hemp);
}
Пример #4
0
Файл: hemp.c Проект: 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);
    }
}
Пример #5
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;
}