diff --git a/Tests/GenericCollections/ListTests.cs b/Tests/GenericCollections/ListTests.cs index a217572..7dd1061 100644 --- a/Tests/GenericCollections/ListTests.cs +++ b/Tests/GenericCollections/ListTests.cs @@ -11,6 +11,16 @@ namespace GenericCollections [TestClass] public class ListTests { + [Cleanup] + public void CleanupTests() + { + // StaticListHolder is shared static state (see nanoframework/Home#1821 + // repro below); clear it so List_StaticField_* tests start empty on + // every run/retry, even if a prior test's assertions failed. + StaticListHolder.Numbers.Clear(); + StaticListHolder.Items.Clear(); + } + [TestMethod] public void List_Constructor_Default() { @@ -1203,6 +1213,32 @@ public void List_GenericMethod_NewObj_InsertAndIterate() Assert.AreEqual(4, result[3]); } + [TestMethod] + public void List_StaticField_ValueType_Add() + { + // Reproduces nanoframework/Home#1821: a List held in a static readonly + // field initialized via a field initializer (i.e. constructed from the + // declaring type's own .cctor), then Add()'d to from another method. + StaticListHolder.Numbers.Add(1); + StaticListHolder.Numbers.Add(2); + + Assert.AreEqual(2, StaticListHolder.Numbers.Count); + Assert.AreEqual(1, StaticListHolder.Numbers[0]); + Assert.AreEqual(2, StaticListHolder.Numbers[1]); + } + + [TestMethod] + public void List_StaticField_ReferenceType_Add() + { + // Reproduces nanoframework/Home#1821 with a reference-type element, + // mirroring the reported List Sensors static field. + StaticListHolder.Items.Add(new DummyClass(1, "One")); + + Assert.AreEqual(1, StaticListHolder.Items.Count); + Assert.AreEqual(1, StaticListHolder.Items[0].Id); + Assert.AreEqual("One", StaticListHolder.Items[0].Name); + } + // Generic helper methods that exercise NEWOBJ with MVAR TypeSpecs. // Each method creates a new List from within a generic method body, // producing IL with MethodRef tokens whose owner TypeSpec is List. @@ -1316,6 +1352,14 @@ public DummyClass(int id, string name) } } + // Field initializers below run as part of this type's own .cctor, matching the + // pattern reported in nanoframework/Home#1821 (`private static readonly List ... = new List();`). + internal static class StaticListHolder + { + internal static readonly List Numbers = new List(); + internal static readonly List Items = new List(); + } + internal struct DummyStruct { public int Id { get; set; }