Skip to content

Commit

Permalink
test for generic explicit implementation
Browse files Browse the repository at this point in the history
test for 5dd5cb5
  • Loading branch information
jakubmisek committed May 9, 2024
1 parent 5dd5cb5 commit 24bee47
Showing 1 changed file with 122 additions and 9 deletions.
131 changes: 122 additions & 9 deletions src/Tests/Peachpie.Runtime.Tests/ClrFeaturesTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;
using ComponentAce.Compression.Libs.zlib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Pchp.Core;
using Pchp.Core.Reflection;
Expand All @@ -19,7 +19,7 @@ string CompileAndRun(string code)
}
}

string CompileAndRun(Context ctx, string code)
string CompileAndRun(Context ctx, string code, string[] additionalReferences = null)
{
var outputStream = new MemoryStream();

Expand All @@ -37,7 +37,7 @@ string CompileAndRun(Context ctx, string code)
IsSubmission = false,
//EmitDebugInformation = true,
Location = new Location(Path.GetFullPath("dummy.php"), 0, 0),
//AdditionalReferences = AdditionalReferences,
AdditionalReferences = additionalReferences,
}, code);

// run
Expand Down Expand Up @@ -112,12 +112,6 @@ class C {
}
}

class ClrEventTestClass
{
public event EventHandler e;
public void fire() => e?.Invoke(this, EventArgs.Empty);
}

[TestMethod]
public void ClrEvent()
{
Expand All @@ -143,5 +137,124 @@ public void ClrEvent()
Assert.AreEqual("1", output);
}
}

[TestMethod]
public void ExplicitOverridesWithGenerics()
{
using (var ctx = Context.CreateEmpty())
{
// compiler must bind all the generic explicitly defined properties and methods correctly
CompileAndRun(ctx, $@"<?php
class MyCollection extends {typeof(ObservableCollection).GetPhpTypeInfo().Name} //
{{
}}
",
additionalReferences: new[] { typeof(ObservableCollection).Assembly.Location }
);

Assert.IsNotNull(ctx.Create("MyCollection"));
}
}
}

#region Test Classes

/// <summary>
/// Implements both <see cref="ObservableCollection{T}"/> and PHP's <see cref="Iterator"/>.
/// </summary>
public class ObservableCollection : ObservableCollection<object>, Iterator, ArrayAccess
{
private int _position = 0;

public ObservableCollection() : base()
{

}

public ObservableCollection(PhpArray phpArray)
{
if (phpArray.IntrinsicEnumerator == null)
{
return;
}

foreach (var item in phpArray)
{
Add(item.Value.Alias);
}
}

public void SetAll(PhpArray phpArray)
{
if (phpArray.IntrinsicEnumerator == null)
{
return;
}

Clear();

foreach (var item in phpArray)
{
Add(item.Value.Alias);
}
}

public void rewind()
{
_position = 0;
}

public void next()
{
_position++;
}

public bool valid()
{
return _position >= 0 && _position < Count;
}

public PhpValue key()
{
return _position;
}

public PhpValue current()
{
return PhpValue.FromClr(this[_position]);
}

public PhpValue offsetGet(PhpValue offset)
{
return PhpValue.FromClr(this[offset.ToInt()]);
}

public void offsetSet(PhpValue offset, PhpValue value)
{
this[offset.ToInt()] = value.ToClr();
}

public void offsetUnset(PhpValue offset)
{
RemoveAt(offset.ToInt());
}

public bool offsetExists(PhpValue offset)
{
return offset.ToInt() >= 0 && offset.ToInt() < Count;
}

public PhpArray toArray()
{
return new PhpArray(this);
}
}

class ClrEventTestClass
{
public event EventHandler e;
public void fire() => e?.Invoke(this, EventArgs.Empty);
}

#endregion
}

0 comments on commit 24bee47

Please sign in to comment.