|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using NUnit.Framework; |
| 6 | + |
| 7 | +namespace UnityNuGet.Tests |
| 8 | +{ |
| 9 | + public class PlatformDefinitionTests |
| 10 | + { |
| 11 | + [Test] |
| 12 | + public void CanFindDefinitions() |
| 13 | + { |
| 14 | + var platformDefs = PlatformDefinition.CreateAllPlatforms(); |
| 15 | + |
| 16 | + // Look-up by OS should return the most general configuration |
| 17 | + var win = platformDefs.Find(UnityOs.Windows); |
| 18 | + Assert.IsNotNull(win); |
| 19 | + Assert.AreEqual(win.Cpu, UnityCpu.AnyCpu); |
| 20 | + |
| 21 | + // Look-up explicit configuration |
| 22 | + var win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64); |
| 23 | + Assert.IsNotNull(win64); |
| 24 | + Assert.AreEqual(win64.Os, win.Os); |
| 25 | + Assert.AreEqual(win64.Cpu, UnityCpu.X64); |
| 26 | + Assert.True(win.Children.Contains(win64)); |
| 27 | + |
| 28 | + // Look-up invalid configuration |
| 29 | + var and = platformDefs.Find(UnityOs.Android, UnityCpu.None); |
| 30 | + Assert.IsNull(and); |
| 31 | + } |
| 32 | + |
| 33 | + [Test] |
| 34 | + public void RemainingPlatforms_NoneVisited() |
| 35 | + { |
| 36 | + var platformDefs = PlatformDefinition.CreateAllPlatforms(); |
| 37 | + var visited = new HashSet<PlatformDefinition>(); |
| 38 | + |
| 39 | + // If no platform was visited, the remaining platforms should be the (AnyOS, AnyCPU) config. |
| 40 | + var remaining = platformDefs.GetRemainingPlatforms(visited); |
| 41 | + Assert.IsNotNull(remaining); |
| 42 | + Assert.AreEqual(1, remaining.Count); |
| 43 | + Assert.AreEqual(remaining.First(), platformDefs); |
| 44 | + } |
| 45 | + |
| 46 | + [Test] |
| 47 | + public void RemainingPlatforms_OneVisited() |
| 48 | + { |
| 49 | + var platformDefs = PlatformDefinition.CreateAllPlatforms(); |
| 50 | + |
| 51 | + foreach (var child in platformDefs.Children) |
| 52 | + { |
| 53 | + var visited = new HashSet<PlatformDefinition>() { child }; |
| 54 | + var remaining = platformDefs.GetRemainingPlatforms(visited); |
| 55 | + |
| 56 | + // We should get all other children, except the one already visited |
| 57 | + Assert.AreEqual(platformDefs.Children.Count, remaining.Count + 1); |
| 58 | + foreach (var r in remaining) |
| 59 | + { |
| 60 | + Assert.AreNotEqual(r, child); |
| 61 | + Assert.IsTrue(platformDefs.Children.Contains(r)); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + [Test] |
| 67 | + public void RemainingPlatforms_LeafVisited() |
| 68 | + { |
| 69 | + var platformDefs = PlatformDefinition.CreateAllPlatforms(); |
| 70 | + var win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64); |
| 71 | + var visited = new HashSet<PlatformDefinition>() { win64 }; |
| 72 | + |
| 73 | + // The remaining platforms should be all non-windows, as well as all !x64 windows |
| 74 | + var expected = platformDefs.Children |
| 75 | + .Except(new[] { win64.Parent }) |
| 76 | + .Concat( |
| 77 | + win64.Parent.Children |
| 78 | + .Except(new[] { win64 })) |
| 79 | + .ToHashSet(); |
| 80 | + var actual = platformDefs.GetRemainingPlatforms(visited); |
| 81 | + Assert.IsTrue(expected.SetEquals(actual)); |
| 82 | + } |
| 83 | + |
| 84 | + [TestCase("")] |
| 85 | + [TestCase("base")] |
| 86 | + public void TestConfigPath_Root(string basePath) |
| 87 | + { |
| 88 | + var platformDefs = PlatformDefinition.CreateAllPlatforms(); |
| 89 | + var file = new PlatformFile("a/b/c.dll", platformDefs); |
| 90 | + |
| 91 | + // We don't use extra paths for the (AnyOS, AnyCPU) configuration |
| 92 | + var actual = file.GetDestinationPath(basePath); |
| 93 | + var expected = Path.Combine( |
| 94 | + basePath, |
| 95 | + Path.GetFileName(file.SourcePath)); |
| 96 | + Assert.AreEqual(actual, expected); |
| 97 | + } |
| 98 | + |
| 99 | + [TestCase("")] |
| 100 | + [TestCase("base")] |
| 101 | + public void TestConfigPath_OsOnly(string basePath) |
| 102 | + { |
| 103 | + var platformDefs = PlatformDefinition.CreateAllPlatforms(); |
| 104 | + var win = platformDefs.Find(UnityOs.Windows); |
| 105 | + var file = new PlatformFile("a/b/c.dll", win); |
| 106 | + |
| 107 | + var actual = file.GetDestinationPath(basePath); |
| 108 | + var expected = Path.Combine( |
| 109 | + basePath, |
| 110 | + "Windows", |
| 111 | + Path.GetFileName(file.SourcePath)); |
| 112 | + Assert.AreEqual(actual, expected); |
| 113 | + } |
| 114 | + |
| 115 | + [TestCase("")] |
| 116 | + [TestCase("base")] |
| 117 | + public void TestConfigPath_Full(string basePath) |
| 118 | + { |
| 119 | + var platformDefs = PlatformDefinition.CreateAllPlatforms(); |
| 120 | + var win64 = platformDefs.Find(UnityOs.Windows, UnityCpu.X64); |
| 121 | + var file = new PlatformFile("a/b/c.dll", win64); |
| 122 | + |
| 123 | + var actual = file.GetDestinationPath(basePath); |
| 124 | + var expected = Path.Combine( |
| 125 | + basePath, |
| 126 | + "Windows", |
| 127 | + "x86_64", |
| 128 | + Path.GetFileName(file.SourcePath)); |
| 129 | + Assert.AreEqual(actual, expected); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments