예제 #1
0
        public void using_alias_are_a_bit_normalized_by_removing_white_spaces()
        {
            ICodeWorkspace  workspace = CreateWorkspace();
            INamespaceScope global    = workspace.Global;
            INamespaceScope nAXa      = global.FindOrCreateNamespace("A.X.a");
            INamespaceScope nAXb      = global.FindOrCreateNamespace("A.X.b");
            INamespaceScope nAX       = global.FindOrCreateNamespace("A.X");
            INamespaceScope nA        = global.FindOrCreateNamespace("A");

            global.EnsureUsingAlias("INT", "System.UInt8");
            nAXa.EnsureUsingAlias("INT", "System . UInt8");

            ExtractNamespaces(global).Should().OnlyContain(x => x == "INT");
            ExtractNamespaces(nAXa).Should().BeEmpty();

            var source = workspace.GetGlobalSource();

            Normalize(source).Should().Be(Normalize(
                                              @"namespace A
{
using INT = System.UInt8;
namespace X
    {
        namespace a { }
        namespace b { }
    }
}"
                                              ));

            global.EnsureUsingAlias("CNode", "SNode<string,bool>");
            nAXa.EnsureUsingAlias("CNode", "SNode<STRING, BOOL>");
            nAXa.Append("// The 2 CNode definition differ.").NewLine();

            ExtractNamespaces(global).Should().HaveCount(2).And.OnlyContain(x => x == "INT" || x == "CNode");
            ExtractNamespaces(nAXa).Should().OnlyContain(x => x == "CNode");

            global.FindOrCreateNamespace("ToShowTheTopLevelUsingsCopy");
            source = workspace.GetGlobalSource();
            Normalize(source).Should().Be(Normalize(
                                              @"namespace A {
                    using INT = System.UInt8;
                    using CNode = SNode<string,bool>;
                    namespace X {
                      namespace a {
                        using CNode = SNode<STRING, BOOL>;
                        // The 2 CNode definition differ.
                      }
                      namespace b {
                      }
                    }
                  }
namespace ToShowTheTopLevelUsingsCopy
{
                    using INT = System.UInt8;
                    using CNode = SNode<string,bool>;
}"
                                              ));
        }
예제 #2
0
        public void BuildGenericMethods()
        {
            ICodeWorkspace  workspace = CodeWorkspace.Create();
            INamespaceScope global    = workspace.Global;
            INamespaceScope b         = global.FindOrCreateNamespace("CK._g");

            Type t = typeof(ContainsGenericMethods <>);

            workspace.EnsureAssemblyReference(t);

            b.EnsureUsing(t.Namespace);
            var c = b.CreateType(header => header.Append("class Specialized<T> : ").AppendCSharpName(t, true, true, true).NewLine());

            c.CreateOverride(t.GetMethod("Simple1"))
            .Append("if (arg.Equals(default(T))) throw new System.ArgumentException();").NewLine()
            .Append("return default(TResult);");

            c.CreateOverride(t.GetMethod("Simple2"))
            .Append("=> arg2 is T1;");

            Assembly a = TestHelper.CreateAssembly(workspace.GetGlobalSource(), workspace.AssemblyReferences);

            Type tC = a.GetTypes().Single(n => n.Name == "Specialized`1").MakeGenericType(typeof(int));
            ContainsGenericMethods <int> gotIt = (ContainsGenericMethods <int>)Activator.CreateInstance(tC);

            gotIt.Simple1 <bool>(25).Should().BeFalse();
            gotIt.Simple2(new object(), "test").Should().BeTrue();
        }
예제 #3
0
        static void CombineWorkspace(HashSet <Assembly> assemblies, List <SyntaxTree> trees, ICodeWorkspace c, CSharpParseOptions?options)
        {
            foreach (var a in c.AssemblyReferences)
            {
                assemblies.Add(a);
            }
            var s = c.GetGlobalSource();

            if (!String.IsNullOrWhiteSpace(s))
            {
                trees.Add(SyntaxFactory.ParseSyntaxTree(s, options));
            }
        }
