Skip to content

Commit

Permalink
TreeView constructor test progress
Browse files Browse the repository at this point in the history
  • Loading branch information
huntercfreeman committed May 1, 2024
1 parent caa472e commit aa1468d
Show file tree
Hide file tree
Showing 19 changed files with 688 additions and 32 deletions.
17 changes: 17 additions & 0 deletions Source/Lib/Common/FileSystems/Models/IAbsolutePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,21 @@ public interface IAbsolutePath : IPath
{
public IFileSystemDrive? RootDrive { get; }
public bool IsRootDirectory { get; }

/// <summary>
/// One might prefer <see cref="IEnvironmentProvider.AbsolutePathFactory(string, bool)"/> instead
/// of this method.<br/><br/>
///
/// This version adds an extra function invocation for no reason. To use
/// <see cref="IEnvironmentProvider.AbsolutePathFactory(string, bool)"/> may be a negligible
/// optimization however.<br/><br/>
///
/// Keep this method here, it provides more clear documentation on how to create an instance
/// of <see cref="IAbsolutePath"/>. Having to invoke a method on the <see cref="IEnvironmentProvider"/>
/// is a bit hard to find.
/// </summary>
public static IAbsolutePath Factory(string path, bool isDirectory, IEnvironmentProvider environmentProvider)
{
return environmentProvider.AbsolutePathFactory(path, isDirectory);
}
}
17 changes: 17 additions & 0 deletions Source/Lib/Common/FileSystems/Models/IRelativePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,21 @@ public interface IRelativePath : IPath
/// Given "../../../Homework/math.txt". The count is 3.
/// </summary>
public int UpDirDirectiveCount { get; }

/// <summary>
/// One might prefer <see cref="IEnvironmentProvider.RelativePathFactory(string, bool)"/> instead
/// of this method.<br/><br/>
///
/// This version adds an extra function invocation for no reason. To use
/// <see cref="IEnvironmentProvider.RelativePathFactory(string, bool)"/> may be a negligible
/// optimization however.<br/><br/>
///
/// Keep this method here, it provides more clear documentation on how to create an instance
/// of <see cref="IRelativePath"/>. Having to invoke a method on the <see cref="IEnvironmentProvider"/>
/// is a bit hard to find.
/// </summary>
public static IRelativePath Factory(string path, bool isDirectory, IEnvironmentProvider environmentProvider)
{
return environmentProvider.RelativePathFactory(path, isDirectory);
}
}
122 changes: 122 additions & 0 deletions Source/Tests/Ide/Basis/IdeTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using Fluxor;
using Luthetus.Common.RazorLib.BackgroundTasks.Models;
using Luthetus.Common.RazorLib.Installations.Models;
using Luthetus.Common.RazorLib.Misc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.JSInterop;
using Luthetus.Ide.RazorLib.Installations.Models;
using Luthetus.TextEditor.RazorLib.Installations.Models;
using Luthetus.Common.RazorLib.FileSystems.Models;
using Luthetus.Common.RazorLib.Exceptions;

namespace Luthetus.Ide.Tests.Basis;

