using System.Collections.Generic; using Microsoft.AspNetCore.Razor.TagHelpers; using Util.Ui.Configs; using Xunit; namespace Util.Ui.Tests.Configs { /// /// UI配置测试 /// public class ConfigTest { /// /// 创建配置 /// private Config CreateConfig() { TagHelperAttributeList attributes = new TagHelperAttributeList { { "a", 1 }, { "b", true } }; TagHelperContext context = new TagHelperContext( attributes, new Dictionary(), Util.Helpers.Id.Create() ); return new Config( context, null ); } /// /// 测试获取值 /// [Fact] public void TestGetValue_1() { var config = CreateConfig(); Assert.Equal( "1", config.GetValue( "a" ) ); } /// /// 测试获取值 - 泛型 /// [Fact] public void TestGetValue_2() { var config = CreateConfig(); Assert.Equal( 1, config.GetValue( "a" ) ); } /// /// 测试获取布尔值 /// [Fact] public void TestGetBoolValue() { var config = CreateConfig(); Assert.Equal( "true", config.GetBoolValue( "b" ) ); } /// /// 测试设置属性 /// [Fact] public void TestSetAttribute() { var config = CreateConfig(); config.SetAttribute( "SetAttribute", 3 ); Assert.Equal( 3, config.GetValue( "SetAttribute" ) ); } /// /// 测试设置属性 - 替换已存在的属性 /// [Fact] public void TestSetAttribute_2() { var config = CreateConfig(); config.SetAttribute( "a", 2 ); Assert.Equal( 2, config.GetValue( "a" ) ); } /// /// 测试设置属性 - 属性已存在则忽略 /// [Fact] public void TestSetAttribute_3() { var config = CreateConfig(); config.SetAttribute( "a", 2, false ); Assert.Equal( 1, config.GetValue( "a" ) ); } /// /// 测试移除属性 /// [Fact] public void TestRemoveAttribute() { var config = CreateConfig(); Assert.Equal( 1, config.GetValue( "a" ) ); config.RemoveAttribute( "a" ); Assert.Equal( 0, config.GetValue( "a" ) ); } /// /// 测试复制配置 - 复制一个属性 /// [Fact] public void TestCopy_1() { TagHelperAttributeList attributes = new TagHelperAttributeList { { "a", 1 } }; TagHelperContext context = new TagHelperContext( attributes, new Dictionary(), Util.Helpers.Id.Create() ); var config = new Config( context, null ); var copy = config.Copy(); Assert.Equal( 1, copy.GetValue( "a" ) ); } /// /// 测试复制配置 - 复制并修改一个属性,不会影响原配置 /// [Fact] public void TestCopy_2() { TagHelperAttributeList attributes = new TagHelperAttributeList { { "a", 1 } }; TagHelperContext context = new TagHelperContext( attributes, new Dictionary(), Util.Helpers.Id.Create() ); var config = new Config( context, null ); var copy = config.Copy(); copy.SetAttribute( "a",2 ); Assert.Equal( 1, config.GetValue( "a" ) ); Assert.Equal( 2, copy.GetValue( "a" ) ); } } }