From f9aeb82aaa2f8b0613b2caaad71b04217005b538 Mon Sep 17 00:00:00 2001 From: Friedrich von Never Date: Fri, 30 Dec 2022 22:17:09 +0100 Subject: [PATCH] (#63) Tests: fix everything after the resource migration --- WpfMath.Shared/Data/WpfMathResourceMarker.cs | 6 ++++++ WpfMath.Shared/Utils/AssemblyEx.cs | 12 ++++++++++++ WpfMath.Shared/Utils/TupleExtensions.cs | 4 +++- WpfMath.Shared/WpfMath.Shared.csproj | 7 ------- src/WpfMath/Colors/PredefinedColorParser.cs | 5 +++-- src/WpfMath/DefaultTexFontParser.cs | 5 ++++- {WpfMath.Shared => src/WpfMath}/Fonts/cmex10.ttf | Bin {WpfMath.Shared => src/WpfMath}/Fonts/cmmi10.ttf | Bin {WpfMath.Shared => src/WpfMath}/Fonts/cmr10.ttf | Bin {WpfMath.Shared => src/WpfMath}/Fonts/cmsy10.ttf | Bin src/WpfMath/GlueSettingsParser.cs | 6 ++++-- src/WpfMath/PredefinedTexFormulaSettingsParser.cs | 6 ++++-- src/WpfMath/TexPredefinedFormulaParser.cs | 7 ++++--- src/WpfMath/TexSymbolParser.cs | 7 ++++--- src/WpfMath/WpfMath.csproj | 7 +++++++ 15 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 WpfMath.Shared/Data/WpfMathResourceMarker.cs create mode 100644 WpfMath.Shared/Utils/AssemblyEx.cs rename {WpfMath.Shared => src/WpfMath}/Fonts/cmex10.ttf (100%) rename {WpfMath.Shared => src/WpfMath}/Fonts/cmmi10.ttf (100%) rename {WpfMath.Shared => src/WpfMath}/Fonts/cmr10.ttf (100%) rename {WpfMath.Shared => src/WpfMath}/Fonts/cmsy10.ttf (100%) diff --git a/WpfMath.Shared/Data/WpfMathResourceMarker.cs b/WpfMath.Shared/Data/WpfMathResourceMarker.cs new file mode 100644 index 00000000..e2a52458 --- /dev/null +++ b/WpfMath.Shared/Data/WpfMathResourceMarker.cs @@ -0,0 +1,6 @@ +namespace WpfMath.Data; + +/// Marks an assembly containing the WPF-Math resource files (XML and fonts). +public static class WpfMathResourceMarker +{ +} diff --git a/WpfMath.Shared/Utils/AssemblyEx.cs b/WpfMath.Shared/Utils/AssemblyEx.cs new file mode 100644 index 00000000..93ed5c59 --- /dev/null +++ b/WpfMath.Shared/Utils/AssemblyEx.cs @@ -0,0 +1,12 @@ +using System; +using System.IO; +using System.Reflection; + +namespace WpfMath.Utils; + +internal static class AssemblyEx +{ + public static Stream ReadResource(this Assembly assembly, string resourceName) => + assembly.GetManifestResourceStream(resourceName) + ?? throw new Exception($"Cannot find resource {resourceName} in assembly {assembly}."); +} diff --git a/WpfMath.Shared/Utils/TupleExtensions.cs b/WpfMath.Shared/Utils/TupleExtensions.cs index 46e7881e..5f33c6c1 100644 --- a/WpfMath.Shared/Utils/TupleExtensions.cs +++ b/WpfMath.Shared/Utils/TupleExtensions.cs @@ -1,6 +1,8 @@ +using System; + namespace WpfMath.Utils { -#if NET452 // not needed for .NET Core 3.0+ because there're System.TupleExtensions +#if NET452 // not needed for .NET Core 3.0+ because there are System.TupleExtensions internal static class TupleExtensions { public static void Deconstruct(this Tuple tuple, out T1 x1, out T2 x2) diff --git a/WpfMath.Shared/WpfMath.Shared.csproj b/WpfMath.Shared/WpfMath.Shared.csproj index c1b0180a..cb8558ea 100644 --- a/WpfMath.Shared/WpfMath.Shared.csproj +++ b/WpfMath.Shared/WpfMath.Shared.csproj @@ -14,13 +14,6 @@ - - - - - - - diff --git a/src/WpfMath/Colors/PredefinedColorParser.cs b/src/WpfMath/Colors/PredefinedColorParser.cs index 5c34b9ff..91f684c5 100644 --- a/src/WpfMath/Colors/PredefinedColorParser.cs +++ b/src/WpfMath/Colors/PredefinedColorParser.cs @@ -2,9 +2,10 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; -using System.Reflection; using System.Windows.Media; using System.Xml.Linq; +using WpfMath.Data; +using WpfMath.Utils; namespace WpfMath.Colors { @@ -17,7 +18,7 @@ public class PredefinedColorParser : IColorParser private PredefinedColorParser(string resourceName) { - using (var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) + using (var resource = typeof(WpfMathResourceMarker).Assembly.ReadResource(resourceName)) { var doc = XDocument.Load(resource); _colors = Parse(doc.Root); diff --git a/src/WpfMath/DefaultTexFontParser.cs b/src/WpfMath/DefaultTexFontParser.cs index 587009b1..aafa98e2 100644 --- a/src/WpfMath/DefaultTexFontParser.cs +++ b/src/WpfMath/DefaultTexFontParser.cs @@ -4,6 +4,8 @@ using System.Reflection; using System.Windows.Media; using System.Xml.Linq; +using WpfMath.Data; +using WpfMath.Utils; namespace WpfMath { @@ -48,7 +50,8 @@ private static void SetCharChildParsers() public DefaultTexFontParser() { - var doc = XDocument.Load(new System.IO.StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)!)); + using var resource = typeof(WpfMathResourceMarker).Assembly.ReadResource(resourceName); + var doc = XDocument.Load(resource); this.rootElement = doc.Root; this.parsedTextStyles = new Dictionary(); diff --git a/WpfMath.Shared/Fonts/cmex10.ttf b/src/WpfMath/Fonts/cmex10.ttf similarity index 100% rename from WpfMath.Shared/Fonts/cmex10.ttf rename to src/WpfMath/Fonts/cmex10.ttf diff --git a/WpfMath.Shared/Fonts/cmmi10.ttf b/src/WpfMath/Fonts/cmmi10.ttf similarity index 100% rename from WpfMath.Shared/Fonts/cmmi10.ttf rename to src/WpfMath/Fonts/cmmi10.ttf diff --git a/WpfMath.Shared/Fonts/cmr10.ttf b/src/WpfMath/Fonts/cmr10.ttf similarity index 100% rename from WpfMath.Shared/Fonts/cmr10.ttf rename to src/WpfMath/Fonts/cmr10.ttf diff --git a/WpfMath.Shared/Fonts/cmsy10.ttf b/src/WpfMath/Fonts/cmsy10.ttf similarity index 100% rename from WpfMath.Shared/Fonts/cmsy10.ttf rename to src/WpfMath/Fonts/cmsy10.ttf diff --git a/src/WpfMath/GlueSettingsParser.cs b/src/WpfMath/GlueSettingsParser.cs index 015a62a0..651c6a14 100644 --- a/src/WpfMath/GlueSettingsParser.cs +++ b/src/WpfMath/GlueSettingsParser.cs @@ -1,7 +1,8 @@ using System; using System.Collections.Generic; -using System.Reflection; using System.Xml.Linq; +using WpfMath.Data; +using WpfMath.Utils; namespace WpfMath { @@ -58,7 +59,8 @@ private static void SetStyleMappings() public GlueSettingsParser() { - var doc = XDocument.Load(new System.IO.StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)!)); + using var resource = typeof(WpfMathResourceMarker).Assembly.ReadResource(resourceName); + var doc = XDocument.Load(resource); this.rootElement = doc.Root; this.glueTypes = new List(); diff --git a/src/WpfMath/PredefinedTexFormulaSettingsParser.cs b/src/WpfMath/PredefinedTexFormulaSettingsParser.cs index 0cbee94f..29382a68 100644 --- a/src/WpfMath/PredefinedTexFormulaSettingsParser.cs +++ b/src/WpfMath/PredefinedTexFormulaSettingsParser.cs @@ -1,7 +1,8 @@ using System.Collections.Generic; using System.Diagnostics; -using System.Reflection; using System.Xml.Linq; +using WpfMath.Data; +using WpfMath.Utils; namespace WpfMath { @@ -31,7 +32,8 @@ private static void AddToMap(IEnumerable mapList, string[] table) public TexPredefinedFormulaSettingsParser() { - var doc = XDocument.Load(new System.IO.StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)!)); + using var resource = typeof(WpfMathResourceMarker).Assembly.ReadResource(resourceName); + var doc = XDocument.Load(resource); this.rootElement = doc.Root; } diff --git a/src/WpfMath/TexPredefinedFormulaParser.cs b/src/WpfMath/TexPredefinedFormulaParser.cs index 5d2708e2..90643cd1 100644 --- a/src/WpfMath/TexPredefinedFormulaParser.cs +++ b/src/WpfMath/TexPredefinedFormulaParser.cs @@ -2,11 +2,11 @@ using System.Collections.Generic; using System.Diagnostics; using System.Globalization; -using System.IO; -using System.Reflection; using System.Windows.Media; using System.Xml.Linq; +using WpfMath.Data; using WpfMath.Parsers.PredefinedFormulae; +using WpfMath.Utils; namespace WpfMath { @@ -83,7 +83,8 @@ private static Type[] GetArgumentTypes(IEnumerable args) public TexPredefinedFormulaParser() { - var doc = XDocument.Load(new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)!)); + using var resource = typeof(WpfMathResourceMarker).Assembly.ReadResource(resourceName); + var doc = XDocument.Load(resource); this.rootElement = doc.Root; } diff --git a/src/WpfMath/TexSymbolParser.cs b/src/WpfMath/TexSymbolParser.cs index 211b6590..35aeffc8 100644 --- a/src/WpfMath/TexSymbolParser.cs +++ b/src/WpfMath/TexSymbolParser.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; -using System.IO; -using System.Reflection; using System.Xml.Linq; using WpfMath.Atoms; +using WpfMath.Data; +using WpfMath.Utils; namespace WpfMath { @@ -38,7 +38,8 @@ private static void SetTypeMappings() public TexSymbolParser() { // for 3.5 - var doc = XDocument.Load(new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)!)); + using var resource = typeof(WpfMathResourceMarker).Assembly.ReadResource(resourceName); + var doc = XDocument.Load(resource); this.rootElement = doc.Root; } diff --git a/src/WpfMath/WpfMath.csproj b/src/WpfMath/WpfMath.csproj index f8a13c76..0859b4ba 100644 --- a/src/WpfMath/WpfMath.csproj +++ b/src/WpfMath/WpfMath.csproj @@ -36,6 +36,13 @@ Fixed: true + + + + + + +