go/action_example_test.go
Snider 5be20af4b0 feat: eliminate fmt, string concat — add core.Println, use Concat/Path everywhere
New primitive: core.Println() wraps fmt.Println.

Replaced across all test + example files:
- fmt.Println → Println (17 example files)
- fmt.Sprintf → Concat + Sprint
- dir + "/file" → Path(dir, "file") (path security)
- "str" + var → Concat("str", var) (AX consistency)

"fmt" import is now zero across all test files.
String concat with + is zero across all test files.

Remaining 9 stdlib imports (all Go infrastructure):
testing, context, time, sync, embed, io/fs, bytes, gzip, base64

558 tests, 84.5% coverage.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-25 19:42:39 +00:00

59 lines
1.3 KiB
Go

package core_test
import (
"context"
. "dappco.re/go/core"
)
func ExampleAction_Run() {
c := New()
c.Action("double", func(_ context.Context, opts Options) Result {
return Result{Value: opts.Int("n") * 2, OK: true}
})
r := c.Action("double").Run(context.Background(), NewOptions(
Option{Key: "n", Value: 21},
))
Println(r.Value)
// Output: 42
}
func ExampleAction_Exists() {
c := New()
Println(c.Action("missing").Exists())
c.Action("present", func(_ context.Context, _ Options) Result { return Result{OK: true} })
Println(c.Action("present").Exists())
// Output:
// false
// true
}
func ExampleAction_Run_panicRecovery() {
c := New()
c.Action("boom", func(_ context.Context, _ Options) Result {
panic("explosion")
})
r := c.Action("boom").Run(context.Background(), NewOptions())
Println(r.OK)
// Output: false
}
func ExampleAction_Run_entitlementDenied() {
c := New()
c.Action("premium", func(_ context.Context, _ Options) Result {
return Result{Value: "secret", OK: true}
})
c.SetEntitlementChecker(func(action string, _ int, _ context.Context) Entitlement {
if action == "premium" {
return Entitlement{Allowed: false, Reason: "upgrade"}
}
return Entitlement{Allowed: true, Unlimited: true}
})
r := c.Action("premium").Run(context.Background(), NewOptions())
Println(r.OK)
// Output: false
}