Skip to content

Commit

Permalink
[improv]支持多版本框架的单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Nov 18, 2024
1 parent 100cd21 commit 48cc8bb
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 211 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ jobs:
test:
runs-on: windows-latest

strategy:
matrix:
dotnet-version: [ '4.6.2', '5.x', '6.x', '7.x', '8.x', '9.x' ]

steps:
- uses: actions/checkout@v4
- name: Setup dotNET
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
6.x
7.x
8.x
9.x
dotnet-version: ${{ matrix.dotnet-version }}
- name: Build
run: |
dotnet build -c Release
Expand Down
4 changes: 4 additions & 0 deletions NewLife.Core/Common/SysConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Reflection;
using NewLife.Configuration;
using NewLife.Reflection;
using NewLife.Security;
Expand Down Expand Up @@ -128,6 +129,9 @@ public static AssemblyX? SysAssembly
var asm = AssemblyX.Entry;
if (asm != null) return asm;

var sm = Assembly.GetCallingAssembly();
if (sm != null) return AssemblyX.Create(sm);

var list = AssemblyX.GetMyAssemblies();

// 最后编译那一个
Expand Down
6 changes: 4 additions & 2 deletions NewLife.Core/Reflection/AssemblyX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -673,10 +673,12 @@ public static List<AssemblyX> GetMyAssemblies()
// 加载程序集列表很容易抛出异常,全部屏蔽
try
{
if (asmx.FileVersion.IsNullOrEmpty()) continue;
//if (asmx.FileVersion.IsNullOrEmpty()) continue;

var file = "";
//file = asmx.Asm.CodeBase;
#if NETFRAMEWORK
file = asmx.Asm.CodeBase;
#endif
if (file.IsNullOrEmpty()) file = asmx.Asm.Location;
if (file.IsNullOrEmpty()) continue;

Expand Down
2 changes: 2 additions & 0 deletions XUnitTest.Core/Buffers/SpanReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void Test2(Boolean isLittle)
writer.WriteEncodedInt(n);
Assert.Equal(n, reader.ReadEncodedInt());

#if NET6_0_OR_GREATER
var buf = Rand.NextBytes(57);
writer.Write(buf);
Assert.Equal(buf, reader.ReadBytes(buf.Length));
Expand All @@ -98,6 +99,7 @@ public void Test2(Boolean isLittle)
var span3 = new ReadOnlySpan<Byte>(buf);
writer.Write(span3);
Assert.Equal(span3, reader.ReadBytes(buf.Length));
#endif

var str = Rand.NextString(33);
writer.Write(str);
Expand Down
23 changes: 8 additions & 15 deletions XUnitTest.Core/Common/SysConfigTest.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NewLife.Common;
using NewLife.Common;
using Xunit;

namespace XUnitTest.Common
{
namespace XUnitTest.Common;

public class SysConfigTest
public class SysConfigTest
{
[Fact(DisplayName = "系统名称")]
public void DisplayNameTest()
{
[Fact(DisplayName = "系统名称")]
public void DisplayNameTest()
{
var asm = SysConfig.SysAssembly;
Assert.NotNull(asm);
}
var asm = SysConfig.SysAssembly;
Assert.NotNull(asm);
}
}
9 changes: 4 additions & 5 deletions XUnitTest.Core/Common/UtilityTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Text.Json.Nodes;

using Microsoft.Extensions.Primitives;
using Microsoft.Extensions.Primitives;
using NewLife.Log;
using Xunit;

Expand Down Expand Up @@ -298,14 +295,16 @@ public void NegativeNumber()
Assert.Equal(+1, v);
}

#if NET6_0_OR_GREATER
[Fact]
public void JsonNodeTest()
{
var json = "{\"Code\":1}";
var jsonNode = JsonNode.Parse(json);
var jsonNode = System.Text.Json.Nodes.JsonNode.Parse(json);

var c = jsonNode["Code"];

Assert.Equal(1, c.ToInt());
}
#endif
}
2 changes: 1 addition & 1 deletion XUnitTest.Core/Configuration/HttpConfigProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public HttpConfigProviderTests()
else
{
_server = "http://127.0.0.1:7080,http://10.0.0.1:7080";
File.WriteAllText(file, _server);
File.WriteAllText(file.EnsureDirectory(true), _server);
}
}

Expand Down
26 changes: 17 additions & 9 deletions XUnitTest.Core/Http/HttpServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static HttpServerTests()
}

[Fact]
public async void MapDelegate()
public async Task MapDelegate()
{
_server.Map("/", () => "<h1>Hello NewLife!</h1></br> " + DateTime.Now.ToFullString() + "</br><img src=\"logos/leaf.png\" />");

Expand All @@ -46,7 +46,7 @@ public async void MapDelegate()
}

[Fact]
public async void MapApi()
public async Task MapApi()
{
_server.Map("/user", (String act, Int32 uid) => new { code = 0, data = $"User.{act}({uid}) success!" });

Expand All @@ -57,7 +57,7 @@ public async void MapApi()
}

[Fact]
public async void MapStaticFiles()
public async Task MapStaticFiles()
{
XTrace.WriteLine("root: {0}", "http/".GetFullPath());
_server.MapStaticFiles("/logos", "http/");
Expand All @@ -70,7 +70,7 @@ public async void MapStaticFiles()
}

//[Fact]
//public async void MapController()
//public async Task MapController()
//{
// _server.MapController<ApiController>("/api");

Expand All @@ -81,7 +81,7 @@ public async void MapStaticFiles()
//}

