Skip to content

Commit

Permalink
Fix ITuple not available in older Unity versions.
Browse files Browse the repository at this point in the history
  • Loading branch information
timcassell committed Sep 11, 2023
1 parent 17958f1 commit a0bece1
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion Package/Tests/CoreTests/APIs/MergeSettledTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,24 @@
using NUnit.Framework;
using Proto.Promises;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;

#if UNITY_5_5_OR_NEWER && !NET_LEGACY && !UNITY_2021_2_OR_NEWER
// ITuple interface was added in net471, which Unity 2018 and 2019 don't support.
namespace System.Runtime.CompilerServices
{
public interface ITuple
{
int Length { get; }

object this[int index] { get; }
}
}
#endif

namespace ProtoPromiseTests.APIs
{
public class MergeSettledTests
Expand All @@ -27,6 +41,27 @@ public void Teardown()
TestHelper.Cleanup();
}

private class TupleWrapper : ITuple
{
private readonly ArrayList list = new ArrayList(7);

public object this[int index] { get { return list[index]; } }

public int Length { get { return list.Count; } }

internal TupleWrapper(object tuple)
{
var type = tuple.GetType();
for (int i = 1; i <= 7; ++i)
{
var fieldInfo = type.GetField("Item" + i);
if (fieldInfo == null) break;

list.Add(fieldInfo.GetValue(tuple));
}
}
}

// There are a lot of MergeSettled methods, it's easier to use reflection to test them all.
public class MergeSettledArg
{
Expand Down Expand Up @@ -218,7 +253,7 @@ private static void Complete(object deferred, MergeSettledArg opts)

private static Promise<ITuple> ConvertPromise<T>(Promise<T> promise)
{
return promise.Then(v => v as ITuple);
return promise.Then(v => v as ITuple ?? new TupleWrapper(v));
}

private const string rejectValue = "reject";
Expand Down

0 comments on commit a0bece1

Please sign in to comment.