Skip to content

Commit

Permalink
Test framework: Return fully typed TestPackageManager from TestDub
Browse files Browse the repository at this point in the history
Using covariant return type, we can avoid the need to change the base class,
and leave all the overloading in the child class.
  • Loading branch information
Geod24 committed Dec 28, 2023
1 parent b044acc commit 518b585
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
15 changes: 0 additions & 15 deletions source/dub/packagemanager.d
Original file line number Diff line number Diff line change
Expand Up @@ -243,21 +243,6 @@ class PackageManager {
return this.lookup(name, ver);
}

/**
* Adds a `Package` to this `PackageManager`
*
* This is currently only available in unittests as it is a convenience
* function used by `TestDub`, but could be generalized once IO has been
* abstracted away from this class.
*/
version (unittest) Package add(Package pkg)
{
// See `PackageManager.addPackages` for inspiration.
assert(!pkg.subPackages.length, "Subpackages are not yet supported");
this.m_internal.fromPath ~= pkg;
return pkg;
}

/// ditto
deprecated("Use the overload that accepts a `Version` as second argument")
Package getPackage(string name, string ver, bool enable_overrides = true)
Expand Down
26 changes: 26 additions & 0 deletions source/dub/test/base.d
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ public class TestDub : Dub
/// Reintroduce parent overloads
public alias loadPackage = Dub.loadPackage;

/**
* Returns a fully typed `TestPackageManager`
*
* This exposes the fully typed `PackageManager`, so that client
* can call convenience functions on it directly.
*/
public override @property inout(TestPackageManager) packageManager() inout
{
return cast(inout(TestPackageManager)) this.m_packageManager;
}

/**
* Creates a package with the provided recipe
*
Expand Down Expand Up @@ -212,6 +223,21 @@ package class TestPackageManager : PackageManager

return null;
}

/**
* Adds a `Package` to this `PackageManager`
*
* This is currently only available in unittests as it is a convenience
* function used by `TestDub`, but could be generalized once IO has been
* abstracted away from this class.
*/
public Package add(Package pkg)
{
// See `PackageManager.addPackages` for inspiration.
assert(!pkg.subPackages.length, "Subpackages are not yet supported");
this.m_internal.fromPath ~= pkg;
return pkg;
}
}

/**
Expand Down

0 comments on commit 518b585

Please sign in to comment.