Sfoglia il codice sorgente

adding Test Attachments helper
+build settings

Vicente Penades 3 anni fa
parent
commit
40cfcfb495

+ 11 - 4
src/Directory.Build.props

@@ -7,7 +7,7 @@
 
   <PropertyGroup>
     <Authors>Vicente Penades</Authors>    
-    <Copyright>Copyright (c) 2021 Vicente Penades</Copyright>
+    <Copyright>Copyright (c) 2022 Vicente Penades</Copyright>
     <Description>SharpGLTF is a C# library for reading and writing glTF2 3D models</Description>    
   </PropertyGroup>  
 
@@ -101,9 +101,11 @@
   </PropertyGroup>
 
   <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
-    <DebugSymbols>true</DebugSymbols>
-    <IncludeSymbols>true</IncludeSymbols>
-    <SymbolPackageFormat>snupkg</SymbolPackageFormat>    
+    
+    <DebugSymbols>true</DebugSymbols>    
+    <SymbolPackageFormat>snupkg</SymbolPackageFormat>
+
+    <IncludeSymbols>true</IncludeSymbols>    
     
     <GenerateDocumentationFile>true</GenerateDocumentationFile>
     <DocumentationFile>bin\$(Configuration)\$(AssemblyName).xml</DocumentationFile>
@@ -122,7 +124,12 @@
 
     <!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md -->
     
+    <!-- better alternative than source link -->
     <EmbedAllSources>true</EmbedAllSources>
+    
+    <!-- conceal source code full paths -->
+    <PathMap>$(MSBuildProjectDirectory)=X:\sources</PathMap>
+    
     <!-- Optional: Embed source files that are not tracked by the source control manager in the PDB -->
     <!--<EmbedUntrackedSources>true</EmbedUntrackedSources>-->
     

+ 1 - 0
tests/SharpGLTF.Core.Tests/Animations/AnimationSamplingTests.cs

