Skip to content

Commit

Permalink
Add test to client generator (#18)
Browse files Browse the repository at this point in the history
* Deserializer tests

* Extensions tests

* Commands tests

* Generator tests
  • Loading branch information
lukmccall authored Jan 14, 2020
1 parent 573930b commit c5ef2eb
Show file tree
Hide file tree
Showing 51 changed files with 1,125 additions and 94 deletions.
6 changes: 6 additions & 0 deletions CookBook.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "logger", "logger\logger.csp
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "client-generator", "client-generator\client-generator.csproj", "{B628F3AE-7A95-49AD-81C6-0547C44AC75F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "client-generator-tests", "client-generator-tests\client-generator-tests.csproj", "{3542E4FD-9618-4957-B31A-6314BEE2B6AE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -25,5 +27,9 @@ Global
{B628F3AE-7A95-49AD-81C6-0547C44AC75F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B628F3AE-7A95-49AD-81C6-0547C44AC75F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B628F3AE-7A95-49AD-81C6-0547C44AC75F}.Release|Any CPU.Build.0 = Release|Any CPU
{3542E4FD-9618-4957-B31A-6314BEE2B6AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3542E4FD-9618-4957-B31A-6314BEE2B6AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3542E4FD-9618-4957-B31A-6314BEE2B6AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3542E4FD-9618-4957-B31A-6314BEE2B6AE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
48 changes: 48 additions & 0 deletions client-generator-tests/App/BackFileEntryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using client_generator.App;
using Xunit;

namespace client_generator_tests.App
{
public class BackFileEntryTest
{

[Fact]
public void CorrectPath()
{
// Arrange
var paths = new List<string>
{
"/path/a/b",
"/a/b/c/d",
"/a/b/c/d/../t/s",
};

// Act
var backPaths = paths.Select(x => new BackFileEntry(x).ParentDirectory);

// Arrange
Assert.Equal(paths.Select(x => Directory.GetParent(x).FullName), backPaths);
}

[Fact]
public void SlashPath()
{
// Arrange
var paths = new List<string>
{
"/",
"//"
};

// Act
var backPaths = paths.Select(x => new BackFileEntry(x).ParentDirectory);

// Arrange
Assert.Equal(paths.Select(x => x), backPaths);
}

}
}
45 changes: 45 additions & 0 deletions client-generator-tests/App/Commands/ChangeWindowCommandTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using client_generator.App;
using client_generator.App.Commands;
using Moq;
using Terminal.Gui;
using Xunit;

namespace client_generator_tests.App.Commands
{
public class ChangeWindowCommandTest
{

[Fact]
public void ChangeWindow()
{
// Arrange
var window = new Mock<Window>(null);
var appController = new Mock<IAppController>();
appController.Setup(x => x.ChangeWindow(window.Object)).Verifiable();
var command = new ChangeWindowCommand(appController.Object, window.Object);

// Act
command.Execute();

// Assert
appController.Verify(x => x.ChangeWindow(It.IsAny<Window>()), Times.Once);
appController.Verify(x => x.ChangeWindow(window.Object), Times.Once);
}

[Fact]
public void ConstructionShouldBeEffectless()
{
// Arrange
var window = new Mock<Window>(null);
var appController = new Mock<IAppController>();
appController.Setup(x => x.ChangeWindow(window.Object)).Verifiable();

// Act
var command = new ChangeWindowCommand(appController.Object, window.Object);

appController.Verify(x => x.ChangeWindow(It.IsAny<Window>()), Times.Never);

}

}
}
53 changes: 53 additions & 0 deletions client-generator-tests/App/Commands/CommandWithConnectedWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using client_generator.App;
using client_generator.App.Commands;
using client_generator.App.Windows;
using client_generator.Generators;
using Moq;
using Newtonsoft.Json;
using Terminal.Gui;
using Xunit;

namespace client_generator_tests.App.Commands
{
public class CommandWithConnectedWindow
{

[Fact]
public void OnExecuteShouldChangeWindow()
{
// Arrange
var window = new Mock<Window>(null);
var appController = new Mock<IAppController>();
appController.Setup(x => x.GetCurrentWindow()).Returns(window.Object);
appController.Setup(x => x.ChangeWindow(It.IsAny<Window>())).Verifiable();

var commands = new (Type type, ICommand command)[]
{
(typeof(GeneratorSettingsWindow),
new EditGeneratorSettingsCommand(appController.Object, new GeneratorSettings())),

(typeof(JsonSettingsWindow),
new EditJsonDeserializationSettingsCommand(appController.Object, new JsonSerializerSettings())),

(typeof(FileSelectorWindow),
new SelectFileCommand(appController.Object, entry => {} ))
};

// Act
foreach (var (_, command) in commands)
{
command.Execute();
}

// Assert
foreach (var (type, _) in commands)
{
appController.Verify(
x => x.ChangeWindow(It.Is<Window>(changedWindow =>
changedWindow.GetType() == type)), Times.Once);
}
}

}
}
42 changes: 42 additions & 0 deletions client-generator-tests/App/Commands/ExitCommandTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using client_generator.App;
using client_generator.App.Commands;
using Moq;
using Terminal.Gui;
using Xunit;

namespace client_generator_tests.App.Commands
{
public class ExitCommandTest
{

[Fact]
public void Exit()
{
// Arrange
var appController = new Mock<IAppController>();
appController.Setup(x => x.ExitApp()).Verifiable();
var command = new ExitAppCommand(appController.Object);

// Act
command.Execute();

// Assert
appController.Verify(x => x.ExitApp(), Times.Once);
}

[Fact]
public void ConstructionShouldBeEffectless()
{
// Arrange
var appController = new Mock<IAppController>();
appController.Setup(x => x.ExitApp()).Verifiable();

// Act
var command = new ExitAppCommand(appController.Object);

// Assert
appController.Verify(x => x.ExitApp(), Times.Never);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System.Collections.Generic;
using System.Linq;
using client_generator.Deserializer.Helpers.Builders;
using client_generator_tests.Helpers;
using Xunit;

namespace client_generator_tests.Deserializer.Builders
{
public class SuspendBuildsManagerTest
{

[Fact]
public void SimpleBuild()
{
// Arrange
var builds = new Dictionary<string, BuildableObject>
{
{"1", new BuildableObject {Id = "1", RequiredIds = new string[0]}}
};
var builder = new SuspendBuildsManager<BuildableObject, string>(builds, BuildableObject.BuildMethod);

// Act
var resultStatus = builder.Build();
var result = builder.GetResult();

// Assert
Assert.True(resultStatus, "Build should be possible");
Assert.Single(result.Values);
Assert.Equal("1", result["1"]);
}

[Fact]
public void MultiTaskBuild()
{
// Arrange
var builds = new Dictionary<string, BuildableObject>
{
{"1", new BuildableObject {Id = "a", RequiredIds = new string[0]}},
{"2", new BuildableObject {Id = "b", RequiredIds = new string[0]}},
{"3", new BuildableObject {Id = "c", RequiredIds = new string[0]}}
};
var builder = new SuspendBuildsManager<BuildableObject, string>(builds, BuildableObject.BuildMethod);

// Act
var resultStatus = builder.Build();
var result = builder.GetResult();

// Assert
Assert.True(resultStatus, "Build should be possible");
Assert.Equal(builds.Keys.Count, result.Keys.Count);
Assert.Equal(builds.Select(x => x.Value.Id), result.Select(x => x.Value));
}

[Fact]
public void MultiDependentTaskBuild()
{
// Arrange
var builds = new Dictionary<string, BuildableObject>
{
{"1", new BuildableObject {Id = "a", RequiredIds = new[] {"2"}}},
{"2", new BuildableObject {Id = "b", RequiredIds = new string[0]}},
{"3", new BuildableObject {Id = "c", RequiredIds = new[] {"4"}}},
{"4", new BuildableObject {Id = "d", RequiredIds = new string[0]}},
{"5", new BuildableObject {Id = "e", RequiredIds = new[] {"4"}}}
};
var builder = new SuspendBuildsManager<BuildableObject, string>(builds, BuildableObject.BuildMethod);

// Act
var resultStatus = builder.Build();
var result = builder.GetResult();

// Assert
Assert.True(resultStatus, "Build should be possible");
Assert.Equal(builds.Keys.Count, result.Keys.Count);
Assert.Equal(builds.Select(x => x.Value.Id).OrderBy(x => x),
result.Select(x => x.Value).OrderBy(x => x));
}


[Fact]
public void ImpossibleBuild()
{
// Arrange
var builds = new Dictionary<string, BuildableObject>
{
{"1", new BuildableObject {Id = "a", RequiredIds = new[] {"2"}}},
{"2", new BuildableObject {Id = "b", RequiredIds = new[] {"2"}}},
{"3", new BuildableObject {Id = "c", RequiredIds = new[] {"4"}}},
{"4", new BuildableObject {Id = "d", RequiredIds = new string[0]}},
{"5", new BuildableObject {Id = "e", RequiredIds = new[] {"4"}}}
};
var builder = new SuspendBuildsManager<BuildableObject, string>(builds, BuildableObject.BuildMethod);

// Act
var resultStatus = builder.Build();
var result = builder.GetResult();

// Assert
Assert.False(resultStatus, "Build should be possible");
Assert.Equal(3, result.Keys.Count);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using client_generator.Deserializer.Helpers;
using client_generator_tests.Helpers;
using Xunit;

namespace client_generator_tests.Deserializer.Helpers
{
public class OpenApiDeserializerAssemblyIteratorTest
{

[Fact]
public void ReturnsCorrectTypes()
{
// Arrange
var typesArray = new[]
{
typeof(string),
typeof(FakeDeserializer),
typeof(char),
typeof(FakeDeserializer)
};
var iterator = new OpenApiDeserializerAssemblyIterator(typesArray);

// Act
var counter = typesArray.Count(type => type == typeof(FakeDeserializer));
var getTypes = new List<Type>();
while (iterator.MoveNext())
{
getTypes.Add(iterator.Current);
counter--;
}

// Assert
Assert.Equal(0, counter);
Assert.True(getTypes.All(type => type == typeof(FakeDeserializer)), "Iterator returns incorrect type.");
}

[Fact]
public void ReturnsNothingOnEmptyCollection()
{
// Arrange
var typeArray = new Type[0];

// Act
var iterator = new OpenApiDeserializerAssemblyIterator(typeArray);

// Assert
Assert.False(iterator.MoveNext(), "Collection should be empty");
}


[Fact]
public void ReturnsNothingOnNullCollection()
{
// Act
var iterator = new OpenApiDeserializerAssemblyIterator(null);

// Assert
Assert.False(iterator.MoveNext(), "Collection should be empty");
}
}
}
Loading

0 comments on commit c5ef2eb

Please sign in to comment.