Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for some auxiliary classes #1192

Merged
merged 10 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
vncoelho marked this conversation as resolved.
Show resolved Hide resolved
{
[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