/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/EventDeclarationTests.cs

http://github.com/icsharpcode/ILSpy · C# · 104 lines · 82 code · 5 blank · 17 comment · 0 complexity · dadfb09ac0d33b6c1aafe60d3368c5de MD5 · raw file

  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using NUnit.Framework;
  20. namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers
  21. {
  22. [TestFixture]
  23. public class EventDeclarationTests
  24. {
  25. [Test]
  26. public void SimpleEventDeclarationTest()
  27. {
  28. ParseUtilCSharp.AssertTypeMember(
  29. "event EventHandler MyEvent;",
  30. new EventDeclaration {
  31. ReturnType = new SimpleType("EventHandler"),
  32. Variables = {
  33. new VariableInitializer {
  34. Name = "MyEvent"
  35. }
  36. }});
  37. }
  38. [Test]
  39. public void MultipleEventDeclarationTest()
  40. {
  41. ParseUtilCSharp.AssertTypeMember(
  42. "public event EventHandler A = null, B = delegate {};",
  43. new EventDeclaration {
  44. Modifiers = Modifiers.Public,
  45. ReturnType = new SimpleType("EventHandler"),
  46. Variables = {
  47. new VariableInitializer {
  48. Name = "A",
  49. Initializer = new NullReferenceExpression()
  50. },
  51. new VariableInitializer {
  52. Name = "B",
  53. Initializer = new AnonymousMethodExpression() { Body = new BlockStatement ()}
  54. }
  55. }});
  56. }
  57. [Test]
  58. public void AddRemoveEventDeclarationTest()
  59. {
  60. ParseUtilCSharp.AssertTypeMember(
  61. "public event System.EventHandler MyEvent { add { } remove { } }",
  62. new CustomEventDeclaration {
  63. Modifiers = Modifiers.Public,
  64. ReturnType = new MemberType {
  65. Target = new SimpleType("System"),
  66. MemberName = "EventHandler"
  67. },
  68. Name = "MyEvent",
  69. AddAccessor = new Accessor { Body = new BlockStatement() },
  70. RemoveAccessor = new Accessor { Body = new BlockStatement() }
  71. });
  72. }
  73. [Test]
  74. public void EventImplementingGenericInterfaceDeclarationTest()
  75. {
  76. ParseUtilCSharp.AssertTypeMember(
  77. "event EventHandler MyInterface<string>.MyEvent { add { } [Attr] remove {} }",
  78. new CustomEventDeclaration {
  79. ReturnType = new SimpleType("EventHandler"),
  80. PrivateImplementationType = new SimpleType{
  81. Identifier = "MyInterface",
  82. TypeArguments = { new PrimitiveType("string") }
  83. },
  84. Name = "MyEvent",
  85. AddAccessor = new Accessor { Body = new BlockStatement() },
  86. RemoveAccessor = new Accessor {
  87. Attributes = {
  88. new AttributeSection {
  89. Attributes = {
  90. new Attribute { Type = new SimpleType("Attr") }
  91. }
  92. }
  93. },
  94. Body = new BlockStatement()
  95. }
  96. });
  97. }
  98. }
  99. }