{"action":"created","issue":{"url":"https://api.github.com/repos/nunit/nunit/issues/2439","repository_url":"https://api.github.com/repos/nunit/nunit","labels_url":"https://api.github.com/repos/nunit/nunit/issues/2439/labels{/name}","comments_url":"https://api.github.com/repos/nunit/nunit/issues/2439/comments","events_url":"https://api.github.com/repos/nunit/nunit/issues/2439/events","html_url":"https://github.com/nunit/nunit/pull/2439","id":257598706,"number":2439,"title":"Some fixes for TestFixtureSource applied to generic classes","user":{"login":"StanEgo","id":1964984,"avatar_url":"https://avatars3.githubusercontent.com/u/1964984?v=4","gravatar_id":"","url":"https://api.github.com/users/StanEgo","html_url":"https://github.com/StanEgo","followers_url":"https://api.github.com/users/StanEgo/followers","following_url":"https://api.github.com/users/StanEgo/following{/other_user}","gists_url":"https://api.github.com/users/StanEgo/gists{/gist_id}","starred_url":"https://api.github.com/users/StanEgo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/StanEgo/subscriptions","organizations_url":"https://api.github.com/users/StanEgo/orgs","repos_url":"https://api.github.com/users/StanEgo/repos","events_url":"https://api.github.com/users/StanEgo/events{/privacy}","received_events_url":"https://api.github.com/users/StanEgo/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":9,"created_at":"2017-09-14T05:23:49Z","updated_at":"2017-10-17T11:25:28Z","closed_at":"2017-10-17T11:05:25Z","author_association":"CONTRIBUTOR","pull_request":{"url":"https://api.github.com/repos/nunit/nunit/pulls/2439","html_url":"https://github.com/nunit/nunit/pull/2439","diff_url":"https://github.com/nunit/nunit/pull/2439.diff","patch_url":"https://github.com/nunit/nunit/pull/2439.patch"},"body":"This is a simple solution when we pass generic type arguments directly to the TestFixture. Works fine.\r\n\r\n```\r\npublic interface IValidEntity { }\r\npublic class EntityOne : IValidEntity { }\r\npublic class EntityTwo { }\r\npublic class EntityThree : IValidEntity { }\r\n\r\n    [TestFixture(typeof(EntityOne))]\r\n    [TestFixture(typeof(EntityTwo))]\r\n    [TestFixture(typeof(EntityThree))]\r\n    public class TestUsingSimpleCases<TEntity>\r\n    {\r\n        [Test]\r\n        public void TestMethod()\r\n        {\r\n            Assert.That(typeof(IValidEntity).IsAssignableFrom(typeof(TEntity)));\r\n        }\r\n    }\r\n```\r\n\r\nThe same, but we fetch the types array from the \"Source\" field with array of types.\r\n\r\n```\r\n[TestFixtureSource(typeof(RegistryType), nameof(RegistryType.Source))]\r\npublic class TestUsingTypeSource<TEntity>\r\n{\r\n\t[Test]\r\n\tpublic void TestMethod()\r\n\t{\r\n\t\tAssert.That(typeof(IValidEntity).IsAssignableFrom(typeof(TEntity)));\r\n\t}\r\n}\r\n\r\npublic class RegistryType\r\n{\r\n\tpublic static readonly Type[] Source = new[]\r\n\t{\r\n\t\ttypeof(EntityOne),\r\n\t\ttypeof(EntityTwo),\r\n\t\ttypeof(EntityThree)\r\n\t};\r\n}\r\n```\r\n\r\nFirstly we get error in NUnitTestFixtureBuilder.cs:100, because TypeArgs is null. For TestFixtureAttribute it's set as an empty array by default, so things are ok. But when generated from source ITextFixtureData.TypeArgs contains null.\r\n\r\nWe can also create an empty array, but then we should do it in any ITestFixtureData implementation including some created by users. So I think it is better to apply a small fix to builder itself so it will have the same behaviour not only when TypeArgs.Length == 0, but also when it is null.\r\n\r\nBut when I tried to solve this problem without NUnit fixing, have found another issue. For example I've replaced types array with ITestFixtureData array that was looking as valid solution and I was able to initialize TypeArgs with an empty array as TestFixtureAttribute does (it is also ITestFixtureData).\r\n\r\n```\r\n[TestFixtureSource(typeof(RegistryData), nameof(RegistryData.Source))]\r\npublic class TestUsingDataSource<TEntity>\r\n{\r\n\t[Test]\r\n\tpublic void TestMethod()\r\n\t{\r\n\t\tAssert.That(typeof(IValidEntity).IsAssignableFrom(typeof(TEntity)));\r\n\t}\r\n}\r\n\r\npublic class CustomData : ITestFixtureData\r\n{\r\n\tpublic Type[] TypeArgs { get; }\r\n\tpublic string TestName { get; }\r\n\tpublic RunState RunState { get; }\r\n\tpublic object[] Arguments { get; }\r\n\tpublic IPropertyBag Properties { get; }\r\n\r\n\tpublic CustomData(Type type)\r\n\t{\r\n\t\tArguments = new object[] { type };\r\n\t\tTypeArgs = new Type[0];\r\n\t\tProperties = new PropertyBag();\r\n\t\tRunState = RunState.Runnable;\r\n\t}\r\n}\r\n\r\npublic class RegistryData\r\n{\r\n\tpublic static readonly ITestFixtureData[] Source = new ITestFixtureData[]\r\n\t{\r\n\t\tnew CustomData(typeof(EntityOne)),\r\n\t\tnew CustomData(typeof(EntityTwo)),\r\n\t\tnew CustomData(typeof(EntityThree)),\r\n\t};\r\n}\r\n```\r\n\r\nBut now exception happens in in TestFixtureSourceAttribute.cs:100. It takes iterator through ITestFixtureData items and send each item from it to NUnitTestFixtureBuilder.BuildFrom that also accepts ITestFixtureData. But parameter in foreach defined as TestFixtureParameters, so we get two implicit castings and the first one fails for any other implementation of ITestFixtureData rather than TestFixtureParameters. I've applied a small fix here as well.\r\n\r\nI believe this PR also closes #1346.\r\n\r\nPS. This PR failed to build, but for the reason inherited from master branch. I can't build it as well with the same error."},"comment":{"url":"https://api.github.com/repos/nunit/nunit/issues/comments/337201164","html_url":"https://github.com/nunit/nunit/pull/2439#issuecomment-337201164","issue_url":"https://api.github.com/repos/nunit/nunit/issues/2439","id":337201164,"user":{"login":"StanEgo","id":1964984,"avatar_url":"https://avatars3.githubusercontent.com/u/1964984?v=4","gravatar_id":"","url":"https://api.github.com/users/StanEgo","html_url":"https://github.com/StanEgo","followers_url":"https://api.github.com/users/StanEgo/followers","following_url":"https://api.github.com/users/StanEgo/following{/other_user}","gists_url":"https://api.github.com/users/StanEgo/gists{/gist_id}","starred_url":"https://api.github.com/users/StanEgo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/StanEgo/subscriptions","organizations_url":"https://api.github.com/users/StanEgo/orgs","repos_url":"https://api.github.com/users/StanEgo/repos","events_url":"https://api.github.com/users/StanEgo/events{/privacy}","received_events_url":"https://api.github.com/users/StanEgo/received_events","type":"User","site_admin":false},"created_at":"2017-10-17T11:25:28Z","updated_at":"2017-10-17T11:25:28Z","author_association":"CONTRIBUTOR","body":"@ChrisMaddock sure, I'll do it. Just working on the new formula of this issue, because it might be more interesting that was initially. E.g. the reason we can't use ITestFixtureData was trying to access OriginalArguments, but this arguments may be the part of adjustment interface in #2104, so there is a change the design would be more SOLID. But this requires some additional investigation."}}
{"actor":{"display_login":"StanEgo"}}