Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to add a Unity project to test compilation #636

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 15 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ README.html

samples/dotnet/project.lock.json
YamlDotNet/Properties/AssemblyInfo.Generated.cs

.vs
/YamlDotNet/Properties/AssemblyInfo.cs
BenchmarkDotNet.Artifacts

/YamlDotNet.AotTest/exitcode.txt
/tools/build/Properties/launchSettings.json

/YamlDotNet.Unity/Library
/YamlDotNet.Unity/Logs
/YamlDotNet.Unity/MonoBleedingEdge
/YamlDotNet.Unity/UserSettings
/YamlDotNet.Unity/YamlDotNet.Unity_Data

.vs
/YamlDotNet/Properties/AssemblyInfo.cs
BenchmarkDotNet.Artifacts

/YamlDotNet.AotTest/exitcode.txt
/tools/build/Properties/launchSettings.json
/YamlDotNet.Unity
*.meta
/YamlDotNet/**/*.meta
/YamlDotNet.Unity/build
8 changes: 8 additions & 0 deletions YamlDotNet.Unity/Assets/Examples.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions YamlDotNet.Unity/Assets/Examples/ConvertYamlToJson.cs
11 changes: 11 additions & 0 deletions YamlDotNet.Unity/Assets/Examples/ConvertYamlToJson.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions YamlDotNet.Unity/Assets/Examples/DeserializeObjectGraph.cs
11 changes: 11 additions & 0 deletions YamlDotNet.Unity/Assets/Examples/DeserializeObjectGraph.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions YamlDotNet.Unity/Assets/Examples/Helpers.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

147 changes: 147 additions & 0 deletions YamlDotNet.Unity/Assets/Examples/Helpers/ExampleRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Xunit.Abstractions;

using UnityEngine;
using UnityEngine.Serialization;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace YamlDotNet.Samples.Helpers {
public class ExampleRunner : MonoBehaviour {

private StringTestOutputHelper helper = new StringTestOutputHelper();

public string[] disabledTests = new string[] {};

private class StringTestOutputHelper : ITestOutputHelper {
private StringBuilder output = new StringBuilder();
public void WriteLine() {
output.AppendLine();
}
public void WriteLine(string value) {
output.AppendLine(value);
}
public void WriteLine(string format, params object[] args) {
output.AppendFormat(format, args);
output.AppendLine();
}

public override string ToString() { return output.ToString(); }
public void Clear() { output = new StringBuilder(); }
}

public static string[] GetAllTestNames() {
bool skipMethods;
var results = new List<string>();
foreach (Type t in Assembly.GetExecutingAssembly().GetTypes()) {
if (t.Namespace == "YamlDotNet.Samples" && t.IsClass) {
skipMethods = false;
foreach (MethodInfo mi in t.GetMethods()) {
if (mi.Name == "Main") {
SampleAttribute sa = (SampleAttribute) Attribute.GetCustomAttribute(mi, typeof(SampleAttribute));
if (sa != null) {
results.Add(t.Name);
skipMethods = true;
break;
}
}
if (skipMethods) break;
}
}
}
return results.ToArray();
}

public static string[] GetAllTestDisplayNames() {
bool skipMethods;
var results = new List<string>();
foreach (Type t in Assembly.GetExecutingAssembly().GetTypes()) {
if (t.Namespace == "YamlDotNet.Samples" && t.IsClass) {
skipMethods = false;
foreach (MethodInfo mi in t.GetMethods()) {
if (mi.Name == "Main") {
SampleAttribute sa = (SampleAttribute) Attribute.GetCustomAttribute(mi, typeof(SampleAttribute));
if (sa != null) {
results.Add(sa.DisplayName);
skipMethods = true;
break;
}
}
if (skipMethods) break;
}
}
}
return results.ToArray();
}

private void Start() {
bool skipMethods;
foreach (Type t in Assembly.GetExecutingAssembly().GetTypes()) {
if (t.Namespace == "YamlDotNet.Samples" && t.IsClass && Array.IndexOf(disabledTests, t.Name) == -1) {
skipMethods = false;
foreach (MethodInfo mi in t.GetMethods()) {
if (mi.Name == "Main") {
SampleAttribute sa = (SampleAttribute) Attribute.GetCustomAttribute(mi, typeof(SampleAttribute));
if (sa != null) {
helper.WriteLine("{0} - {1}", sa.DisplayName, sa.Description);
var testObject = t.GetConstructor(new Type[] { typeof(StringTestOutputHelper) }).Invoke(new object[] { helper });
mi.Invoke(testObject, new object[] {});
Debug.Log(helper.ToString());
helper.Clear();
skipMethods = true;
break;
}
}
if (skipMethods) break;
}
}
}
}
}

#if UNITY_EDITOR
[CustomEditor(typeof(ExampleRunner))]
public class ExampleRunnerEditor : Editor {
private ExampleRunner runner;
private string[] allTests;
private string[] allDisplayNames;
private bool[] enabledTests;

public void OnEnable() {
runner = (ExampleRunner) target;

allTests = ExampleRunner.GetAllTestNames();
allDisplayNames = ExampleRunner.GetAllTestDisplayNames();
enabledTests = new bool[allTests.Length];
for (int i = 0; i < allTests.Length; i++)
enabledTests[i] = Array.IndexOf(runner.disabledTests, allTests[i]) == -1;
}

public override void OnInspectorGUI() {
int nextDisabledIndex = 0;
for (int i = 0; i < allTests.Length; i++) {
EditorGUI.BeginChangeCheck();
if (!enabledTests[i])
nextDisabledIndex++;
enabledTests[i] = EditorGUILayout.Toggle(allDisplayNames[i], enabledTests[i]);
if (EditorGUI.EndChangeCheck()) {
if (enabledTests[i]) {
var l = new List<string>(runner.disabledTests);
l.Remove(allTests[i]);
runner.disabledTests = l.ToArray();
} else {
var l = new List<string>(runner.disabledTests);
l.Insert(nextDisabledIndex, allTests[i]);
runner.disabledTests = l.ToArray();
}
}
}
}
}
#endif
}
11 changes: 11 additions & 0 deletions YamlDotNet.Unity/Assets/Examples/Helpers/ExampleRunner.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions YamlDotNet.Unity/Assets/Examples/Helpers/ITestOutputHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Xunit.Abstractions
{
public interface ITestOutputHelper {
void WriteLine();
void WriteLine(string value);
void WriteLine(string format, params object[] args);
}
}
11 changes: 11 additions & 0 deletions YamlDotNet.Unity/Assets/Examples/Helpers/ITestOutputHelper.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions YamlDotNet.Unity/Assets/Examples/Helpers/SampleAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace YamlDotNet.Samples.Helpers
{
/// <summary>
/// Marks a test as being a code sample.
/// </summary>
internal class SampleAttribute : System.Attribute
{
public string DisplayName { get; set; }
public string Description { get; set; }
}
}
11 changes: 11 additions & 0 deletions YamlDotNet.Unity/Assets/Examples/Helpers/SampleAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions YamlDotNet.Unity/Assets/Examples/LoadingAYamlStream.cs
11 changes: 11 additions & 0 deletions YamlDotNet.Unity/Assets/Examples/LoadingAYamlStream.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions YamlDotNet.Unity/Assets/Examples/SerializeObjectGraph.cs
11 changes: 11 additions & 0 deletions YamlDotNet.Unity/Assets/Examples/SerializeObjectGraph.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions YamlDotNet.Unity/Assets/Examples/YamlDotNet.Examples.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "YamlDotNet.Examples",
"references": [
"YamlDotNet"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions YamlDotNet.Unity/Assets/Scenes.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.