~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Libraries/NRefactory/Test/Parser/GlobalScope/AttributeSectionTests.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.IO;
 
6
using NUnit.Framework;
 
7
using ICSharpCode.NRefactory.Parser;
 
8
using ICSharpCode.NRefactory.Parser.VB;
 
9
using ICSharpCode.NRefactory.Ast;
 
10
 
 
11
namespace ICSharpCode.NRefactory.Tests.Ast
 
12
{
 
13
        [TestFixture]
 
14
        public class AttributeSectionTests
 
15
        {
 
16
                [Test]
 
17
                public void AttributeOnStructure()
 
18
                {
 
19
                        string program = @"
 
20
<StructLayout( LayoutKind.Explicit )> _
 
21
Public Structure MyUnion
 
22
 
 
23
        <FieldOffset( 0 )> Public i As Integer
 
24
        < FieldOffset( 0 )> Public d As Double
 
25
        
 
26
End Structure 'MyUnion
 
27
";
 
28
                        TypeDeclaration decl = ParseUtilVBNet.ParseGlobal<TypeDeclaration>(program);
 
29
                        Assert.AreEqual("StructLayout", decl.Attributes[0].Attributes[0].Name);
 
30
                }
 
31
                
 
32
                [Test]
 
33
                public void AttributeOnModule()
 
34
                {
 
35
                        string program = @"
 
36
<HideModule> _
 
37
Public Module MyExtra
 
38
 
 
39
        Public i As Integer
 
40
        Public d As Double
 
41
        
 
42
End Module
 
43
";
 
44
                        TypeDeclaration decl = ParseUtilVBNet.ParseGlobal<TypeDeclaration>(program);
 
45
                        Assert.AreEqual("HideModule", decl.Attributes[0].Attributes[0].Name);
 
46
                }
 
47
                
 
48
                [Test]
 
49
                public void GlobalAttributeVB()
 
50
                {
 
51
                        string program = @"<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
 
52
Public Class Form1
 
53
        
 
54
End Class";
 
55
                        TypeDeclaration decl = ParseUtilVBNet.ParseGlobal<TypeDeclaration>(program);
 
56
                        Assert.AreEqual("Microsoft.VisualBasic.CompilerServices.DesignerGenerated", decl.Attributes[0].Attributes[0].Name);
 
57
                }
 
58
                
 
59
                [Test]
 
60
                public void GlobalAttributeCSharp()
 
61
                {
 
62
                        string program = @"[global::Microsoft.VisualBasic.CompilerServices.DesignerGenerated()]
 
63
[someprefix::DesignerGenerated()]
 
64
public class Form1 {
 
65
}";
 
66
                        TypeDeclaration decl = ParseUtilCSharp.ParseGlobal<TypeDeclaration>(program);
 
67
                        Assert.AreEqual("Microsoft.VisualBasic.CompilerServices.DesignerGenerated", decl.Attributes[0].Attributes[0].Name);
 
68
                        Assert.AreEqual("someprefix.DesignerGenerated", decl.Attributes[1].Attributes[0].Name);
 
69
                }
 
70
                
 
71
                [Test]
 
72
                public void AssemblyAttributeCSharp()
 
73
                {
 
74
                        string program = @"[assembly: System.Attribute()]";
 
75
                        AttributeSection decl = ParseUtilCSharp.ParseGlobal<AttributeSection>(program);
 
76
                        Assert.AreEqual(new Location(1, 1), decl.StartLocation);
 
77
                        Assert.AreEqual("assembly", decl.AttributeTarget);
 
78
                }
 
79
                
 
80
                [Test]
 
81
                public void AssemblyAttributeCSharpWithNamedArguments()
 
82
                {
 
83
                        string program = @"[assembly: Foo(1, namedArg: 2, prop = 3)]";
 
84
                        AttributeSection decl = ParseUtilCSharp.ParseGlobal<AttributeSection>(program);
 
85
                        Assert.AreEqual("assembly", decl.AttributeTarget);
 
86
                        var a = decl.Attributes[0];
 
87
                        Assert.AreEqual("Foo", a.Name);
 
88
                        Assert.AreEqual(2, a.PositionalArguments.Count);
 
89
                        Assert.AreEqual(1, a.NamedArguments.Count);
 
90
                        Assert.AreEqual(1, ((PrimitiveExpression)a.PositionalArguments[0]).Value);
 
91
                        NamedArgumentExpression nae = a.PositionalArguments[1] as NamedArgumentExpression;
 
92
                        Assert.AreEqual("namedArg", nae.Name);
 
93
                        Assert.AreEqual(2, ((PrimitiveExpression)nae.Expression).Value);
 
94
                        nae = a.NamedArguments[0];
 
95
                        Assert.AreEqual("prop", nae.Name);
 
96
                        Assert.AreEqual(3, ((PrimitiveExpression)nae.Expression).Value);
 
97
                }
 
98
                
 
99
                [Test]
 
100
                public void ModuleAttributeCSharp()
 
101
                {
 
102
                        string program = @"[module: System.Attribute()]";
 
103
                        AttributeSection decl = ParseUtilCSharp.ParseGlobal<AttributeSection>(program);
 
104
                        Assert.AreEqual(new Location(1, 1), decl.StartLocation);
 
105
                        Assert.AreEqual("module", decl.AttributeTarget);
 
106
                }
 
107
                
 
108
                [Test]
 
109
                public void TypeAttributeCSharp()
 
110
                {
 
111
                        string program = @"[type: System.Attribute()] class Test {}";
 
112
                        TypeDeclaration type = ParseUtilCSharp.ParseGlobal<TypeDeclaration>(program);
 
113
                        AttributeSection decl = type.Attributes[0];
 
114
                        Assert.AreEqual(new Location(1, 1), decl.StartLocation);
 
115
                        Assert.AreEqual("type", decl.AttributeTarget);
 
116
                }
 
117
                
 
118
                [Test]
 
119
                public void AssemblyAttributeVBNet()
 
120
                {
 
121
                        string program = @"<assembly: System.Attribute()>";
 
122
                        AttributeSection decl = ParseUtilVBNet.ParseGlobal<AttributeSection>(program);
 
123
                        Assert.AreEqual(new Location(1, 1), decl.StartLocation);
 
124
                        Assert.AreEqual("assembly", decl.AttributeTarget);
 
125
                }
 
126
                
 
127
                [Test]
 
128
                public void ModuleAttributeTargetEscapedVB()
 
129
                {
 
130
                        // check that this doesn't crash the parser:
 
131
                        ParseUtilVBNet.ParseGlobal<AttributeSection>("<[Module]: SuppressMessageAttribute>", true);
 
132
                }
 
133
        }
 
134
}