Falcon.SugarApi/Falcon.SugarApi.Test/StringExtendTest.cs

66 lines
2.0 KiB
C#
Raw Normal View History

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Falcon.SugarApi;
namespace Falcon.SugarApi.Test
{
/// <summary>
/// 字符串扩展方法测试
/// </summary>
[TestClass]
public class StringExtendTest
{
[TestMethod]
public void TestSplit() {
var str = $"a,b.c;de。fg";
var strArr = str.SplitStr();
Assert.AreEqual(7, strArr.Length, "分割后长度应该为7");
str = "a,,, ,b ,c c,";
strArr = str.SplitStr();
Assert.AreEqual(3, strArr.Length, "分割后长度应该为3");
Assert.AreEqual("a", strArr[0]);
Assert.AreEqual("b", strArr[1]);
Assert.AreEqual("c c", strArr[2]);
str = null;
strArr = str.SplitStr();
Assert.IsNotNull(strArr);
Assert.AreEqual(0, strArr.Length);
//指定分隔符
str = $"a,b.c;de。fg";
strArr = str.SplitStr(',');
Assert.AreEqual(2, strArr.Length, "分割后长度应该为7");
Assert.AreEqual("a", strArr[0]);
Assert.AreEqual("b.c;de。fg", strArr[1]);
strArr = str.SplitStr(',', '.');
Assert.AreEqual(3, strArr.Length, "分割后长度应该为7");
Assert.AreEqual("a", strArr[0]);
Assert.AreEqual("b", strArr[1]);
Assert.AreEqual("c;de。fg", strArr[2]);
}
/// <summary>
/// 测试ToDefault
/// </summary>
[TestMethod]
public void TestToDefault() {
var str = "";
var r = "";
r = str.ToDefault("abc");
Assert.AreEqual("abc", r);
str = null;
r = str.ToDefault("abc");
Assert.AreEqual("abc", r);
str = "123";
r = str.ToDefault("abc");
Assert.AreEqual("123", r);
}
}
}