public void JsonConstructorMustHaveAllParametersMatchingMemberNamesCaseInsensitive() { TestBothKinds(@"[ObjectLiteral] public C1(int someParameter) {}", () => { Assert.That(AllErrors.Count, Is.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("C1") && m.Contains("parameter") && m.Contains("matching") && m.Contains("someParameter"))); }, expectErrors: true); }
protected void Prepare(string source, bool minimizeNames = true, bool expectErrors = false) { IProjectContent project = new CSharpProjectContent(); var parser = new CSharpParser(); using (var rdr = new StringReader(source)) { var pf = new CSharpUnresolvedFile("File.cs"); var syntaxTree = parser.Parse(rdr, pf.FileName); syntaxTree.AcceptVisitor(new TypeSystemConvertVisitor(pf)); project = project.AddOrUpdateFiles(pf); } project = project.AddAssemblyReferences(new[] { Common.Mscorlib }); var compilation = project.CreateCompilation(); errorReporter = new MockErrorReporter(!expectErrors); Metadata = new MetadataImporter.ScriptSharpMetadataImporter(minimizeNames); Metadata.Prepare(compilation.GetAllTypeDefinitions(), compilation.MainAssembly, errorReporter); AllErrors = errorReporter.AllMessages.ToList().AsReadOnly(); AllErrorTexts = errorReporter.AllMessagesText.ToList().AsReadOnly(); if (expectErrors) { AllErrorTexts.Should().NotBeEmpty("Compile should have generated errors"); } else { AllErrorTexts.Should().BeEmpty("Compile should not generate errors"); } AllTypes = compilation.MainAssembly.TopLevelTypeDefinitions.SelectMany(SelfAndNested).ToDictionary(t => t.ReflectionName); }
public void ArgumentTypesForJsonConstructorMustMatchMemberTypes() { TestBothKinds(@"[ObjectLiteral] public C1(int someParameter) {} public string SomeParameter;", () => { Assert.That(AllErrors.Count, Is.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("someParameter") && m.Contains("System.String") && m.Contains("System.Int32"))); }, expectErrors: true); }
public void TypeWithoutSerializableAttributeCanInheritRecordButNotOtherSerializableType() { Prepare(@"using System; using System.Runtime.CompilerServices; class C1 : Record {}", expectErrors: false); Assert.That(Metadata.IsSerializable(AllTypes["C1"]), Is.True); Prepare(@"using System; using System.Runtime.CompilerServices; [Serializable] class B {} class C1 : B {}", expectErrors: true); Assert.That(AllErrorTexts.Any(m => m.Contains("C1") && m.Contains("B") && m.Contains("cannot inherit from the serializable type"))); }
public void InlineConstantAttributeCannotBeAppliedToNonConstField() { Prepare(@"using System.Runtime.CompilerServices; public class C1 { [InlineConstant] public static int Value = 42; }", expectErrors: true); Assert.That(AllErrorTexts.Count, Is.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("C1.Value"))); Assert.That(AllErrorTexts.Any(m => m.Contains("InlineConstantAttribute"))); }
public void BindThisToFirstParameterCannotBeUsedOnDelegateWithoutParameters() { Prepare( @"using System.Runtime.CompilerServices; [BindThisToFirstParameter] delegate void D1(); }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("D1") && m.Contains("BindThisToFirstParameterAttribute") && m.Contains("does not have any parameters"))); }
public void ExpandParamsAttributeCanOnlyBeAppliedToDelegateWithParamArray() { Prepare( @"using System.Runtime.CompilerServices; [ExpandParams] delegate void D1(int[] args); }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("D1") && m.Contains("ExpandParamsAttribute") && m.Contains("params"))); }
public void SerializableTypesCannotImplementNonSerializableInterfaces() { Prepare(@"using System; using System.Runtime.CompilerServices; interface I1 {} [Serializable] sealed class C1 : I1 {}", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("C1") && m.Contains("serializable type") && m.Contains("cannot implement") && m.Contains("I1"))); Prepare(@"interface I1 {} sealed class C1 : System.Record, I1 {}", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("C1") && m.Contains("serializable type") && m.Contains("cannot implement") && m.Contains("I1"))); }
public void SerializableTypesCannotDeclareVirtualMembers() { TestBothKinds(@"public virtual int M1() {}", () => { Assert.That(AllErrorTexts.Any(m => m.Contains("C1") && m.Contains("M1") && m.Contains("cannot declare") && m.Contains("virtual"))); }, expectErrors: true); TestBothKinds(@"public virtual int P1 { get; set; }", () => { Assert.That(AllErrorTexts.Any(m => m.Contains("C1") && m.Contains("P1") && m.Contains("cannot declare") && m.Contains("virtual"))); }, expectErrors: true); }
public void TestOrAsyncTestAttributeCannotBeSpecifiedOnTypeThatIsNotATestFixture() { Prepare("using System.Testing; public class C1 { [Test] public void M() {} }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("TestAttribute") && m.Contains("TestFixtureAttribute"))); Prepare("using System.Testing; public class C1 { [AsyncTest] public void M() {} }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("TestAttribute") && m.Contains("TestFixtureAttribute"))); }
public void ObjectLiteralAttributeCannotBeUsedOnConstructorForNonSerializableType() { Prepare( @"public class C1 { [System.Runtime.CompilerServices.ObjectLiteral] public C1() { } }", expectErrors: true); Assert.That(AllErrorTexts.Count, Is.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("C1") && m.Contains("serializable type") && m.Contains("ObjectLiteralAttribute"))); }
public void ExpandParamsAttributeCanOnlyBeAppliedToConstructorWithParamArray() { Prepare( @"using System.Runtime.CompilerServices; class C1 { [ExpandParams] public C1(int a, int b, int[] c) {} }", expectErrors: true); Assert.That(AllErrorTexts.Count, Is.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("C1") && m.Contains("constructor") && m.Contains("params") && m.Contains("ExpandParamsAttribute"))); }
public void CannotSpecifyScriptSkipOnOverridablePropertyAccessors() { Prepare( @"using System.Runtime.CompilerServices; class C { public virtual int Prop { [ScriptSkip] get; [ScriptSkip] set; } }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(2)); Assert.That(AllErrorTexts.Any(m => m.Contains("C.get_Prop") && m.Contains("ScriptSkipAttribute") && m.Contains("overridable"))); Assert.That(AllErrorTexts.Any(m => m.Contains("C.set_Prop") && m.Contains("ScriptSkipAttribute") && m.Contains("overridable"))); }
public void JsonConstructorCannotHaveRefOrOutParameters() { TestBothKinds(@"[ObjectLiteral] public C1(ref int someParameter) {} public string SomeParameter;", () => { Assert.That(AllErrors.Count, Is.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("someParameter") && m.Contains("ref"))); }, expectErrors: true); TestBothKinds(@"[ObjectLiteral] public C1(out int someParameter) {} public string SomeParameter;", () => { Assert.That(AllErrors.Count, Is.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("someParameter") && m.Contains("out"))); }, expectErrors: true); }
public void ErrorInInlineCodeForSerializableTypePropertyAccessorIsReported() { TestBothKinds(@"int Prop1 { [InlineCode(""{a}"")] get; [InlineCode(""X"")] set; }", () => { Assert.That(AllErrors.Count, Is.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("C1.get_Prop1") && m.Contains("{a}"))); }, expectErrors: true); TestBothKinds(@"int Prop1 { [InlineCode(""X"")] get; [InlineCode(""{a}"")] set; }", () => { Assert.That(AllErrors.Count, Is.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("C1.set_Prop1") && m.Contains("{a}"))); }, expectErrors: true); }
public void IfInlineCodeIsSpecifiedForOneAccessorOfSerializableTypeInstancePropertyItMustAlsoBeSpecifiedOnTheOther() { TestBothKinds(@"int Prop1 { [InlineCode(""X"")] get; set; }", () => { Assert.That(AllErrors.Count, Is.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("C1.Prop1") && m.Contains("InlineCodeAttribute"))); }, expectErrors: true); TestBothKinds(@"int Prop1 { get; [InlineCode(""X"")] set; }", () => { Assert.That(AllErrors.Count, Is.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("C1.Prop1") && m.Contains("InlineCodeAttribute"))); }, expectErrors: true); }
public void ScriptNameCannotBeBlank() { Prepare( @"using System.Runtime.CompilerServices; class C { [ScriptName("""")] public event System.EventHandler Evt; }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("C.Evt") && m.Contains("ScriptNameAttribute") && m.Contains("event") && m.Contains("cannot be empty"))); }
public void CannotSpecifyInlineCodeOnOverridableEventAccessors() { Prepare( @"using System.Runtime.CompilerServices; class C { public virtual event System.EventHandler Evt { [InlineCode(""X"")] add {} [InlineCode(""X"")] remove {} } }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(2)); Assert.That(AllErrorTexts.Any(m => m.Contains("C.add_Evt") && m.Contains("InlineCodeAttribute") && m.Contains("overridable"))); Assert.That(AllErrorTexts.Any(m => m.Contains("C.remove_Evt") && m.Contains("InlineCodeAttribute") && m.Contains("overridable"))); }
public void ScriptNameCannotBeBlank() { Prepare( @"using System.Runtime.CompilerServices; class C { [ScriptName("""")] public int this[int x] { get { return 0; } set {} } }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("C") && m.Contains("ScriptNameAttribute") && m.Contains("indexer") && m.Contains("cannot be empty"))); }
public void CannotSpecifyIntrinsicPropertyAttributeOnOverridableProperties() { Prepare( @"using System.Runtime.CompilerServices; class C1 { [IntrinsicProperty] public virtual int this[int x] { get { return 0; } set {} } }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("indexer") && m.Contains("IntrinsicPropertyAttribute") && m.Contains("overridable"))); }
public void CannotSpecifyIntrinsicPropertyAttributeOnInterfaceProperties() { Prepare( @"using System.Runtime.CompilerServices; interface I1 { [IntrinsicProperty] int this[int x] { get; set; } }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("indexer") && m.Contains("IntrinsicPropertyAttribute") && m.Contains("interface member"))); }
public void TypeWithSerializableAttributeCanInheritFromObjectOrRecordOrAnotherSerializableTypeButNotFromNonSerializableType() { Prepare(@"using System; using System.Runtime.CompilerServices; class B {} [Serializable] class C1 : Object {}", expectErrors: false); // No error is good enough Prepare(@"using System; using System.Runtime.CompilerServices; class B {} [Serializable] class C1 : Record {}", expectErrors: false); // No error is good enough Prepare(@"using System; using System.Runtime.CompilerServices; [Serializable] class B {} [Serializable] class C1 : B {}", expectErrors: false); // No error is good enough Prepare(@"using System; using System.Runtime.CompilerServices; class B {} [Serializable] class C1 : B {}", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("C1") && m.Contains("must inherit from another serializable type, System.Object or System.Record"))); }
public void SerializableTypesCannotDeclareInstanceEvents() { Prepare(@"using System; using System.Runtime.CompilerServices; [Serializable] sealed class C1 { event System.EventHandler Evt; }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("C1") && m.Contains("serializable type") && m.Contains("cannot declare instance event"))); Prepare(@"using System.Runtime.CompilerServices; sealed class C1 : System.Record { event System.EventHandler Evt; }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("C1") && m.Contains("serializable type") && m.Contains("cannot declare instance event"))); // But static events are OK Prepare(@"using System.Runtime.CompilerServices; [Record] sealed class C1 { static event System.EventHandler Evt; }", expectErrors: false); Prepare(@"using System.Runtime.CompilerServices; sealed class C1 : System.Record { static event System.EventHandler Evt; }", expectErrors: false); }
public void TestFixtureClassCannotDeclareMethodWithScriptNameRunTests() { Prepare( @"using System.Testing; [TestFixture] public class C1 { public void RunTests() { } } ", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("TestFixtureAttribute") && m.Contains("runTests"))); }
public void CannotSpecifyIntrinsicPropertyAttributeOnPropertiesThatOverrideBaseMembers() { Prepare( @"using System.Runtime.CompilerServices; class B { public virtual int Prop { get; set; } } class D : B { [IntrinsicProperty] public sealed override int Prop { get; set; } }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("D.Prop") && m.Contains("IntrinsicPropertyAttribute") && m.Contains("overrides"))); }
public void CannotSpecifyInlineCodeOnEventAccessorsImplementingInterfaceMembers() { Prepare( @"using System.Runtime.CompilerServices; interface I { event System.EventHandler Evt; } class C : I { public event System.EventHandler Evt { [InlineCode(""|some code|"")] add {} [InlineCode(""|some code|"")] remove {} } }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(2)); Assert.That(AllErrorTexts.Any(m => m.Contains("C.add_Evt") && m.Contains("InlineCodeAttribute") && m.Contains("interface member"))); Assert.That(AllErrorTexts.Any(m => m.Contains("C.remove_Evt") && m.Contains("InlineCodeAttribute") && m.Contains("interface member"))); }
public void CannotSpecifyScriptSkipOnPropertyAccessorsImplementingInterfaceMembers() { Prepare( @"using System.Runtime.CompilerServices; interface I { int Prop { get; set; } } class C : I { public int Prop { [ScriptSkip] get { return 0; } [ScriptSkip] set {} } }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(2)); Assert.That(AllErrorTexts.Any(m => m.Contains("C.get_Prop") && m.Contains("ScriptSkipAttribute") && m.Contains("interface member"))); Assert.That(AllErrorTexts.Any(m => m.Contains("C.set_Prop") && m.Contains("ScriptSkipAttribute") && m.Contains("interface member"))); }
public void CannotSpecifyScriptSkipOnPropertyAccessorsThatOverrideBaseMembers() { Prepare( @"using System.Runtime.CompilerServices; class B { public virtual int Prop { get; set; } } class D : B { public sealed override int Prop { [ScriptSkip] get { return 0; } [ScriptSkip] set {} } }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(2)); Assert.That(AllErrorTexts.Any(m => m.Contains("D.get_Prop") && m.Contains("ScriptSkipAttribute") && m.Contains("overrides"))); Assert.That(AllErrorTexts.Any(m => m.Contains("D.set_Prop") && m.Contains("ScriptSkipAttribute") && m.Contains("overrides"))); }
public void CannotSpecifyInlineCodeOnIndexerAccessorsThatOverrideBaseMembers() { Prepare( @"using System.Runtime.CompilerServices; class B { public virtual int this[int x] { get { return 0; } set {} } } class D : B { public sealed override int this[int x] { [InlineCode(""X"")] get { return 0; } [InlineCode(""X"")] set {} } }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(2)); Assert.That(AllErrorTexts.Any(m => m.Contains("D.get_Item") && m.Contains("InlineCodeAttribute") && m.Contains("overrides"))); Assert.That(AllErrorTexts.Any(m => m.Contains("D.set_Item") && m.Contains("InlineCodeAttribute") && m.Contains("overrides"))); }
public void CannotSpecifyIntrinsicPropertyAttributeOnPropertiesImplementingInterfaceMembers() { Prepare( @"using System.Runtime.CompilerServices; interface I { int this[int x] { get; set; } } class C1 : I { [IntrinsicProperty] public int this[int x] { get { return 0; } set {} } }", expectErrors: true); Assert.That(AllErrorTexts, Has.Count.EqualTo(1)); Assert.That(AllErrorTexts.Any(m => m.Contains("indexer") && m.Contains("IntrinsicPropertyAttribute") && m.Contains("interface member"))); }