|
19 | 19 | #endregion -- License Terms -- |
20 | 20 |
|
21 | 21 | using System; |
| 22 | +#if !NETSTANDARD2_0 |
22 | 23 | using System.CodeDom.Compiler; |
| 24 | +#endif // !NETSTANDARD2_0 |
23 | 25 | using System.Collections; |
24 | 26 | using System.Collections.Generic; |
| 27 | +#if NETSTANDARD2_0 |
| 28 | +using System.Globalization; |
| 29 | +#endif // NETSTANDARD2_0 |
25 | 30 | using System.IO; |
26 | 31 | using System.Linq; |
27 | 32 | using System.Reflection; |
28 | 33 | using System.Text.RegularExpressions; |
29 | 34 |
|
| 35 | +#if NETSTANDARD2_0 |
| 36 | +using Microsoft.CodeAnalysis; |
| 37 | +using Microsoft.CodeAnalysis.CSharp; |
| 38 | +using Microsoft.CodeAnalysis.Emit; |
| 39 | +#endif // NETSTANDARD2_0 |
| 40 | + |
30 | 41 | using MsgPack.Serialization.Reflection; |
31 | 42 |
|
32 | 43 | using NUnit.Framework; |
@@ -1708,6 +1719,8 @@ private static void TestOnWorkerAppDomainForMultiple( string geneartedAssemblyFi |
1708 | 1719 |
|
1709 | 1720 | private static void AssertValidCode( IEnumerable<SerializerCodeGenerationResult> results ) |
1710 | 1721 | { |
| 1722 | +#if !NETSTANDARD2_0 |
| 1723 | + |
1711 | 1724 | var result = |
1712 | 1725 | CodeDomProvider |
1713 | 1726 | .CreateProvider( "C#" ) |
@@ -1735,14 +1748,88 @@ private static void AssertValidCode( IEnumerable<SerializerCodeGenerationResult> |
1735 | 1748 | { |
1736 | 1749 | File.Delete( result.PathToAssembly ); |
1737 | 1750 | } |
| 1751 | + |
| 1752 | +#else // !NETSTANDARD2_0 |
| 1753 | + |
| 1754 | + var assemblyName = "CodeGenerationAssembly" + DateTime.UtcNow.ToString( "yyyyMMddHHmmssfff" ); |
| 1755 | + var metadataList = |
| 1756 | + new TempFileDependentAssemblyManager( TestContext.CurrentContext.TestDirectory ).CodeSerializerDependentAssemblies |
| 1757 | + .Concat( new[] { Assembly.GetExecutingAssembly().Location } ) |
| 1758 | + .Select( |
| 1759 | + a => |
| 1760 | + a is string |
| 1761 | + ? AssemblyMetadata.CreateFromFile( a as string ) |
| 1762 | + : AssemblyMetadata.CreateFromImage( a as byte[] ) |
| 1763 | + ).ToArray(); |
| 1764 | + try |
| 1765 | + { |
| 1766 | + var compilation = |
| 1767 | + CSharpCompilation.Create( |
| 1768 | + assemblyName, |
| 1769 | + results.Select( r => CSharpSyntaxTree.ParseText( File.ReadAllText( r.FilePath ) ) ), |
| 1770 | + metadataList.Select( m => m.GetReference() ), |
| 1771 | + new CSharpCompilationOptions( |
| 1772 | + OutputKind.DynamicallyLinkedLibrary, |
| 1773 | + optimizationLevel: OptimizationLevel.Debug, |
| 1774 | + // Suppress CS0436 because gen/*.cs will conflict with testing serializers. |
| 1775 | + specificDiagnosticOptions: new[] { new KeyValuePair<string, ReportDiagnostic>( "CS0436", ReportDiagnostic.Suppress ) } |
| 1776 | + ) |
| 1777 | + ); |
| 1778 | + |
| 1779 | + var emitOptions = new EmitOptions( runtimeMetadataVersion: "v4.0.30319" ); |
| 1780 | + EmitResult result; |
| 1781 | + using ( var buffer = new MemoryStream() ) |
| 1782 | + { |
| 1783 | + result = compilation.Emit( buffer, options: emitOptions ); |
| 1784 | + } |
| 1785 | + |
| 1786 | + Assert.That( |
| 1787 | + result.Diagnostics.Any( d => d.Severity == DiagnosticSeverity.Error || d.Severity == DiagnosticSeverity.Warning ), |
| 1788 | + Is.False, |
| 1789 | + String.Join( Environment.NewLine, GetCompileErrorLines( result.Diagnostics ).ToArray() ) |
| 1790 | + ); |
| 1791 | + } |
| 1792 | + finally |
| 1793 | + { |
| 1794 | + foreach ( var metadata in metadataList ) |
| 1795 | + { |
| 1796 | + metadata.Dispose(); |
| 1797 | + } |
| 1798 | + } |
| 1799 | +#endif // !NETSTANDARD2_0 |
1738 | 1800 | } |
1739 | 1801 |
|
| 1802 | +#if !NETSTANDARD2_0 |
| 1803 | + |
1740 | 1804 | private static IEnumerable<string> GetCompileErrorLines( CompilerError error ) |
1741 | 1805 | { |
1742 | 1806 | yield return error.ToString(); |
1743 | 1807 | yield return File.ReadAllLines( error.FileName ).Skip( error.Line - 1 ).First(); |
1744 | 1808 | } |
1745 | 1809 |
|
| 1810 | +#else // !NETSTANDARD2_0 |
| 1811 | + |
| 1812 | + private static IEnumerable<string> GetCompileErrorLines( IEnumerable<Diagnostic> diagnostics ) |
| 1813 | + { |
| 1814 | + return |
| 1815 | + diagnostics.Select( |
| 1816 | + ( diagnostic, i ) => |
| 1817 | + String.Format( |
| 1818 | + CultureInfo.InvariantCulture, |
| 1819 | + "[{0}]{1}:{2}:(File:{3}, Line:{4}, Column:{5}):{6}", |
| 1820 | + i, |
| 1821 | + diagnostic.Severity, |
| 1822 | + diagnostic.Id, |
| 1823 | + diagnostic.Location.GetLineSpan().Path, |
| 1824 | + diagnostic.Location.GetLineSpan().StartLinePosition.Line, |
| 1825 | + diagnostic.Location.GetLineSpan().StartLinePosition.Character, |
| 1826 | + diagnostic.GetMessage() |
| 1827 | + ) |
| 1828 | + ); |
| 1829 | + } |
| 1830 | + |
| 1831 | +#endif // !NETSTANDARD2_0 |
| 1832 | + |
1746 | 1833 | public sealed class Tester : MarshalByRefObject |
1747 | 1834 | { |
1748 | 1835 | public void DoTest( string testAssemblyFile, int packerCompatiblityOptions, int serializationMethod, byte[] bytesValue, byte[] expectedPackedValue, int expectedSerializerTypeCounts, TestType testType ) |
|
0 commit comments