Skip to content

Commit

Permalink
Unit tests for some auxiliary classes (neo-project#1192)
Browse files Browse the repository at this point in the history
  • Loading branch information
eryeer authored and Tommo-L committed Jun 22, 2020
1 parent 83053eb commit 09cb4fa
Show file tree
Hide file tree
Showing 11 changed files with 1,135 additions and 75 deletions.
27 changes: 27 additions & 0 deletions neo.UnitTests/IO/UT_ByteArrayComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;

namespace Neo.UnitTests.IO
{
[TestClass]
public class UT_ByteArrayComparer
{
[TestMethod]
public void TestCompare()
{
ByteArrayComparer comparer = new ByteArrayComparer();
byte[] x = new byte[0], y = new byte[0];
comparer.Compare(x, y).Should().Be(0);

x = new byte[] { 1 };
comparer.Compare(x, y).Should().Be(1);
y = x;
comparer.Compare(x, y).Should().Be(0);

x = new byte[] { 1 };
y = new byte[] { 2 };
comparer.Compare(x, y).Should().Be(-1);
}
}
}
51 changes: 51 additions & 0 deletions neo.UnitTests/Plugins/TestLogPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.Extensions.Configuration;
using Neo.Plugins;

namespace Neo.UnitTests.Plugins
{
public class TestLogPlugin : Plugin, ILogPlugin
{
public TestLogPlugin() : base() { }

public string Output { set; get; }

public override void Configure() { }

public new void Log(string source, LogLevel level, string message)
{
Output = source + "_" + level.ToString() + "_" + message;
}

public void LogMessage(string message)
{
Log(message);
}

public bool TestOnMessage(object message)
{
return OnMessage(message);
}

public IConfigurationSection TestGetConfiguration()
{
return GetConfiguration();
}

protected override bool OnMessage(object message) => true;

public static bool TestResumeNodeStartup()
{
return ResumeNodeStartup();
}

public static void TestSuspendNodeStartup()
{
SuspendNodeStartup();
}

public static void TestLoadPlugins(NeoSystem system)
{
LoadPlugins(system);
}
}
}
83 changes: 83 additions & 0 deletions neo.UnitTests/Plugins/UT_Plugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Plugins;
using System;

namespace Neo.UnitTests.Plugins
{
[TestClass]
public class UT_Plugin
{
private static readonly object locker = new object();

[TestMethod]
public void TestGetConfigFile()
{
var pp = new TestLogPlugin();
var file = pp.ConfigFile;
file.EndsWith("config.json").Should().BeTrue();
}

[TestMethod]
public void TestGetName()
{
var pp = new TestLogPlugin();
pp.Name.Should().Be("TestLogPlugin");
}

[TestMethod]
public void TestGetVersion()
{
var pp = new TestLogPlugin();
Action action = () => pp.Version.ToString();
action.Should().NotThrow();
}

[TestMethod]
public void TestLog()
{
var lp = new TestLogPlugin();
lp.LogMessage("Hello");
lp.Output.Should().Be("Plugin:TestLogPlugin_Info_Hello");
}

[TestMethod]
public void TestSendMessage()
{
lock (locker)
{
Plugin.Plugins.Clear();
Plugin.SendMessage("hey1").Should().BeFalse();

var lp = new TestLogPlugin();
Plugin.SendMessage("hey2").Should().BeTrue();
}
}

[TestMethod]
public void TestNotifyPluginsLoadedAfterSystemConstructed()
{
var pp = new TestLogPlugin();
Action action = () => Plugin.NotifyPluginsLoadedAfterSystemConstructed();
action.Should().NotThrow();
}

[TestMethod]
public void TestResumeNodeStartupAndSuspendNodeStartup()
{
var system = TestBlockchain.InitializeMockNeoSystem();
TestLogPlugin.TestLoadPlugins(system);
TestLogPlugin.TestSuspendNodeStartup();
TestLogPlugin.TestSuspendNodeStartup();
TestLogPlugin.TestResumeNodeStartup().Should().BeFalse();
TestLogPlugin.TestResumeNodeStartup().Should().BeTrue();
}

[TestMethod]
public void TestGetConfiguration()
{
var pp = new TestLogPlugin();
pp.TestGetConfiguration().Key.Should().Be("PluginConfiguration");
}
}
}
Loading

0 comments on commit 09cb4fa

Please sign in to comment.