Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ test-rts:
test-rts-db:
$(MAKE) -C test

test-stdlib: dist/bin/acton
test-stdlib: distribution
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep test-stdlib from requiring full distribution build

Changing test-stdlib to depend on distribution makes this target pull in the full release graph (distribution1), including backend artifacts and $(DEPS) (via DIST_BACKEND_FILES/dist/bin/actondb), before running stdlib tests. That is a behavioral regression for developers/CI jobs that only expect stdlib tests and can now fail in offline/minimal environments or become much slower; a narrower prerequisite such as dist/base (plus dist/bin/acton) would preserve the test target’s scope.

Useful? React with 👍 / 👎.

cd compiler && stack test acton --ta '-p "stdlib"'
$(MAKE) -C test tls-test-server
cd test/stdlib_tests && "$(ACTON)" test
Expand Down
71 changes: 71 additions & 0 deletions base/src/result.act
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@


class Result[T,E] (value):
def is_ok(self) -> bool:
raise NotImplementedError("Result.is_ok")
def is_err(self) -> bool:
return not self.is_ok()
def ok(self) -> ?T:
raise NotImplementedError("Result.ok")
def err(self) -> ?E:
raise NotImplementedError("Result.err")
def unwrap(self) -> T:
raise NotImplementedError("Result.unwrap")
def unwrap_err(self) -> E:
raise NotImplementedError("Result.unwrap_err")
def __bool__(self) -> bool:
return self.is_ok()

class Ok[T,E] (Result[T,E]):
value: T

def __init__(self, value: T) -> None:
self.value = value

def is_ok(self) -> bool:
return True

def ok(self) -> ?T:
return self.value

def err(self) -> ?E:
return None

def unwrap(self) -> T:
return self.value

def unwrap_err(self) -> E:
raise ValueError("Called unwrap_err on {repr(self)}")

def __str__(self) -> str:
return "Ok({self.value})"

def __repr__(self) -> str:
return "Ok({repr(self.value)})"

class Err[T,E] (Result[T,E]):
error: E

def __init__(self, error: E) -> None:
self.error = error

def is_ok(self) -> bool:
return False

def ok(self) -> ?T:
return None

def err(self) -> ?E:
return self.error

def unwrap(self) -> T:
raise ValueError("Called unwrap on {repr(self)}")

def unwrap_err(self) -> E:
return self.error

def __str__(self) -> str:
return "Err({self.error})"

def __repr__(self) -> str:
return "Err({repr(self.error)})"
44 changes: 44 additions & 0 deletions test/stdlib_tests/src/test_result.act
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import result
import testing

def _test_result_ok():
ok = result.Ok(7)

testing.assertTrue(ok.is_ok())
testing.assertFalse(ok.is_err())
testing.assertTrue(bool(ok))
testing.assertEqual(ok.ok(), 7)
testing.assertEqual(ok.err(), None)
testing.assertEqual(ok.unwrap(), 7)
testing.assertEqual(str(ok), "Ok(7)")
testing.assertEqual(repr(ok), "Ok(7)")


def _test_result_err():
err = result.Err("boom")

testing.assertFalse(err.is_ok())
testing.assertTrue(err.is_err())
testing.assertFalse(bool(err))
testing.assertEqual(err.ok(), None)
testing.assertEqual(err.err(), "boom")
testing.assertEqual(err.unwrap_err(), "boom")
testing.assertEqual(str(err), "Err(boom)")
testing.assertEqual(repr(err), "Err('boom')")


def _test_result_unwrap_errors():
ok = result.Ok(7)
err = result.Err("boom")

try:
ok.unwrap_err()
raise testing.NotRaisesError("Ok.unwrap_err() should raise ValueError")
except ValueError:
pass

try:
err.unwrap()
raise testing.NotRaisesError("Err.unwrap() should raise ValueError")
except ValueError:
pass
Loading