@@ -9,6 +9,7 @@ using NUnit.Framework;
 namespace SharpGLTF
 {
     [TestFixture]
+    [AttachmentPathFormat("*/TestResults/Animations/?")]
     [Category("Core.Animations")]
     public class AnimationSamplingTests
     {

+ 15 - 5
tests/SharpGLTF.Core.Tests/AssemblyAPITests.cs

@@ -56,7 +56,7 @@ namespace SharpGLTF
 
             var API = DumpAssemblyAPI.GetTypeSignature(type.GetTypeInfo()).OrderBy(item => item).ToArray();
 
-            TestContext.CurrentContext.AttachText("TestAPI.txt", API);
+            AttachmentInfo.From("TestAPI.txt").WriteTextLines(API);
 
             foreach (var l in API)
             {
@@ -72,9 +72,14 @@ namespace SharpGLTF
 
             var assembly = typeof(Schema2.ModelRoot).Assembly;
 
-            var API = DumpAssemblyAPI.GetAssemblySignature(assembly).OrderBy(item => item).ToArray();
+            var API = DumpAssemblyAPI
+                .GetAssemblySignature(assembly)
+                .OrderBy(item => item)
+                .ToArray();
 
-            TestContext.CurrentContext.AttachText($"API.Core.{Schema2.Asset.AssemblyInformationalVersion}.txt", API);
+            AttachmentInfo
+                .From($"API.Core.{Schema2.Asset.AssemblyInformationalVersion}.txt")
+                .WriteTextLines(API);            
 
             _CheckBackwardsCompatibility("API.Core.1.0.0-alpha0011.txt", API);
         }
@@ -86,9 +91,14 @@ namespace SharpGLTF
 
             var assembly = typeof(Schema2.Toolkit).Assembly;
 
-            var API = DumpAssemblyAPI.GetAssemblySignature(assembly).OrderBy(item => item).ToArray();
+            var API = DumpAssemblyAPI
+                .GetAssemblySignature(assembly)
+                .OrderBy(item => item)
+                .ToArray();
 
-            TestContext.CurrentContext.AttachText($"API.Toolkit.{Schema2.Asset.AssemblyInformationalVersion}.txt", API);
+            AttachmentInfo
+                .From($"API.Toolkit.{Schema2.Asset.AssemblyInformationalVersion}.txt")
+                .WriteTextLines(API);
 
             _CheckBackwardsCompatibility("API.Toolkit.1.0.0-alpha0011.txt", API);
         }

+ 10 - 0
tests/SharpGLTF.Core.Tests/Properties/AssemblyInfo.cs

@@ -0,0 +1,10 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+using NUnit.Framework;
+
+[assembly: ResourcePathFormat("*/Assets")]
+[assembly: AttachmentPathFormat("*/TestResults/?")]

+ 4 - 3
tests/SharpGLTF.Core.Tests/Runtime/SceneTemplateTests.cs

@@ -11,6 +11,7 @@ using Plotly;
 namespace SharpGLTF.Runtime
 {
     [Category("Core.Runtime")]
+    [AttachmentPathFormat("*/TestResults/Runtime/?")]
     public class SceneTemplateTests
     {
         [Test]
@@ -95,9 +96,9 @@ namespace SharpGLTF.Runtime
             var scenePlot = new PlotlyScene();
             scenePlot.AppendTriangles(worldTriangles, c=>c);
 
-            scenePlot
-                .ToHtml()
-                .AttachToCurrentTest("result.html", (content, finfo) => System.IO.File.WriteAllText(finfo.FullName, content));
+            AttachmentInfo
+                .From("result.html")
+                .WriteAllText(scenePlot.ToHtml());
         }
 
         [Test]

+ 2 - 1
tests/SharpGLTF.Core.Tests/Schema2/LoadAndSave/LoadGeneratedTests.cs

@@ -10,7 +10,8 @@ namespace SharpGLTF.Schema2.LoadAndSave
     /// <summary>
     /// Test cases for models found in <see href="https://github.com/bghgary/glTF-Asset-Generator"/>
     /// </summary>
-    [TestFixture]
+    [TestFixture]    
+    [AttachmentPathFormat("*/TestResults/LoadAndSave/?")]
     [Category("Model Load and Save")]
     public class LoadGeneratedTests
     {

+ 1 - 0
tests/SharpGLTF.Core.Tests/Schema2/LoadAndSave/LoadInvalidTests.cs

@@ -7,6 +7,7 @@ using NUnit.Framework;
 namespace SharpGLTF.Schema2.LoadAndSave
 {
     [TestFixture]
+    [AttachmentPathFormat("*/TestResults/LoadAndSave/?")]
     [Category("Model Load and Save")]
     public class LoadInvalidTests
     {

+ 1 - 0
tests/SharpGLTF.Core.Tests/Schema2/LoadAndSave/LoadSampleTests.cs

@@ -11,6 +11,7 @@ namespace SharpGLTF.Schema2.LoadAndSave
     /// Test cases for models found in <see href="https://github.com/KhronosGroup/glTF-Sample-Models"/> and more....
     /// </summary>
     [TestFixture]
+    [AttachmentPathFormat("*/TestResults/LoadAndSave/?")]
     [Category("Model Load and Save")]
     public class LoadSampleTests
     {

+ 1 - 0
tests/SharpGLTF.Core.Tests/Schema2/LoadAndSave/LoadSpecialModelsTest.cs

@@ -12,6 +12,7 @@ namespace SharpGLTF.Schema2.LoadAndSave
     /// Test cases for models found in <see href="https://github.com/KhronosGroup/glTF-Blender-Exporter"/>
     /// </summary>
     [TestFixture]
+    [AttachmentPathFormat("*/TestResults/LoadAndSave/?")]
     [Category("Model Load and Save")]
     public class LoadSpecialModelsTest
     {

+ 1 - 0
tests/SharpGLTF.Core.Tests/Schema2/LoadAndSave/RegressionTests.cs

@@ -10,6 +10,7 @@ using SharpGLTF.Validation;
 
 namespace SharpGLTF.Schema2.LoadAndSave
 {
+    [AttachmentPathFormat("*/TestResults/LoadAndSave/?")]
     internal class RegressionTests
     {
         [Test]

+ 23 - 20
tests/SharpGLTF.NUnit/NUnitGltfUtils.cs

@@ -29,15 +29,13 @@ namespace SharpGLTF
 
         public static void AttachToCurrentTest(this ModelRoot model, string fileName, Animation animation, float time)
         {
+            // wavefront does't like files paths with spaces because
+            // some implementations would not find the material file
             fileName = fileName.Replace(" ", "_");
 
-            // find the output path for the current test
-            fileName = TestContext.CurrentContext.GetAttachmentPath(fileName, true);
-
-            model.SaveAsWavefront(fileName, animation, time);
-
-            // Attach the saved file to the current test
-            TestContext.AddTestAttachment(fileName);
+            AttachmentInfo
+                .From(fileName)
+                .WriteFile(f => model.SaveAsWavefront(f.FullName, animation, time));
         }
 
         public static string AttachToCurrentTest<TvG, TvM, TvS>(this Geometry.MeshBuilder<TvG, TvM, TvS> mesh, string fileName)
@@ -57,22 +55,23 @@ namespace SharpGLTF
 
         public static string AttachToCurrentTest(this ModelRoot model, string fileName, WriteSettings settings = null)
         {
-            // find the output path for the current test
-            fileName = TestContext.CurrentContext.GetAttachmentPath(fileName, true);
-
             string validationPath = null;
 
             if (fileName.ToLowerInvariant().EndsWith(".glb"))
             {
-                model.SaveGLB(fileName, settings);
-                validationPath = fileName;
+                validationPath = fileName = AttachmentInfo
+                    .From(fileName)
+                    .WriteFile(f => model.SaveGLB(f.FullName, settings))
+                    .FullName;
             }
             else if (fileName.ToLowerInvariant().EndsWith(".gltf"))
             {
                 if (settings == null) settings = new WriteSettings { JsonIndented = true };
 
-                model.Save(fileName, settings);
-                validationPath = fileName;
+                validationPath = fileName = AttachmentInfo
+                    .From(fileName)
+                    .WriteFile(f => model.Save(f.FullName, settings))
+                    .FullName;
             }
             else if (fileName.ToLowerInvariant().EndsWith(".obj"))
             {
@@ -80,7 +79,11 @@ namespace SharpGLTF
                 if (Node.Flatten(model.DefaultScene).Any(n => n.GetGpuInstancing() != null)) return fileName;                
 
                 fileName = fileName.Replace(" ", "_");
-                model.SaveAsWavefront(fileName);
+
+                fileName = AttachmentInfo
+                    .From(fileName)
+                    .WriteFile(f => model.SaveAsWavefront(f.FullName))
+                    .FullName;
             }
             else if (fileName.ToLowerInvariant().EndsWith(".plotly"))
             {
@@ -90,11 +93,11 @@ namespace SharpGLTF
                     .ToPlotly()
                     .ToHtml();
 
-                System.IO.File.WriteAllText(fileName, html);
-            }
-
-            // Attach the saved file to the current test
-            TestContext.AddTestAttachment(fileName);            
+                fileName = AttachmentInfo
+                    .From(fileName)
+                    .WriteAllText(html)
+                    .FullName;
+            }           
 
             if (validationPath != null)
             {

+ 1 - 32
tests/SharpGLTF.NUnit/NUnitUtils.cs

@@ -42,14 +42,7 @@ namespace SharpGLTF
             return path;
         }
 
-        public static void AttachText(this TestContext context, string fileName, string[] lines)
-        {
-            fileName = context.GetAttachmentPath(fileName, true);
-
-            System.IO.File.WriteAllLines(fileName, lines.ToArray());
-
-            TestContext.AddTestAttachment(fileName);
-        }
+        
 
         public static void AttachShowDirLink(this TestContext context)
         {
@@ -64,29 +57,5 @@ namespace SharpGLTF
 
             TestContext.AddTestAttachment(linkPath);
         }
-
-        public static string AttachToCurrentTest(this Byte[] data, string fileName)
-        {
-            fileName = TestContext.CurrentContext.GetAttachmentPath(fileName, true);
-
-            System.IO.File.WriteAllBytes(fileName, data);
-
-            TestContext.AddTestAttachment(fileName);
-
-            return fileName;
-        }
-
-        public static T AttachToCurrentTest<T>(this T target, string fileName, Action<T, System.IO.FileInfo> onSave)
-        {
-            var filePath = TestContext.CurrentContext.GetAttachmentPath(fileName, true);
-
-            var finfo = new System.IO.FileInfo(filePath);
-
-            onSave(target, finfo);
-
-            if (System.IO.File.Exists(filePath)) TestContext.AddTestAttachment(filePath);
-
-            return target;
-        }
     }
 }

+ 6 - 11
tests/SharpGLTF.NUnit/Plotting.cs

@@ -235,27 +235,22 @@ namespace SharpGLTF
         }
     }
 
-
     public static class PlottingNUnit
     {
         public static void AttachToCurrentTest(this Plotting.Point2Series points, string fileName)
         {
             System.Diagnostics.Debug.Assert(fileName.ToLowerInvariant().EndsWith(".html"));
 
-            fileName = NUnit.Framework.TestContext.CurrentContext.GetAttachmentPath(fileName);
-
-            points.DrawToFile(fileName);
-
-            if (System.IO.File.Exists(fileName)) NUnit.Framework.TestContext.AddTestAttachment(fileName);
+            NUnit.Framework.AttachmentInfo
+                .From(fileName)
+                .WriteFile(f => points.DrawToFile(f.FullName));
         }
 
         public static void AttachToCurrentTest(this IEnumerable<Plotting.Point2Series> series, string fileName)
         {
-            fileName = NUnit.Framework.TestContext.CurrentContext.GetAttachmentPath(fileName);
-
-            Plotting.Point2Series.DrawToFile(fileName, series.ToArray());
-
-            if (System.IO.File.Exists(fileName)) NUnit.Framework.TestContext.AddTestAttachment(fileName);
+            NUnit.Framework.AttachmentInfo
+                .From(fileName)
+                .WriteFile(f => Plotting.Point2Series.DrawToFile(f.FullName, series.ToArray()));
         }
     }
 }

+ 3 - 2
tests/SharpGLTF.NUnit/SharpGLTF.NUnit.csproj

@@ -7,10 +7,11 @@
     <LangVersion>latest</LangVersion>
   </PropertyGroup>
 
-  <ItemGroup>    
-    <PackageReference Include="GltfValidator" Version="2.0.0-dev.3.8.202205072307" />    
+  <ItemGroup>
     <PackageReference Include="nunit" Version="3.13.3" />
     <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
+    <PackageReference Include="GltfValidator" Version="2.0.0-dev.3.8.202205072307" />
+    <PackageReference Include="TestAttachments.NUnit" Version="3.0.0-Preview-20220609-1233" />
   </ItemGroup>
 
   <ItemGroup>    

+ 49 - 0
tests/SharpGLTF.ThirdParty.Tests/SandboxTests.cs

@@ -0,0 +1,49 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Numerics;
+using System.Text;
+using System.Threading.Tasks;
+
+using NUnit.Framework;
+
+using SharpGLTF.Materials;
+using SharpGLTF.Schema2;
+
+namespace SharpGLTF.ThirdParty
+{
+    [ResourcePathFormat("*\\Assets")]
+    [AttachmentPathFormat("*\\?")]
+    internal class SandboxTests
+    {
+        [Test]
+        public void MaterialCreate()
+        {
+            Memory.MemoryImage.TryParseMime64("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAHXpUWHRUaXRsZQAACJlzSU1LLM0pCUmtKCktSgUAKVIFt/VCuZ8AAAAoelRYdEF1dGhvcgAACJkLy0xOzStJVQhIzUtMSS1WcCzKTc1Lzy8BAG89CQyAoFAQAAAANElEQVQoz2O8cuUKAwxoa2vD2VevXsUqzsRAIqC9Bsb///8TdDey+CD0Awsx7h6NB5prAADPsx0VAB8VRQAAAABJRU5ErkJggg==", out var memoryImage);
+
+            var material = new MaterialBuilder()
+                .WithAlpha(Materials.AlphaMode.OPAQUE);            
+
+            material.WithMetallicRoughnessShader()
+                .WithMetallicRoughness(0, 1)
+                .WithChannelImage(KnownChannel.BaseColor, memoryImage)
+                .UseChannel(KnownChannel.BaseColor)
+                    .Texture
+                    .WithSampler(
+                        TextureWrapMode.CLAMP_TO_EDGE,
+                        TextureWrapMode.MIRRORED_REPEAT,
+                        TextureMipMapFilter.NEAREST_MIPMAP_NEAREST,
+                        TextureInterpolationFilter.NEAREST
+                    );
+
+            material
+                .WithSpecularFactor(memoryImage, 0)
+                .WithSpecularColor(memoryImage, Vector3.Zero);
+
+            var model = ModelRoot.CreateModel();
+            model.CreateMaterial(material);
+
+            TestContext.WriteLine(model.GetJsonPreview());
+        }
+    }
+}

+ 11 - 4
tests/SharpGLTF.Toolkit.Tests/Materials/ContentSharingTests.cs

@@ -61,10 +61,17 @@ namespace SharpGLTF.Materials
             TestContext.CurrentContext.AttachGltfValidatorLinks();            
 
             var tex1Bytes = System.IO.File.ReadAllBytes(System.IO.Path.Combine(AssetsPath, "shannon.png"));
-            var tex2Bytes = System.IO.File.ReadAllBytes(System.IO.Path.Combine(AssetsPath, "Texture1.jpg"));
-
-            var tex1 = tex1Bytes.AttachToCurrentTest("shared-shannon.png");
-            var tex2 = tex2Bytes.AttachToCurrentTest("subdir\\shared-in-dir-Texture1.jpg");
+            var tex2Bytes = System.IO.File.ReadAllBytes(System.IO.Path.Combine(AssetsPath, "Texture1.jpg"));
+
+            var tex1 = AttachmentInfo
+                .From("shared-shannon.png")
+                .WriteAllBytes(tex1Bytes)
+                .FullName;
+
+            var tex2 = AttachmentInfo
+                .From("subdir\\shared-in-dir-Texture1.jpg")
+                .WriteAllBytes(tex2Bytes)
+                .FullName;
 
             // create a material using our shared texture
             var material1 = new MaterialBuilder()                

+ 12 - 0
tests/SharpGLTF.Toolkit.Tests/Properties/AssemblyInfo.cs

@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SharpGLTF.Properties
+{
+    internal class AssemblyInfo
+    {
+    }
+}