factis
Factis is an object that helps you responsibly track state across multi-step testing scenarios.
Installation
go get -u codeberg.org/ess/factis
How
facts := factis.New()
// Factis can memorize a fact
err := facts.Memorize("sausages", "gold")
// But once it learns something, it is set in its ways
err = facts.Memorize("sausages", "pork")
if err == nil {
fmt.Println("factis somehow overwrote a state item")
} else {
fmt.Println("that's a good little factis:", err)
}
// It's also really good at recalling things that it knows
sausages, err := facts.Recall("sausages")
if err == nil {
fmt.Println("sausages:", sausages)
} else {
fmt.Println("uh-oh, factis done forgot:", err)
}
// Of course, it's allowed to forget things that it knows
something, err := facts.Forget("sausages")
if err == nil {
fmt.Println("factis has forgotten that sausages are", something)
} else {
fmt.Println("dagnabbit, it's refusing to forget sausages:", err)
}
// It's not allowed to forget about things that it doesn't know, though
something, err = facts.Forget("everything you know about lampshades")
if err == nil {
fmt.Println("wait, factis isn't supposed to know about lampshades")
} else {
fmt.Println("can't fool ol' factis:", err)
}
Why?
So, I came over from the Ruby world some time back. Over there, it was widely known that tracking state via the normal means (basically globals) was a Bad Scene (TM) that nobody was doing anything about. So, I did something about it.
In short, tracking state via globals is dangerous for a few reasons, not the least of which being that you can't trust that what's in the global var you depend on hasn't been changed out-of-band.
I'm over here in the Go world more these days, and I have it on good authority that it's not a problem at all. We don't use globals for state tracking ... there are no globals!
Well, friend, here's the thing. The keyword isn't "global" here, it's "out-of-band."
So, here's factis to at least my rescue.
History