hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit d7f7075927ae26c06724ec1eb62ad778a2f2b3e7
parent 5baeb39f3c3a049c9c182cb53ab976fe9d2289fa
Author: Armin Preiml <apreiml@strohwolke.at>
Date:   Sat, 13 May 2023 18:43:50 +0200

mime: move test entries into a +test file

Move the default mime types into a +test tagged file so that they don't
interfere with the systems mime database.

Also loop through all exts in tests to find the expected ones. The tests
broke previously if a /etc/mime.types exists and [[load_systemdb]] is
called before the init function that added the test mime types. The text
mime type in systemdb can have multiple exts, that caused the length
check to fail.

Signed-off-by: Armin Preiml <apreiml@strohwolke.at>

Diffstat:
Mmime/database.ha | 14--------------
Amime/entries+test.ha | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmime/lookup.ha | 32--------------------------------
Mscripts/gen-stdlib | 19++++++++++++++-----
4 files changed, 89 insertions(+), 51 deletions(-)

diff --git a/mime/database.ha b/mime/database.ha @@ -47,20 +47,6 @@ fn hashtable_insert(item: *mimetype) void = { }; }; -const text_plain: mimetype = mimetype { - mime = "text/plain", - exts = ["txt"], -}; - -const text_hare: mimetype = mimetype { - mime = "text/x-hare", - exts = ["ha"], -}; - -@init fn init() void = { - register(&text_plain, &text_hare); -}; - @fini fn fini() void = { for (let i = 0z; i < len(heap_db); i += 1) { free(heap_db[i].mime); diff --git a/mime/entries+test.ha b/mime/entries+test.ha @@ -0,0 +1,75 @@ +// License: MPL-2.0 + +const text_plain: mimetype = mimetype { + mime = "text/plain", + exts = ["txt"], +}; + +const text_hare: mimetype = mimetype { + mime = "text/x-hare", + exts = ["ha"], +}; + +@init fn init() void = { + register(&text_plain, &text_hare); +}; + +@test fn lookup_mime() void = { + assert(lookup_mime("foo/bar") == null); + + const result = lookup_mime("text/plain") as *mimetype; + assert(result.mime == "text/plain"); + assert(len(result.exts) >= 1); + + let extfound = false; + for (let i = 0z; i < len(result.exts); i += 1) { + if (result.exts[i] == "txt") { + extfound = true; + }; + }; + assert(extfound); + + const result = lookup_mime("text/x-hare") as *mimetype; + assert(result.mime == "text/x-hare"); + assert(len(result.exts) >= 1); + + let extfound = false; + for (let i = 0z; i < len(result.exts); i += 1) { + if (result.exts[i] == "ha") { + extfound = true; + }; + }; + assert(extfound); +}; + + +@test fn lookup_ext() void = { + assert(lookup_ext("foo") == null); + assert(lookup_ext(".foo") == null); + + const result = lookup_ext("txt") as *mimetype; + assert(result.mime == "text/plain"); + + let extfound = false; + for (let i = 0z; i < len(result.exts); i += 1) { + if (result.exts[i] == "txt") { + extfound = true; + }; + }; + assert(extfound); + + const result = lookup_ext(".txt") as *mimetype; + assert(result.mime == "text/plain"); + + const result = lookup_ext("ha") as *mimetype; + assert(result.mime == "text/x-hare"); + assert(len(result.exts) >= 1); + + let extfound = false; + for (let i = 0z; i < len(result.exts); i += 1) { + if (result.exts[i] == "ha") { + extfound = true; + }; + }; + assert(extfound); +}; diff --git a/mime/lookup.ha b/mime/lookup.ha @@ -17,20 +17,6 @@ export fn lookup_mime(mime: str) const nullable *mimetype = { return null; }; -@test fn lookup_mime() void = { - assert(lookup_mime("foo/bar") == null); - - const result = lookup_mime("text/plain") as *mimetype; - assert(result.mime == "text/plain"); - assert(len(result.exts) == 1); - assert(result.exts[0] == "txt"); - - const result = lookup_mime("text/x-hare") as *mimetype; - assert(result.mime == "text/x-hare"); - assert(len(result.exts) == 1); - assert(result.exts[0] == "ha"); -}; - // Looks up a Media Type based on a file extension, with or without the leading // '.' character, returning null if unknown. export fn lookup_ext(ext: str) const nullable *mimetype = { @@ -47,21 +33,3 @@ export fn lookup_ext(ext: str) const nullable *mimetype = { }; return null; }; - -@test fn lookup_ext() void = { - assert(lookup_ext("foo") == null); - assert(lookup_ext(".foo") == null); - - const result = lookup_ext("txt") as *mimetype; - assert(result.mime == "text/plain"); - assert(len(result.exts) == 1); - assert(result.exts[0] == "txt"); - - const result = lookup_ext(".txt") as *mimetype; - assert(result.mime == "text/plain"); - - const result = lookup_ext("ha") as *mimetype; - assert(result.mime == "text/x-hare"); - assert(len(result.exts) == 1); - assert(result.exts[0] == "ha"); -}; diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib @@ -982,11 +982,20 @@ math() { mime() { # This module is not built by default because gen-stdlib does not do a good # job of resolving @init dependency ordering issues - gen_srcs mime \ - database.ha \ - lookup.ha \ - parse.ha \ - system.ha + if [ $testing -eq 0 ]; then + gen_srcs mime \ + database.ha \ + lookup.ha \ + parse.ha \ + system.ha + else + gen_srcs mime \ + database.ha \ + lookup.ha \ + parse.ha \ + system.ha \ + entries+test.ha + fi gen_ssa mime ascii errors strings hash::fnv encoding::utf8 bufio \ fs io os fmt }