package dotenv_tests import "core:os" import "core:testing" import "lib:dotenv" @(test) test_parse_basic :: proc(t: ^testing.T) { env := dotenv.parse("API_URL=http://127.0.0.1:8080\nDEBUG=true\nPORT=8080\n") defer dotenv.destroy(env) testing.expect(t, env["API_URL"] == "http://127.0.0.1:8080") testing.expect(t, env["DEBUG"] == "true") testing.expect(t, env["PORT"] == "8080") } @(test) test_parse_ignores_comments_and_blank_lines :: proc(t: ^testing.T) { env := dotenv.parse("# leading comment\n\n \nFOO=bar \nBAZ = qux \n") defer dotenv.destroy(env) testing.expect(t, env["FOO"] == "bar", "value should be trimmed") testing.expect(t, env["BAZ"] == "qux", "key and value should be trimmed around '='") testing.expect(t, "API_URL" not_in env, "comment-only lines should not be parsed") } @(test) test_parse_quoted_values :: proc(t: ^testing.T) { env := dotenv.parse("GREETING=\"hello world\"\nEMPTY=\"\"\n") defer dotenv.destroy(env) testing.expect(t, env["GREETING"] == "hello world", "quoted value with inner space") testing.expect(t, env["EMPTY"] == "", "double-quoted empty value") } @(test) test_parse_skips_lines_without_equals :: proc(t: ^testing.T) { env := dotenv.parse("not-an-assignment\nOK=yep\n") defer dotenv.destroy(env) testing.expect(t, env["OK"] == "yep") testing.expect(t, "not-an-assignment" not_in env, "line without '=' should be skipped") } @(test) test_get_string_falls_back_to_file :: proc(t: ^testing.T) { env := dotenv.parse("FOO=from_file\n") defer dotenv.destroy(env) testing.expect(t, dotenv.get_string(env, "FOO") == "from_file", "missing env var should use file value") testing.expect(t, dotenv.get_string(env, "MISSING") == "", "absent everywhere should be empty") } @(test) test_get_string_prefers_real_env :: proc(t: ^testing.T) { env := dotenv.parse("FOO=from_file\n") defer dotenv.destroy(env) testing.expect(t, os.set_env("FOO", "from_env") == nil) defer os.unset_env("FOO") testing.expect(t, dotenv.get_string(env, "FOO") == "from_env", "real env var should win over file") } @(test) test_get_bool :: proc(t: ^testing.T) { env := dotenv.parse("A=true\nB=0\nC=FALSE\nD=garbage\n") defer dotenv.destroy(env) testing.expect(t, dotenv.get_bool(env, "A", false), "\"true\" should parse to true") testing.expect(t, !dotenv.get_bool(env, "B", true), "\"0\" should parse to false") testing.expect(t, !dotenv.get_bool(env, "C", true), "\"FALSE\" should parse to false") testing.expect(t, dotenv.get_bool(env, "D", true), "unparsable value should fall back to default") testing.expect(t, dotenv.get_bool(env, "MISSING", true), "missing key should fall back to default") } @(test) test_get_int :: proc(t: ^testing.T) { env := dotenv.parse("PORT=8080\nNEG=-42\nHEX=0x1F\nGARBAGE=abc\n") defer dotenv.destroy(env) testing.expect(t, dotenv.get_int(env, "PORT", -1) == 8080, "decimal int") testing.expect(t, dotenv.get_int(env, "NEG", 0) == -42, "negative int") testing.expect(t, dotenv.get_int(env, "HEX", 0) == 31, "hex int") testing.expect(t, dotenv.get_int(env, "GARBAGE", 7) == 7, "unparsable value should fall back to default") testing.expect(t, dotenv.get_int(env, "MISSING", 7) == 7, "missing key should fall back to default") }