[Fact]
public async void MapMyHttpHandler()
public async Task MapMyHttpHandler()
{
_server.Map("/my", new MyHttpHandler());

Expand All @@ -102,7 +102,7 @@ public void ProcessRequest(IHttpContext context)
}

[Fact]
public async void BigPost()
public async Task BigPost()
{
_server.Map("/my2", new MyHttpHandler2());

Expand All @@ -111,8 +111,16 @@ public async void BigPost()
var buf1 = new Byte[8 * 1024];
var buf2 = new Byte[8 * 1024];

#if NET462
for (var i = 0; i < buf1.Length; i++)
{
buf1[i] = (Byte)'0';
buf2[i] = (Byte)'0';
}
#else
Array.Fill(buf1, (Byte)'0');
Array.Fill(buf2, (Byte)'0');
#endif

buf1[0] = (Byte)'1';
buf2[0] = (Byte)'2';
Expand Down Expand Up @@ -143,18 +151,18 @@ public void ProcessRequest(IHttpContext context)
}

[Fact]
public async void MapWebSocket()
public async Task MapWebSocket()
{
_server.Map("/ws", new WebSocketHandler());

var content = "Hello NewLife".GetBytes();

var client = new ClientWebSocket();
await client.ConnectAsync(new Uri("ws://127.0.0.1:18080/ws"), default);
await client.SendAsync(content, System.Net.WebSockets.WebSocketMessageType.Text, true, default);
await client.SendAsync(new ArraySegment<Byte>(content), System.Net.WebSockets.WebSocketMessageType.Text, true, default);

var buf = new Byte[1024];
var rs = await client.ReceiveAsync(buf, default);
var rs = await client.ReceiveAsync(new ArraySegment<Byte>(buf), default);
Assert.EndsWith("说,Hello NewLife", new Packet(buf, 0, rs.Count).ToStr());

await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "通信完成", default);
Expand Down
4 changes: 4 additions & 0 deletions XUnitTest.Core/IO/CsvFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public void MemoryTest()
}

var txt = ms.ToArray().ToStr();
#if NET462
var lines = txt.Split([Environment.NewLine], StringSplitOptions.RemoveEmptyEntries);
#else
var lines = txt.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
#endif
Assert.Equal(3, lines.Length);
Assert.Equal("Code,Name,Enable,CreateTime", lines[0]);
Assert.Equal($"1234,Stone,1,{((DateTime)list[0][3]).ToFullString()}", lines[1]);
Expand Down
61 changes: 30 additions & 31 deletions XUnitTest.Core/IO/ExcelReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,43 @@
using NewLife.IO;
using Xunit;

namespace XUnitTest.IO
namespace XUnitTest.IO;

public class ExcelReaderTests
{
public class ExcelReaderTests
[Fact]
public void Test1()
{
[Fact]
public void Test1()
{
var type = GetType();
var stream = type.Assembly.GetManifestResourceStream(type.Namespace + ".excel.xlsx");
Assert.NotNull(stream);
var type = GetType();
var stream = type.Assembly.GetManifestResourceStream(type.Namespace + ".excel.xlsx");
Assert.NotNull(stream);

var reader = new ExcelReader(stream, Encoding.UTF8);
var rows = reader.ReadRows().ToList();
Assert.Equal(927, rows.Count);
var reader = new ExcelReader(stream, Encoding.UTF8);
var rows = reader.ReadRows().ToList();
Assert.Equal(927, rows.Count);

var names = "序号,名字,昵称,启用,年龄,生日,时间,余额,比率".Split(",");
var fields = rows[0].Cast<String>().ToArray();
Assert.Equal(names.Length, fields.Length);
for (var i = 0; i < names.Length; i++)
{
Assert.Equal(names[i], fields[i]);
}

var values = "111,Stone,大石头,1,36.6,1984-07-01,2020-03-04 20:08:45,323.452,0.234".Split(",");
var row1 = rows[1];
Assert.Equal(values.Length, row1.Length);
for (var i = 0; i < values.Length; i++)
{
Assert.Equal(values[i], row1[i]);
}
var names = "序号,名字,昵称,启用,年龄,生日,时间,余额,比率".Split(',');
var fields = rows[0].Cast<String>().ToArray();
Assert.Equal(names.Length, fields.Length);
for (var i = 0; i < names.Length; i++)
{
Assert.Equal(names[i], fields[i]);
}

//[Fact]
public void Test2()
var values = "111,Stone,大石头,1,36.6,1984-07-01,2020-03-04 20:08:45,323.452,0.234".Split(',');
var row1 = rows[1];
Assert.Equal(values.Length, row1.Length);
for (var i = 0; i < values.Length; i++)
{
var reader = new ExcelReader("test.xlsx");
var rows = reader.ReadRows().ToList();
Assert.Equal(927, rows.Count);
Assert.Equal(values[i], row1[i]);
}
}

//[Fact]
public void Test2()
{
var reader = new ExcelReader("test.xlsx");
var rows = reader.ReadRows().ToList();
Assert.Equal(927, rows.Count);
}
}
2 changes: 2 additions & 0 deletions XUnitTest.Core/Reflection/ReflectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ public void DateTimeOffsetChangeTypeTest()
Assert.Equal(target, rs);
}

#if NET6_0_OR_GREATER
[Fact]
public void DateOnlyChangeTypeTest()
{
Expand All @@ -322,6 +323,7 @@ public void TimeOnlyChangeTypeTest()
var rs = value.ChangeType(targetType);
Assert.Equal(target, rs);
}
#endif

[Fact]
public void AsListTest()
Expand Down
Loading

0 comments on commit 48cc8bb

Please sign in to comment.