From 52bf759e7ff2426f8340e54c173cd840f3b858e1 Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L de Mello" Date: Wed, 14 May 2025 11:33:34 -0300 Subject: [PATCH] docs(tinyssert): add example of using dependency injection --- tinyssert/tinyssert.go | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tinyssert/tinyssert.go b/tinyssert/tinyssert.go index cf2b5a2..85ef2f8 100644 --- a/tinyssert/tinyssert.go +++ b/tinyssert/tinyssert.go @@ -52,6 +52,49 @@ // value := "not value" // assert.Equal(expected, value) // "expected \"value\", got \"not value\"" with the call stack and returns false // } +// +// Preferably, when using assertions inside production code or libraries, you can use +// the assertions via dependency injection. This provides a easy way to disable +// assertions in production (see [NewDisabled]) while being able to test an API without +// changing it: +// +// package main +// +// import ( +// "flag" +// "log/slog" +// +// "forge.capytal.company/loreddev/x/tinyssert" +// ) +// +// var debug = flag.Bool("debug", false, "Run the application in debug mode") +// +// func init() { +// flag.Parse() +// } +// +// func main() { +// logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{})) +// assert := tinyssert.NewDisabled() +// if *debug { +// assert := tinyssert.New(tinyssert.WithLogger(logger)) +// } +// +// app := App{logger: logger, assert: assert} +// +// app.Start() +// } +// +// type App struct { +// logger: *slog.Logger +// assert: tinyssert.Assertions +// } +// +// function (app *App) Start() { +// app.assert.OK(app.logger, "Logger must be initialized before the application") +// +// // ... +// } package tinyssert import (