借助网络配置,控制Modbus工具的日志显示,避免卡死界面
智能大石头 authored at 2021-12-11 22:09:19
3.78 KiB
XCoder
using System;
using System.ComponentModel;
using System.IO;
using System.Text;
using NewLife.XCoder.Tools.FileEncoding;

namespace XCoder.App.Tests;

/// <summary>文件编码探测与转换测试</summary>
public class FileEncodingServiceTests
{
    static FileEncodingServiceTests() => Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

    [Fact]
    [DisplayName("无 BOM 的 UTF-8 文件被识别为 utf-8")]
    public void Detect_Utf8_NoBom()
    {
        var path = CreateFile("中文内容 abc", new UTF8Encoding(false));
        try
        {
            var result = FileEncodingService.Detect(path);

            Assert.Equal("utf-8", result.EncodingName);
            Assert.False(result.HasBom);
            Assert.Contains("中文内容", result.Preview);
        }
        finally
        {
            File.Delete(path);
        }
    }

    [Fact]
    [DisplayName("带 BOM 的 UTF-8 文件被识别为 utf-8-bom")]
    public void Detect_Utf8_WithBom()
    {
        var path = CreateFile("中文内容", new UTF8Encoding(true));
        try
        {
            var result = FileEncodingService.Detect(path);

            Assert.Equal("utf-8-bom", result.EncodingName);
            Assert.True(result.HasBom);
        }
        finally
        {
            File.Delete(path);
        }
    }

    [Fact]
    [DisplayName("转换为 GB18030 后可再次识别")]
    public void Convert_To_Gb18030_ThenDetect()
    {
        var path = CreateFile("转换测试内容", new UTF8Encoding(false));
        var output = path + ".gb";
        try
        {
            var result = FileEncodingService.Convert(path, "gb18030", false, output);

            Assert.Equal("utf-8", result.SourceEncoding);
            Assert.Equal("gb18030", result.TargetEncoding);
            Assert.Null(result.BackupPath);

            var detect = FileEncodingService.Detect(output);
            Assert.Equal("gb18030", detect.EncodingName);
        }
        finally
        {
            File.Delete(path);
            if (File.Exists(output)) File.Delete(output);
        }
    }

    [Fact]
    [DisplayName("原地转换生成 .bak 备份且内容可还原")]
    public void Convert_InPlace_CreatesBackup()
    {
        var path = CreateFile("备份测试", new UTF8Encoding(false));
        try
        {
            var original = File.ReadAllBytes(path);
            var result = FileEncodingService.Convert(path, "gb18030", false);

            Assert.Equal(path + ".bak", result.BackupPath);
            Assert.True(File.Exists(result.BackupPath));
            Assert.Equal(original, File.ReadAllBytes(result.BackupPath));
        }
        finally
        {
            File.Delete(path);
            if (File.Exists(path + ".bak")) File.Delete(path + ".bak");
        }
    }

    [Fact]
    [DisplayName("文件不存在时抛 FileNotFoundException")]
    public void Detect_Missing_Throws()
    {
        var path = Path.Combine(Path.GetTempPath(), "xcoder-encoding-" + Guid.NewGuid().ToString("N") + ".txt");

        Assert.Throws<FileNotFoundException>(() => FileEncodingService.Detect(path));
    }

    [Fact]
    [DisplayName("空路径抛 ArgumentException")]
    public void Detect_EmptyPath_Throws()
        => Assert.Throws<ArgumentException>(() => FileEncodingService.Detect("  "));

    private static String CreateFile(String text, Encoding encoding)
    {
        var path = Path.Combine(Path.GetTempPath(), "xcoder-encoding-" + Guid.NewGuid().ToString("N") + ".txt");
        var body = encoding.GetBytes(text);
        var preamble = encoding.GetPreamble();
        var data = new Byte[preamble.Length + body.Length];
        Buffer.BlockCopy(preamble, 0, data, 0, preamble.Length);
        Buffer.BlockCopy(body, 0, data, preamble.Length, body.Length);
        File.WriteAllBytes(path, data);

        return path;
    }
}