From 05361353e9b7b719f183b204a3a217a52f6c58bf Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Fri, 23 Feb 2024 20:28:51 +0100 Subject: [PATCH] Rename methods used for unwrapping values The renames are as follows: - "unwrap" is now "get" - "unwrap_or" is now "or" - "unwrap_or_else" is now "or_else" - "expect" is now "or_panic" These methods are renamed to make the method names a bit shorter, remove the need for sticking "unwrap" in the names, and because "unwrapping" isn't really clearly defined in the context of Option and Result types (especially Result as both Ok and Error can be unwrapped). This fixes https://github.com/inko-lang/inko/issues/662. Changelog: changed --- docs/source/getting-started/concurrency.md | 4 +- docs/source/getting-started/control-flow.md | 2 +- docs/source/getting-started/files.md | 24 +- .../getting-started/hello-error-handling.md | 24 +- docs/source/getting-started/sockets.md | 42 +- inko/src/command/test.rs | 4 +- std/src/std/fs/file.inko | 10 +- std/src/std/fs/path.inko | 38 +- std/src/std/io.inko | 8 +- std/src/std/json.inko | 16 +- std/src/std/net/ip.inko | 4 +- std/src/std/net/socket.inko | 186 +++---- std/src/std/option.inko | 40 +- std/src/std/result.inko | 46 +- std/src/std/sys.inko | 20 +- std/src/std/test.inko | 28 +- std/test/compiler/test_diagnostics.inko | 10 +- std/test/std/fs/test_file.inko | 126 ++--- std/test/std/fs/test_path.inko | 34 +- std/test/std/net/test_socket.inko | 500 +++++++++--------- std/test/std/test_env.inko | 8 +- std/test/std/test_float.inko | 2 +- std/test/std/test_io.inko | 4 +- std/test/std/test_map.inko | 4 +- std/test/std/test_option.inko | 18 +- std/test/std/test_result.inko | 26 +- std/test/std/test_sys.inko | 6 +- std/test/std/test_test.inko | 2 +- 28 files changed, 618 insertions(+), 618 deletions(-) diff --git a/docs/source/getting-started/concurrency.md b/docs/source/getting-started/concurrency.md index e0246a144..806936d3f 100644 --- a/docs/source/getting-started/concurrency.md +++ b/docs/source/getting-started/concurrency.md @@ -161,9 +161,9 @@ class async Main { fn async main { let server = recover TcpServer .new(IpAddress.v4(127, 0, 0, 1), port: 40_000) - .unwrap + .get - let client = recover server.accept.unwrap + let client = recover server.accept.get } } ``` diff --git a/docs/source/getting-started/control-flow.md b/docs/source/getting-started/control-flow.md index 70f6059a4..cb7adad8d 100644 --- a/docs/source/getting-started/control-flow.md +++ b/docs/source/getting-started/control-flow.md @@ -181,7 +181,7 @@ The `throw` keyword is only available in methods of which the return type is a ## try `try` takes an expression of which the type is either `std.result.Result` or -`std.option.Option`, and unwraps it. If the value is a `Result.Error` or an +`std.option.Option`, and gets it. If the value is a `Result.Error` or an `Option.None`, the value is returned as-is. Consider this example of using `try` with an `Option` value: diff --git a/docs/source/getting-started/files.md b/docs/source/getting-started/files.md index 5095af966..7f8945a13 100644 --- a/docs/source/getting-started/files.md +++ b/docs/source/getting-started/files.md @@ -16,13 +16,13 @@ import std.stdio.STDOUT class async Main { fn async main { let out = STDOUT.new - let file = ReadWriteFile.new('hello.txt').unwrap + let file = ReadWriteFile.new('hello.txt').get let bytes = ByteArray.new - file.write_string('Hello, world!').unwrap - file.seek(0).unwrap - file.read_all(bytes).unwrap - out.write_bytes(bytes).unwrap + file.write_string('Hello, world!').get + file.seek(0).get + file.read_all(bytes).get + out.write_bytes(bytes).get } } ``` @@ -43,7 +43,7 @@ We used `ReadWriteFile` to open a file for both reading and writing, using the cursor to the start of the file, then read the data back, and write it to the terminal. -For the sake of brevity we've ignored error handling by using `unwrap`, +For the sake of brevity we've ignored error handling by using `get`, resulting in the program terminating in the event of an error. Of course in a real program you'll want more fine-grained error handling, but for the sake of brevity we'll pretend our program won't produce any errors. @@ -62,11 +62,11 @@ import std.stdio.STDOUT class async Main { fn async main { let out = STDOUT.new - let file = ReadOnlyFile.new('hello.txt').unwrap + let file = ReadOnlyFile.new('hello.txt').get let bytes = ByteArray.new - file.read_all(bytes).unwrap - out.write_bytes(bytes).unwrap + file.read_all(bytes).get + out.write_bytes(bytes).get } } ``` @@ -78,9 +78,9 @@ an error such as this: ``` Stack trace (the most recent call comes last): [...]/files.inko:7 in main.Main.main - [...]/std/src/std/result.inko:119 in std.result.Result.unwrap + [...]/std/src/std/result.inko:119 in std.result.Result.get [...]/std/src/std/process.inko:15 in std.process.panic -Process 'Main' (0x5645bdf31740) panicked: Result.unwrap can't unwrap an Error +Process 'Main' (0x5645bdf31740) panicked: Result.get expects an Ok(_), but an Error(_) is found ``` ## Write-only files @@ -92,7 +92,7 @@ import std.fs.file.WriteOnlyFile class async Main { fn async main { - let file = WriteOnlyFile.new('hello.txt').unwrap + let file = WriteOnlyFile.new('hello.txt').get file.write_string('Hello, world!') } diff --git a/docs/source/getting-started/hello-error-handling.md b/docs/source/getting-started/hello-error-handling.md index 8d0d10faa..ee68d2748 100644 --- a/docs/source/getting-started/hello-error-handling.md +++ b/docs/source/getting-started/hello-error-handling.md @@ -16,14 +16,14 @@ import std.stdio.STDOUT class async Main { fn async main { - STDOUT.new.print('Hello, world!').unwrap + STDOUT.new.print('Hello, world!').get } } ``` -The change made here compared to the previous tutorial is the addition of -`.unwrap` at the end of the `print()` line. We'll look into what this does -later. Now run the program as follows: +The change made here compared to the previous tutorial is the addition of `.get` +at the end of the `print()` line. We'll look into what this does later. Now run +the program as follows: ```bash inko run hello.inko | true @@ -34,16 +34,16 @@ The output of this program should be something similar to the following: ``` Stack trace (the most recent call comes last): [...]/hello.inko:5 in main.Main.main - [...]/std/src/std/result.inko:119 in std.result.Result.unwrap + [...]/std/src/std/result.inko:119 in std.result.Result.get [...]/std/src/std/process.inko:15 in std.process.panic -Process 'Main' (0x5637caa83150) panicked: Result.unwrap can't unwrap an Error +Process 'Main' (0x5637caa83150) panicked: Result.get expects an Ok(_), but an Error(_) is found ``` What happened here is that the `print()` failed to write to the standard output -stream. The `unwrap` method call turns such an error into a "panic". A panic is -a type of error that terminates the program. Panics are the result of bugs in -your code, and as such can't be handled at runtime. In a well written program, -such errors shouldn't occur. +stream. The `get` method call turns such an error into a "panic". A panic is a +type of error that terminates the program. Panics are the result of bugs in your +code, and as such can't be handled at runtime. In a well written program, such +errors shouldn't occur. ## Handling the error @@ -81,8 +81,8 @@ If all went well, the program should run _without_ printing anything to the terminal. This may seem surprising at first, but is indeed the correct behaviour: the -`print()` call still fails, but instead of calling `unwrap` we assign the result -to the variable `_`. By assigning the result of `print()` to `_`, the compiler +`print()` call still fails, but instead of calling `get` we assign the result to +the variable `_`. By assigning the result of `print()` to `_`, the compiler won't emit any warnings because the result isn't used, nor will it emit any warnings because the variable assigned to isn't used. diff --git a/docs/source/getting-started/sockets.md b/docs/source/getting-started/sockets.md index 6ea76aa1d..41833a848 100644 --- a/docs/source/getting-started/sockets.md +++ b/docs/source/getting-started/sockets.md @@ -18,17 +18,17 @@ import std.stdio.STDOUT class async Main { fn async main { let stdout = STDOUT.new - let server = UdpSocket.new(IpAddress.v4(0, 0, 0, 0), port: 0).unwrap - let client = UdpSocket.new(IpAddress.v4(0, 0, 0, 0), port: 0).unwrap - let addr = server.local_address.unwrap + let server = UdpSocket.new(IpAddress.v4(0, 0, 0, 0), port: 0).get + let client = UdpSocket.new(IpAddress.v4(0, 0, 0, 0), port: 0).get + let addr = server.local_address.get - client.connect(addr.ip.unwrap, addr.port).unwrap - client.write_string('Hello, world!').unwrap + client.connect(addr.ip.get, addr.port).get + client.write_string('Hello, world!').get let bytes = ByteArray.new - server.read(into: bytes, size: 32).unwrap - stdout.write_bytes(bytes).unwrap + server.read(into: bytes, size: 32).get + stdout.write_bytes(bytes).get } } ``` @@ -52,8 +52,8 @@ possible. Our sockets are created as follows: ```inko -let server = UdpSocket.new(IpAddress.v4(0, 0, 0, 0), port: 0).unwrap -let client = UdpSocket.new(IpAddress.v4(0, 0, 0, 0), port: 0).unwrap +let server = UdpSocket.new(IpAddress.v4(0, 0, 0, 0), port: 0).get +let client = UdpSocket.new(IpAddress.v4(0, 0, 0, 0), port: 0).get ``` What happens here is that we create two sockets that bind themselves to IP @@ -64,10 +64,10 @@ worry about using a port that's already in use. Next, we encounter the following: ```inko -let addr = server.local_address.unwrap +let addr = server.local_address.get -client.connect(addr.ip.unwrap, addr.port).unwrap -client.write_string('Hello, world!').unwrap +client.connect(addr.ip.get, addr.port).get +client.write_string('Hello, world!').get ``` Here we get the address of the server we need to connect the client to, which we @@ -79,14 +79,14 @@ We then read the data back from the server: ```inko let bytes = ByteArray.new -server.read(into: bytes, size: 32).unwrap -stdout.write_bytes(bytes).unwrap +server.read(into: bytes, size: 32).get +stdout.write_bytes(bytes).get ``` When using sockets you shouldn't use `read_all` as we did in the files tutorial, because `read_all` won't return until the socket is disconnected. -Just as in the files tutorial, we use `unwrap` to handle errors for the sake of +Just as in the files tutorial, we use `get` to handle errors for the sake of brevity. ## Using TCP sockets @@ -102,12 +102,12 @@ import std.stdio.STDOUT class async Main { fn async main { let stdout = STDOUT.new - let server = TcpServer.new(IpAddress.v4(0, 0, 0, 0), port: 9999).unwrap - let client = server.accept.unwrap + let server = TcpServer.new(IpAddress.v4(0, 0, 0, 0), port: 9999).get + let client = server.accept.get let bytes = ByteArray.new - client.read(into: bytes, size: 32).unwrap - stdout.write_bytes(bytes).unwrap + client.read(into: bytes, size: 32).get + stdout.write_bytes(bytes).get } } ``` @@ -124,9 +124,9 @@ import std.net.socket.TcpClient class async Main { fn async main { - let client = TcpClient.new(IpAddress.v4(0, 0, 0, 0), port: 9999).unwrap + let client = TcpClient.new(IpAddress.v4(0, 0, 0, 0), port: 9999).get - client.write_string('Hello, world!').unwrap + client.write_string('Hello, world!').get } } ``` diff --git a/inko/src/command/test.rs b/inko/src/command/test.rs index 3a1e02bd6..2859b6f64 100644 --- a/inko/src/command/test.rs +++ b/inko/src/command/test.rs @@ -150,7 +150,9 @@ fn generate_main_test_module(tests: Vec) -> String { source.push_str(&line); } - source.push_str(" tests.filter = Filter.from_string(env.arguments.opt(0).unwrap_or(''))\n"); + source.push_str( + " tests.filter = Filter.from_string(env.arguments.opt(0).or(''))\n", + ); source.push_str(" tests.run\n"); source.push_str(" }\n"); source.push_str("}\n"); diff --git a/std/src/std/fs/file.inko b/std/src/std/fs/file.inko index f301ec7cb..0ec178944 100644 --- a/std/src/std/fs/file.inko +++ b/std/src/std/fs/file.inko @@ -95,7 +95,7 @@ class pub ReadOnlyFile { # # import std.fs.file.ReadOnlyFile # - # let handle = ReadOnlyFile.new('/dev/null').unwrap + # let handle = ReadOnlyFile.new('/dev/null').get fn pub static new[T: ToPath](path: ref T) -> Result[ReadOnlyFile, Error] { let path = path.to_path @@ -157,7 +157,7 @@ class pub WriteOnlyFile { # # import std.fs.file.WriteOnlyFile # - # let file = WriteOnlyFile.new('/dev/null').unwrap + # let file = WriteOnlyFile.new('/dev/null').get fn pub static new[T: ToPath](path: ref T) -> Result[WriteOnlyFile, Error] { let path = path.to_path @@ -177,7 +177,7 @@ class pub WriteOnlyFile { # # import std.fs.file.WriteOnlyFile # - # let file = WriteOnlyFile.append('/dev/null').unwrap + # let file = WriteOnlyFile.append('/dev/null').get fn pub static append[T: ToPath](path: ref T) -> Result[WriteOnlyFile, Error] { let path = path.to_path @@ -246,7 +246,7 @@ class pub ReadWriteFile { # # import std.fs.file.ReadWriteFile # - # let handle = ReadWriteFile.new('/dev/null').unwrap + # let handle = ReadWriteFile.new('/dev/null').get fn pub static new[T: ToPath](path: ref T) -> Result[ReadWriteFile, Error] { let path = path.to_path @@ -266,7 +266,7 @@ class pub ReadWriteFile { # # import std.fs.file.ReadWriteFile # - # let handle = ReadWriteFile.append('/dev/null').unwrap + # let handle = ReadWriteFile.append('/dev/null').get fn pub static append[T: ToPath](path: ref T) -> Result[ReadWriteFile, Error] { let path = path.to_path diff --git a/std/src/std/fs/path.inko b/std/src/std/fs/path.inko index 73df7be51..2f763392c 100644 --- a/std/src/std/fs/path.inko +++ b/std/src/std/fs/path.inko @@ -305,7 +305,7 @@ class pub Path { # # let path = Path.new('README.md') # - # path.created_at.unwrap # => DateTime { ... } + # path.created_at.get # => DateTime { ... } fn pub created_at -> Result[DateTime, Error] { match inko_path_created_at(_INKO.process, @path) { case { @tag = 0, @value = val } -> Result.Ok(DateTime.from_timestamp( @@ -331,7 +331,7 @@ class pub Path { # # let path = Path.new('README.md') # - # path.modified_at.unwrap # => DateTime { ... } + # path.modified_at.get # => DateTime { ... } fn pub modified_at -> Result[DateTime, Error] { match inko_path_modified_at(_INKO.process, @path) { case { @tag = 0, @value = val } -> Result.Ok(DateTime.from_timestamp( @@ -357,7 +357,7 @@ class pub Path { # # let path = Path.new('README.md') # - # path.accessed_at.unwrap # => DateTime { ... } + # path.accessed_at.get # => DateTime { ... } fn pub accessed_at -> Result[DateTime, Error] { match inko_path_accessed_at(_INKO.process, @path) { case { @tag = 0, @value = val } -> Result.Ok(DateTime.from_timestamp( @@ -599,7 +599,7 @@ class pub Path { # # import std.fs.path.Path # - # Path.new('/foo/../bar').expand.unwrap # => Path.new('/bar') + # Path.new('/foo/../bar').expand.get # => Path.new('/bar') fn pub expand -> Result[Path, Error] { match inko_path_expand(_INKO.state, @path) { case { @tag = 0, @value = v } -> Result.Ok(Path.new(v as String)) @@ -633,7 +633,7 @@ class pub Path { # # let out = STDOUT.new # let path = Path.new('.') - # let iter = path.list.expect('failed to create the iterator') + # let iter = path.list.or_panic('failed to create the iterator') # # iter.each fn (result) { # match result { @@ -666,7 +666,7 @@ class pub Path { # # import std.fs.path.Path # - # Path.new('/tmp').list_all.unwrap.next + # Path.new('/tmp').list_all.get.next # # => Option.Some(Result.Ok(DirectoryEntry { ... })) fn pub list_all -> Result[Stream[Result[DirectoryEntry, Error]], Error] { list.map fn (iter) { @@ -702,10 +702,10 @@ class pub Path { # import std.fs.path.Path # # let path = Path.new('/tmp/test.txt') - # let handle = WriteOnlyFile.new(path).unwrap + # let handle = WriteOnlyFile.new(path).get # - # handle.write_string('hello').unwrap - # path.remove_file.unwrap + # handle.write_string('hello').get + # path.remove_file.get fn pub remove_file -> Result[Nil, Error] { match inko_file_remove(_INKO.process, @path) { case { @tag = 1, @value = _ } -> Result.Ok(nil) @@ -725,8 +725,8 @@ class pub Path { # # let path = Path.new('/tmp/foo') # - # path.create_directory.unwrap - # path.remove_directory.unwrap + # path.create_directory.get + # path.remove_directory.get fn pub remove_directory -> Result[Nil, Error] { match inko_directory_remove(_INKO.process, @path) { case { @tag = 1, @value = _ } -> Result.Ok(nil) @@ -751,8 +751,8 @@ class pub Path { # # import std.fs.path.Path # - # Path.new('/tmp/foo/bar').create_directory_all.unwrap - # Path.new('/tmp/foo').remove_directory_all.unwrap + # Path.new('/tmp/foo/bar').create_directory_all.get + # Path.new('/tmp/foo').remove_directory_all.get fn pub remove_directory_all -> Result[Nil, Error] { match inko_directory_remove_recursive(_INKO.process, @path) { case { @tag = 1, @value = _ } -> Result.Ok(nil) @@ -775,7 +775,7 @@ class pub Path { # # import std.fs.path.Path # - # Path.new('/tmp/test').create_directory.unwrap + # Path.new('/tmp/test').create_directory.get fn pub create_directory -> Result[Nil, Error] { match inko_directory_create(_INKO.process, @path) { case { @tag = 1, @value = _ } -> Result.Ok(nil) @@ -798,7 +798,7 @@ class pub Path { # # import std.fs.path.Path # - # Path.new('/tmp/foo/bar/test').create_directory_all.unwrap + # Path.new('/tmp/foo/bar/test').create_directory_all.get fn pub create_directory_all -> Result[Nil, Error] { match inko_directory_create_recursive(_INKO.process, @path) { case { @tag = 1, @value = _ } -> Result.Ok(nil) @@ -819,10 +819,10 @@ class pub Path { # import std.fs.path.Path # # let path = Path.new('/tmp/test.txt') - # let file = WriteOnlyFile.new(path).unwrap + # let file = WriteOnlyFile.new(path).get # - # file.write_string('hello').unwrap - # path.copy(to: '/tmp/test2.txt').unwrap + # file.write_string('hello').get + # path.copy(to: '/tmp/test2.txt').get fn pub copy[T: ToString](to: ref T) -> Result[Int, Error] { match inko_file_copy(_INKO.process, @path, to.to_string) { case { @tag = 0, @value = v } -> Result.Ok(v as Int) @@ -942,7 +942,7 @@ impl Size for Path { # # let path = Path.new('/dev/null') # - # path.size.unwrap # => 0 + # path.size.get # => 0 fn pub size -> Result[Int, Error] { match inko_file_size(_INKO.process, @path) { case { @tag = 0, @value = v } -> Result.Ok(v) diff --git a/std/src/std/io.inko b/std/src/std/io.inko index 5d3118959..218879cc2 100644 --- a/std/src/std/io.inko +++ b/std/src/std/io.inko @@ -390,7 +390,7 @@ trait pub BufferedRead: Read { # import std.fs.file.ReadOnlyFile # import std.io.BufferedReader # - # let file = ReadOnlyFile.new('README.md').unwrap + # let file = ReadOnlyFile.new('README.md').get # let reader = BufferedReader.new(file) # # reader.read_byte # => Result.Ok(Option.Some(35)) @@ -407,7 +407,7 @@ trait pub BufferedRead: Read { # import std.fs.file.ReadOnlyFile # import std.io.BufferedReader # - # let file = ReadOnlyFile.new('README.md').unwrap + # let file = ReadOnlyFile.new('README.md').get # let reader = BufferedReader.new(file) # let bytes = ByteArray.new # @@ -441,7 +441,7 @@ trait pub BufferedRead: Read { # import std.fs.file.ReadOnlyFile # import std.io.BufferedReader # - # let file = ReadOnlyFile.new('README.md').unwrap + # let file = ReadOnlyFile.new('README.md').get # let reader = BufferedReader.new(file) # let bytes = ByteArray.new # @@ -459,7 +459,7 @@ trait pub BufferedRead: Read { # import std.fs.file.ReadOnlyFile # import std.io.BufferedReader # - # let file = ReadOnlyFile.new('README.md').unwrap + # let file = ReadOnlyFile.new('README.md').get # let reader = BufferedReader.new(file) # # reader.bytes.next # => Option.Some(Result.Ok(35)) diff --git a/std/src/std/json.inko b/std/src/std/json.inko index b329407d7..7169a98df 100644 --- a/std/src/std/json.inko +++ b/std/src/std/json.inko @@ -12,7 +12,7 @@ # # import std.json.Json # -# Json.parse('[10]').unwrap # => Json.Array([Json.Int(10)]) +# Json.parse('[10]').get # => Json.Array([Json.Int(10)]) # # The parser enforces limits on the number of nested objects and the size of # strings. These limits can be adjusted by using the `Parser` type directly like @@ -528,7 +528,7 @@ class pub Parser { # # let parser = Parser.new('[10, 20]') # - # parser.parse.unwrap # => Json.Array([Json.Int(10), Json.Int(20)]) + # parser.parse.get # => Json.Array([Json.Int(10), Json.Int(20)]) fn pub move parse -> Result[Json, Error] { let result = try value @@ -616,8 +616,8 @@ class pub Parser { throw error("'{high.format(Format.Hex)}' is an invalid UTF-16 surrogate") } - try expect(BSLASH) - try expect(LOWER_U) + try or_panic(BSLASH) + try or_panic(LOWER_U) let low = try codepoint @@ -712,7 +712,7 @@ class pub Parser { case Some(DQUOTE) -> { let key = try string_value - try expect(COLON) + try or_panic(COLON) let val = try value @@ -793,7 +793,7 @@ class pub Parser { # At this point we've already validated the input format, and it's # compatible with the underlying float parser, so no extra checks are # needed. - let res = Result.Ok(Json.Float(Float.parse(@buffer).unwrap)) + let res = Result.Ok(Json.Float(Float.parse(@buffer).get)) @buffer.clear res @@ -855,7 +855,7 @@ class pub Parser { res } - fn mut expect(byte: Int) -> Result[Nil, Error] { + fn mut or_panic(byte: Int) -> Result[Nil, Error] { match advance { case Some(val) if val == byte -> Result.Ok(nil) case Some(val) -> throw error( @@ -874,7 +874,7 @@ class pub Parser { let mut index = 0 let max = name.size - while index < max { try expect(name.byte(index := index + 1)) } + while index < max { try or_panic(name.byte(index := index + 1)) } Result.Ok(nil) } diff --git a/std/src/std/net/ip.inko b/std/src/std/net/ip.inko index a5f5349d5..50f0ec21c 100644 --- a/std/src/std/net/ip.inko +++ b/std/src/std/net/ip.inko @@ -229,7 +229,7 @@ class pub Ipv6Address { # # import std.net.ip.Ipv6Address # - # Ipv6Address.parse('::1').unwrap.v6? # => true + # Ipv6Address.parse('::1').get.v6? # => true fn pub static parse(input: String) -> Option[Ipv6Address] { let bytes = input.to_byte_array let mut cursor = 0 @@ -619,7 +619,7 @@ class pub Ipv4Address { # # import std.net.ip.Ipv4Address # - # let addr = Ipv4Address.parse('1.2.3.4').unwrap + # let addr = Ipv4Address.parse('1.2.3.4').get # # addr.v4? # => true fn pub static parse(input: String) -> Option[Ipv4Address] { diff --git a/std/src/std/net/socket.inko b/std/src/std/net/socket.inko index 15a7866cd..50e4233c8 100644 --- a/std/src/std/net/socket.inko +++ b/std/src/std/net/socket.inko @@ -40,12 +40,12 @@ # import std.net.socket.TcpServer # import std.time.Duration # -# let server = TcpServer.new(ip: IpAddress.v4(0, 0, 0, 0), port: 9000).unwrap +# let server = TcpServer.new(ip: IpAddress.v4(0, 0, 0, 0), port: 9000).get # # server.socket.timeout_after = Duration.from_secs(3) # # # This times out after roughly three seconds. -# server.accept.unwrap +# server.accept.get # # For more information about timeouts versus deadlines, consider reading [this # article](https://vorpus.org/blog/timeouts-and-cancellation-for-humans/). @@ -331,7 +331,7 @@ class pub Socket { # # import std.net.socket.(Type, Socket) # - # Socket.ipv4(Type.DGRAM).unwrap + # Socket.ipv4(Type.DGRAM).get fn pub static ipv4(type: Type) -> Result[Socket, Error] { let sock = RawSocket { @inner = 0 as Int32, @@ -351,7 +351,7 @@ class pub Socket { # # import std.net.socket.(Type, Socket) # - # Socket.ipv6(Type.DGRAM).unwrap + # Socket.ipv6(Type.DGRAM).get fn pub static ipv6(type: Type) -> Result[Socket, Error] { let sock = RawSocket { @inner = 0 as Int32, @@ -406,9 +406,9 @@ class pub Socket { # import std.net.socket.(Socket, Type) # import std.net.ip.IpAddress # - # let socket = Socket.ipv4(Type.DGRAM).unwrap + # let socket = Socket.ipv4(Type.DGRAM).get # - # socket.bind(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).unwrap + # socket.bind(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).get fn pub mut bind[T: ToString](ip: ref T, port: Int) -> Result[Nil, Error] { match inko_socket_bind(@raw, ip.to_string, port) { case { @tag = 1, @value = _ } -> Result.Ok(nil) @@ -425,12 +425,12 @@ class pub Socket { # import std.net.socket.(Socket, Type) # import std.net.ip.IpAddress # - # let listener = Socket.ipv4(Type.STREAM).unwrap - # let client = Socket.ipv4(Type.STREAM).unwrap + # let listener = Socket.ipv4(Type.STREAM).get + # let client = Socket.ipv4(Type.STREAM).get # - # socket.bind(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).unwrap - # socket.listen.unwrap - # client.connect(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).unwrap + # socket.bind(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).get + # socket.listen.get + # client.connect(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).get fn pub mut connect[T: ToString](ip: ref T, port: Int) -> Result[Nil, Error] { match inko_socket_connect( _INKO.state, _INKO.process, @raw, ip.to_string, port, @deadline @@ -450,10 +450,10 @@ class pub Socket { # import std.net.socket.(Socket, Type) # import std.net.ip.IpAddress # - # let socket = Socket.ipv4(Type.STREAM).unwrap + # let socket = Socket.ipv4(Type.STREAM).get # - # socket.bind(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).unwrap - # socket.listen.unwrap + # socket.bind(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).get + # socket.listen.get fn pub mut listen -> Result[Nil, Error] { match inko_socket_listen(@raw, MAXIMUM_LISTEN_BACKLOG) { case { @tag = 1, @value = _ } -> Result.Ok(nil) @@ -472,19 +472,19 @@ class pub Socket { # import std.net.socket.(Socket, Type) # import std.net.ip.IpAddress # - # let listener = Socket.ipv4(Type.STREAM).unwrap - # let stream = Socket.ipv4(Type.STREAM).unwrap + # let listener = Socket.ipv4(Type.STREAM).get + # let stream = Socket.ipv4(Type.STREAM).get # - # listener.bind(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).unwrap - # listener.listen.unwrap + # listener.bind(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).get + # listener.listen.get # - # stream.connect(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).unwrap - # stream.write_string('ping').unwrap + # stream.connect(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).get + # stream.write_string('ping').get # - # let client = listener.accept.unwrap + # let client = listener.accept.get # let buffer = ByteArray.new # - # client.read(into: buffer, size: 4).unwrap + # client.read(into: buffer, size: 4).get # # buffer.to_string # => 'ping' fn pub accept -> Result[Socket, Error] { @@ -511,16 +511,16 @@ class pub Socket { # import std.net.socket.(Socket, Type) # import std.net.ip.IpAddress # - # let socket = Socket.ipv4(Type.DGRAM).unwrap + # let socket = Socket.ipv4(Type.DGRAM).get # - # socket.bind(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).unwrap + # socket.bind(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).get # socket # .send_string_to( # string: 'hello', # ip: IpAddress.v4(0, 0, 0, 0), # port: 9999 # ) - # .unwrap + # .get fn pub mut send_string_to[T: ToString]( string: String, ip: ref T, @@ -543,17 +543,17 @@ class pub Socket { # import std.net.socket.(Socket, Type) # import std.net.ip.IpAddress # - # let socket = Socket.ipv4(Type.DGRAM).unwrap + # let socket = Socket.ipv4(Type.DGRAM).get # let bytes = 'hello'.to_byte_array # - # socket.bind(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).unwrap + # socket.bind(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).get # socket # .send_bytes_to( # bytes: bytes, # ip: IpAddress.v4(0, 0, 0, 0), # port: 9999 # ) - # .unwrap + # .get fn pub mut send_bytes_to[T: ToString]( bytes: ref ByteArray, ip: ref T, @@ -580,7 +580,7 @@ class pub Socket { # import std.net.socket.(Socket, Type) # import std.net.ip.IpAddress # - # let socket = Socket.ipv4(Type.DGRAM).unwrap + # let socket = Socket.ipv4(Type.DGRAM).get # let bytes = ByteArray.new # # socket @@ -589,9 +589,9 @@ class pub Socket { # ip: IpAddress.v4(0, 0, 0, 0), # port: 9999 # ) - # .unwrap + # .get # - # let received_from = socket.receive_from(bytes: bytes, size: 5).unwrap + # let received_from = socket.receive_from(bytes: bytes, size: 5).get # # bytes.to_string # => 'hello' # received_from.address # => '0.0.0.0' @@ -813,9 +813,9 @@ class pub UdpSocket { # import std.net.socket.UdpSocket # import std.net.ip.IpAddress # - # let ip = IpAddress.parse('0.0.0.0').unwrap + # let ip = IpAddress.parse('0.0.0.0').get # - # UdpSocket.new(ip, port: 0).unwrap + # UdpSocket.new(ip, port: 0).get fn pub static new(ip: ref IpAddress, port: Int) -> Result[UdpSocket, Error] { let socket = if ip.v6? { try Socket.ipv6(Type.DGRAM) @@ -841,11 +841,11 @@ class pub UdpSocket { # import std.net.ip.IpAddress # # let socket1 = - # UdpSocket.new(ip: IpAddress.v4(0, 0, 0, 0), port: 40_000).unwrap + # UdpSocket.new(ip: IpAddress.v4(0, 0, 0, 0), port: 40_000).get # let socket2 = - # UdpSocket.new(ip: IpAddress.v4(0, 0, 0, 0), port: 41_000).unwrap + # UdpSocket.new(ip: IpAddress.v4(0, 0, 0, 0), port: 41_000).get # - # socket1.connect(ip: IpAddress.v4(0, 0, 0, 0), port: 41_000).unwrap + # socket1.connect(ip: IpAddress.v4(0, 0, 0, 0), port: 41_000).get fn pub mut connect[T: ToString](ip: ref T, port: Int) -> Result[Nil, Error] { @socket.connect(ip, port) } @@ -860,7 +860,7 @@ class pub UdpSocket { # import std.net.ip.IpAddress # # let socket = - # UdpSocket.new(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).unwrap + # UdpSocket.new(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).get # # socket # .send_string_to( @@ -868,7 +868,7 @@ class pub UdpSocket { # ip: IpAddress.v4(0, 0, 0, 0), # port: 9999 # ) - # .unwrap + # .get fn pub mut send_string_to[T: ToString]( string: String, ip: ref T, @@ -887,7 +887,7 @@ class pub UdpSocket { # import std.net.ip.IpAddress # # let socket = - # UdpSocket.new(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).unwrap + # UdpSocket.new(ip: IpAddress.v4(0, 0, 0, 0), port: 9999).get # let bytes = 'hello'.to_byte_array # # socket @@ -896,7 +896,7 @@ class pub UdpSocket { # ip: IpAddress.v4(0, 0, 0, 0), # port: 9999 # ) - # .unwrap + # .get fn pub mut send_bytes_to[T: ToString]( bytes: ref ByteArray, ip: ref T, @@ -971,9 +971,9 @@ class pub TcpClient { # import std.net.socket.TcpClient # import std.net.ip.IpAddress # - # let ip = IpAddress.parse('127.0.0.1').unwrap + # let ip = IpAddress.parse('127.0.0.1').get # - # TcpClient.new(ip, port: 40_000).unwrap + # TcpClient.new(ip, port: 40_000).get fn pub static new(ip: ref IpAddress, port: Int) -> Result[TcpClient, Error] { let socket = if ip.v6? { try Socket.ipv6(Type.STREAM) @@ -1005,7 +1005,7 @@ class pub TcpClient { # port: 40_000, # timeout_after: Duration.from_secs(5) # ) - # .unwrap + # .get fn pub static with_timeout[T: ToInstant]( ip: ref IpAddress, port: Int, @@ -1102,9 +1102,9 @@ class pub TcpServer { # import std.net.socket.TcpServer # import std.net.ip.IpAddress # - # let ip = IpAddress.parse('0.0.0.0').unwrap + # let ip = IpAddress.parse('0.0.0.0').get # - # TcpServer.new(ip, port: 40_000).unwrap + # TcpServer.new(ip, port: 40_000).get fn pub static new(ip: ref IpAddress, port: Int) -> Result[TcpServer, Error] { let socket = if ip.v6? { try Socket.ipv6(Type.STREAM) @@ -1131,16 +1131,16 @@ class pub TcpServer { # import std.net.ip.IpAddress # # let listener = - # TcpServer.new(ip: IpAddress.v4(127, 0, 0, 1), port: 40_000).unwrap + # TcpServer.new(ip: IpAddress.v4(127, 0, 0, 1), port: 40_000).get # let client = - # TcpClient.new(ip: IpAddress.v4(127, 0, 0, 1), port: 40_000).unwrap + # TcpClient.new(ip: IpAddress.v4(127, 0, 0, 1), port: 40_000).get # # client.write_string('ping') # - # let connection = listener.accept.unwrap + # let connection = listener.accept.get # let buffer = ByteArray.new # - # connection.read(into: buffer, size: 4).unwrap + # connection.read(into: buffer, size: 4).get # # buffer.to_string # => 'ping' fn pub accept -> Result[TcpClient, Error] { @@ -1299,7 +1299,7 @@ class pub UnixSocket { # # import std.net.socket.(Type, UnixSocket) # - # UnixSocket.new(Type.DGRAM).unwrap + # UnixSocket.new(Type.DGRAM).get fn pub static new(type: Type) -> Result[UnixSocket, Error] { let sock = RawSocket { @inner = 0 as Int32, @@ -1353,9 +1353,9 @@ class pub UnixSocket { # # import std.net.socket.(Type, UnixSocket) # - # let socket = UnixSocket.new(Type.DGRAM).unwrap + # let socket = UnixSocket.new(Type.DGRAM).get # - # socket.bind('/tmp/test.sock').unwrap + # socket.bind('/tmp/test.sock').get fn pub mut bind[T: ToString](path: ref T) -> Result[Nil, Error] { match inko_socket_bind(@raw, path.to_string, 0) { case { @tag = 1, @value = _ } -> Result.Ok(nil) @@ -1371,13 +1371,13 @@ class pub UnixSocket { # # import std.net.socket.(Type, UnixSocket) # - # let listener = UnixSocket.new(Type.STREAM).unwrap - # let stream = UnixSocket.new(Type.STREAM).unwrap + # let listener = UnixSocket.new(Type.STREAM).get + # let stream = UnixSocket.new(Type.STREAM).get # - # listener.bind('/tmp/test.sock').unwrap - # listener.listen.unwrap + # listener.bind('/tmp/test.sock').get + # listener.listen.get # - # stream.connect('/tmp/test.sock').unwrap + # stream.connect('/tmp/test.sock').get fn pub mut connect[T: ToString](path: ref T) -> Result[Nil, Error] { match inko_socket_connect( _INKO.state, _INKO.process, @raw, path.to_string, 0, @deadline @@ -1396,10 +1396,10 @@ class pub UnixSocket { # # import std.net.socket.(Type, UnixSocket) # - # let socket = UnixSocket.new(Type.STREAM).unwrap + # let socket = UnixSocket.new(Type.STREAM).get # - # socket.bind('/tmp/test.sock').unwrap - # socket.listen.unwrap + # socket.bind('/tmp/test.sock').get + # socket.listen.get fn pub mut listen -> Result[Nil, Error] { match inko_socket_listen(@raw, MAXIMUM_LISTEN_BACKLOG) { case { @tag = 1, @value = _ } -> Result.Ok(nil) @@ -1417,19 +1417,19 @@ class pub UnixSocket { # # import std.net.socket.(Type, UnixSocket) # - # let listener = UnixSocket.new(Type.STREAM).unwrap - # let stream = UnixSocket.new(Type.STREAM).unwrap + # let listener = UnixSocket.new(Type.STREAM).get + # let stream = UnixSocket.new(Type.STREAM).get # - # listener.bind('/tmp/test.sock').unwrap - # listener.listen.unwrap + # listener.bind('/tmp/test.sock').get + # listener.listen.get # - # stream.connect('/tmp/test.sock').unwrap - # stream.write_string('ping').unwrap + # stream.connect('/tmp/test.sock').get + # stream.write_string('ping').get # - # let client = listener.accept.unwrap + # let client = listener.accept.get # let buffer = ByteArray.new # - # client.read(into: buffer, size: 4).unwrap + # client.read(into: buffer, size: 4).get # # buffer.to_string # => 'ping' fn pub accept -> Result[UnixSocket, Error] { @@ -1455,10 +1455,10 @@ class pub UnixSocket { # # import std.net.socket.(Type, UnixSocket) # - # let socket = UnixSocket.new(Type.DGRAM).unwrap + # let socket = UnixSocket.new(Type.DGRAM).get # - # socket.bind('/tmp/test.sock').unwrap - # socket.send_string_to(string: 'hello', address: '/tmp/test.sock').unwrap + # socket.bind('/tmp/test.sock').get + # socket.send_string_to(string: 'hello', address: '/tmp/test.sock').get fn pub mut send_string_to[T: ToString]( string: String, address: ref T @@ -1481,11 +1481,11 @@ class pub UnixSocket { # # import std.net.socket.(Type, UnixSocket) # - # let socket = UnixSocket.new(Type.DGRAM).unwrap + # let socket = UnixSocket.new(Type.DGRAM).get # let bytes = 'hello'.to_byte_array # - # socket.bind('/tmp/test.sock').unwrap - # socket.send_bytes_to(bytes: bytes, address: '/tmp/test.sock').unwrap + # socket.bind('/tmp/test.sock').get + # socket.send_bytes_to(bytes: bytes, address: '/tmp/test.sock').get fn pub mut send_bytes_to[T: ToString]( bytes: ref ByteArray, address: ref T @@ -1512,12 +1512,12 @@ class pub UnixSocket { # # import std.net.socket.(Type, UnixSocket) # - # let socket = UnixSocket.new(Type.DGRAM).unwrap + # let socket = UnixSocket.new(Type.DGRAM).get # let bytes = ByteArray.new # - # socket.send_string_to('hello', address: '/tmp/test.sock').unwrap + # socket.send_string_to('hello', address: '/tmp/test.sock').get # - # let received_from = socket.receive_from(bytes: bytes, size: 5).unwrap + # let received_from = socket.receive_from(bytes: bytes, size: 5).get # # bytes.to_string # => 'hello' # received_from.to_string # => '/tmp/test.sock' @@ -1667,7 +1667,7 @@ class pub UnixDatagram { # # import std.net.socket.UnixDatagram # - # UnixDatagram.new('/tmp/test.sock').unwrap + # UnixDatagram.new('/tmp/test.sock').get fn pub static new[T: ToString]( address: ref T ) -> Result[UnixDatagram, Error] { @@ -1689,10 +1689,10 @@ class pub UnixDatagram { # # import std.net.socket.UnixDatagram # - # let socket1 = UnixDatagram.new('/tmp/test1.sock').unwrap - # let socket2 = UnixDatagram.new('/tmp/test2.sock').unwrap + # let socket1 = UnixDatagram.new('/tmp/test1.sock').get + # let socket2 = UnixDatagram.new('/tmp/test2.sock').get # - # socket1.connect('/tmp/test2.sock').unwrap + # socket1.connect('/tmp/test2.sock').get fn pub mut connect[T: ToString](address: ref T) -> Result[Nil, Error] { @socket.connect(address) } @@ -1705,9 +1705,9 @@ class pub UnixDatagram { # # import std.net.socket.UnixDatagram # - # let socket = UnixDatagram.new('/tmp/test.sock').unwrap + # let socket = UnixDatagram.new('/tmp/test.sock').get # - # socket.send_string_to(string: 'hello', address: '/tmp/test.sock').unwrap + # socket.send_string_to(string: 'hello', address: '/tmp/test.sock').get fn pub mut send_string_to[T: ToString]( string: String, address: ref T @@ -1723,10 +1723,10 @@ class pub UnixDatagram { # # import std.net.socket.UnixDatagram # - # let socket = UnixDatagram.new('/tmp/test.sock').unwrap + # let socket = UnixDatagram.new('/tmp/test.sock').get # let bytes = 'hello'.to_byte_array # - # socket.send_bytes_to(bytes: bytes, address: '/tmp/test.sock').unwrap + # socket.send_bytes_to(bytes: bytes, address: '/tmp/test.sock').get fn pub mut send_bytes_to[T: ToString]( bytes: ref ByteArray, address: ref T @@ -1798,9 +1798,9 @@ class pub UnixClient { # # import std.net.socket.(UnixServer, UnixClient) # - # let listener = UnixServer.new('/tmp/test.sock').unwrap + # let listener = UnixServer.new('/tmp/test.sock').get # - # UnixClient.new('/tmp/test.sock').unwrap + # UnixClient.new('/tmp/test.sock').get fn pub static new[T: ToString](address: ref T) -> Result[UnixClient, Error] { let socket = try UnixSocket.new(Type.STREAM) @@ -1826,7 +1826,7 @@ class pub UnixClient { # address: '/tmp/test.sock' # timeout_after: Duration.from_secs(5) # ) - # .unwrap + # .get fn pub static with_timeout[A: ToString, I: ToInstant]( address: ref A, timeout_after: I @@ -1912,7 +1912,7 @@ class pub UnixServer { # # import std.net.socket.UnixServer # - # UnixServer.new('/tmp/test.sock').unwrap + # UnixServer.new('/tmp/test.sock').get fn pub static new[T: ToString](address: ref T) -> Result[UnixServer, Error] { let socket = try UnixSocket.new(Type.STREAM) @@ -1931,15 +1931,15 @@ class pub UnixServer { # # import std.net.socket.(UnixServer, UnixClient) # - # let listener = UnixServer.new('/tmp/test.sock').unwrap - # let client = UnixClient.new('/tmp/test.sock').unwrap + # let listener = UnixServer.new('/tmp/test.sock').get + # let client = UnixClient.new('/tmp/test.sock').get # # client.write_string('ping') # - # let connection = listener.accept.unwrap + # let connection = listener.accept.get # let buffer = ByteArray.new # - # connection.read(into: buffer, size: 4).unwrap + # connection.read(into: buffer, size: 4).get # # buffer.to_string # => 'ping' fn pub accept -> Result[UnixClient, Error] { diff --git a/std/src/std/option.inko b/std/src/std/option.inko index 036178fc2..24af9e024 100644 --- a/std/src/std/option.inko +++ b/std/src/std/option.inko @@ -35,54 +35,50 @@ class pub enum Option[T] { } } - # Returns the wrapped value. - # - # This method panics when used with a `None`. + # Returns the value wrapped by `Some`, or panics if `self` is a `None`. # # # Examples # - # Option.Some(10).unwrap # => 10 - fn pub move unwrap -> T { - match self { - case Some(v) -> v - case _ -> panic("Option.unwrap can't unwrap a None") - } + # Option.Some(10).get # => 10 + fn pub move get -> T { + or_panic('Option.get expects a Some(_), but a None is found') } - # Returns the wrapped value, triggering a panic with a custom message for a - # `None`. + # Returns the value wrapped by `Some`, or panics with the given message if + # `self` is a `None`. # # # Examples # - # Option.Some(10).expect('a number must be present') # => 10 - fn pub move expect(message: String) -> T { + # Option.Some(10).or_panic('a number must be present') # => 10 + fn pub move or_panic(message: String) -> T { match self { case Some(v) -> v case _ -> panic(message) } } - # Returns the wrapped value for a `Some`, or the argument for a `None`. + # Returns the value wrapped by `Some`, or returns `default` if `self` is a + # `None`. # # # Examples # - # Option.Some(10).unwrap_or(0) # => 10 - # Option.None.unwrap_or(0) # => 0 - fn pub move unwrap_or(default: T) -> T { + # Option.Some(10).or(0) # => 10 + # Option.None.or(0) # => 0 + fn pub move or(default: T) -> T { match self { case Some(v) -> v case None -> default } } - # Returns the wrapped value for a `Some`, or calls the closure and returns its - # value. + # Returns the value wrapped by `Some`, or returns the closure's return value + # if `self` is a `None`. # # # Examples # - # Option.Some(10).unwrap_or_else fn { 0 } # => 10 - # Option.None.unwrap_or_else fn { 0 } # => 0 - fn pub move unwrap_or_else(block: fn -> T) -> T { + # Option.Some(10).or_else fn { 0 } # => 10 + # Option.None.or_else fn { 0 } # => 0 + fn pub move or_else(block: fn -> T) -> T { match self { case Some(v) -> v case None -> block.call diff --git a/std/src/std/result.inko b/std/src/std/result.inko index c13f195f7..8df876bed 100644 --- a/std/src/std/result.inko +++ b/std/src/std/result.inko @@ -102,7 +102,8 @@ class pub enum Result[T, E] { } } - # Unwraps `self` into its `Ok` value. + # Returns the value wrapped by the `Ok` case, or panics if `self` is an + # `Error`. # # # Panics # @@ -112,60 +113,59 @@ class pub enum Result[T, E] { # # let res: Result[Int, String] = Result.Ok(42) # - # res.unwrap # => 42 - fn pub move unwrap -> T { - match self { - case Ok(val) -> val - case _ -> panic("Result.unwrap can't unwrap an Error") - } + # res.get # => 42 + fn pub move get -> T { + or_panic('Result.get expects an Ok(_), but an Error(_) is found') } - # Returns the wrapped `Ok` value, triggering a panic with a custom message for - # a `Error`. + # Returns the value wrapped by `Ok`, or panics with the given message if + # `self` is an `Error`. # # # Examples # # let res: Result[Int, String] = Result.Ok(10) # - # res.expect('a number must be present') # => 10 - fn pub move expect(message: String) -> T { + # res.or_panic('a number must be present') # => 10 + fn pub move or_panic(message: String) -> T { match self { case Ok(v) -> v case _ -> panic(message) } } - # Unwraps `self` into its `Ok` value, returning a default value if `self` is - # an `Error`. + # Returns the value wrapped by `Ok`, or returns `default` if `self` is an + # `Error`. # # # Examples # # let foo: Result[Int, String] = Result.Ok(42) # let bar: Result[Int, String] = Result.Error('oops!') # - # foo.unwrap_or(0) # => 42 - # bar.unwrap_or(0) # => 0 - fn pub move unwrap_or(default: T) -> T { + # foo.or(0) # => 42 + # bar.or(0) # => 0 + fn pub move or(default: T) -> T { match self { case Ok(val) -> val case _ -> default } } - # Unwraps `self` into its `Ok` value, returning a default value if `self` is - # an `Error`. + # Returns the value wrapped by `Ok`, or returns the closure's return value if + # `self` is an `Error`. + # + # The argument passed to the closure is the value wrapped by the `Error` case. # # # Examples # # let foo: Result[Int, String] = Result.Ok(42) # let bar: Result[Int, String] = Result.Error('oops!') # - # foo.unwrap_or_else fn { 0 } # => 42 - # bar.unwrap_or_else fn { 0 } # => 0 - fn pub move unwrap_or_else(block: fn -> T) -> T { + # foo.or_else fn (_) { 0 } # => 42 + # bar.or_else fn (_) { 0 } # => 0 + fn pub move or_else(block: fn (E) -> T) -> T { match self { - case Ok(val) -> val - case _ -> block.call + case Ok(v) -> v + case Error(v) -> block.call(v) } } diff --git a/std/src/std/sys.inko b/std/src/std/sys.inko index e2038aeae..f097b0b54 100644 --- a/std/src/std/sys.inko +++ b/std/src/std/sys.inko @@ -139,13 +139,13 @@ impl ToInt for Stream { # # import std.sys.(Command, Stream) # -# Command.new('ls').stdout(Stream.Piped).spawn.unwrap +# Command.new('ls').stdout(Stream.Piped).spawn.get # # We can also ignore a stream: # # import std.sys.(Command, Stream) # -# Command.new('ls').stderr(Stream.Null).spawn.unwrap +# Command.new('ls').stderr(Stream.Null).spawn.get # # # Waiting for the child process # @@ -155,9 +155,9 @@ impl ToInt for Stream { # # import std.sys.Command # -# let child = Command.new('ls').unwrap +# let child = Command.new('ls').get # -# child.wait.unwrap +# child.wait.get # # There's also `ChildProcess.try_wait`, which returns immediately if the process # is still running; instead of waiting for it to finish. @@ -168,11 +168,11 @@ impl ToInt for Stream { # # import std.sys.Command # -# let child = Command.new('ls').unwrap +# let child = Command.new('ls').get # let bytes = ByteArray.new # -# child.wait.unwrap -# child.stdout.read_all(bytes).unwrap +# child.wait.get +# child.stdout.read_all(bytes).get class pub Command { # The path to the program to spawn. let @program: String @@ -329,9 +329,9 @@ class pub Command { # # # Examples # - # let child = Command.new('ls').spawn.unwrap + # let child = Command.new('ls').spawn.get # - # child.wait.unwrap + # child.wait.get fn pub spawn -> Result[ChildProcess, Error] { let vars = [] @@ -350,7 +350,7 @@ class pub Command { @stdin.to_int, @stdout.to_int, @stderr.to_int, - @directory.as_ref.unwrap_or(ref ''), + @directory.as_ref.or(ref ''), ) { case { @tag = 0, @value = v } -> Result.Ok( ChildProcess { @raw = v as Pointer[UInt8] } diff --git a/std/src/std/test.inko b/std/src/std/test.inko index 2fdbfeeb1..e4c89ba25 100644 --- a/std/src/std/test.inko +++ b/std/src/std/test.inko @@ -211,20 +211,20 @@ class pub Plain[T: mut + Write] { impl Reporter for Plain { fn pub mut passed(test: Test) { @tests += 1 - @out.write_string(green('.')).unwrap - @out.flush.unwrap + @out.write_string(green('.')).get + @out.flush.get } fn pub mut failed(test: Test) { @tests += 1 @failed.push(test) - @out.write_string(red('F')).unwrap - @out.flush.unwrap + @out.write_string(red('F')).get + @out.flush.get } fn pub move finished(duration: Duration, seed: Int) -> Bool { if @failed.size > 0 { - @out.print("\n\nFailures:").unwrap + @out.print("\n\nFailures:").get @failed.iter.each_with_index fn (test_index, test) { test.failures.iter.each_with_index fn (failure_index, fail) { @@ -240,7 +240,7 @@ impl Reporter for Plain { {indent} {green("expected:")} {fail.expected} {indent} {red("got:")} {fail.got}" ) - .unwrap + .get } } } @@ -260,7 +260,7 @@ impl Reporter for Plain { .print( "\nFinished running {@tests} tests in {dur}, {failures}, seed: {seed}" ) - .unwrap + .get @failed.empty? } @@ -312,7 +312,7 @@ class pub Failure { let frame = debug .stacktrace(skip: 3) .pop - .expect('at least one stack frame must be present') + .or_panic('at least one stack frame must be present') match frame { case { @path = path, @line = line } -> Failure { @@ -385,7 +385,7 @@ class pub Process { let @stdin: String fn static new(id: Int) -> Process { - let cmd = Command.new(env.executable.unwrap) + let cmd = Command.new(env.executable.get) cmd.stdin(Stream.Piped) cmd.stdout(Stream.Piped) @@ -503,7 +503,7 @@ class pub Tests { .reverse_iter .find fn (frame) { frame.path.tail.starts_with?(TEST_PREFIX) } .else fn { trace.opt(trace.size - 1) } - .expect('at least one stack frame must be present') + .or_panic('at least one stack frame must be present') .clone Test.new(id, name, frame.path.clone, frame.line, code) @@ -519,12 +519,12 @@ class pub Tests { # This is useful when writing tests that perform operations that might fail # but in general shouldn't, such as connecting to a socket that's expected to # be available. Such tests could sporadically fail, resulting in any - # unwrapping terminating the entire test suite. Using this method, you can + # getping terminating the entire test suite. Using this method, you can # instead use the `try` operator. So instead of this: # # t.test('Foo') fn (t) { - # let a = foo.unwrap - # let b = bar(a).unwrap + # let a = foo.get + # let b = bar(a).get # ... # } # @@ -602,7 +602,7 @@ class pub Tests { if code != 101 { return } - let got = output.stderr.split("\n").last.unwrap_or('the process panicked') + let got = output.stderr.split("\n").last.or('the process panicked') test.failures.push(Failure.new(got, 'the process not to panic')) } diff --git a/std/test/compiler/test_diagnostics.inko b/std/test/compiler/test_diagnostics.inko index 7a026df14..9c635a3c1 100644 --- a/std/test/compiler/test_diagnostics.inko +++ b/std/test/compiler/test_diagnostics.inko @@ -77,10 +77,10 @@ fn check(compiler: String, name: String, file: Path) -> Array[Diagnostic] { cmd.argument(extra_src.to_string) } - let child = cmd.spawn.expect('failed to start the compiler') + let child = cmd.spawn.or_panic('failed to start the compiler') let output = ByteArray.new - child.wait.expect('failed to wait for the compiler') + child.wait.or_panic('failed to wait for the compiler') child.stderr.read_all(output) match parse_output(dir.to_string, output) { @@ -249,7 +249,7 @@ impl Format for Diagnostic { fn pub tests(t: mut Tests) { let base = env .working_directory - .unwrap_or_else fn { '.'.to_path } + .or_else fn (_) { '.'.to_path } .join('test') .join('diagnostics') @@ -257,7 +257,7 @@ fn pub tests(t: mut Tests) { base .list - .expect("the test/diagnostics directory couldn't be found") + .or_panic("the test/diagnostics directory couldn't be found") .each fn (entry) { let test_file = match entry { case Ok({ @path = path, @type = File }) @@ -271,7 +271,7 @@ fn pub tests(t: mut Tests) { t.test("{name} diagnostics") fn move (t) { let file = ReadOnlyFile .new(test_file.clone) - .expect("the test file {test_file} must exist") + .or_panic("the test file {test_file} must exist") match parse_test(file) { case Ok(exp) -> t.equal(check(compiler, name, test_file.clone), exp) diff --git a/std/test/std/fs/test_file.inko b/std/test/std/fs/test_file.inko index adf6f627b..c47c5591e 100644 --- a/std/test/std/fs/test_file.inko +++ b/std/test/std/fs/test_file.inko @@ -4,16 +4,16 @@ import std.fs.path.Path import std.test.Tests fn write(string: String, to: ref Path) { - let file = WriteOnlyFile.new(to.clone).unwrap + let file = WriteOnlyFile.new(to.clone).get - file.write_string(string).unwrap + file.write_string(string).get } fn read(from: ref Path) -> String { - let file = ReadOnlyFile.new(from.clone).unwrap + let file = ReadOnlyFile.new(from.clone).get let bytes = ByteArray.new - file.read_all(bytes).unwrap + file.read_all(bytes).get bytes.into_string } @@ -28,7 +28,7 @@ fn pub tests(t: mut Tests) { t.true(ReadOnlyFile.new(path.clone).ok?) - path.remove_file.unwrap + path.remove_file.get } t.test('ReadOnlyFile.read') fn (t) { @@ -36,14 +36,14 @@ fn pub tests(t: mut Tests) { write('test', to: path) - let handle = ReadOnlyFile.new(path.clone).unwrap + let handle = ReadOnlyFile.new(path.clone).get let bytes = ByteArray.new - handle.read(into: bytes, size: 4).unwrap + handle.read(into: bytes, size: 4).get t.equal(bytes.into_string, 'test') - path.remove_file.unwrap + path.remove_file.get } t.test('ReadOnlyFile.seek') fn (t) { @@ -51,15 +51,15 @@ fn pub tests(t: mut Tests) { write('test', to: path) - let handle = ReadOnlyFile.new(path.clone).unwrap + let handle = ReadOnlyFile.new(path.clone).get let bytes = ByteArray.new - handle.seek(1).unwrap - handle.read(into: bytes, size: 4).unwrap + handle.seek(1).get + handle.read(into: bytes, size: 4).get t.equal(bytes.into_string, 'est') - path.remove_file.unwrap + path.remove_file.get } t.test('ReadOnlyFile.size') fn (t) { @@ -67,11 +67,11 @@ fn pub tests(t: mut Tests) { write('test', to: path) - let handle = ReadOnlyFile.new(path.clone).unwrap + let handle = ReadOnlyFile.new(path.clone).get - t.true(handle.size.unwrap >= 0) + t.true(handle.size.get >= 0) - path.remove_file.unwrap + path.remove_file.get } t.test('WriteOnlyFile.new') fn (t) { @@ -80,7 +80,7 @@ fn pub tests(t: mut Tests) { t.true(WriteOnlyFile.new(path.clone).ok?) t.true(WriteOnlyFile.new(path.clone).ok?) - path.remove_file.unwrap + path.remove_file.get } t.test('WriteOnlyFile.append') fn (t) { @@ -89,72 +89,72 @@ fn pub tests(t: mut Tests) { t.true(WriteOnlyFile.append(path.clone).ok?) t.true(WriteOnlyFile.append(path.clone).ok?) - path.remove_file.unwrap + path.remove_file.get } t.test('WriteOnlyFile.write_bytes') fn (t) { let path = env.temporary_directory.join("inko-test-{t.id}") { - let handle = WriteOnlyFile.new(path.clone).unwrap + let handle = WriteOnlyFile.new(path.clone).get - handle.write_bytes('test'.to_byte_array).unwrap + handle.write_bytes('test'.to_byte_array).get } { - let handle = WriteOnlyFile.append(path.clone).unwrap + let handle = WriteOnlyFile.append(path.clone).get - handle.write_bytes('ing'.to_byte_array).unwrap + handle.write_bytes('ing'.to_byte_array).get } t.equal(read(path), 'testing') - path.remove_file.unwrap + path.remove_file.get } t.test('WriteOnlyFile.write_string') fn (t) { let path = env.temporary_directory.join("inko-test-{t.id}") { - let handle = WriteOnlyFile.new(path.clone).unwrap + let handle = WriteOnlyFile.new(path.clone).get - handle.write_string('test').unwrap + handle.write_string('test').get } { - let handle = WriteOnlyFile.append(path.clone).unwrap + let handle = WriteOnlyFile.append(path.clone).get - handle.write_string('ing').unwrap + handle.write_string('ing').get } t.equal(read(path), 'testing') - path.remove_file.unwrap + path.remove_file.get } t.test('WriteOnlyFile.flush') fn (t) { let path = env.temporary_directory.join("inko-test-{t.id}") - let handle = WriteOnlyFile.new(path.clone).unwrap + let handle = WriteOnlyFile.new(path.clone).get - handle.write_string('test').unwrap - handle.flush.unwrap + handle.write_string('test').get + handle.flush.get t.equal(read(path), 'test') - path.remove_file.unwrap + path.remove_file.get } t.test('WriteOnlyFile.seek') fn (t) { let path = env.temporary_directory.join("inko-test-{t.id}") - let handle = WriteOnlyFile.new(path.clone).unwrap + let handle = WriteOnlyFile.new(path.clone).get - handle.write_string('test').unwrap - handle.seek(1).unwrap - handle.write_string('ing').unwrap + handle.write_string('test').get + handle.seek(1).get + handle.write_string('ing').get t.equal(read(path), 'ting') - path.remove_file.unwrap + path.remove_file.get } t.test('ReadWriteFile.new') fn (t) { @@ -163,7 +163,7 @@ fn pub tests(t: mut Tests) { t.true(ReadWriteFile.new(path.clone).ok?) t.true(ReadWriteFile.new(path.clone).ok?) - path.remove_file.unwrap + path.remove_file.get } t.test('ReadWriteFile.append') fn (t) { @@ -172,7 +172,7 @@ fn pub tests(t: mut Tests) { t.true(ReadWriteFile.append(path.clone).ok?) t.true(ReadWriteFile.append(path.clone).ok?) - path.remove_file.unwrap + path.remove_file.get } t.test('ReadWriteFile.read') fn (t) { @@ -180,79 +180,79 @@ fn pub tests(t: mut Tests) { write('test', to: path) - let handle = ReadWriteFile.new(path.clone).unwrap + let handle = ReadWriteFile.new(path.clone).get let bytes = ByteArray.new - handle.read(bytes, size: 4).unwrap + handle.read(bytes, size: 4).get t.equal(bytes.to_string, 'test') - path.remove_file.unwrap + path.remove_file.get } t.test('ReadWriteFile.write_bytes') fn (t) { let path = env.temporary_directory.join("inko-test-{t.id}") { - let handle = ReadWriteFile.new(path.clone).unwrap + let handle = ReadWriteFile.new(path.clone).get - handle.write_bytes('test'.to_byte_array).unwrap + handle.write_bytes('test'.to_byte_array).get } { - let handle = ReadWriteFile.append(path.clone).unwrap + let handle = ReadWriteFile.append(path.clone).get - handle.write_bytes('ing'.to_byte_array).unwrap + handle.write_bytes('ing'.to_byte_array).get } t.equal(read(path), 'testing') - path.remove_file.unwrap + path.remove_file.get } t.test('ReadWriteFile.write_string') fn (t) { let path = env.temporary_directory.join("inko-test-{t.id}") { - let handle = ReadWriteFile.new(path.clone).unwrap + let handle = ReadWriteFile.new(path.clone).get - handle.write_string('test').unwrap + handle.write_string('test').get } { - let handle = ReadWriteFile.append(path.clone).unwrap + let handle = ReadWriteFile.append(path.clone).get - handle.write_string('ing').unwrap + handle.write_string('ing').get } t.equal(read(path), 'testing') - path.remove_file.unwrap + path.remove_file.get } t.test('ReadWriteFile.flush') fn (t) { let path = env.temporary_directory.join("inko-test-{t.id}") - let handle = ReadWriteFile.new(path.clone).unwrap + let handle = ReadWriteFile.new(path.clone).get - handle.write_string('test').unwrap - handle.flush.unwrap + handle.write_string('test').get + handle.flush.get t.equal(read(path), 'test') - path.remove_file.unwrap + path.remove_file.get } t.test('ReadWriteFile.seek') fn (t) { let path = env.temporary_directory.join("inko-test-{t.id}") - let handle = ReadWriteFile.new(path.clone).unwrap + let handle = ReadWriteFile.new(path.clone).get - handle.write_string('test').unwrap - handle.seek(1).unwrap - handle.write_string('ing').unwrap + handle.write_string('test').get + handle.seek(1).get + handle.write_string('ing').get t.equal(read(path), 'ting') - path.remove_file.unwrap + path.remove_file.get } t.test('ReadWriteFile.size') fn (t) { @@ -260,10 +260,10 @@ fn pub tests(t: mut Tests) { write('test', to: path) - let handle = ReadWriteFile.new(path.clone).unwrap + let handle = ReadWriteFile.new(path.clone).get - t.true(handle.size.unwrap >= 0) + t.true(handle.size.get >= 0) - path.remove_file.unwrap + path.remove_file.get } } diff --git a/std/test/std/fs/test_path.inko b/std/test/std/fs/test_path.inko index 5711b92b0..f2854f850 100644 --- a/std/test/std/fs/test_path.inko +++ b/std/test/std/fs/test_path.inko @@ -33,18 +33,18 @@ fn valid_date?(time: Result[DateTime, Error]) -> Bool { } fn read(from: ref Path) -> String { - let file = ReadOnlyFile.new(from.clone).unwrap + let file = ReadOnlyFile.new(from.clone).get let bytes = ByteArray.new - file.read_all(bytes).unwrap + file.read_all(bytes).get bytes.into_string } fn write(string: String, to: ref Path) { - let file = WriteOnlyFile.new(to.clone).unwrap + let file = WriteOnlyFile.new(to.clone).get - file.write_string(string).unwrap + file.write_string(string).get } fn strip_prefix(path: String, prefix: String) -> Option[Path] { @@ -63,7 +63,7 @@ fn pub tests(t: mut Tests) { write('test', to: path) t.true(path.file?) - path.remove_file.unwrap + path.remove_file.get } t.test('Path.directory?') fn (t) { @@ -149,7 +149,7 @@ fn pub tests(t: mut Tests) { } t.test('Path.size') fn (t) { - t.true(env.temporary_directory.size.unwrap >= 0) + t.true(env.temporary_directory.size.get >= 0) } t.test('Path.clone') fn (t) { @@ -168,7 +168,7 @@ fn pub tests(t: mut Tests) { let temp = env.temporary_directory let bar = temp.join('foo').join('bar') - bar.create_directory_all.unwrap + bar.create_directory_all.get let expanded = bar.join('..').join('..').expand @@ -200,12 +200,12 @@ fn pub tests(t: mut Tests) { with_directory(t.id) fn (root) { let foo = root.join('foo') - root.create_directory.unwrap - foo.create_directory.unwrap + root.create_directory.get + foo.create_directory.get let entry = root .list - .unwrap + .get .select_map fn (e) { e.ok } .find fn (e) { e.path == foo } @@ -226,7 +226,7 @@ fn pub tests(t: mut Tests) { let bar = foo.join('bar') let baz = bar.join('baz') - baz.create_directory_all.unwrap + baz.create_directory_all.get write('a', foo.join('a.txt')) write('b', foo.join('b.txt')) write('c', bar.join('c.txt')) @@ -240,7 +240,7 @@ fn pub tests(t: mut Tests) { .map fn (e) { match e { case { @path = p } -> p } } .to_array } - .unwrap_or_else fn { [] } + .or_else fn (_) { [] } t.equal(paths.size, 4) t.true(paths.contains?(foo.join('a.txt'))) @@ -269,7 +269,7 @@ fn pub tests(t: mut Tests) { with_directory(t.id) fn (path) { t.true(path.remove_directory.error?) - path.create_directory.unwrap + path.create_directory.get t.true(path.remove_directory.ok?) t.false(path.directory?) @@ -296,7 +296,7 @@ fn pub tests(t: mut Tests) { t.test('Path.remove_directory') fn (t) { with_directory(t.id) fn (path) { - path.create_directory.unwrap + path.create_directory.get t.true(path.remove_directory.ok?) t.true(path.remove_directory.error?) @@ -308,7 +308,7 @@ fn pub tests(t: mut Tests) { with_directory(t.id) fn (root) { let path = root.join('foo').join('bar') - path.create_directory_all.unwrap + path.create_directory_all.get t.true(root.remove_directory_all.ok?) t.true(root.remove_directory_all.error?) @@ -325,8 +325,8 @@ fn pub tests(t: mut Tests) { t.true(path1.copy(to: path2).ok?) t.equal(read(path2), 'test') - path1.remove_file.unwrap - path2.remove_file.unwrap + path1.remove_file.get + path2.remove_file.get } t.test('Path.extension') fn (t) { diff --git a/std/test/std/net/test_socket.inko b/std/test/std/net/test_socket.inko index e95a0e463..dd6f0337e 100644 --- a/std/test/std/net/test_socket.inko +++ b/std/test/std/net/test_socket.inko @@ -80,30 +80,30 @@ fn pub tests(t: mut Tests) { t.test('Socket.bind') fn (t) { { - let sock = Socket.ipv4(Type.STREAM).unwrap + let sock = Socket.ipv4(Type.STREAM).get t.true(sock.bind(ip: 'foo', port: 0).error?) } { - let sock = Socket.ipv4(Type.STREAM).unwrap + let sock = Socket.ipv4(Type.STREAM).get t.true(sock.bind(ip: '0.0.0.0', port: 0).ok?) } } t.test('Socket.connect') fn (t) { - let listener = Socket.ipv4(Type.STREAM).unwrap - let stream1 = Socket.ipv4(Type.STREAM).unwrap + let listener = Socket.ipv4(Type.STREAM).get + let stream1 = Socket.ipv4(Type.STREAM).get - listener.bind(ip: '127.0.0.1', port: 0).unwrap - listener.listen.unwrap + listener.bind(ip: '127.0.0.1', port: 0).get + listener.listen.get - let addr = listener.local_address.unwrap + let addr = listener.local_address.get - t.true(stream1.connect(addr.ip.unwrap, addr.port).ok?) + t.true(stream1.connect(addr.ip.get, addr.port).ok?) - let stream2 = Socket.ipv4(Type.STREAM).unwrap + let stream2 = Socket.ipv4(Type.STREAM).get t.true( # connect() may not immediately raise a "connection refused" error, due @@ -118,35 +118,35 @@ fn pub tests(t: mut Tests) { } t.test('Socket.listen') fn (t) { - let socket = Socket.ipv4(Type.STREAM).unwrap + let socket = Socket.ipv4(Type.STREAM).get - socket.bind(ip: '0.0.0.0', port: 0).unwrap + socket.bind(ip: '0.0.0.0', port: 0).get t.true(socket.listen.ok?) } t.test('Socket.accept') fn (t) { - let server = Socket.ipv4(Type.STREAM).unwrap - let client = Socket.ipv4(Type.STREAM).unwrap + let server = Socket.ipv4(Type.STREAM).get + let client = Socket.ipv4(Type.STREAM).get - server.bind(ip: '127.0.0.1', port: 0).unwrap - server.listen.unwrap + server.bind(ip: '127.0.0.1', port: 0).get + server.listen.get - let addr = server.local_address.unwrap + let addr = server.local_address.get t.equal(client.connect(addr.address.clone, addr.port), Result.Ok(nil)) - let connection = server.accept.unwrap + let connection = server.accept.get - t.equal(connection.local_address, Result.Ok(server.local_address.unwrap)) + t.equal(connection.local_address, Result.Ok(server.local_address.get)) } t.test('Socket.send_string_to') fn (t) { - let socket = Socket.ipv4(Type.DGRAM).unwrap + let socket = Socket.ipv4(Type.DGRAM).get - socket.bind(ip: '127.0.0.1', port: 0).unwrap + socket.bind(ip: '127.0.0.1', port: 0).get - let send_to = socket.local_address.unwrap + let send_to = socket.local_address.get let buffer = ByteArray.new t.equal( @@ -157,11 +157,11 @@ fn pub tests(t: mut Tests) { } t.test('Socket.send_bytes_to') fn (t) { - let socket = Socket.ipv4(Type.DGRAM).unwrap + let socket = Socket.ipv4(Type.DGRAM).get - socket.bind(ip: '127.0.0.1', port: 0).unwrap + socket.bind(ip: '127.0.0.1', port: 0).get - let send_to = socket.local_address.unwrap + let send_to = socket.local_address.get let buffer = ByteArray.new t.equal( @@ -177,13 +177,13 @@ fn pub tests(t: mut Tests) { } t.test('Socket.receive_from') fn (t) { - let listener = Socket.ipv4(Type.DGRAM).unwrap - let client = Socket.ipv4(Type.DGRAM).unwrap + let listener = Socket.ipv4(Type.DGRAM).get + let client = Socket.ipv4(Type.DGRAM).get - listener.bind(ip: '127.0.0.1', port: 0).unwrap - client.bind(ip: '127.0.0.1', port: 0).unwrap + listener.bind(ip: '127.0.0.1', port: 0).get + client.bind(ip: '127.0.0.1', port: 0).get - let send_to = listener.local_address.unwrap + let send_to = listener.local_address.get t.equal( client.send_string_to('ping', send_to.address, send_to.port.clone), @@ -197,115 +197,115 @@ fn pub tests(t: mut Tests) { } t.test('Socket.local_address with an unbound socket') fn (t) { - let socket = Socket.ipv4(Type.DGRAM).unwrap - let address = socket.local_address.unwrap + let socket = Socket.ipv4(Type.DGRAM).get + let address = socket.local_address.get t.equal(address.address, '0.0.0.0') t.equal(address.port, 0) } t.test('Socket.local_address with a bound socket') fn (t) { - let socket = Socket.ipv4(Type.DGRAM).unwrap + let socket = Socket.ipv4(Type.DGRAM).get - socket.bind(ip: '127.0.0.1', port: 0).unwrap + socket.bind(ip: '127.0.0.1', port: 0).get - let local_address = socket.local_address.unwrap + let local_address = socket.local_address.get t.equal(local_address.address, '127.0.0.1') t.true(local_address.port > 0) } t.test('Socket.peer_address with a disconnected socket') fn (t) { - let socket = Socket.ipv4(Type.DGRAM).unwrap + let socket = Socket.ipv4(Type.DGRAM).get t.true(socket.peer_address.error?) } t.test('Socket.peer_address with a connected socket') fn (t) { - let listener = Socket.ipv4(Type.STREAM).unwrap - let client = Socket.ipv4(Type.STREAM).unwrap + let listener = Socket.ipv4(Type.STREAM).get + let client = Socket.ipv4(Type.STREAM).get - listener.bind(ip: '127.0.0.1', port: 0).unwrap - listener.listen.unwrap + listener.bind(ip: '127.0.0.1', port: 0).get + listener.listen.get - let addr = listener.local_address.unwrap + let addr = listener.local_address.get t.equal(client.connect(addr.address, addr.port), Result.Ok(nil)) t.equal(client.peer_address, Result.Ok(addr)) } t.test('Socket.ttl') fn (t) { - let socket = Socket.ipv4(Type.STREAM).unwrap + let socket = Socket.ipv4(Type.STREAM).get t.true((socket.ttl = 10).ok?) } t.test('Socket.only_ipv6?') fn (t) { - let socket = Socket.ipv6(Type.STREAM).unwrap + let socket = Socket.ipv6(Type.STREAM).get t.true((socket.only_ipv6 = true).ok?) } t.test('Socket.no_delay?') fn (t) { - let socket = Socket.ipv4(Type.STREAM).unwrap + let socket = Socket.ipv4(Type.STREAM).get t.true((socket.no_delay = true).ok?) } t.test('Socket.broadcast?') fn (t) { - let socket = Socket.ipv4(Type.DGRAM).unwrap + let socket = Socket.ipv4(Type.DGRAM).get t.true((socket.broadcast = true).ok?) } t.test('Socket.linger') fn (t) { - let socket = Socket.ipv4(Type.STREAM).unwrap + let socket = Socket.ipv4(Type.STREAM).get let duration = Duration.from_secs(5) t.true((socket.linger = duration).ok?) } t.test('Socket.receive_buffer_size') fn (t) { - let socket = Socket.ipv4(Type.STREAM).unwrap + let socket = Socket.ipv4(Type.STREAM).get t.true((socket.receive_buffer_size = 256).ok?) } t.test('Socket.send_buffer_size') fn (t) { - let socket = Socket.ipv4(Type.STREAM).unwrap + let socket = Socket.ipv4(Type.STREAM).get t.true((socket.send_buffer_size = 256).ok?) } t.test('Socket.keepalive') fn (t) { - let socket = Socket.ipv4(Type.STREAM).unwrap + let socket = Socket.ipv4(Type.STREAM).get t.true((socket.keepalive = true).ok?) } t.test('Socket.reuse_adress') fn (t) { - let socket = Socket.ipv6(Type.DGRAM).unwrap + let socket = Socket.ipv6(Type.DGRAM).get t.true((socket.reuse_address = true).ok?) } t.test('Socket.reuse_port') fn (t) { - let socket = Socket.ipv6(Type.DGRAM).unwrap + let socket = Socket.ipv6(Type.DGRAM).get t.true((socket.reuse_port = true).ok?) } t.test('Socket.shutdown_read') fn (t) { - let listener = Socket.ipv4(Type.STREAM).unwrap - let stream = Socket.ipv4(Type.STREAM).unwrap + let listener = Socket.ipv4(Type.STREAM).get + let stream = Socket.ipv4(Type.STREAM).get - listener.bind(ip: '127.0.0.1', port: 0).unwrap - listener.listen.unwrap + listener.bind(ip: '127.0.0.1', port: 0).get + listener.listen.get - let addr = listener.local_address.unwrap + let addr = listener.local_address.get t.equal(stream.connect(addr.address, addr.port), Result.Ok(nil)) - stream.shutdown_read.unwrap + stream.shutdown_read.get let bytes = ByteArray.new @@ -314,46 +314,46 @@ fn pub tests(t: mut Tests) { } t.test('Socket.shutdown_write') fn (t) { - let listener = Socket.ipv4(Type.STREAM).unwrap - let stream = Socket.ipv4(Type.STREAM).unwrap + let listener = Socket.ipv4(Type.STREAM).get + let stream = Socket.ipv4(Type.STREAM).get - listener.bind(ip: '127.0.0.1', port: 0).unwrap - listener.listen.unwrap + listener.bind(ip: '127.0.0.1', port: 0).get + listener.listen.get - let addr = listener.local_address.unwrap + let addr = listener.local_address.get t.equal(stream.connect(addr.address, addr.port), Result.Ok(nil)) - stream.shutdown_write.unwrap + stream.shutdown_write.get t.true(stream.write_string('ping').error?) } t.test('Socket.shutdown shuts down the writing half') fn (t) { - let listener = Socket.ipv4(Type.STREAM).unwrap - let stream = Socket.ipv4(Type.STREAM).unwrap + let listener = Socket.ipv4(Type.STREAM).get + let stream = Socket.ipv4(Type.STREAM).get - listener.bind(ip: '127.0.0.1', port: 0).unwrap - listener.listen.unwrap + listener.bind(ip: '127.0.0.1', port: 0).get + listener.listen.get - let addr = listener.local_address.unwrap + let addr = listener.local_address.get t.equal(stream.connect(addr.address, addr.port), Result.Ok(nil)) - stream.shutdown.unwrap + stream.shutdown.get t.true(stream.write_string('ping').error?) } t.test('Socket.shutdown shuts down the reading half') fn (t) { - let listener = Socket.ipv4(Type.STREAM).unwrap - let stream = Socket.ipv4(Type.STREAM).unwrap + let listener = Socket.ipv4(Type.STREAM).get + let stream = Socket.ipv4(Type.STREAM).get - listener.bind(ip: '127.0.0.1', port: 0).unwrap - listener.listen.unwrap + listener.bind(ip: '127.0.0.1', port: 0).get + listener.listen.get - let addr = listener.local_address.unwrap + let addr = listener.local_address.get t.equal(stream.connect(addr.address, addr.port), Result.Ok(nil)) - stream.shutdown.unwrap + stream.shutdown.get let bytes = ByteArray.new @@ -362,17 +362,17 @@ fn pub tests(t: mut Tests) { } t.test('Socket.try_clone') fn (t) { - let socket = Socket.ipv4(Type.STREAM).unwrap + let socket = Socket.ipv4(Type.STREAM).get t.true(socket.try_clone.ok?) } t.test('Socket.read') fn (t) { - let socket = Socket.ipv4(Type.DGRAM).unwrap + let socket = Socket.ipv4(Type.DGRAM).get - socket.bind(ip: '127.0.0.1', port: 0).unwrap + socket.bind(ip: '127.0.0.1', port: 0).get - let addr = socket.local_address.unwrap + let addr = socket.local_address.get let bytes = ByteArray.new t.equal( @@ -383,18 +383,18 @@ fn pub tests(t: mut Tests) { } t.test('Socket.write_bytes') fn (t) { - let listener = Socket.ipv4(Type.STREAM).unwrap - let stream = Socket.ipv4(Type.STREAM).unwrap + let listener = Socket.ipv4(Type.STREAM).get + let stream = Socket.ipv4(Type.STREAM).get - listener.bind(ip: '127.0.0.1', port: 0).unwrap - listener.listen.unwrap + listener.bind(ip: '127.0.0.1', port: 0).get + listener.listen.get - let addr = listener.local_address.unwrap + let addr = listener.local_address.get t.equal(stream.connect(addr.address, addr.port), Result.Ok(nil)) - stream.write_bytes('ping'.to_byte_array).unwrap + stream.write_bytes('ping'.to_byte_array).get - let connection = listener.accept.unwrap + let connection = listener.accept.get let bytes = ByteArray.new t.equal(connection.read(into: bytes, size: 4), Result.Ok(4)) @@ -402,18 +402,18 @@ fn pub tests(t: mut Tests) { } t.test('Socket.write_string') fn (t) { - let listener = Socket.ipv4(Type.STREAM).unwrap - let stream = Socket.ipv4(Type.STREAM).unwrap + let listener = Socket.ipv4(Type.STREAM).get + let stream = Socket.ipv4(Type.STREAM).get - listener.bind(ip: '127.0.0.1', port: 0).unwrap - listener.listen.unwrap + listener.bind(ip: '127.0.0.1', port: 0).get + listener.listen.get - let addr = listener.local_address.unwrap + let addr = listener.local_address.get t.equal(stream.connect(addr.address, addr.port), Result.Ok(nil)) - stream.write_string('ping').unwrap + stream.write_string('ping').get - let connection = listener.accept.unwrap + let connection = listener.accept.get let bytes = ByteArray.new t.equal(connection.read(into: bytes, size: 4), Result.Ok(4)) @@ -421,23 +421,23 @@ fn pub tests(t: mut Tests) { } t.test('Socket.flush') fn (t) { - let socket = Socket.ipv4(Type.STREAM).unwrap + let socket = Socket.ipv4(Type.STREAM).get t.equal(socket.flush, Result.Ok(nil)) } t.test('Socket.timeout_after=') fn (t) { - let server = Socket.ipv4(Type.STREAM).unwrap + let server = Socket.ipv4(Type.STREAM).get - server.bind(ip: '127.0.0.1', port: 0).unwrap - server.listen.unwrap + server.bind(ip: '127.0.0.1', port: 0).get + server.listen.get server.timeout_after = Duration.from_secs(0) t.true(server.accept.error?) } t.test('Socket.reset_deadline') fn (t) { - let socket = Socket.ipv4(Type.STREAM).unwrap + let socket = Socket.ipv4(Type.STREAM).get socket.timeout_after = Duration.from_secs(10) t.true(socket.deadline > 0) @@ -462,9 +462,9 @@ fn pub tests(t: mut Tests) { t.test('UdpSocket.connect') fn (t) { let ip = IpAddress.V4(Ipv4Address.new(127, 0, 0, 1)) - let socket1 = UdpSocket.new(ip: ip.clone, port: 0).unwrap - let socket2 = UdpSocket.new(ip: ip, port: 0).unwrap - let addr = socket2.local_address.unwrap + let socket1 = UdpSocket.new(ip: ip.clone, port: 0).get + let socket2 = UdpSocket.new(ip: ip, port: 0).get + let addr = socket2.local_address.get t.true(socket1.connect(addr.address, addr.port).ok?) } @@ -472,9 +472,9 @@ fn pub tests(t: mut Tests) { t.test('UdpSocket.send_string_to') fn (t) { let socket = UdpSocket .new(ip: IpAddress.V4(Ipv4Address.new(127, 0, 0, 1)), port: 0) - .unwrap + .get - let addr = socket.local_address.unwrap + let addr = socket.local_address.get t.equal( socket.send_string_to('ping', addr.address, addr.port), Result.Ok(4) @@ -488,8 +488,8 @@ fn pub tests(t: mut Tests) { t.test('UdpSocket.send_bytes_to') fn (t) { let ip = IpAddress.V4(Ipv4Address.new(127, 0, 0, 1)) - let socket = UdpSocket.new(ip: ip, port: 0).unwrap - let addr = socket.local_address.unwrap + let socket = UdpSocket.new(ip: ip, port: 0).get + let addr = socket.local_address.get socket .send_bytes_to( @@ -497,7 +497,7 @@ fn pub tests(t: mut Tests) { addr.address, addr.port ) - .unwrap + .get let bytes = ByteArray.new @@ -507,13 +507,13 @@ fn pub tests(t: mut Tests) { t.test('UdpSocket.receive_from') fn (t) { let ip = IpAddress.V4(Ipv4Address.new(127, 0, 0, 1)) - let listener = UdpSocket.new(ip: ip.clone, port: 0).unwrap - let client = UdpSocket.new(ip: ip, port: 0).unwrap - let addr = listener.local_address.unwrap + let listener = UdpSocket.new(ip: ip.clone, port: 0).get + let client = UdpSocket.new(ip: ip, port: 0).get + let addr = listener.local_address.get client .send_string_to('ping', addr.address, addr.port) - .unwrap + .get let bytes = ByteArray.new @@ -523,8 +523,8 @@ fn pub tests(t: mut Tests) { t.test('UdpSocket.local_address') fn (t) { let ip = IpAddress.V4(Ipv4Address.new(127, 0, 0, 1)) - let socket = UdpSocket.new(ip: ip, port: 0).unwrap - let local_address = socket.local_address.unwrap + let socket = UdpSocket.new(ip: ip, port: 0).get + let local_address = socket.local_address.get t.equal(local_address.address, '127.0.0.1') t.true(local_address.port > 0) @@ -532,15 +532,15 @@ fn pub tests(t: mut Tests) { t.test('UdpSocket.try_clone') fn (t) { let ip = IpAddress.V4(Ipv4Address.new(127, 0, 0, 1)) - let socket = UdpSocket.new(ip: ip, port: 0).unwrap + let socket = UdpSocket.new(ip: ip, port: 0).get t.true(socket.try_clone.ok?) } t.test('UdpSocket.read') fn (t) { let ip = IpAddress.V4(Ipv4Address.new(127, 0, 0, 1)) - let socket = UdpSocket.new(ip: ip, port: 0).unwrap - let addr = socket.local_address.unwrap + let socket = UdpSocket.new(ip: ip, port: 0).get + let addr = socket.local_address.get t.equal( socket.send_string_to('ping', addr.address, addr.port), Result.Ok(4) @@ -554,15 +554,15 @@ fn pub tests(t: mut Tests) { t.test('UdpSocket.write_bytes') fn (t) { let ip = IpAddress.V4(Ipv4Address.new(127, 0, 0, 1)) - let server_socket = UdpSocket.new(ip: ip.clone, port: 0).unwrap - let client_socket = UdpSocket.new(ip: ip, port: 0).unwrap - let addr = server_socket.local_address.unwrap + let server_socket = UdpSocket.new(ip: ip.clone, port: 0).get + let client_socket = UdpSocket.new(ip: ip, port: 0).get + let addr = server_socket.local_address.get t.equal( client_socket.connect(addr.address, addr.port), Result.Ok(nil) ) - client_socket.write_bytes('ping'.to_byte_array).unwrap + client_socket.write_bytes('ping'.to_byte_array).get let bytes = ByteArray.new @@ -572,34 +572,34 @@ fn pub tests(t: mut Tests) { t.test('UdpSocket.flush') fn (t) { let ip = IpAddress.V4(Ipv4Address.new(127, 0, 0, 1)) - let socket = UdpSocket.new(ip: ip, port: 0).unwrap + let socket = UdpSocket.new(ip: ip, port: 0).get t.equal(socket.flush, Result.Ok(nil)) } t.test('TcpClient.new') fn (t) { - let listener = Socket.ipv4(Type.STREAM).unwrap + let listener = Socket.ipv4(Type.STREAM).get - listener.bind(ip: '127.0.0.1', port: 0).unwrap - listener.listen.unwrap + listener.bind(ip: '127.0.0.1', port: 0).get + listener.listen.get - let addr = listener.local_address.unwrap + let addr = listener.local_address.get - t.true(TcpClient.new(addr.ip.unwrap, addr.port).ok?) + t.true(TcpClient.new(addr.ip.get, addr.port).ok?) } t.test('TcpClient.with_timeout') fn (t) { - let listener = Socket.ipv4(Type.STREAM).unwrap + let listener = Socket.ipv4(Type.STREAM).get - listener.bind(ip: '127.0.0.1', port: 0).unwrap - listener.listen.unwrap + listener.bind(ip: '127.0.0.1', port: 0).get + listener.listen.get - let addr = listener.local_address.unwrap + let addr = listener.local_address.get t.true( TcpClient .with_timeout( - addr.ip.unwrap, + addr.ip.get, addr.port, timeout_after: Duration.from_secs(2) ) @@ -623,7 +623,7 @@ fn pub tests(t: mut Tests) { try listener.listen let addr = try listener.local_address - let stream = try TcpClient.new(addr.ip.unwrap, addr.port) + let stream = try TcpClient.new(addr.ip.get, addr.port) let local_addr = try stream.local_address t.equal(local_addr.address, '127.0.0.1') @@ -638,7 +638,7 @@ fn pub tests(t: mut Tests) { try listener.listen let addr = try listener.local_address - let stream = try TcpClient.new(addr.ip.unwrap, addr.port) + let stream = try TcpClient.new(addr.ip.get, addr.port) let peer_addr = try stream.peer_address t.equal(peer_addr.address, addr.address) @@ -653,7 +653,7 @@ fn pub tests(t: mut Tests) { try listener.listen let addr = try listener.local_address - let stream = try TcpClient.new(addr.ip.unwrap, addr.port) + let stream = try TcpClient.new(addr.ip.get, addr.port) let bytes = ByteArray.new let client = try listener.accept @@ -671,7 +671,7 @@ fn pub tests(t: mut Tests) { try listener.listen let addr = try listener.local_address - let stream = try TcpClient.new(addr.ip.unwrap, addr.port) + let stream = try TcpClient.new(addr.ip.get, addr.port) let connection = try listener.accept let bytes = ByteArray.new @@ -688,7 +688,7 @@ fn pub tests(t: mut Tests) { try listener.listen let addr = try listener.local_address - let stream = try TcpClient.new(addr.ip.unwrap, addr.port) + let stream = try TcpClient.new(addr.ip.get, addr.port) let connection = try listener.accept let bytes = ByteArray.new @@ -705,7 +705,7 @@ fn pub tests(t: mut Tests) { try listener.listen let addr = try listener.local_address - let stream = try TcpClient.new(addr.ip.unwrap, addr.port) + let stream = try TcpClient.new(addr.ip.get, addr.port) stream.flush } @@ -717,7 +717,7 @@ fn pub tests(t: mut Tests) { try listener.listen let addr = try listener.local_address - let stream = try TcpClient.new(addr.ip.unwrap, addr.port) + let stream = try TcpClient.new(addr.ip.get, addr.port) t.equal(stream.shutdown_read, Result.Ok(nil)) @@ -735,7 +735,7 @@ fn pub tests(t: mut Tests) { try listener.listen let addr = try listener.local_address - let stream = try TcpClient.new(addr.ip.unwrap, addr.port) + let stream = try TcpClient.new(addr.ip.get, addr.port) t.equal(stream.shutdown_write, Result.Ok(nil)) t.true(stream.write_string('ping').error?) @@ -749,7 +749,7 @@ fn pub tests(t: mut Tests) { try listener.listen let addr = try listener.local_address - let stream = try TcpClient.new(addr.ip.unwrap, addr.port) + let stream = try TcpClient.new(addr.ip.get, addr.port) t.equal(stream.shutdown, Result.Ok(nil)) t.true(stream.write_string('ping').error?) @@ -763,7 +763,7 @@ fn pub tests(t: mut Tests) { try listener.listen let addr = try listener.local_address - let stream = try TcpClient.new(addr.ip.unwrap, addr.port) + let stream = try TcpClient.new(addr.ip.get, addr.port) t.equal(stream.shutdown, Result.Ok(nil)) @@ -775,13 +775,13 @@ fn pub tests(t: mut Tests) { } t.ok('TcpClient.try_clone') fn (t) { - let listener = Socket.ipv4(Type.STREAM).unwrap + let listener = Socket.ipv4(Type.STREAM).get try listener.bind(ip: '127.0.0.1', port: 0) try listener.listen let addr = try listener.local_address - let client = try TcpClient.new(addr.ip.unwrap, addr.port) + let client = try TcpClient.new(addr.ip.get, addr.port) t.true(client.try_clone.ok?) Result.Ok(nil) @@ -792,17 +792,17 @@ fn pub tests(t: mut Tests) { t.true(TcpServer.new(ip: ip.clone, port: 0).ok?) - let listener = TcpServer.new(ip: ip, port: 0).unwrap - let addr = listener.local_address.unwrap + let listener = TcpServer.new(ip: ip, port: 0).get + let addr = listener.local_address.get - t.true(TcpServer.new(addr.ip.unwrap, addr.port).ok?) + t.true(TcpServer.new(addr.ip.get, addr.port).ok?) } t.ok('TcpServer.accept') fn (t) { let ip = IpAddress.V4(Ipv4Address.new(127, 0, 0, 1)) let listener = try TcpServer.new(ip: ip, port: 0) let addr = try listener.local_address - let stream = try TcpClient.new(addr.ip.unwrap, addr.port) + let stream = try TcpClient.new(addr.ip.get, addr.port) let connection = try listener.accept t.equal(connection.local_address, stream.peer_address) @@ -811,8 +811,8 @@ fn pub tests(t: mut Tests) { t.test('TcpServer.local_address') fn (t) { let ip = IpAddress.V4(Ipv4Address.new(127, 0, 0, 1)) - let listener = TcpServer.new(ip: ip, port: 0).unwrap - let addr = listener.local_address.unwrap + let listener = TcpServer.new(ip: ip, port: 0).get + let addr = listener.local_address.get t.equal(addr.address, '127.0.0.1') t.true(addr.port > 0) @@ -820,7 +820,7 @@ fn pub tests(t: mut Tests) { t.test('TcpServer.try_clone') fn (t) { let ip = IpAddress.V4(Ipv4Address.new(127, 0, 0, 1)) - # let server = TcpServer.new(ip: ip, port: 0).unwrap + # let server = TcpServer.new(ip: ip, port: 0).get let server = match TcpServer.new(ip: ip, port: 0) { case Ok(v) -> v case Error(e) -> panic(e.to_string) @@ -876,39 +876,39 @@ fn pub tests(t: mut Tests) { } t.test('UnixSocket.bind') fn (t) { - let socket1 = UnixSocket.new(Type.STREAM).unwrap - let socket2 = UnixSocket.new(Type.STREAM).unwrap + let socket1 = UnixSocket.new(Type.STREAM).get + let socket2 = UnixSocket.new(Type.STREAM).get let path = SocketPath.new(t.id) t.true(socket1.bind(path).ok?) t.true(socket2.bind(path).error?) if env.OS == 'linux' { - let socket = UnixSocket.new(Type.STREAM).unwrap + let socket = UnixSocket.new(Type.STREAM).get t.true(socket.bind("\0inko-test-{t.id}").ok?) } } t.test('UnixSocket.connect') fn (t) { - let listener = UnixSocket.new(Type.STREAM).unwrap - let stream = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get + let stream = UnixSocket.new(Type.STREAM).get let path = SocketPath.new(t.id) t.true(stream.connect(path).error?) - listener.bind(path).unwrap - listener.listen.unwrap + listener.bind(path).get + listener.listen.get t.true(stream.connect(path).ok?) if env.OS == 'linux' { - let listener = UnixSocket.new(Type.STREAM).unwrap - let stream = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get + let stream = UnixSocket.new(Type.STREAM).get let addr = "\0inko-test-{t.id}" - listener.bind(addr).unwrap - listener.listen.unwrap + listener.bind(addr).get + listener.listen.get t.true(stream.connect(addr).ok?) } @@ -916,30 +916,30 @@ fn pub tests(t: mut Tests) { t.test('UnixSocket.listen') fn (t) { let path = SocketPath.new(t.id) - let socket = UnixSocket.new(Type.STREAM).unwrap + let socket = UnixSocket.new(Type.STREAM).get - socket.bind(path).unwrap + socket.bind(path).get t.true(socket.listen.ok?) } t.test('UnixSocket.accept') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap - let stream = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get + let stream = UnixSocket.new(Type.STREAM).get - listener.bind(path).unwrap - listener.listen.unwrap + listener.bind(path).get + listener.listen.get t.equal(stream.connect(path), Result.Ok(nil)) - let client = listener.accept.unwrap + let client = listener.accept.get t.equal(client.peer_address, stream.local_address) } t.test('UnixSocket.send_string_to') fn (t) { let path = SocketPath.new(t.id) - let socket = UnixSocket.new(Type.DGRAM).unwrap + let socket = UnixSocket.new(Type.DGRAM).get t.equal(socket.bind(path), Result.Ok(nil)) t.equal(socket.send_string_to('ping', path), Result.Ok(4)) @@ -952,7 +952,7 @@ fn pub tests(t: mut Tests) { t.test('UnixSocket.send_bytes_to') fn (t) { let path = SocketPath.new(t.id) - let socket = UnixSocket.new(Type.DGRAM).unwrap + let socket = UnixSocket.new(Type.DGRAM).get t.equal(socket.bind(path), Result.Ok(nil)) t.equal(socket.send_bytes_to('ping'.to_byte_array, path), Result.Ok(4)) @@ -965,8 +965,8 @@ fn pub tests(t: mut Tests) { t.test('UnixSocket.receive_from') fn (t) { let pair = SocketPath.pair(t.id) - let listener = UnixSocket.new(Type.DGRAM).unwrap - let client = UnixSocket.new(Type.DGRAM).unwrap + let listener = UnixSocket.new(Type.DGRAM).get + let client = UnixSocket.new(Type.DGRAM).get t.equal(listener.bind(pair.0), Result.Ok(nil)) t.equal(client.bind(pair.1), Result.Ok(nil)) @@ -979,34 +979,34 @@ fn pub tests(t: mut Tests) { } t.test('UnixSocket.local_address with an unbound socket') fn (t) { - let socket = UnixSocket.new(Type.DGRAM).unwrap - let address = socket.local_address.unwrap + let socket = UnixSocket.new(Type.DGRAM).get + let address = socket.local_address.get t.equal(address, UnixAddress.new('')) } t.test('UnixSocket.local_address with a bound socket') fn (t) { let path = SocketPath.new(t.id) - let socket = UnixSocket.new(Type.DGRAM).unwrap + let socket = UnixSocket.new(Type.DGRAM).get - socket.bind(path).unwrap + socket.bind(path).get t.equal(socket.local_address, Result.Ok(UnixAddress.new(path.to_string))) } t.test('UnixSocket.peer_address with a disconnected socket') fn (t) { - let socket = UnixSocket.new(Type.DGRAM).unwrap + let socket = UnixSocket.new(Type.DGRAM).get t.true(socket.peer_address.error?) } t.test('UnixSocket.peer_address with a connected socket') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap - let client = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get + let client = UnixSocket.new(Type.STREAM).get - listener.bind(path).unwrap - listener.listen.unwrap + listener.bind(path).get + listener.listen.get t.equal(client.connect(path), Result.Ok(nil)) t.equal(client.peer_address, listener.local_address) @@ -1014,7 +1014,7 @@ fn pub tests(t: mut Tests) { t.test('UnixSocket.read') fn (t) { let path = SocketPath.new(t.id) - let socket = UnixSocket.new(Type.DGRAM).unwrap + let socket = UnixSocket.new(Type.DGRAM).get t.equal(socket.bind(path), Result.Ok(nil)) t.equal(socket.send_string_to('ping', path), Result.Ok(4)) @@ -1027,15 +1027,15 @@ fn pub tests(t: mut Tests) { t.test('UnixSocket.write_bytes') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap - let stream = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get + let stream = UnixSocket.new(Type.STREAM).get - listener.bind(path).unwrap - listener.listen.unwrap + listener.bind(path).get + listener.listen.get t.equal(stream.connect(path), Result.Ok(nil)) - stream.write_bytes('ping'.to_byte_array).unwrap + stream.write_bytes('ping'.to_byte_array).get - let connection = listener.accept.unwrap + let connection = listener.accept.get let bytes = ByteArray.new t.equal(connection.read(into: bytes, size: 4), Result.Ok(4)) @@ -1044,15 +1044,15 @@ fn pub tests(t: mut Tests) { t.test('UnixSocket.write_string') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap - let stream = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get + let stream = UnixSocket.new(Type.STREAM).get - listener.bind(path).unwrap - listener.listen.unwrap + listener.bind(path).get + listener.listen.get t.equal(stream.connect(path), Result.Ok(nil)) - stream.write_string('ping').unwrap + stream.write_string('ping').get - let connection = listener.accept.unwrap + let connection = listener.accept.get let bytes = ByteArray.new t.equal(connection.read(into: bytes, size: 4), Result.Ok(4)) @@ -1060,32 +1060,32 @@ fn pub tests(t: mut Tests) { } t.test('UnixSocket.flush') fn (t) { - let socket = UnixSocket.new(Type.STREAM).unwrap + let socket = UnixSocket.new(Type.STREAM).get t.equal(socket.flush, Result.Ok(nil)) } t.test('UnixSocket.receive_buffer_size') fn (t) { - let socket = UnixSocket.new(Type.STREAM).unwrap + let socket = UnixSocket.new(Type.STREAM).get t.true((socket.receive_buffer_size = 256).ok?) } t.test('UnixSocket.send_buffer_size') fn (t) { - let socket = UnixSocket.new(Type.STREAM).unwrap + let socket = UnixSocket.new(Type.STREAM).get t.true((socket.send_buffer_size = 256).ok?) } t.test('UnixSocket.shutdown_read') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap - let stream = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get + let stream = UnixSocket.new(Type.STREAM).get - listener.bind(path).unwrap - listener.listen.unwrap + listener.bind(path).get + listener.listen.get t.equal(stream.connect(path), Result.Ok(nil)) - stream.shutdown_read.unwrap + stream.shutdown_read.get let bytes = ByteArray.new @@ -1095,39 +1095,39 @@ fn pub tests(t: mut Tests) { t.test('UnixSocket.shutdown_write') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap - let stream = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get + let stream = UnixSocket.new(Type.STREAM).get - listener.bind(path).unwrap - listener.listen.unwrap + listener.bind(path).get + listener.listen.get t.equal(stream.connect(path), Result.Ok(nil)) - stream.shutdown_write.unwrap + stream.shutdown_write.get t.true(stream.write_string('ping').error?) } t.test('UnixSocket.shutdown shuts down the writing half') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap - let stream = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get + let stream = UnixSocket.new(Type.STREAM).get - listener.bind(path).unwrap - listener.listen.unwrap + listener.bind(path).get + listener.listen.get t.equal(stream.connect(path), Result.Ok(nil)) - stream.shutdown.unwrap + stream.shutdown.get t.true(stream.write_string('ping').error?) } t.test('UnixSocket.shutdown shuts down the reading half') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap - let stream = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get + let stream = UnixSocket.new(Type.STREAM).get - listener.bind(path).unwrap - listener.listen.unwrap + listener.bind(path).get + listener.listen.get t.equal(stream.connect(path), Result.Ok(nil)) - stream.shutdown.unwrap + stream.shutdown.get let bytes = ByteArray.new @@ -1136,16 +1136,16 @@ fn pub tests(t: mut Tests) { } t.test('UnixSocket.try_clone') fn (t) { - let socket = UnixSocket.new(Type.STREAM).unwrap + let socket = UnixSocket.new(Type.STREAM).get t.true(socket.try_clone.ok?) } t.test('UnixSocket.timeout_after=') fn (t) { let path = SocketPath.new(t.id) - let server = UnixSocket.new(Type.STREAM).unwrap + let server = UnixSocket.new(Type.STREAM).get - server.bind(path).unwrap + server.bind(path).get server.listen server.timeout_after = Duration.from_secs(0) @@ -1153,7 +1153,7 @@ fn pub tests(t: mut Tests) { } t.test('UnixSocket.reset_deadline') fn (t) { - let socket = UnixSocket.new(Type.STREAM).unwrap + let socket = UnixSocket.new(Type.STREAM).get socket.timeout_after = Duration.from_secs(10) t.true(socket.deadline > 0) @@ -1171,15 +1171,15 @@ fn pub tests(t: mut Tests) { t.test('UnixDatagram.connect') fn (t) { let pair = SocketPath.pair(t.id) - let _socket1 = UnixDatagram.new(pair.0).unwrap - let socket2 = UnixDatagram.new(pair.1).unwrap + let _socket1 = UnixDatagram.new(pair.0).get + let socket2 = UnixDatagram.new(pair.1).get t.true(socket2.connect(pair.0).ok?) } t.test('UnixDatagram.send_to') fn (t) { let path = SocketPath.new(t.id) - let socket = UnixDatagram.new(path).unwrap + let socket = UnixDatagram.new(path).get t.equal(socket.send_string_to('ping', path), Result.Ok(4)) @@ -1191,8 +1191,8 @@ fn pub tests(t: mut Tests) { t.test('UnixDatagram.receive_from') fn (t) { let pair = SocketPath.pair(t.id) - let listener = UnixDatagram.new(pair.0).unwrap - let client = UnixDatagram.new(pair.1).unwrap + let listener = UnixDatagram.new(pair.0).get + let client = UnixDatagram.new(pair.1).get t.equal(client.send_string_to('ping', pair.0), Result.Ok(4)) @@ -1204,21 +1204,21 @@ fn pub tests(t: mut Tests) { t.test('UnixDatagram.local_address') fn (t) { let path = SocketPath.new(t.id) - let socket = UnixDatagram.new(path).unwrap + let socket = UnixDatagram.new(path).get t.equal(socket.local_address, Result.Ok(UnixAddress.new(path.to_string))) } t.test('UnixDatagram.try_clone') fn (t) { let path = SocketPath.new(t.id) - let socket = UnixDatagram.new(path).unwrap + let socket = UnixDatagram.new(path).get t.true(socket.try_clone.ok?) } t.test('UnixDatagram.read') fn (t) { let path = SocketPath.new(t.id) - let socket = UnixDatagram.new(path).unwrap + let socket = UnixDatagram.new(path).get t.equal(socket.send_string_to('ping', address: path), Result.Ok(4)) @@ -1230,8 +1230,8 @@ fn pub tests(t: mut Tests) { t.test('UnixDatagram.write_bytes') fn (t) { let pair = SocketPath.pair(t.id) - let server = UnixDatagram.new(pair.0).unwrap - let client = UnixDatagram.new(pair.1).unwrap + let server = UnixDatagram.new(pair.0).get + let client = UnixDatagram.new(pair.1).get t.equal(client.connect(pair.0), Result.Ok(nil)) @@ -1244,27 +1244,27 @@ fn pub tests(t: mut Tests) { t.test('UnixDatagram.flush') fn (t) { let path = SocketPath.new(t.id) - let socket = UnixDatagram.new(path).unwrap + let socket = UnixDatagram.new(path).get t.equal(socket.flush, Result.Ok(nil)) } t.test('UnixClient.new') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get - listener.bind(path).unwrap - listener.listen.unwrap + listener.bind(path).get + listener.listen.get t.true(UnixClient.new(path).ok?) } t.test('UnixClient.with_timeout') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get - listener.bind(path).unwrap - listener.listen.unwrap + listener.bind(path).get + listener.listen.get # Unlike IP sockets there's no unroutable address we can use, so at best we # can assert the following doesn't time out. This means this test is mostly @@ -1278,7 +1278,7 @@ fn pub tests(t: mut Tests) { t.ok('UnixClient.local_address') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get try listener.bind(path) try listener.listen @@ -1291,7 +1291,7 @@ fn pub tests(t: mut Tests) { t.ok('UnixClient.peer_address') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get try listener.bind(path) try listener.listen @@ -1304,7 +1304,7 @@ fn pub tests(t: mut Tests) { t.ok('UnixClient.read') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get try listener.bind(path) try listener.listen @@ -1323,7 +1323,7 @@ fn pub tests(t: mut Tests) { t.ok('UnixClient.write_bytes') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get try listener.bind(path) try listener.listen @@ -1340,7 +1340,7 @@ fn pub tests(t: mut Tests) { t.ok('UnixClient.write_string') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get try listener.bind(path) try listener.listen @@ -1357,7 +1357,7 @@ fn pub tests(t: mut Tests) { t.ok('UnixClient.flush') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get try listener.bind(path) try listener.listen @@ -1367,7 +1367,7 @@ fn pub tests(t: mut Tests) { t.ok('UnixClient.shutdown_read') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get try listener.bind(path) try listener.listen @@ -1385,7 +1385,7 @@ fn pub tests(t: mut Tests) { t.ok('UnixClient.shutdown_write') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get try listener.bind(path) try listener.listen @@ -1399,7 +1399,7 @@ fn pub tests(t: mut Tests) { t.ok('UnixClient.shutdown shuts down the writing half') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get try listener.bind(path) try listener.listen @@ -1413,7 +1413,7 @@ fn pub tests(t: mut Tests) { t.ok('UnixClient.shutdown shuts down the reading half') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get try listener.bind(path) try listener.listen @@ -1431,7 +1431,7 @@ fn pub tests(t: mut Tests) { t.ok('UnixClient.try_clone') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixSocket.new(Type.STREAM).unwrap + let listener = UnixSocket.new(Type.STREAM).get try listener.bind(path) try listener.listen @@ -1457,15 +1457,15 @@ fn pub tests(t: mut Tests) { t.test('UnixServer.local_address') fn (t) { let path = SocketPath.new(t.id) - let listener = UnixServer.new(path).unwrap - let addr = listener.local_address.unwrap + let listener = UnixServer.new(path).get + let addr = listener.local_address.get t.equal(addr, UnixAddress.new(path.to_string)) } t.test('UnixServer.try_clone') fn (t) { let path = SocketPath.new(t.id) - let server = UnixServer.new(path).unwrap + let server = UnixServer.new(path).get t.true(server.try_clone.ok?) } diff --git a/std/test/std/test_env.inko b/std/test/std/test_env.inko index 2c8d62a16..42535d49c 100644 --- a/std/test/std/test_env.inko +++ b/std/test/std/test_env.inko @@ -17,7 +17,7 @@ fn pub tests(t: mut Tests) { t.fork( 'env.opt', - child: fn { STDOUT.new.write_string(env.opt('INKO_TEST').unwrap_or('?')) }, + child: fn { STDOUT.new.write_string(env.opt('INKO_TEST').or('?')) }, test: fn (test, process) { process.variable('INKO_TEST', 'foo') test.equal(process.spawn.stdout, 'foo') @@ -51,7 +51,7 @@ fn pub tests(t: mut Tests) { } t.test('env.working_directory') fn (t) { - let path = env.working_directory.unwrap + let path = env.working_directory.get t.true(path.directory?) } @@ -62,7 +62,7 @@ fn pub tests(t: mut Tests) { let out = STDOUT.new env.working_directory = env.temporary_directory - out.write_string(env.working_directory.unwrap.to_string) + out.write_string(env.working_directory.get.to_string) }, test: fn (test, process) { test.equal(process.spawn.stdout, env.temporary_directory.to_string) @@ -86,7 +86,7 @@ fn pub tests(t: mut Tests) { ) t.test('env.executable') fn (t) { - let path = env.executable.unwrap + let path = env.executable.get t.true(path.file?) } diff --git a/std/test/std/test_float.inko b/std/test/std/test_float.inko index 91eb304cc..6d6fa5115 100644 --- a/std/test/std/test_float.inko +++ b/std/test/std/test_float.inko @@ -49,7 +49,7 @@ fn pub tests(t: mut Tests) { t.equal(Float.parse('Infinity'), Option.Some(Float.infinity)) t.equal(Float.parse('-inf'), Option.Some(Float.negative_infinity)) t.equal(Float.parse('-Infinity'), Option.Some(Float.negative_infinity)) - t.true(Float.parse('NaN').unwrap.not_a_number?) + t.true(Float.parse('NaN').get.not_a_number?) t.equal(Float.parse(' 1.2'), Option.None) t.equal(Float.parse('1.2 '), Option.None) diff --git a/std/test/std/test_io.inko b/std/test/std/test_io.inko index 317dd1507..7a971f4f7 100644 --- a/std/test/std/test_io.inko +++ b/std/test/std/test_io.inko @@ -141,7 +141,7 @@ fn pub tests(t: mut Tests) { t.test('Read.read_all') fn (t) { let reader = Reader.new let bytes = ByteArray.new - let size = reader.read_all(bytes).unwrap + let size = reader.read_all(bytes).get t.equal(size, 3) t.equal(bytes, ByteArray.from_array([1, 2, 3])) @@ -150,7 +150,7 @@ fn pub tests(t: mut Tests) { t.test('Write.print') fn (t) { let writer = Writer.new - writer.print('foo').unwrap + writer.print('foo').get t.equal(writer.buffer, "foo\n".to_byte_array) } diff --git a/std/test/std/test_map.inko b/std/test/std/test_map.inko index 2ae56b99a..2585ac6ab 100644 --- a/std/test/std/test_map.inko +++ b/std/test/std/test_map.inko @@ -9,7 +9,7 @@ fn pub tests(t: mut Tests) { map.set('name', 'Alice') - let entry = map.iter.next.unwrap + let entry = map.iter.next.get t.equal(entry.key, 'name') } @@ -19,7 +19,7 @@ fn pub tests(t: mut Tests) { map.set('name', 'Alice') - let entry = map.iter.next.unwrap + let entry = map.iter.next.get t.equal(entry.value, 'Alice') } diff --git a/std/test/std/test_option.inko b/std/test/std/test_option.inko index 1c46f7473..e72592e9b 100644 --- a/std/test/std/test_option.inko +++ b/std/test/std/test_option.inko @@ -18,29 +18,29 @@ fn pub tests(t: mut Tests) { t.equal(b.as_mut, Option.None) } - t.test('Option.unwrap with a Some') fn (t) { - t.equal(Option.Some(42).unwrap, 42) + t.test('Option.get with a Some') fn (t) { + t.equal(Option.Some(42).get, 42) } t.test('Option.expect with a Some') fn (t) { - t.equal(Option.Some(42).expect('foo'), 42) + t.equal(Option.Some(42).or_panic('foo'), 42) } - t.panic('Option.unwrap with a None') fn { + t.panic('Option.get with a None') fn { let opt: Option[Int] = Option.None - opt.unwrap + opt.get } t.panic('Option.expect with a None') fn { let opt: Option[Int] = Option.None - opt.expect('foo') + opt.or_panic('foo') } - t.test('Option.unwrap_or') fn (t) { - t.equal(Option.Some(42).unwrap_or(0), 42) - t.equal(Option.None.unwrap_or(0), 0) + t.test('Option.or') fn (t) { + t.equal(Option.Some(42).or(0), 42) + t.equal(Option.None.or(0), 0) } t.test('Option.map') fn (t) { diff --git a/std/test/std/test_result.inko b/std/test/std/test_result.inko index 151fd72e5..c59539bed 100644 --- a/std/test/std/test_result.inko +++ b/std/test/std/test_result.inko @@ -44,44 +44,46 @@ fn pub tests(t: mut Tests) { t.equal(bar.error, Option.Some('oops!')) } - t.test('Result.unwrap with an Ok') fn (t) { + t.test('Result.get with an Ok') fn (t) { let res: Result[Int, String] = Result.Ok(42) - t.equal(res.unwrap, 42) + t.equal(res.get, 42) } - t.panic('Result.unwrap with an Error') fn { + t.panic('Result.get with an Error') fn { let res: Result[Int, Int] = Result.Error(0) - res.unwrap + res.get } t.test('Result.expect with an Ok') fn (t) { let res: Result[Int, String] = Result.Ok(42) - t.equal(res.expect('foo'), 42) + t.equal(res.or_panic('foo'), 42) } t.panic('Result.expect with an Error') fn { let res: Result[Int, Int] = Result.Error(0) - res.expect('foo') + res.or_panic('foo') } - t.test('Result.unwrap_or') fn (t) { + t.test('Result.or') fn (t) { let foo: Result[Int, String] = Result.Ok(42) let bar: Result[Int, String] = Result.Error('oops!') - t.equal(foo.unwrap_or(0), 42) - t.equal(bar.unwrap_or(0), 0) + t.equal(foo.or(0), 42) + t.equal(bar.or(0), 0) } - t.test('Result.unwrap_or_else') fn (t) { + t.test('Result.or_else') fn (t) { let foo: Result[Int, String] = Result.Ok(42) let bar: Result[Int, String] = Result.Error('oops!') + let baz: Result[Int, Int] = Result.Error(42) - t.equal(foo.unwrap_or_else fn { 0 }, 42) - t.equal(bar.unwrap_or_else fn { 0 }, 0) + t.equal(foo.or_else fn (_) { 0 }, 42) + t.equal(bar.or_else fn (_) { 0 }, 0) + t.equal(baz.or_else fn (v) { v }, 42) } t.test('Result.map') fn (t) { diff --git a/std/test/std/test_sys.inko b/std/test/std/test_sys.inko index b5b3a77ce..23c04262c 100644 --- a/std/test/std/test_sys.inko +++ b/std/test/std/test_sys.inko @@ -75,13 +75,13 @@ fn pub tests(t: mut Tests) { cmd.stdout(Stream.Piped) cmd.argument('--help') - let child = cmd.spawn.unwrap + let child = cmd.spawn.get - child.wait.unwrap + child.wait.get let bytes = ByteArray.new - child.stdout.read_all(bytes).unwrap + child.stdout.read_all(bytes).get t.true(bytes.into_string.contains?('Usage: inko')) } diff --git a/std/test/std/test_test.inko b/std/test/std/test_test.inko index 54ac6e492..7e0c65a7f 100644 --- a/std/test/std/test_test.inko +++ b/std/test/std/test_test.inko @@ -139,7 +139,7 @@ fn pub tests(t: mut Tests) { } t.test('Filter.from_string') fn (t) { - let exe = env.executable.unwrap + let exe = env.executable.get t.equal(Filter.from_string(''), Filter.None) t.equal(Filter.from_string('foo'), Filter.Pattern('foo'))