public class IdeTestBase
{
protected static void Test_RegisterServices(out ServiceProvider serviceProvider)
{
var backgroundTaskService = new BackgroundTaskServiceSynchronous();

var serviceCollection = new ServiceCollection()
.AddScoped<IJSRuntime, DoNothingJsRuntime>()
.AddLuthetusIdeRazorLibServices(new LuthetusHostingInformation(LuthetusHostingKind.UnitTesting, backgroundTaskService))
.AddFluxor(options => options.ScanAssemblies(
typeof(LuthetusCommonConfig).Assembly,
typeof(LuthetusTextEditorConfig).Assembly,
typeof(LuthetusIdeConfig).Assembly));

serviceProvider = serviceCollection.BuildServiceProvider();

var store = serviceProvider.GetRequiredService<IStore>();
store.InitializeAsync().Wait();

var environmentProvider = serviceProvider.GetRequiredService<IEnvironmentProvider>();
var fileSystemProvider = serviceProvider.GetRequiredService<IFileSystemProvider>();

if (environmentProvider.GetType() != typeof(InMemoryEnvironmentProvider))
{
throw new LuthetusCommonException($"When unit testing one must use the {nameof(InMemoryEnvironmentProvider)} " +
$"implementation of {nameof(IEnvironmentProvider)}. This will avoid any side effects from running the tests.");
}

if (fileSystemProvider.GetType() != typeof(InMemoryFileSystemProvider))
{
throw new LuthetusCommonException($"When unit testing one must use the {nameof(InMemoryFileSystemProvider)} " +
$"implementation of {nameof(IFileSystemProvider)} This will avoid any side effects from running the tests.");
}
}

/// <summary>
/// The files shown in the 'tree' below will be written to the in-memory filesystem.
/// ---------------------------------------------<br/>
/// Root<br/>
/// ∙└───Homework<br/>
/// ∙∙∙∙∙∙├───Math<br/>
/// ∙∙∙∙∙∙│∙∙∙∙├───addition.txt<br/>
/// ∙∙∙∙∙∙│∙∙∙∙└───subtraction.txt<br/>
/// ∙∙∙∙∙∙│<br/>
/// ∙∙∙∙∙∙└───Biology<br/>
/// ∙∙∙∙∙∙∙∙∙∙∙├───nervousSystem.txt<br/>
/// ∙∙∙∙∙∙∙∙∙∙∙└───skeletalSystem.txt<br/>
/// </summary>
protected static void Test_CreateFileSystem(IServiceProvider serviceProvider)
{
// Cast as the in-memory provider to ensure the actual filesystem will not be used.
InMemoryEnvironmentProvider environmentProvider =
(InMemoryEnvironmentProvider)serviceProvider.GetRequiredService<IEnvironmentProvider>();

// Cast as the in-memory provider to ensure the actual filesystem will not be used.
InMemoryFileSystemProvider fileSystemProvider =
(InMemoryFileSystemProvider)serviceProvider.GetRequiredService<IFileSystemProvider>();

WriteToInMemoryFileSystem(
environmentProvider,
fileSystemProvider);
}

private static void WriteToInMemoryFileSystem(
InMemoryEnvironmentProvider inMemoryEnvironmentProvider,
InMemoryFileSystemProvider inMemoryFileSystemProvider)
{
var dsc = inMemoryEnvironmentProvider.DirectorySeparatorChar;

inMemoryFileSystemProvider.File.WriteAllTextAsync(
$"{dsc}Homework{dsc}Math{dsc}addition.txt",
"3 + 7 = 10");

inMemoryFileSystemProvider.File.WriteAllTextAsync(
$"{dsc}Homework{dsc}Math{dsc}subtraction.txt",
"10 - 3 = 7");

inMemoryFileSystemProvider.File.WriteAllTextAsync(
$"{dsc}Homework{dsc}Biology{dsc}nervousSystem.txt",
"The nervous system is...");

inMemoryFileSystemProvider.File.WriteAllTextAsync(
$"{dsc}Homework{dsc}Biology{dsc}skeletalSystem.txt",
"The skeletal system is...");
}

protected class Test_WellKnownPaths
{
public class Directories
{
public const string Root = "/";
public const string Homework = "/Homework/";
public const string Math = "/Homework/Math/";
public const string Biology = "/Homework/Biology/";

public const string NonExistingDirectory = "/Homework/Hamburger/";
}

public class Files
{
public const string AdditionTxt = "/Homework/Math/addition.txt";
public const string SubtractionTxt = "/Homework/Math/subtraction.txt";
public const string NervousSystemTxt = "/Homework/Biology/nervousSystem.txt";
public const string SkeletalSystemTxt = "/Homework/Biology/skeletalSystem.txt";

public const string NonExistingFile = "/Homework/Hamburger/recipe.txt";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
using Luthetus.Common.RazorLib.TreeViews.Models;
using Luthetus.Ide.RazorLib.TreeViewImplementations.Models;
using Luthetus.Ide.RazorLib.ComponentRenderers.Models;
using Microsoft.Extensions.DependencyInjection;

namespace Luthetus.Ide.Tests.Basis.TreeViewImplementations.Models;

/// <summary>
/// <see cref="TreeViewAbsolutePath"/>
/// </summary>
public class TreeViewAbsolutePathTests
public class TreeViewAbsolutePathTests : IdeTestBase
{
/// <summary>
/// <see cref="TreeViewAbsolutePath(IAbsolutePath, ILuthetusIdeComponentRenderers, ILuthetusCommonComponentRenderers, IFileSystemProvider, IEnvironmentProvider, bool, bool)"/>
Expand All @@ -22,7 +23,35 @@ public class TreeViewAbsolutePathTests
[Fact]
public void Constructor()
{
throw new NotImplementedException();
Test_RegisterServices(out var serviceProvider);
Test_CreateFileSystem(serviceProvider);

var ideComponentRenderers = serviceProvider.GetRequiredService<ILuthetusIdeComponentRenderers>();
var commonComponentRenderers = serviceProvider.GetRequiredService<ILuthetusCommonComponentRenderers>();
var fileSystemProvider = serviceProvider.GetRequiredService<IFileSystemProvider>();
var environmentProvider = serviceProvider.GetRequiredService<IEnvironmentProvider>();

var absolutePath = environmentProvider.AbsolutePathFactory(Test_WellKnownPaths.Files.NervousSystemTxt, false);

var isExpandable = true;
var isExpanded = true;

var treeView = new TreeViewAbsolutePath(
absolutePath,
ideComponentRenderers,
commonComponentRenderers,
fileSystemProvider,
environmentProvider,
isExpandable,
isExpanded);

Assert.Equal(treeView.Item, absolutePath);
Assert.Equal(treeView.IdeComponentRenderers, ideComponentRenderers);
Assert.Equal(treeView.CommonComponentRenderers, commonComponentRenderers);
Assert.Equal(treeView.FileSystemProvider, fileSystemProvider);
Assert.Equal(treeView.EnvironmentProvider, environmentProvider);
Assert.Equal(treeView.IsExpandable, isExpandable);
Assert.Equal(treeView.IsExpanded, isExpanded);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
using Luthetus.CompilerServices.Lang.DotNetSolution.Models.Project;
using Luthetus.Ide.RazorLib.TreeViewImplementations.Models;
using Luthetus.Ide.RazorLib.ComponentRenderers.Models;
using Luthetus.Common.RazorLib.ComponentRenderers.Models;
using Luthetus.Common.RazorLib.Namespaces.Models;
using Microsoft.Extensions.DependencyInjection;

namespace Luthetus.Ide.Tests.Basis.TreeViewImplementations.Models;

/// <summary>
/// <see cref="TreeViewCSharpProjectDependencies"/>
/// </summary>
public class TreeViewCSharpProjectDependenciesTests
public class TreeViewCSharpProjectDependenciesTests : IdeTestBase
{
/// <summary>
/// <see cref="TreeViewCSharpProjectDependencies(CSharpProjectDependencies, ILuthetusIdeComponentRenderers, IFileSystemProvider, IEnvironmentProvider, bool, bool)"/>
Expand All @@ -21,7 +24,36 @@ public class TreeViewCSharpProjectDependenciesTests
[Fact]
public void Constructor()
{
throw new NotImplementedException();
Test_RegisterServices(out var serviceProvider);
Test_CreateFileSystem(serviceProvider);

var ideComponentRenderers = serviceProvider.GetRequiredService<ILuthetusIdeComponentRenderers>();
var commonComponentRenderers = serviceProvider.GetRequiredService<ILuthetusCommonComponentRenderers>();
var fileSystemProvider = serviceProvider.GetRequiredService<IFileSystemProvider>();
var environmentProvider = serviceProvider.GetRequiredService<IEnvironmentProvider>();

var cSharpProjectDependencies = new CSharpProjectDependencies(
new NamespacePath(
"ProjectOne",
environmentProvider.AbsolutePathFactory("/ProjectOne/ProjectOne.csproj", false)));

var isExpandable = true;
var isExpanded = true;

var treeView = new TreeViewCSharpProjectDependencies(
cSharpProjectDependencies,
ideComponentRenderers,
fileSystemProvider,
environmentProvider,
isExpandable,
isExpanded);

Assert.Equal(treeView.Item, cSharpProjectDependencies);
Assert.Equal(treeView.IdeComponentRenderers, ideComponentRenderers);
Assert.Equal(treeView.FileSystemProvider, fileSystemProvider);
Assert.Equal(treeView.EnvironmentProvider, environmentProvider);
Assert.Equal(treeView.IsExpandable, isExpandable);
Assert.Equal(treeView.IsExpanded, isExpanded);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
using Luthetus.Ide.RazorLib.TreeViewImplementations.Models;
using Luthetus.Ide.RazorLib.Nugets.Models;
using Luthetus.Ide.RazorLib.ComponentRenderers.Models;
using Luthetus.Common.RazorLib.ComponentRenderers.Models;
using Luthetus.Common.RazorLib.Namespaces.Models;
using Luthetus.CompilerServices.Lang.DotNetSolution.Models.Project;
using Microsoft.Extensions.DependencyInjection;

namespace Luthetus.Ide.Tests.Basis.TreeViewImplementations.Models;

/// <summary>
/// <see cref="TreeViewCSharpProjectNugetPackageReference"/>
/// </summary>
public class TreeViewCSharpProjectNugetPackageReferenceTests
public class TreeViewCSharpProjectNugetPackageReferenceTests : IdeTestBase
{
/// <summary>
/// <see cref="TreeViewCSharpProjectNugetPackageReference(CSharpProjectNugetPackageReference, ILuthetusIdeComponentRenderers, IFileSystemProvider, IEnvironmentProvider, bool, bool)"/>
Expand All @@ -21,7 +25,35 @@ public class TreeViewCSharpProjectNugetPackageReferenceTests
[Fact]
public void Constructor()
{
throw new NotImplementedException();
Test_RegisterServices(out var serviceProvider);
Test_CreateFileSystem(serviceProvider);

var ideComponentRenderers = serviceProvider.GetRequiredService<ILuthetusIdeComponentRenderers>();
var commonComponentRenderers = serviceProvider.GetRequiredService<ILuthetusCommonComponentRenderers>();
var fileSystemProvider = serviceProvider.GetRequiredService<IFileSystemProvider>();
var environmentProvider = serviceProvider.GetRequiredService<IEnvironmentProvider>();

var cSharpProjectNugetPackageReference = new CSharpProjectNugetPackageReference(
"/ProjectOne/ProjectOne.csproj",
new LightWeightNugetPackageRecord(string.Empty, string.Empty, string.Empty));

var isExpandable = true;
var isExpanded = true;

var treeView = new TreeViewCSharpProjectNugetPackageReference(
cSharpProjectNugetPackageReference,
ideComponentRenderers,
fileSystemProvider,
environmentProvider,
isExpandable,
isExpanded);

Assert.Equal(treeView.Item, cSharpProjectNugetPackageReference);
Assert.Equal(treeView.IdeComponentRenderers, ideComponentRenderers);
Assert.Equal(treeView.FileSystemProvider, fileSystemProvider);
Assert.Equal(treeView.EnvironmentProvider, environmentProvider);
Assert.Equal(treeView.IsExpandable, isExpandable);
Assert.Equal(treeView.IsExpanded, isExpanded);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
using Luthetus.CompilerServices.Lang.DotNetSolution.Models.Project;
using Luthetus.Ide.RazorLib.TreeViewImplementations.Models;
using Luthetus.Ide.RazorLib.ComponentRenderers.Models;
using Microsoft.Extensions.DependencyInjection;
using Luthetus.Common.RazorLib.Namespaces.Models;
using Luthetus.Common.RazorLib.ComponentRenderers.Models;

namespace Luthetus.Ide.Tests.Basis.TreeViewImplementations.Models;

/// <summary>
/// <see cref="TreeViewCSharpProjectNugetPackageReferences"/>
/// </summary>
public class TreeViewCSharpProjectNugetPackageReferencesTests
public class TreeViewCSharpProjectNugetPackageReferencesTests : IdeTestBase
{
/// <summary>
/// <see cref="TreeViewCSharpProjectNugetPackageReferences(CSharpProjectNugetPackageReferences, ILuthetusIdeComponentRenderers, IFileSystemProvider, IEnvironmentProvider, bool, bool)"/>
Expand All @@ -21,7 +24,36 @@ public class TreeViewCSharpProjectNugetPackageReferencesTests
[Fact]
public void Constructor()
{
throw new NotImplementedException();
Test_RegisterServices(out var serviceProvider);
Test_CreateFileSystem(serviceProvider);

var ideComponentRenderers = serviceProvider.GetRequiredService<ILuthetusIdeComponentRenderers>();
var commonComponentRenderers = serviceProvider.GetRequiredService<ILuthetusCommonComponentRenderers>();
var fileSystemProvider = serviceProvider.GetRequiredService<IFileSystemProvider>();
var environmentProvider = serviceProvider.GetRequiredService<IEnvironmentProvider>();

var cSharpProjectNugetPackageReferences = new CSharpProjectNugetPackageReferences(
new NamespacePath(
"ProjectOne",
environmentProvider.AbsolutePathFactory("/ProjectOne/ProjectOne.csproj", false)));

var isExpandable = true;
var isExpanded = true;

var treeView = new TreeViewCSharpProjectNugetPackageReferences(
cSharpProjectNugetPackageReferences,
ideComponentRenderers,
fileSystemProvider,
environmentProvider,
isExpandable,
isExpanded);

Assert.Equal(treeView.Item, cSharpProjectNugetPackageReferences);
Assert.Equal(treeView.IdeComponentRenderers, ideComponentRenderers);
Assert.Equal(treeView.FileSystemProvider, fileSystemProvider);
Assert.Equal(treeView.EnvironmentProvider, environmentProvider);
Assert.Equal(treeView.IsExpandable, isExpandable);
Assert.Equal(treeView.IsExpanded, isExpanded);
}

/// <summary>
Expand Down
Loading

0 comments on commit aa1468d

Please sign in to comment.