aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-08-20 17:06:38 +0900
committerOphestra <cat@gensokyo.uk>2025-08-20 17:06:38 +0900
commit31f0dd36df3ad5574ac4e9cab19b943f5c129ea2 (patch)
tree4e7641ce495e60c4979e291f771a212f4ffe758c
parent9aec2f46fe775e1e885df32ed77bdef125aa5391 (diff)
absolute: efficient equivalence check method
This is more efficient and makes the call site cleaner. Signed-off-by: Ophestra <cat@gensokyo.uk>
-rw-r--r--container/absolute.go9
-rw-r--r--container/absolute_test.go25
2 files changed, 33 insertions, 1 deletions
diff --git a/container/absolute.go b/container/absolute.go
index 8f1fa23b..637b035c 100644
--- a/container/absolute.go
+++ b/container/absolute.go
@@ -39,6 +39,15 @@ func (a *Absolute) String() string {
return a.pathname
}
+func (a *Absolute) Is(v *Absolute) bool {
+ if a == nil && v == nil {
+ return true
+ }
+ return a != nil && v != nil &&
+ a.pathname != zeroString && v.pathname != zeroString &&
+ a.pathname == v.pathname
+}
+
// NewAbs checks pathname and returns a new [Absolute] if pathname is absolute.
func NewAbs(pathname string) (*Absolute, error) {
if !isAbs(pathname) {
diff --git a/container/absolute_test.go b/container/absolute_test.go
index b3682516..e04ad5ca 100644
--- a/container/absolute_test.go
+++ b/container/absolute_test.go
@@ -69,7 +69,7 @@ func TestNewAbs(t *testing.T) {
wantPanic := `path "etc" is not absolute`
if r := recover(); r != wantPanic {
- t.Errorf("MustAbsolute: panic = %v; want %v", r, wantPanic)
+ t.Errorf("MustAbs: panic = %v; want %v", r, wantPanic)
}
}()
@@ -98,6 +98,29 @@ func TestAbsoluteString(t *testing.T) {
})
}
+func TestAbsoluteIs(t *testing.T) {
+ testCases := []struct {
+ name string
+ a, v *Absolute
+ want bool
+ }{
+ {"nil", (*Absolute)(nil), (*Absolute)(nil), true},
+ {"nil a", (*Absolute)(nil), MustAbs("/"), false},
+ {"nil v", MustAbs("/"), (*Absolute)(nil), false},
+ {"zero", new(Absolute), new(Absolute), false},
+ {"zero a", new(Absolute), MustAbs("/"), false},
+ {"zero v", MustAbs("/"), new(Absolute), false},
+ {"equals", MustAbs("/"), MustAbs("/"), true},
+ }
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := tc.a.Is(tc.v); got != tc.want {
+ t.Errorf("Is: %v, want %v", got, tc.want)
+ }
+ })
+ }
+}
+
type sCheck struct {
Pathname *Absolute `json:"val"`
Magic int `json:"magic"`