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(); }
public void demonstrating_simple_transformations() { var workspace = CodeWorkspace.Create(); workspace.Global .FindOrCreateNamespace("Original") .EnsureUsing("System") .CreateType("public static class DBNullWillBeReplaced") .Append("public static object V = DBNull.Value;").NewLine(); var gen = new CodeGenerator(CodeWorkspace.Factory); gen.Modules.Add(new DoChangeDBNull()); gen.Modules.Add(new ThisModuleUsesTheRealDBNullValue()); var r = gen.Generate(workspace, TestHelper.RandomDllPath, false, Assembly.LoadFrom); r.LogResult(TestHelper.Monitor); r.Success.Should().BeTrue(); gen.Modules.Should().BeEmpty(); var replaced = r.Assembly.ExportedTypes.Single(t => t.FullName == "Original.DBNullWillBeReplaced"); replaced.GetField("V", BindingFlags.Static | BindingFlags.Public).GetValue(null) .Should().Be("I'm the DBNull."); var real = r.Assembly.ExportedTypes.Single(t => t.FullName == "RealDBNull"); real.GetField("V", BindingFlags.Static | BindingFlags.Public).GetValue(null) .Should().Be(DBNull.Value); }
ITypeScope CreateTypeScope() { INamespaceScope global = CodeWorkspace.Create().Global; INamespaceScope ns = global.FindOrCreateNamespace("A.Simple.Namespace"); return(ns.CreateType(s => s.Append("public class ClassName"))); }
public void TypeDefinition_can_be_modified() { INamespaceScope global = CodeWorkspace.Create().Global; var t = global.CreateType("public class Derived : Base"); t.Definition.BaseTypes.Should().BeEquivalentTo(new[] { new ExtendedTypeName("Base") }); t.Definition.BaseTypes.Add(new ExtendedTypeName("Name.Space.IOther")); global.ToString().Should().Contain("Name.Space.IOther"); }
public void TypeDefinition_can_start_the_body() { INamespaceScope global = CodeWorkspace.Create().Global; var t = global.CreateType("public class Derived : Base { truc"); t.Definition.BaseTypes.Add(new ExtendedTypeName("Name.Space.IOther")); var s = global.ToString(); s = s !.Replace(Environment.NewLine, "").Replace(" ", ""); s.Should().EndWith("Name.Space.IOther{truc}"); }
public DynamicAssembly() { var name = Guid.NewGuid().ToString(); var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(name), AssemblyBuilderAccess.Run); StubModuleBuilder = assemblyBuilder.DefineDynamicModule(name); _memory = new Dictionary <object, object?>(); Code = CodeWorkspace.Create(); Debug.Assert(typeof(StObjGenAttribute).FullName == "CK.Core.StObjGenAttribute"); Code.TypeCreated += t => t.Definition.Attributes.Ensure(CodeAttributeTarget.Type).Attributes.Add(new AttributeDefinition("CK.Core.StObjGen")); }
public void Function_can_use_lambda_definition() { var t = CodeWorkspace.Create().Global.CreateType("class C"); var block = t.CreateFunction("int B()"); block.Append(" ").Append(" return 3;"); block.ToString().Replace("\r", "").Replace("\n", "").Should().Be("int B(){ return 3;}"); var lambda = t.CreateFunction("int L()"); lambda.Append(" ").Append(" => 3;"); lambda.ToString().Replace("\r", "").Replace("\n", "").Should().Be("int L() => 3;"); }
public void added_CodeGenerated_attributes_thanks_to_the_events() { INamespaceScope g = CodeWorkspace.Create().Global; g.Workspace.TypeCreated += Workspace_TypeCreated; var t1 = g.GeneratedByComment().CreateType("public class C1"); var t2 = g.FindOrCreateNamespace("Yop.Yup.Yip").CreateType(t => t.Append("private ref readonly struct XRQ { public readonly int X = 3; }")); var text = g.ToString(); // Here we add the a "Name" property (the real StObjGenAttribute has no parameters nor properties). text.Should().Contain(@"[type: StObjGen(Name = @""XRQ"")]readonly ref struct XRQ"); text.Should().Contain(@"[type: StObjGen(Name = @""C1"")]public class C1"); }
public void non_generic_Append_works_the_same() { var g1 = CodeWorkspace.Create().Global; var t1 = g1.Append(ESByte.Min).Append(';').Append(ESByte.Max).Append(';') .Append(EShort.Min).Append(';').Append(EShort.Max).Append(';') .Append(EULong.Min).Append(';').Append(EULong.Max).Append(';').ToString(); var g2 = CodeWorkspace.Create().Global; var t2 = g2.Append((object)ESByte.Min).Append(';').Append((object)ESByte.Max).Append(';') .Append((object)EShort.Min).Append(';').Append((object)EShort.Max).Append(';') .Append((object)EULong.Min).Append(';').Append((object)EULong.Max).Append(';').ToString(); t2.Should().Be(t1); }
public void BeforeNamespace_is_a_Part_that_can_have_Parts() { var g = CodeWorkspace.Create().Global; g.Append("AFTER"); g.BeforeNamespace.Append("BEFORE"); g.EnsureUsing("A.Name.Space"); g.ToString().Should().StartWith("BEFORE"); g.BeforeNamespace.CreatePart().Append("POST"); g.BeforeNamespace.CreatePart(top: true).Append("ANTE"); g.ToString().Should().StartWith("ANTE" + Environment.NewLine + "BEFORE" + Environment.NewLine + "POST"); }
public void writing_before_the_using_statements() { { var g = CodeWorkspace.Create().Global; g.EnsureUsing("A.Name.Space"); g.ToString().Should().StartWith("using", "All the code is INSIDE the namespace and AFTER the usings."); } { var g = CodeWorkspace.Create().Global; g.Append("AFTER"); g.BeforeNamespace.Append("BEFORE"); g.EnsureUsing("A.Name.Space"); g.ToString().Should().StartWith("BEFORE"); } }
public void merging_code_space_with_parts() { var c1 = CodeWorkspace.Create(); var c2 = CodeWorkspace.Create(); INamespaceScopePart c1Part1 = c1.Global.CreatePart(); INamespaceScope c1Part1Sub = c1Part1.CreatePart(); c1.Global.Append("n°0 (but after having created the 2 parts!)").NewLine(); INamespaceScope c1Part2 = c1.Global.CreatePart(); c1Part2.Append("2 - n°0").NewLine(); c1Part2.Append("2 - n°1").NewLine(); c1Part1.Append("1 - n°0").NewLine(); c1Part1.Append("1 - n°1").NewLine(); c1.Global.Append("n°1").NewLine(); c1Part1Sub.Append("Hop! (Later but in a sup part of the first part).").NewLine(); INamespaceScope c2Part1 = c1.Global.CreatePart(); INamespaceScope c2Part2 = c1.Global.CreatePart(); c2.Global.Append("!n°0").NewLine(); c2Part2.Append("!2 - n°0").NewLine(); c2Part2.Append("!2 - n°1").NewLine(); c2Part1.Append("!1 - n°0").NewLine(); c2Part1.Append("!1 - n°1").NewLine(); c2.Global.Append("!n°1").NewLine(); c1.MergeWith(c2); string code = c1.GetGlobalSource().Trim(); code.Should().Be(@" Hop! (Later but in a sup part of the first part). 1 - n°0 1 - n°1 n°0 (but after having created the 2 parts!) 2 - n°0 2 - n°1 n°1 !1 - n°0 !1 - n°1 !2 - n°0 !2 - n°1 !n°0 !n°1 ".Trim().ReplaceLineEndings()); }
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(); }
public void merging_code_space_with_one_type() { var c1 = CodeWorkspace.Create(); var c2 = CodeWorkspace.Create(); c1.EnsureAssemblyReference(typeof(object), typeof(MergeTests)); c2.EnsureAssemblyReference(typeof(Enumerable), typeof(TestFixtureAttribute), typeof(MergeTests)); c1.Global.EnsureUsing("A"); var ns1 = c1.Global.FindOrCreateNamespace("Sub"); var t1 = ns1.CreateType(h => h.Append("class C {//t1-Code").NewLine()); t1.Append("//t1-MoreCode"); c2.Global.EnsureUsing("B"); var ns2 = c2.Global.FindOrCreateNamespace("Sub"); var t2 = ns2.CreateType("class C {//t2-Code" + Environment.NewLine); t2.Append("//t2-MoreCode"); c1.MergeWith(c2); c1.AssemblyReferences.Should().BeEquivalentTo(new[] { typeof(object).Assembly, typeof(Enumerable).Assembly, typeof(TestFixtureAttribute).Assembly, typeof(MergeTests).Assembly }); string code = c1.GetGlobalSource(); code.Should().Be( "namespace Sub" + Environment.NewLine + "{" + Environment.NewLine + "using A;" + Environment.NewLine + "using B;" + Environment.NewLine + "class C" + Environment.NewLine + "{" + Environment.NewLine + "//t1-Code" + Environment.NewLine + "//t1-MoreCode" + "//t2-Code" + Environment.NewLine + "//t2-MoreCode" + Environment.NewLine + "}" + Environment.NewLine + "}"); }
public void playing_with_parts() { INamespaceScope g = CodeWorkspace.Create().Global; INamespaceScopePart gSub = g.CreatePart(); INamespaceScopePart gSub2 = gSub.CreatePart(); ITypeScope gSub2Type1 = gSub2.CreateType("class GSub2Type1"); ITypeScope gSub2Type2 = gSub2.CreateType("class GSub2Type2"); ITypeScopePart gSub2Type1Part1 = gSub2Type1.CreatePart(); ITypeScopePart gSub2Type1Part2 = gSub2Type1.CreatePart(); IFunctionScope gSub2Type1Part2F1 = gSub2Type1Part2.CreateFunction("void Action()"); IFunctionScopePart gSub2Type1Part2F1Part1 = gSub2Type1Part2F1.CreatePart(); g.Append("g:"); gSub.Append("gSub:"); gSub2.Append("gSub2:"); gSub2Type1.Append("gSub2Type1:"); gSub2Type2.Append("gSub2Type2:"); gSub2Type1Part1.Append("gSub2Type1Part1:"); gSub2Type1Part2.Append("gSub2Type1Part2:"); gSub2Type1Part2F1.Append("gSub2Type1Part2F1:"); gSub2Type1Part2F1Part1.Append("gSub2Type1Part2F1Part1:"); var s = g.ToString().Trim(); s.Should().Be(@" gSub2: gSub: g: class GSub2Type1 { gSub2Type1Part1: gSub2Type1Part2: gSub2Type1: void Action() { gSub2Type1Part2F1Part1: gSub2Type1Part2F1: } } class GSub2Type2 { gSub2Type2: }".Trim().ReplaceLineEndings()); }
public void parsing_only_tests() { var workspace = CodeWorkspace.Create(); var global = workspace.Global; global.EnsureUsing("System"); global.CreateType("public class Tester") .Append("public bool OK => true;").NewLine(); Assembly a = TestHelper.CreateAssembly(workspace.GetGlobalSource(), workspace.AssemblyReferences); a.Should().NotBeNull(); var g = new CodeGenerator(CodeWorkspace.Factory); var r = g.Generate(workspace, null, skipCompilation: true); r.Success.Should().BeTrue(); r.Sources.Should().HaveCount(1); }
public void workspace_events() { var ws = CodeWorkspace.Create(); var tracker = new Tracker(ws); INamespaceScope g = ws.Global; tracker.AllCount.Should().Be(0); var t1 = g.CreateType("public class C1"); var ns1 = g.FindOrCreateNamespace("Yop.Yup.Yip"); tracker.NamespaceCount.Should().Be(3); tracker.TypeCount.Should().Be(1); var t2 = ns1.CreateType(t => t.Append("private ref readonly struct XRQ { public readonly int X = 3; }")); tracker.TypeCount.Should().Be(2); var f1 = t2.CreateFunction("static public void Func() => X;"); tracker.NamespaceCount.Should().Be(3); tracker.TypeCount.Should().Be(2); tracker.FunctionCount.Should().Be(1); }
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); }
public void extreme_enum_values() { var workspace = CodeWorkspace.Create(); var global = workspace.Global; global.EnsureUsing("System") .EnsureUsing("CK.CodeGen.Roslyn.Tests"); global.Append(@" namespace CK.CodeGen.Roslyn.Tests { enum ESByte : sbyte { Min = SByte.MinValue, Max = SByte.MaxValue } enum EByte : byte { Min = Byte.MinValue, Max = Byte.MaxValue } enum EShort : short { Min = Int16.MinValue, Max = Int16.MaxValue } enum EUShort : ushort { Min = UInt16.MinValue, Max = UInt16.MaxValue } enum ELong : long { Min = Int64.MinValue, Max = Int64.MaxValue } enum EULong : ulong { Min = UInt64.MinValue, Max = UInt64.MaxValue } } "); var t = global.CreateType("public class Tester"); var f = t.CreateFunction("public bool CheckTypes()"); f.Append("ELong eLMax = ").Append(ELong.Max).Append(';').NewLine() .Append("EULong eULMax = ").Append(EULong.Max).Append(';').NewLine() .Append("EShort eSMax = ").Append(EShort.Max).Append(';').NewLine() .Append("EUShort eUSMax = ").Append(EUShort.Max).Append(';').NewLine() .Append("EByte eBMax = ").Append(EByte.Max).Append(';').NewLine() .Append("ESByte eSBMax = ").Append(ESByte.Max).Append(';').NewLine(); f.Append("ELong eLMin = ").Append(ELong.Min).Append(';').NewLine() .Append("EULong eULMin = ").Append(EULong.Min).Append(';').NewLine() .Append("EShort eSMin = ").Append(EShort.Min).Append(';').NewLine() .Append("EUShort eUSMin = ").Append(EUShort.Min).Append(';').NewLine() .Append("EByte eBMin = ").Append(EByte.Min).Append(';').NewLine() .Append("ESByte eSBMin = ").Append(ESByte.Min).Append(';').NewLine(); f.Append("return ") .Append("eLMax == ELong.Max").NewLine() .Append("&& eULMax == EULong.Max").NewLine() .Append("&& eSMax == EShort.Max").NewLine() .Append("&& eUSMax == EUShort.Max").NewLine() .Append("&& eBMax == EByte.Max").NewLine() .Append("&& eSBMax == ESByte.Max").NewLine() .Append("&& eLMin == ELong.Min").NewLine() .Append("&& eULMin == EULong.Min").NewLine() .Append("&& eSMin == EShort.Min").NewLine() .Append("&& eUSMin == EUShort.Min").NewLine() .Append("&& eBMin == EByte.Min").NewLine() .Append("&& eSBMin == ESByte.Min").NewLine() .Append(";").NewLine(); string source = workspace.GetGlobalSource(); Assembly a = TestHelper.CreateAssembly(source, workspace.AssemblyReferences); a.Should().NotBeNull(); object tester = Activator.CreateInstance(a.ExportedTypes.Single(t => t.Name == "Tester")); bool success = (bool)tester.GetType().GetMethod("CheckTypes").Invoke(tester, Array.Empty <object>()); success.Should().BeTrue(source); }
public void writing_any_MemberInfo_uses_the_Module() { var workspace = CodeWorkspace.Create(); workspace.EnsureAssemblyReference(typeof(CompileToSourceStringTests), typeof(MethodInfo)); var t = workspace.Global.CreateType("public class MemberFinder"); t.Namespace.EnsureUsing("System.Reflection"); var thisTestMethod = typeof(CompileToSourceStringTests).GetMethod("writing_any_MemberInfo_uses_the_Module"); var ValIGen = typeof(Nested <>).GetMethod("Val", new[] { typeof(int) }); var ValSGen = typeof(Nested <>).GetMethod("Val", new[] { typeof(string) }); var PropGen = typeof(Nested <>).GetProperty("Prop"); var EventGen = typeof(Nested <>).GetEvent("Event"); var CtorGen = typeof(Nested <>).GetConstructor(Type.EmptyTypes); var ValI = typeof(Nested <TestAttribute>).GetMethod("Val", new[] { typeof(int) }); var ValS = typeof(Nested <Comparer <List <KeyValuePair <int, byte> > > >).GetMethod("Val", new[] { typeof(string) }); var Prop = typeof(Nested <Test>).GetProperty("Prop"); var Event = typeof(Nested <string>).GetEvent(nameof(Nested <string> .Event)); var Ctor = typeof(Nested <ICodePart>).GetConstructor(Type.EmptyTypes); t.Append("public readonly static MethodInfo ThisTestMethod = ").Append(thisTestMethod).Append(";"); t.Append("public readonly static MethodInfo ValIGen = ").Append(ValIGen).Append(";"); t.Append("public readonly static MethodInfo ValSGen = ").Append(ValSGen).Append(";"); t.Append("public readonly static PropertyInfo PropGen = ").Append(PropGen).Append(";"); t.Append("public readonly static EventInfo EventGen = ").Append(EventGen).Append(";"); t.Append("public readonly static ConstructorInfo CtorGen = ").Append(CtorGen).Append(";"); t.Append("public readonly static MethodInfo ValI = ").Append(ValI).Append(";"); t.Append("public readonly static MethodInfo ValS = ").Append(ValS).Append(";"); t.Append("public readonly static PropertyInfo Prop = ").Append(Prop).Append(";"); t.Append("public readonly static EventInfo Event = ").Append(Event).Append(";"); t.Append("public readonly static ConstructorInfo Ctor = ").Append(Ctor).Append(";"); Assembly a = TestHelper.CreateAssembly(workspace.GetGlobalSource(), workspace.AssemblyReferences); Type memberFinder = a.ExportedTypes.Single(t => t.Name == "MemberFinder"); var eThisTestMethod = (MethodInfo)memberFinder.GetField("ThisTestMethod").GetValue(null); eThisTestMethod.Should().BeSameAs(thisTestMethod); var eValIGen = (MethodInfo)memberFinder.GetField("ValIGen").GetValue(null); eValIGen.Should().BeSameAs(ValIGen); var eValSGen = (MethodInfo)memberFinder.GetField("ValSGen").GetValue(null); eValSGen.Should().BeSameAs(ValSGen); var ePropGen = (PropertyInfo)memberFinder.GetField("PropGen").GetValue(null); ePropGen.Should().BeSameAs(PropGen); var eEventGen = (EventInfo)memberFinder.GetField("EventGen").GetValue(null); eEventGen.Should().BeSameAs(EventGen); var eCtorGen = (ConstructorInfo)memberFinder.GetField("CtorGen").GetValue(null); eCtorGen.Should().BeSameAs(CtorGen); var eValI = (MethodInfo)memberFinder.GetField("ValI").GetValue(null); eValI.Should().BeSameAs(ValI); var eValS = (MethodInfo)memberFinder.GetField("ValS").GetValue(null); eValS.Should().BeSameAs(ValS); var eProp = (PropertyInfo)memberFinder.GetField("Prop").GetValue(null); eProp.Should().BeSameAs(Prop); var eEvent = (EventInfo)memberFinder.GetField("Event").GetValue(null); eEvent.Should().BeSameAs(Event); var eCtor = (ConstructorInfo)memberFinder.GetField("Ctor").GetValue(null); eCtor.Should().BeSameAs(Ctor); }
public void writing_and_reading_simple_values_of_all_known_types() { var workspace = CodeWorkspace.Create(); var global = workspace.Global; #region Injected code DateTime tD = new DateTime(2017, 5, 27, 13, 47, 23, DateTimeKind.Local); DateTimeOffset tO = new DateTimeOffset(tD.Ticks, TimeSpan.FromMinutes(42)); TimeSpan tT = TimeSpan.FromMilliseconds(987897689); Guid g = new Guid("4CCF5143-11C2-485F-93CE-76B4B8E1AFA0"); object[] array = new object[] { null, true, false, "Hello World!", (sbyte)1, (short)2, 3, 4398798739879743, (byte)5, (ushort)6, (uint)7, (ulong)8, 'a', '\uD800', // A high surrogate '\uDC00', // A low surrogate '\0', '\u3712', '\t', 4.89e-2, 4.8965e-2f, new Decimal(Math.E), g, tD, tO, tT, System.Type.Missing, typeof(Dictionary <string, int>), }; #endregion global.EnsureUsing("System") .EnsureUsing("NUnit.Framework") .EnsureUsing("CK.Core") .EnsureUsing("System.Collections.Generic") .EnsureUsing("System.Linq"); global.CreateType("public class Tester") .Append("public string Run() {").NewLine() .Append(@" DateTime tD = new DateTime(2017,5,27,13,47,23,DateTimeKind.Local); DateTimeOffset tO = new DateTimeOffset( tD.Ticks, TimeSpan.FromMinutes(42) ); TimeSpan tT = TimeSpan.FromMilliseconds( 987897689 ); Guid g = new Guid( ""4CCF5143-11C2-485F-93CE-76B4B8E1AFA0"" ); object[] array = new object[] { null, true, false, ""Hello World!"", (sbyte)1, (short)2, 3, 4398798739879743, (byte)5, (ushort)6, (uint)7, (ulong)8, 'a', '\uD800', // A high surrogate '\uDC00', // A low surrogate '\0', '\u3712', '\t', 4.89e-2, 4.8965e-2f, new decimal( Math.E ), g, tD, tO, tT, System.Type.Missing, typeof(Dictionary<string,int>), }; ") .NewLine() .Append("var rewrite = ").AppendArray(array).Append(";").NewLine() .Append(@" var diff = array .Select( ( o, idx ) => new { O = o, T = rewrite[idx], I = idx } ) .Where( x => (x.O == null && x.T != null) || (x.O != null && !x.O.Equals( x.T )) ) .Select( x => $""{x.I} - {x.O} != {x.T}"" ) .Concatenate(); return diff;").NewLine() .Append("}") .NewLine(); workspace.EnsureAssemblyReference( typeof(object), typeof(ROSpanCharMatcher), typeof(System.Diagnostics.Debug), typeof(System.Linq.Enumerable), typeof(TestFixtureAttribute) ); var source = workspace.GetGlobalSource(); var references = workspace.AssemblyReferences; Assembly a = TestHelper.CreateAssembly(source, references); object tester = Activator.CreateInstance(a.ExportedTypes.Single(t => t.Name == "Tester")); string diff = (string)tester.GetType().GetMethod("Run").Invoke(tester, Array.Empty <object>()); diff.Should().BeEmpty(); }
ICodeWorkspace CreateWorkspace() => CodeWorkspace.Create();