Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ef081859 authored by Paul Duffin's avatar Paul Duffin
Browse files

Add support for converting OptionalPath to Paths

Appending a Path or Paths to a slice of Paths is simple but appending
an OptionalPath requires conditional logic which makes OptionalPaths
harder to use. This change makes it easy to append the embedded Path,
if any, to a slice of Paths.

Bug: 179354495
Test: m nothing
Change-Id: Ibf80a23043c846162e17c3a98b2590bca653b170
parent 475daafb
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -287,6 +287,17 @@ func (p OptionalPath) Path() Path {
	return p.path
}

// AsPaths converts the OptionalPath into Paths.
//
// It returns nil if this is not valid, or a single length slice containing the Path embedded in
// this OptionalPath.
func (p OptionalPath) AsPaths() Paths {
	if !p.valid {
		return nil
	}
	return Paths{p.path}
}

// RelativeToTop returns an OptionalPath with the path that was embedded having been replaced by the
// result of calling Path.RelativeToTop on it.
func (p OptionalPath) RelativeToTop() OptionalPath {
+22 −0
Original line number Diff line number Diff line
@@ -141,6 +141,9 @@ func TestOptionalPath(t *testing.T) {

	path = OptionalPathForPath(nil)
	checkInvalidOptionalPath(t, path)

	path = OptionalPathForPath(PathForTesting("path"))
	checkValidOptionalPath(t, path, "path")
}

func checkInvalidOptionalPath(t *testing.T, path OptionalPath) {
@@ -151,6 +154,10 @@ func checkInvalidOptionalPath(t *testing.T, path OptionalPath) {
	if path.String() != "" {
		t.Errorf("Uninitialized OptionalPath String() should return \"\", not %q", path.String())
	}
	paths := path.AsPaths()
	if len(paths) != 0 {
		t.Errorf("Uninitialized OptionalPath AsPaths() should return empty Paths, not %q", paths)
	}
	defer func() {
		if r := recover(); r == nil {
			t.Errorf("Expected a panic when calling Path() on an uninitialized OptionalPath")
@@ -159,6 +166,21 @@ func checkInvalidOptionalPath(t *testing.T, path OptionalPath) {
	path.Path()
}

func checkValidOptionalPath(t *testing.T, path OptionalPath, expectedString string) {
	t.Helper()
	if !path.Valid() {
		t.Errorf("Initialized OptionalPath should not be invalid")
	}
	if path.String() != expectedString {
		t.Errorf("Initialized OptionalPath String() should return %q, not %q", expectedString, path.String())
	}
	paths := path.AsPaths()
	if len(paths) != 1 {
		t.Errorf("Initialized OptionalPath AsPaths() should return Paths with length 1, not %q", paths)
	}
	path.Path()
}

func check(t *testing.T, testType, testString string,
	got interface{}, err []error,
	expected interface{}, expectedErr []error) {