| @@ -20,6 +20,7 @@ import std/http.{Settings} |
| 20 | 20 | import std/os |
| 21 | 21 | import std/result |
| 22 | 22 | import std/string |
| 23 | +import std/test.{assert_equal} |
| 23 | 24 | import std/process.{type Subject, type proc} |
| 24 | 25 | import std/supervisor.{Child, OneForOne} |
| 25 | 26 | import std/actor |
| @@ -43,12 +44,31 @@ fn settings() -> Result<(Int, Int), String> / <io> { |
| 43 | 44 | /// The environment variable `name` as an Int, or `default` if it is not |
| 44 | 45 | /// set. |
| 45 | 46 | fn int_variable(name: String, default: Int) -> Result<Int, String> / <io> { |
| 46 | | − match os.env(name) { |
| 47 | + int_setting(name, os.env(name), default) |
| 48 | +} |
| 49 | + |
| 50 | +/// `value`, the value of variable `name`, as an Int, or `default` if |
| 51 | +/// there is none. |
| 52 | +fn int_setting(name: String, value: Option<String>, default: Int) -> Result<Int, String> { |
| 53 | + match value { |
| 47 | 54 | None -> Ok(default), |
| 48 | 55 | Some(text) -> string.to_int(text) |> result.map_error(fn(e) { name <> ": " <> e }), |
| 49 | 56 | } |
| 50 | 57 | } |
| 51 | 58 | |
| 59 | +@test |
| 60 | +fn an_unset_variable_takes_its_default() { |
| 61 | + assert_equal(int_setting("PORT", None, 8080), Ok(8080)) |
| 62 | +} |
| 63 | + |
| 64 | +@test |
| 65 | +fn a_set_variable_must_be_a_number() { |
| 66 | + assert_equal(int_setting("PORT", Some("0"), 8080), Ok(0)) |
| 67 | + assert_equal(int_setting("MAX_CONNECTIONS", Some("-1"), 5), Ok(-1)) |
| 68 | + assert_equal(int_setting("PORT", Some("80a"), 8080), Error("PORT: not an integer: \"80a\"")) |
| 69 | + assert_equal(int_setting("PORT", Some(""), 8080), Error("PORT: not an integer: \"\"")) |
| 70 | +} |
| 71 | + |
| 52 | 72 | fn start(port: Int, connections: Int) -> () / <io, proc, ffi> { |
| 53 | 73 | match http.listen("127.0.0.1", port) { |
| 54 | 74 | Ok(server) -> { |