Author: Adam <git@apiote.xyz>
add todo and assert
bagend.go | 33 +++++++++++++++++++++++++++++++++ bagend_test.go | 39 +++++++++++++++++++++++++++++++++++++++ go.mod | 3 +++
diff --git a/bagend.go b/bagend.go new file mode 100644 index 0000000000000000000000000000000000000000..aa8dab1cd80650f176654131f6b824f727ab83a0 --- /dev/null +++ b/bagend.go @@ -0,0 +1,33 @@ +package bagend + +import ( + "fmt" + "runtime" +) + +func TODO(msg string) interface{} { + _, file, no, ok := runtime.Caller(1) + var panicmsg string + if ok { + panicmsg = fmt.Sprintf("TODO %s (%s#%d)", msg, file, no) + } else { + panicmsg = fmt.Sprintf("TODO %s", msg) + } + panic(panicmsg) +} + +func Assert(cond bool, msg string) { + if !cond { + _, file, no, ok := runtime.Caller(1) + var panicmsg string + if ok { + panicmsg = fmt.Sprintf("assertion in %s#%d failed", file, no) + } else { + panicmsg = fmt.Sprintf("assertion failed") + } + if msg != "" { + panicmsg = fmt.Sprintf("%s (%s)", panicmsg, msg) + } + panic(panicmsg) + } +} diff --git a/bagend_test.go b/bagend_test.go new file mode 100644 index 0000000000000000000000000000000000000000..031f7e1888a378f14be56ffdac4a521d51cc7dae --- /dev/null +++ b/bagend_test.go @@ -0,0 +1,39 @@ +package bagend + +import ( + "testing" +) + +func TestTodo(t *testing.T) { + defer func() { + if err := recover(); err == nil { + t.Errorf("expected a panic\n") + } + }() + + TODO("test") +} + +func TestAssertTrue(t *testing.T) { + defer func() { + if err := recover(); err != nil { + t.Errorf("didn’t expect a panic\n") + } + }() + + Assert(2+2 == 4, "2+2 must be equal 4") +} + +func TestAssertFalse(t *testing.T) { + defer func() { + if err := recover(); err == nil { + t.Errorf("expected a panic\n") + } + }() + + Assert(2+2 == 5, "2+2 must be equal 4") +} + +func a() string { + return TODO("a").(string) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000000000000000000000000000000000000..4732a8678e8daf1b9d61f299d35aba92341d082c --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module apiote.xyz/p/bagend + +go 1.18