|
| 1 | +#if UNITY_EDITOR |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using NUnit.Framework; |
| 5 | +using UnityEditor; |
| 6 | + |
| 7 | +/// A `.mensharp` file makes its folder — and every subfolder — MenSharp's, |
| 8 | +/// wherever it lives, and each source's programs land under the nearest marked |
| 9 | +/// folder to it. The probe folders end in `~` so Unity never imports the dummy |
| 10 | +/// scripts (they would not compile); MenSharpSources reads them off disk all |
| 11 | +/// the same, which is exactly the path under test. |
| 12 | +public class MenSharpMarkerTests |
| 13 | +{ |
| 14 | + private const string Outer = "Assets/MenSharpMarkerProbeOuter~"; |
| 15 | + private const string Inner = Outer + "/Gadget"; |
| 16 | + |
| 17 | + [SetUp] |
| 18 | + public void SetUp() |
| 19 | + { |
| 20 | + Directory.CreateDirectory(Inner); |
| 21 | + File.WriteAllText(Outer + "/" + MenSharpMarker.MarkerFileName, "# test\n"); |
| 22 | + File.WriteAllText(Outer + "/Outer.cs", "// dummy\n"); |
| 23 | + File.WriteAllText(Inner + "/Inner.cs", "// dummy\n"); |
| 24 | + } |
| 25 | + |
| 26 | + [TearDown] |
| 27 | + public void TearDown() |
| 28 | + { |
| 29 | + if (Directory.Exists(Outer)) |
| 30 | + { |
| 31 | + Directory.Delete(Outer, true); |
| 32 | + } |
| 33 | + // a compile during a test may have registered the probe folder in |
| 34 | + // UdonSharp's blacklist; take it back out so the tracked settings |
| 35 | + // asset does not accumulate a prefix for a folder that no longer exists |
| 36 | + MenSharpUdonSharpIsolation.StopIgnoring(Outer); |
| 37 | + MenSharpUdonSharpIsolation.StopIgnoring(Inner); |
| 38 | + } |
| 39 | + |
| 40 | + [Test] |
| 41 | + public void AMarkedFolderIsAMenSharpSourceRootWhereverItLives() |
| 42 | + { |
| 43 | + Assert.IsTrue(MenSharpMarker.MarkedRoots().Contains(Outer)); |
| 44 | + Assert.IsTrue(MenSharpSources.IsMenSharpSource(Outer + "/Outer.cs")); |
| 45 | + } |
| 46 | + |
| 47 | + [Test] |
| 48 | + public void SubfoldersOfAMarkedFolderAreIncludedToo() |
| 49 | + { |
| 50 | + Assert.IsTrue(MenSharpSources.IsMenSharpSource(Inner + "/Inner.cs")); |
| 51 | + Assert.AreEqual(Outer, MenSharpMarker.MarkedRootOf(Inner + "/Inner.cs")); |
| 52 | + } |
| 53 | + |
| 54 | + [Test] |
| 55 | + public void CollectClaimsMarkedSourcesWithProgramsUnderTheirRoot() |
| 56 | + { |
| 57 | + MenSharpSources.SourceSet set = MenSharpSources.Collect(); |
| 58 | + // the source is listed as MenSharp |
| 59 | + Assert.IsTrue( |
| 60 | + set.MenSharp.Any(p => MenSharpSources.Normalize(p).EndsWith("/Outer.cs")), |
| 61 | + "Outer.cs should be a MenSharp source"); |
| 62 | + // and never doubled up as a library |
| 63 | + Assert.IsFalse( |
| 64 | + set.Library.Any(p => MenSharpSources.Normalize(p).EndsWith("/Outer.cs")), |
| 65 | + "a marked source must not also be a library"); |
| 66 | + string openable = set.MenSharp.First(p => MenSharpSources.Normalize(p).EndsWith("/Outer.cs")); |
| 67 | + Assert.AreEqual(Outer + "/Programs", set.ProgramsFolderOf[openable]); |
| 68 | + } |
| 69 | + |
| 70 | + [Test] |
| 71 | + public void ANestedMarkerOwnsItsOwnSubtree() |
| 72 | + { |
| 73 | + File.WriteAllText(Inner + "/" + MenSharpMarker.MarkerFileName, "# test\n"); |
| 74 | + try |
| 75 | + { |
| 76 | + Assert.AreEqual(Inner, MenSharpMarker.MarkedRootOf(Inner + "/Inner.cs")); |
| 77 | + MenSharpSources.SourceSet set = MenSharpSources.Collect(); |
| 78 | + string openable = set.MenSharp.First(p => MenSharpSources.Normalize(p).EndsWith("/Inner.cs")); |
| 79 | + Assert.AreEqual(Inner + "/Programs", set.ProgramsFolderOf[openable]); |
| 80 | + } |
| 81 | + finally |
| 82 | + { |
| 83 | + File.Delete(Inner + "/" + MenSharpMarker.MarkerFileName); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /// End to end: the committed Assets/Gadget fixture is a marked folder with |
| 88 | + /// a real behaviour, and its program must compile into its own Programs — |
| 89 | + /// never into Assets/MenSharp/Programs. |
| 90 | + [Test] |
| 91 | + public void ABehaviourInAMarkedFolderCompilesToThatFoldersPrograms() |
| 92 | + { |
| 93 | + MenSharpCompiler.CompileAll(); |
| 94 | + AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); |
| 95 | + var program = AssetDatabase.LoadAssetAtPath<MenSharpProgramAsset>( |
| 96 | + "Assets/Gadget/Programs/GadgetBehaviour.asset"); |
| 97 | + Assert.IsNotNull(program, "GadgetBehaviour should compile into Assets/Gadget/Programs"); |
| 98 | + Assert.IsNull( |
| 99 | + AssetDatabase.LoadAssetAtPath<MenSharpProgramAsset>( |
| 100 | + "Assets/MenSharp/Programs/GadgetBehaviour.asset"), |
| 101 | + "a marked folder's program must not land in Assets/MenSharp/Programs"); |
| 102 | + } |
| 103 | +} |
| 104 | +#endif |
0 commit comments