Skip to content

Commit d0a4d36

Browse files
committed
Add tests for all current BCL cultures, and expand parser to support three-letter locales
1 parent 0f8b90f commit d0a4d36

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

src/NuGet.Core/NuGet.Packaging/ContentModel/ManagedCodeConventions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private static object CodeLanguage_Parser(ReadOnlyMemory<char> name, PatternTabl
160160
/// If matchOnly is true, then an empty string may be returned as a performance optimization.
161161
/// If matchOnly is false, the parsed result will be returned.
162162
/// </summary>
163-
private static object Locale_Parser(ReadOnlyMemory<char> name, PatternTable table, bool matchOnly)
163+
internal static object Locale_Parser(ReadOnlyMemory<char> name, PatternTable table, bool matchOnly)
164164
{
165165
if (table != null)
166166
{
@@ -173,7 +173,8 @@ private static object Locale_Parser(ReadOnlyMemory<char> name, PatternTable tabl
173173

174174
// We use a heuristic here for common locale codes. Locale codes are often
175175
// * two characters for the language: en, es, fr, de
176-
if (name.Length == 2)
176+
// * three characters for the language: agq
177+
if (name.Length == 2 || name.Length == 3)
177178
{
178179
if (matchOnly)
179180
{
@@ -193,6 +194,10 @@ private static object Locale_Parser(ReadOnlyMemory<char> name, PatternTable tabl
193194
}
194195
else if (name.Length >= 5 && name.Span[3] == '-') // e.g agq-CM
195196
{
197+
if (matchOnly)
198+
{
199+
return string.Empty;
200+
}
196201
return name;
197202
}
198203

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Globalization;
7+
using System.Linq;
8+
using Xunit;
9+
10+
namespace NuGet.Client.Test
11+
{
12+
public class ContentModelResourceTests
13+
{
14+
[MemberData(nameof(AllCultures))]
15+
[Theory]
16+
public void CanParseEverySystemKnownCultureResource(CultureInfo culture)
17+
{
18+
var result = ManagedCodeConventions.Locale_Parser(culture.Name.AsMemory(), null, false);
19+
Assert.Equal(culture.Name, result as string);
20+
}
21+
22+
public static IEnumerable<object[]> AllCultures()
23+
{
24+
return CultureInfo.GetCultures(CultureTypes.AllCultures).Where(c => !string.IsNullOrEmpty(c.Name)).Select(culture => new[] { culture });
25+
}
26+
}
27+
}
28+

0 commit comments

Comments
 (0)