指出代码中的错误。我需要创建一个字典并用国家填充它,键是国家代码,例如 RU,值是俄罗斯的名称。我使用 RegionInfo, CultureInfo ,只是循环并填写。第二种方法获取文本并在那里查找国家代码,然后显示它们的值。我正在尝试使用 xUnit(第一次,我没有使用它的经验),当我运行该方法时,它只是无休止地加载并最终溢出所有挂起的内存。
测试:
public class UnitTest1
{
[Fact]
public void ItShould_UnderTesting()
{
//Arrange
var sut = new CountriesFilter();
//Act
var d = sut.Text_Regul("gh bbff swvw'RU', 'DE' ggg ");
//Assert
Assert.NotNull(d);
}
编码:
public class CountriesFilter
{
public static IDictionary<string, string> GettingTextReg()
{
var allCultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
var codeandname = new Dictionary<string, string>(allCultures.Length);
foreach(var i in allCultures)
{
RegionInfo regionInfo = new RegionInfo(i.Name);
if (codeandname.Count(x => x.Key == regionInfo.TwoLetterISORegionName) == 0)
{
codeandname.Add(regionInfo.TwoLetterISORegionName, regionInfo.DisplayName);
}
}
return codeandname;
}
public List<string> Text_Regul(string text)
{
List<string> coun = null;
try
{
var list = GettingTextReg();
string pattern = string.Format("\'({0})\'", string.Join("|", list.Keys));
Match mat = Regex.Match(text, pattern);
coun = new List<string>();
while (mat.Success)
{
coun.Add(mat.Value);
}
} catch(ArgumentNullException e)
{
throw new ArgumentNullException(e.Message);
}
return coun;
}
}
}
是一个无限循环。您不断添加相同的值,迭代之间没有任何变化。
可能你想写