From c277d77899682c05a50cbdaffec09f250669d943 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 7 Jun 2026 11:23:14 +0000 Subject: object/tree/mode: Other tests --- object/tree/mode/mode_test.go | 83 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 object/tree/mode/mode_test.go (limited to 'object/tree') diff --git a/object/tree/mode/mode_test.go b/object/tree/mode/mode_test.go new file mode 100644 index 00000000..0e2cc2aa --- /dev/null +++ b/object/tree/mode/mode_test.go @@ -0,0 +1,83 @@ +package mode_test + +import ( + "testing" + + "lindenii.org/go/furgit/object/tree/mode" + "lindenii.org/go/furgit/object/typ" +) + +func TestObjectType(t *testing.T) { + t.Parallel() + + for _, tc := range []struct { + mode mode.Mode + want typ.Type + }{ + {mode: mode.Directory, want: typ.TypeTree}, + {mode: mode.Regular, want: typ.TypeBlob}, + {mode: mode.Executable, want: typ.TypeBlob}, + {mode: mode.Symlink, want: typ.TypeBlob}, + {mode: mode.Gitlink, want: typ.TypeCommit}, + {mode: mode.Mode(0), want: typ.TypeUnknown}, + } { + if got := tc.mode.ObjectType(); got != tc.want { + t.Fatalf("Mode(%o).ObjectType() = %v, want %v", tc.mode, got, tc.want) + } + } +} + +func TestPredicates(t *testing.T) { + t.Parallel() + + for _, tc := range []struct { + mode mode.Mode + valid bool + isBlobLike bool + isRegularFile bool + }{ + {mode: mode.Directory, valid: true, isBlobLike: false, isRegularFile: false}, + {mode: mode.Regular, valid: true, isBlobLike: true, isRegularFile: true}, + {mode: mode.Executable, valid: true, isBlobLike: true, isRegularFile: true}, + {mode: mode.Symlink, valid: true, isBlobLike: true, isRegularFile: false}, + {mode: mode.Gitlink, valid: true, isBlobLike: false, isRegularFile: false}, + {mode: mode.Mode(0), valid: false, isBlobLike: false, isRegularFile: false}, + {mode: mode.Mode(0o100640), valid: false, isBlobLike: false, isRegularFile: false}, + } { + if got := tc.mode.IsValid(); got != tc.valid { + t.Fatalf("Mode(%o).IsValid() = %v, want %v", tc.mode, got, tc.valid) + } + + if got := tc.mode.IsBlobLike(); got != tc.isBlobLike { + t.Fatalf("Mode(%o).IsBlobLike() = %v, want %v", tc.mode, got, tc.isBlobLike) + } + + if got := tc.mode.IsRegularFile(); got != tc.isRegularFile { + t.Fatalf("Mode(%o).IsRegularFile() = %v, want %v", tc.mode, got, tc.isRegularFile) + } + } +} + +func TestHasSameType(t *testing.T) { + t.Parallel() + + for _, tc := range []struct { + a mode.Mode + b mode.Mode + want bool + }{ + {a: mode.Regular, b: mode.Regular, want: true}, + {a: mode.Regular, b: mode.Executable, want: true}, + {a: mode.Executable, b: mode.Regular, want: true}, + {a: mode.Regular, b: mode.Symlink, want: false}, + {a: mode.Directory, b: mode.Directory, want: true}, + {a: mode.Directory, b: mode.Regular, want: false}, + {a: mode.Symlink, b: mode.Symlink, want: true}, + {a: mode.Gitlink, b: mode.Gitlink, want: true}, + {a: mode.Gitlink, b: mode.Directory, want: false}, + } { + if got := tc.a.HasSameType(tc.b); got != tc.want { + t.Fatalf("Mode(%o).HasSameType(%o) = %v, want %v", tc.a, tc.b, got, tc.want) + } + } +} -- cgit v1.3.1-10-gc9f91