Skip to content

Commit ad8dcfb

Browse files
committed
impl .mensharp
1 parent 53624d1 commit ad8dcfb

15 files changed

Lines changed: 554 additions & 41 deletions

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
/tests/unity-project/obj/
1111
/tests/unity-project/Assets/MenSharp/Programs/
1212
/tests/unity-project/Assets/MenSharp/Programs.meta
13+
/tests/unity-project/Assets/Gadget/Programs/
14+
/tests/unity-project/Assets/Gadget/Programs.meta
1315
/tests/unity-project/Assets/Tests/Generated/
1416
/tests/unity-project/Assets/Tests/Generated.meta
1517
/tests/unity-project/Assets/Scenes/

‎tests/unity-project/Assets/Gadget.meta‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# This folder's C# is compiled by MenSharp to Udon, and everything under it.
2+
# UdonSharp's scanner skips it, so C# 9+ here does not break UdonSharp.
3+
# Delete this file to hand the folder back to UdonSharp / plain C#.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// A behaviour living outside Assets/MenSharp, in a folder marked with a
2+
// `.mensharp` file. It exists to prove the marker path end to end: MenSharp
3+
// compiles it, and its program lands under this folder's own Programs. Kept to
4+
// plain C# so UdonSharp's scanner never trips over it even for the instant
5+
// before isolation registers the folder.
6+
7+
using MenSharp;
8+
using UnityEngine;
9+
10+
public class GadgetBehaviour : MenSharpBehaviour
11+
{
12+
public int doubled;
13+
14+
public void Double()
15+
{
16+
doubled = 21 * 2;
17+
Debug.Log("[gadget] doubled: " + doubled);
18+
}
19+
}

‎tests/unity-project/Assets/Gadget/GadgetBehaviour.cs.meta‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

‎tests/unity-project/Assets/Tests/Editor/MenSharpMarkerTests.cs.meta‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#if UNITY_EDITOR
2+
using NUnit.Framework;
3+
4+
/// MenSharpSceneProcessor.CallbackOrder is a public constant other scene
5+
/// processors order themselves against, so it must stay a compile-time value
6+
/// and keep agreeing with the callbackOrder the processor actually reports.
7+
public class MenSharpSceneProcessorTests
8+
{
9+
[Test]
10+
public void TheCallbackOrderConstantIsStableAndVeryEarly()
11+
{
12+
Assert.AreEqual(-10_000, MenSharpSceneProcessor.CallbackOrder);
13+
}
14+
15+
[Test]
16+
public void TheReportedOrderMatchesThePublishedConstant()
17+
{
18+
Assert.AreEqual(
19+
MenSharpSceneProcessor.CallbackOrder,
20+
new MenSharpSceneProcessor().callbackOrder);
21+
}
22+
}
23+
#endif

‎tests/unity-project/Assets/Tests/Editor/MenSharpSceneProcessorTests.cs.meta‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/unity-project/Assets/UdonSharp/Settings/UdonSharpSettings.asset‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ MonoBehaviour:
1818
newScriptTemplateOverride: {fileID: 0}
1919
scanningDirectoryBlacklist:
2020
- Assets/MenSharp/
21+
- Assets/Gadget/
2122
defaultBehaviourInterfaceType:
2223
buildDebugInfo: 1
2324
includeInlineCode: 1

0 commit comments

Comments
 (0)