~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeSystemConvertVisitorTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2010-2013 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
 
 
19
using System;
 
20
using System.IO;
 
21
using System.Linq;
 
22
using ICSharpCode.NRefactory.Documentation;
 
23
using ICSharpCode.NRefactory.TypeSystem;
 
24
using ICSharpCode.NRefactory.TypeSystem.Implementation;
 
25
using ICSharpCode.NRefactory.TypeSystem.TestCase;
 
26
using ICSharpCode.NRefactory.Utils;
 
27
using NUnit.Framework;
 
28
 
 
29
namespace ICSharpCode.NRefactory.CSharp.Parser
 
30
{
 
31
        [TestFixture]
 
32
        public class TypeSystemConvertVisitorTests : TypeSystemTests
 
33
        {
 
34
                [TestFixtureSetUp]
 
35
                public void FixtureSetUp()
 
36
                {
 
37
                        compilation = ParseTestCase().CreateCompilation();
 
38
                }
 
39
 
 
40
                static IProjectContent CreateContent (IUnresolvedFile unresolvedFile)
 
41
                {
 
42
                        return new CSharpProjectContent()
 
43
                                .AddOrUpdateFiles(unresolvedFile)
 
44
                                .AddAssemblyReferences(new[] {
 
45
                                        CecilLoaderTests.Mscorlib
 
46
                                })
 
47
                                .SetAssemblyName(typeof(TypeSystemTests).Assembly.GetName().Name);
 
48
                }
 
49
                
 
50
                internal static IProjectContent ParseTestCase()
 
51
                {
 
52
                        const string fileName = "TypeSystemTests.TestCase.cs";
 
53
                        
 
54
                        CSharpParser parser = new CSharpParser();
 
55
                        SyntaxTree syntaxTree;
 
56
                        using (Stream s = typeof(TypeSystemTests).Assembly.GetManifestResourceStream(typeof(TypeSystemTests), fileName)) {
 
57
                                syntaxTree = parser.Parse(s, fileName);
 
58
                        }
 
59
                        
 
60
                        return CreateContent(syntaxTree.ToTypeSystem());
 
61
                }
 
62
                
 
63
                [Test]
 
64
                public void ConvertStandaloneTypeReference()
 
65
                {
 
66
                        var typeRef = new MemberType(new SimpleType("System"), "Array").ToTypeReference();
 
67
                        Assert.AreEqual(compilation.FindType(KnownTypeCode.Array), typeRef.Resolve(compilation.TypeResolveContext));
 
68
                }
 
69
                
 
70
                [Test]
 
71
                public void PartialMethodWithImplementation()
 
72
                {
 
73
                        var t = compilation.FindType(typeof(PartialClass));
 
74
                        var methods = t.GetMethods(m => m.Name == "PartialMethodWithImplementation").ToList();
 
75
                        Assert.AreEqual(2, methods.Count);
 
76
                        var method1 = methods.Single(m => m.Parameters[0].Type.FullName == "System.Int32");
 
77
                        var method2 = methods.Single(m => m.Parameters[0].Type.FullName == "System.String");
 
78
                        Assert.AreEqual(2, method1.Parts.Count);
 
79
                        Assert.AreEqual(2, method2.Parts.Count);
 
80
                }
 
81
                
 
82
                [Test]
 
83
                public void PartialMethodWithoutImplementation()
 
84
                {
 
85
                        var t = compilation.FindType(typeof(PartialClass));
 
86
                        var method = t.GetMethods(m => m.Name == "PartialMethodWithoutImplementation").Single();
 
87
                        Assert.AreEqual(1, method.Parts.Count);
 
88
                }
 
89
 
 
90
                [Test]
 
91
                public void CyclicConstants()
 
92
                {
 
93
                        var syntaxTree = SyntaxTree.Parse ("class Test { const int foo = foo; }");
 
94
                        syntaxTree.FileName = "a.cs";
 
95
                        var content = CreateContent (syntaxTree.ToTypeSystem());
 
96
                        var testType = content.CreateCompilation ().MainAssembly.GetTypeDefinition ("", "Test");
 
97
                        Assert.NotNull (testType);
 
98
                        var field = testType.Fields.First ();
 
99
                        Assert.IsTrue (field.IsConst);
 
100
                        Assert.IsNull (field.ConstantValue);
 
101
                }
 
102
        }
 
103
        
 
104
        [TestFixture]
 
105
        public class SerializedTypeSystemConvertVisitorTests : TypeSystemTests
 
106
        {
 
107
                [TestFixtureSetUp]
 
108
                public void FixtureSetUp()
 
109
                {
 
110
                        FastSerializer serializer = new FastSerializer();
 
111
                        using (MemoryStream ms = new MemoryStream()) {
 
112
                                serializer.Serialize(ms, TypeSystemConvertVisitorTests.ParseTestCase());
 
113
                                ms.Position = 0;
 
114
                                var pc = (IProjectContent)serializer.Deserialize(ms);
 
115
                                compilation = pc.CreateCompilation();
 
116
                        }
 
117
                }
 
118
        }
 
119
}