예제 #4
0
        public void BaseTest()
        {
            ICodeWorkspace  workspace = CodeWorkspace.Create();
            INamespaceScope global    = workspace.Global;
            INamespaceScope b         = global.FindOrCreateNamespace("CK._g");

            Type t = typeof(BaseToBeOverridden);

            workspace.EnsureAssemblyReference(t);

            b.EnsureUsing("System")
            .EnsureUsing("System.Collections.Generic")
            .EnsureUsing(t.Namespace);

            var c = b.CreateType(h => h.Append("class Specialized : ").AppendCSharpName(t, true, true, true));

            c.CreatePassThroughConstructors(t);

            c.CreateOverride(t.GetMethod("Simple1"))
            .Append("=> 3712;");

            c.CreateOverride(t.GetMethod("VoidMethod"));

            c.CreateOverride(t.GetMethod("Simple2", BindingFlags.Instance | BindingFlags.NonPublic))
            .Append("=> x + '-' + g.ToString();");

            c.CreateOverride(t.GetMethod("Simple3", BindingFlags.Instance | BindingFlags.NonPublic))
            .Append("g = Guid.NewGuid();").NewLine()
            .Append(@"x = ""Hello World!"" + Simple2( ""YES"", g );").NewLine()
            .Append("return this;");

            c.CreateOverride(t.GetMethod("VerbatimParameters", BindingFlags.Instance | BindingFlags.NonPublic)).Append(" => @this + @operator;");

            Assembly a = TestHelper.CreateAssembly(workspace.GetGlobalSource(), workspace.AssemblyReferences);

            Type tC = a.GetTypes().Single(n => n.Name == "Specialized");
            BaseToBeOverridden gotIt = (BaseToBeOverridden)Activator.CreateInstance(tC, new object[] { 3712 * 3712 });

            gotIt.ValFromCtor.Should().Be(3712 * 3712);
            gotIt.Simple1().Should().Be(3712);
            string s;
            Guid   g = Guid.Empty;

            gotIt.Simple3(out s, ref g, 9).Should().BeSameAs(gotIt);
            s.Should().Be("Hello World!YES-" + g.ToString());
            g.Should().NotBeEmpty();
        }
예제 #5
0
        public void SqlTest()
        {
            ICodeWorkspace  workspace = CodeWorkspace.Create();
            INamespaceScope global    = workspace.Global;
            INamespaceScope b         = global.FindOrCreateNamespace("CK._g");

            workspace.EnsureAssemblyReference(typeof(SqlCommand), typeof(SimpleBase));

            b.EnsureUsing("System")
            .EnsureUsing("System.Collections.Generic")
            .EnsureUsing("Microsoft.Data.SqlClient");

            var type = b.CreateType(w => w.Append("public class GGGG : ").AppendCSharpName(typeof(SimpleBase), true, true, true));

            type.CreateOverride(typeof(SimpleBase).GetMethod("Do"))
            .Append(
                @"if( i.HasValue )
                {
                    i *= i;
                }
                var c = new SqlCommand(""p""+i.ToString());
                var p = c.Parameters.AddWithValue(""@i"", (object)i ?? DBNull.Value);
                return c;");

            var source     = workspace.GetGlobalSource();
            var references = workspace.AssemblyReferences;

            Assembly   a     = TestHelper.CreateAssembly(source, references);
            Type       t     = a.GetTypes().Single(n => n.Name == "GGGG");
            SimpleBase gotIt = (SimpleBase)Activator.CreateInstance(t);
            int?       k     = 67;
            SqlCommand cmd   = gotIt.Do(ref k);

            k.Should().Be(67 * 67);
            cmd.CommandText.Should().Be("p" + k);
            cmd.Parameters.Cast <SqlParameter>().Single().Value.Should().Be(k);
        }