Skip to content

Commit 7fc3886

Browse files
Support .net core 2 and .net standard 2 (#68)
1 parent 6b88af7 commit 7fc3886

5 files changed

Lines changed: 63 additions & 22 deletions

File tree

src/CharsetDetector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ namespace UtfUnknown
7575
/// </summary>
7676
public class CharsetDetector
7777
{
78-
#if NETCOREAPP3_0
78+
#if NETCOREAPP
7979
/// <summary>
8080
/// Adds the encodings of the EncodingProvider object to the common language runtime
8181
/// </summary>
8282
static CharsetDetector()
8383
{
8484
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
8585
}
86-
#endif // NETCOREAPP3_0
86+
#endif
8787

8888
internal InputState InputState;
8989

src/UTF-unknown.csproj

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard1.0;netstandard1.3;net40;netcoreapp3.0</TargetFrameworks>
4+
<TargetFrameworks>net40;netstandard1.0;netstandard1.3;netstandard2.0;netcoreapp2.0;netcoreapp3.0</TargetFrameworks>
55
</PropertyGroup>
66

77
<PropertyGroup>
88
<AssemblyName>UtfUnknown</AssemblyName>
99
<PackageId>UTF.Unknown</PackageId>
10-
<Version>2.0.0</Version> <!-- patched in AppVeyor.yml -->
10+
<Version>2.0.0</Version>
11+
<!-- patched in AppVeyor.yml -->
1112
</PropertyGroup>
1213

1314
<PropertyGroup>
1415
<OutputType>Library</OutputType>
1516
</PropertyGroup>
17+
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
18+
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.6.0" />
19+
</ItemGroup>
1620

1721
<PropertyGroup>
1822
<Authors>Julian Verdurmen, Rudi Pettazzi, Shy Shalom</Authors>
@@ -36,16 +40,15 @@ Features:
3640
- Improve error handling
3741
- Improved unit tests
3842
</Description>
39-
40-
<Copyright/>
43+
44+
<Copyright />
4145

4246
</PropertyGroup>
4347

4448
<PropertyGroup>
4549

4650
<PackageTags>charset;detection;unicode;ascii;netstandard</PackageTags>
4751
<PackageReleaseNotes>
48-
- Added support for CP949 (@HelloWorld017)
4952
</PackageReleaseNotes>
5053
<PackageIconUrl>https://raw.githubusercontent.com/CharsetDetector/UTF-unknown/master/logo.png</PackageIconUrl>
5154
<PackageProjectUrl>https://github.com/CharsetDetector/UTF-unknown</PackageProjectUrl>
@@ -57,7 +60,7 @@ Features:
5760
<SignAssembly>True</SignAssembly>
5861
<AssemblyOriginatorKeyFile>UtfUnknown.snk</AssemblyOriginatorKeyFile>
5962
</PropertyGroup>
60-
63+
6164
<PropertyGroup>
6265
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\UtfUnknown.xml</DocumentationFile>
6366
<NoWarn>1701;1702;1705,1570,1591</NoWarn>

tests/CharsetDetectorTestBatch.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ namespace UtfUnknown.Tests
1717

1818
public class CharsetDetectorTestBatch
1919
{
20+
private const string DIRECTORY_NAME = "TESTS";
2021
private static readonly string TESTS_ROOT = GetTestsPath();
2122
private static readonly string DATA_ROOT = FindRootPath();
22-
23+
2324
private StreamWriter _logWriter;
2425

2526
/// <summary>Initializes a new instance of the <see cref="T:System.Object" /> class.</summary>
@@ -47,12 +48,24 @@ private static string GetTestsPath()
4748
{
4849
var path = TestContext.CurrentContext.TestDirectory;
4950

50-
var directoryName = "TESTS";
51-
52-
var index = path.IndexOf(directoryName, StringComparison.CurrentCultureIgnoreCase);
51+
if (path.IndexOf(DIRECTORY_NAME, StringComparison.CurrentCultureIgnoreCase) >= 0)
52+
{
53+
path = TruncatePath(path);
54+
}
55+
else
56+
{
57+
// fix for .netcoreapp2.1 - TestContext.CurrentContext.TestDirectory is bugged in NUnit under .netcoreapp2.1
58+
path = TestContext.CurrentContext.WorkDirectory;
59+
path = TruncatePath(path);
60+
}
5361

54-
path = path.Substring(0, index + directoryName.Length);
62+
return path;
63+
}
5564

65+
private static string TruncatePath(string path)
66+
{
67+
var index = path.IndexOf(DIRECTORY_NAME, StringComparison.CurrentCultureIgnoreCase);
68+
path = path.Substring(0, index + DIRECTORY_NAME.Length);
5669
return path;
5770
}
5871

@@ -108,10 +121,8 @@ private void TestFile(string expectedCharset, string file)
108121
{
109122
var result = CharsetDetector.DetectFromFile(file);
110123
var detected = result.Detected;
111-
#if !NETCOREAPP3_0
112-
// because System.NotSupportedException in .net core 3.0 for Encoding
113-
_logWriter.WriteLine(string.Format("- {0} ({1}) -> {2}", file, expectedCharset, JsonConvert.SerializeObject(result)));
114-
#endif // !NETCOREAPP3_0
124+
125+
_logWriter.WriteLine($"- {file} ({expectedCharset}) -> {JsonConvert.SerializeObject(result, Formatting.Indented, new EncodingJsonConverter())}");
115126
StringAssert.AreEqualIgnoringCase(expectedCharset, detected.EncodingName,
116127
$"Charset detection failed for {file}. Expected: {expectedCharset}, detected: {detected.EncodingName} ({detected.Confidence * 100.0f:0.00############}% confidence)");
117128
Assert.NotNull(detected.Encoding);

tests/EncodingJsonConverter.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Newtonsoft.Json;
7+
8+
namespace UtfUnknown.Tests
9+
{
10+
public class EncodingJsonConverter : JsonConverter
11+
{
12+
public override bool CanConvert(Type objectType)
13+
{
14+
return typeof(Encoding).IsAssignableFrom(objectType);
15+
}
16+
17+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
18+
{
19+
writer.WriteValue(((Encoding)value).WebName);
20+
}
21+
22+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
23+
{
24+
return Encoding.GetEncoding((string)reader.Value);
25+
}
26+
}
27+
}

tests/UTF-unknown.Tests.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net452;netcoreapp3.0</TargetFrameworks>
4+
<TargetFrameworks>net452;net472;netcoreapp2.1;netcoreapp3.0</TargetFrameworks>
55
<RootNamespace>UtfUnknown.Tests</RootNamespace>
66
<AssemblyName>UtfUnknown.Tests</AssemblyName>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
11-
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
12-
<PackageReference Include="NUnit" Version="3.10.1" />
13-
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
11+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
12+
<PackageReference Include="NUnit" Version="3.12.0" />
13+
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
1414
</ItemGroup>
1515

1616
<ItemGroup>

0 commit comments

Comments
 (0)