public void ClassInheritanceTest() { var pcl = ResolutionTests.CreateCache(@"module modA; class A{} class B:A {} class C:A {} class D:C {}" ); var ctxt = ResolutionContext.Create(pcl, null, pcl[0]["modA"]); var A = GetType("A", ctxt); var B = GetType("B", ctxt); var C = GetType("C", ctxt); var D = GetType("D", ctxt); Assert.IsTrue(ResultComparer.IsEqual(A, A)); Assert.IsTrue(ResultComparer.IsEqual(B, B)); Assert.IsTrue(ResultComparer.IsEqual(C, C)); Assert.IsTrue(ResultComparer.IsEqual(D, D)); Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(A, B)); Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(A, C)); Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(A, D)); Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(B, C)); Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(C, B)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(A, A)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(B, A)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(C, A)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(D, C)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(D, A)); }
public void Test1() { var pcl = ResolutionTests.CreateCache(@"module modA; class A(T = int) { static int prop; static A statA; // 6 } A a = new A(); // 9 void main() { A.prop = 3; // 13 int b = A.prop + 4; // 14 A.statA.statA = new A!float(); // 15 } "); var ctxt = ResolutionContext.Create(pcl, null, pcl[0]["modA"]); var refs = ReferencesFinder.Scan(pcl[0]["modA"]["A"].First(), ctxt) as List <ISyntaxRegion>; Assert.IsNotNull(refs); Assert.AreEqual(8, refs.Count); }
public void TestAccessExpression() { var pcl = ResolutionTests.CreateCache(@"module modA; class A { const int someProp=3; } A a;"); var vp = new StandardValueProvider(ResolutionContext.Create(pcl, null, pcl[0]["modA"])); /* * var v = E("a.someProp", vp); * Assert.IsInstanceOfType(v, typeof(PrimitiveValue)); * Assert.AreEqual(((PrimitiveValue)v).Value,3); */ var v = E("A.someProp", vp); Assert.IsInstanceOfType(typeof(VariableValue), v); var vv = vp[((VariableValue)v).Variable] as PrimitiveValue; Assert.AreEqual(3, vv.Value); }
public override bool Matches(object actual) { var code = actual as string; if (code == null) { return(false); } code += "\n"; var m = DParser.ParseString(code); var cache = ResolutionTests.CreateCache(); (cache [0] as MutableRootPackage).AddModule(m); var ed = new EditorData { ModuleCode = code, CaretOffset = code.Length - 1, CaretLocation = DocumentHelper.OffsetToLocation(code, code.Length - 1), SyntaxTree = m, ParseCache = cache }; var gen = new TestCompletionDataGen(null, null); var res = CodeCompletion.GenerateCompletionData(ed, gen, 'a'); return(neg ? !res : res); }
public void InterfaceInheritanceTest() { var pcl = ResolutionTests.CreateCache(@"module modA; class A {} class B {} interface IA {} interface IB {} interface IC : IA {} interface ID : IC {} class E : A, IA {} class F : B, IA {} class G : A, IC {} class H : B, ID {}" ); var ctxt = ResolutionContext.Create(pcl, null, pcl[0]["modA"]); var A = GetType("A", ctxt); var B = GetType("B", ctxt); var IA = GetType("IA", ctxt); var IB = GetType("IB", ctxt); var IC = GetType("IC", ctxt); var ID = GetType("ID", ctxt); var E = GetType("E", ctxt); var F = GetType("F", ctxt); var G = GetType("G", ctxt); var H = GetType("H", ctxt); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(IC, IA)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(ID, IC)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(ID, IA)); Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(IA, IC)); Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(IA, ID)); Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(IC, IB)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(E, A)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(E, IA)); Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(E, F)); Assert.IsFalse(ResultComparer.IsImplicitlyConvertible(F, E)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(F, B)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(F, IA)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(G, A)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(G, IC)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(G, IA)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(H, B)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(H, ID)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(H, IC)); Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(H, IA)); }
public static EditorData GenEditorData(int caretLine, int caretPos, string focusedModuleCode, params string[] otherModuleCodes) { var cache = ResolutionTests.CreateCache(otherModuleCodes); var ed = new EditorData { ParseCache = cache }; UpdateEditorData(ed, caretLine, caretPos, focusedModuleCode); return(ed); }
public static void TestString(string literal, string content, bool ProvideObjModule = true) { ResolutionContext ctxt = null; var block = new DBlockNode(); if (ProvideObjModule) { ctxt = ResolutionTests.CreateDefCtxt(ResolutionTests.CreateCache(), block); } else { ctxt = ResolutionTests.CreateDefCtxt(new ParseCacheView(new string[] { }), block); } var x = DParser.ParseExpression(literal); Assert.That(x, Is.TypeOf(typeof(IdentifierExpression))); var id = (IdentifierExpression)x; var v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(ArrayValue))); var av = (ArrayValue)v; Assert.That(av.IsString, Is.True); Assert.AreEqual(av.StringValue, content); Assert.IsInstanceOfType(typeof(ArrayType), av.RepresentedType); var ar = (ArrayType)av.RepresentedType; switch (id.Subformat) { case LiteralSubformat.Utf8: Assert.AreEqual(ar.DeclarationOrExpressionBase.ToString(), "immutable(char)[]"); break; case LiteralSubformat.Utf16: Assert.AreEqual(ar.DeclarationOrExpressionBase.ToString(), "immutable(wchar)[]"); break; case LiteralSubformat.Utf32: Assert.AreEqual(ar.DeclarationOrExpressionBase.ToString(), "immutable(dchar)[]"); break; default: Assert.Fail(); return; } }
public void TestTemplateDeductionAsConversion() { var pcl = ResolutionTests.CreateCache(@"module modA; void foo(T:T)(T[] t) {} int[] p=[1,2,3,4,5]; "); var ctxt = ResolutionContext.Create(pcl, null, pcl[0]["modA"]); var foo = pcl[0]["modA"]["foo"].First() as DMethod; ctxt.PushNewScope(foo); var foo_firstArg = TypeDeclarationResolver.Resolve(foo.Parameters[0].Type, ctxt); var p = TypeDeclarationResolver.ResolveIdentifier("p", ctxt, null)[0] as MemberSymbol; Assert.IsTrue(ResultComparer.IsImplicitlyConvertible(p, foo_firstArg[0], ctxt)); ctxt.Pop(); }
public void TestConstEval() { var pcl = ResolutionTests.CreateCache(@"module modA; const a= 234; enum b=123; const int c=125; enum int d=126; "); var vp = new StandardValueProvider(ResolutionContext.Create(pcl, null, pcl[0]["modA"])); var v = E("a", vp); Assert.IsInstanceOfType(typeof(VariableValue), v); var val = vp[((VariableValue)v).Variable]; Assert.IsInstanceOfType(typeof(PrimitiveValue), val); var pv = (PrimitiveValue)val; Assert.AreEqual(pv.Value, 234M); v = E("b", vp); val = vp[((VariableValue)v).Variable]; pv = (PrimitiveValue)val; Assert.AreEqual(pv.Value, 123M); v = E("c", vp); val = vp[((VariableValue)v).Variable]; pv = (PrimitiveValue)val; Assert.AreEqual(pv.Value, 125M); v = E("d", vp); val = vp[((VariableValue)v).Variable]; pv = (PrimitiveValue)val; Assert.AreEqual(pv.Value, 126M); pv = E("d + 4", vp) as PrimitiveValue; Assert.IsNotNull(pv); Assert.AreEqual(130M, pv.Value); pv = E("d + a", vp) as PrimitiveValue; Assert.IsNotNull(pv); Assert.AreEqual(360M, pv.Value); }
public void TestIsExpression() { var pcl = ResolutionTests.CreateCache(@"module modA; class A {} class B : A {} class C : A {} struct DynArg(int i) { static assert (i >= 0); alias i argNr; } template isDynArg(T) { static if (is(typeof(T.argNr))) { // must have the argNr field static if(is(T : DynArg!(T.argNr))) { // now check the exact type static const bool isDynArg = true; } else static const bool isDynArg = false; } else static const bool isDynArg = false; } "); var vp = new StandardValueProvider(ResolutionContext.Create(pcl, null, pcl[0]["modA"])); Assert.IsTrue(EvalIsExpression("char*[] T : U[], U : V*, V", vp)); Assert.IsTrue(EvalIsExpression("string T : U[], U : immutable(V), V : char", vp)); Assert.IsFalse(EvalIsExpression("int[10] X : X[Y], int Y : 5", vp)); Assert.IsTrue(EvalIsExpression("bool : bool", vp)); Assert.IsTrue(EvalIsExpression("bool == bool", vp)); Assert.IsTrue(EvalIsExpression("C : A", vp)); Assert.IsTrue(EvalIsExpression("C : C", vp)); Assert.IsFalse(EvalIsExpression("C == A", vp)); Assert.IsTrue(EvalIsExpression("immutable(char) == immutable", vp)); Assert.IsFalse(EvalIsExpression("string == immutable", vp)); Assert.IsTrue(EvalIsExpression("A == class", vp)); Assert.IsTrue(EvalIsExpression("typeof(A)", vp)); Assert.IsFalse(EvalIsExpression("typeof(D)", vp)); }
public void ParsePerformance1() { //var pc = ParsePhobos(false); var pcl = ResolutionTests.CreateCache(@"module modA; import std.stdio, std.array; class lol{ static int Object; int inc(int i, int k) { return i+k; } void derp() {} const void lolBar(this T)() { auto l=123.inc!int(); lol.Object; writeln(typeid(T)); Object=1; } } void main() { immutable(char)[] arr; destroy(arr); for(int i=0;i<10;i++) writeln(i); return; auto st=new STest(); auto tt = new ModClass!int(); writeln(st.a); static assert(st.a==34); int i = delegate int() { return 123; }(); //int j= 234++; writeln(i); writeln(di,123,123); lol o = new lol(); o.derp(); } "); //pcl.Add(pc); var sw = new Stopwatch(); var main = pcl[0]["modA"]["main"].First() as DMethod; Assert.AreEqual(0, (pcl[0]["modA"] as DModule).ParseErrors.Count); var s = main.Body.SubStatements.Last() as IExpressionContainingStatement; var ctxt = ResolutionContext.Create(pcl, null, main, s.Location); //ctxt.ContextIndependentOptions |= ResolutionOptions.StopAfterFirstOverloads | ResolutionOptions.DontResolveBaseClasses | ResolutionOptions.DontResolveBaseTypes; var x = s.SubExpressions[0]; //pc.UfcsCache.Update(pcl); sw.Restart(); var t = ExpressionTypeEvaluation.EvaluateType(x, ctxt); sw.Stop(); Trace.WriteLine("Took " + sw.Elapsed.TotalMilliseconds + "ms to resolve " + x); }
public void Traits() { var pcl = ResolutionTests.CreateCache(@"module A; int i; string s; abstract class C { int foo(); } class NC { int foo(); } C c; int[] dynArr; int[5] statArr; auto assocArr = ['c' : 23, 'b' : 84]; struct S { void bar() { } void bar(int i) {} void bar(string s) {} static int statInt; } class D { void bar() { } abstract void absBar(); static void statFoo() {} final void finBar() {}; private int privInt; package int packInt; } class E : D{ final override void absBar() { } final void bar() {} } interface I { void bar(); } template Tmpl(){ void bar(); } ", @"module std.someStd;"); var ctxt = ResolutionTests.CreateDefCtxt(pcl, pcl[0]["A"], null); BoolTrait(ctxt, "isArithmetic, int"); BoolTrait(ctxt, "isArithmetic, i, i+1, int"); BoolTrait(ctxt, "isArithmetic", false); BoolTrait(ctxt, "isArithmetic, int*", false); BoolTrait(ctxt, "isArithmetic, s, 123", false); BoolTrait(ctxt, "isArithmetic, 123, s", false); BoolTrait(ctxt, "isAbstractClass, C, c"); BoolTrait(ctxt, "isAbstractClass, C"); BoolTrait(ctxt, "isAbstractClass, int", false); BoolTrait(ctxt, "isAbstractClass, NC", false); BoolTrait(ctxt, "isAbstractClass", false); BoolTrait(ctxt, "isAssociativeArray, assocArr"); BoolTrait(ctxt, "isAssociativeArray, dynArr", false); BoolTrait(ctxt, "isStaticArray, statArr"); BoolTrait(ctxt, "isStaticArray, dynArr", false); BoolTrait(ctxt, "isVirtualMethod, D.bar"); BoolTrait(ctxt, "isVirtualMethod, D.absBar"); BoolTrait(ctxt, "isVirtualMethod, I.bar"); BoolTrait(ctxt, "isVirtualMethod, Tmpl!().bar"); //BoolTrait(ctxt, "isVirtualMethod, E.bar"); //BoolTrait(ctxt, "isVirtualMethod, E.absBar"); BoolTrait(ctxt, "isVirtualMethod, S.bar", false); BoolTrait(ctxt, "isVirtualMethod, D.statFoo", false); BoolTrait(ctxt, "isVirtualMethod, D.finBar", false); BoolTrait(ctxt, "isVirtualFunction, D.bar"); BoolTrait(ctxt, "isVirtualFunction, D.absBar"); BoolTrait(ctxt, "isVirtualFunction, I.bar"); BoolTrait(ctxt, "isVirtualFunction, Tmpl!().bar"); //BoolTrait(ctxt, "isVirtualFunction, E.bar"); //BoolTrait(ctxt, "isVirtualFunction, E.absBar"); BoolTrait(ctxt, "isVirtualFunction, S.bar", false); BoolTrait(ctxt, "isVirtualFunction, D.statFoo", false); BoolTrait(ctxt, "isVirtualFunction, D.finBar"); BoolTrait(ctxt, "hasMember, C, \"foo\""); BoolTrait(ctxt, "hasMember, c, \"foo\""); BoolTrait(ctxt, "hasMember, C, \"noFoo\"", false); BoolTrait(ctxt, "hasMember, int, \"sizeof\""); var x = DParser.ParseExpression(@"__traits(identifier, C.aso.derp)"); var v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(ArrayValue))); var av = v as ArrayValue; Assert.That(av.IsString, Is.True); Assert.That(av.StringValue, Is.EqualTo("C.aso.derp")); x = DParser.ParseExpression("__traits(getMember, c, \"foo\")"); var t = ExpressionTypeEvaluation.EvaluateType(x, ctxt); Assert.That(t, Is.TypeOf(typeof(MemberSymbol))); x = DParser.ParseExpression("__traits(getOverloads, S, \"bar\")"); v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(TypeValue))); Assert.That((v as TypeValue).RepresentedType, Is.TypeOf(typeof(DTuple))); t = ExpressionTypeEvaluation.EvaluateType(x, ctxt); Assert.That(t, Is.TypeOf(typeof(DTuple))); x = DParser.ParseExpression("__traits(getProtection, D.privInt)"); v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(ArrayValue))); av = v as ArrayValue; Assert.That(av.IsString, Is.True); Assert.That(av.StringValue, Is.EqualTo("private")); x = DParser.ParseExpression("__traits(getProtection, D)"); v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(ArrayValue))); av = v as ArrayValue; Assert.That(av.IsString, Is.True); Assert.That(av.StringValue, Is.EqualTo("public")); x = DParser.ParseExpression("__traits(getProtection, D.packInt)"); v = Evaluation.EvaluateValue(x, ctxt); Assert.That(v, Is.TypeOf(typeof(ArrayValue))); av = v as ArrayValue; Assert.That(av.IsString, Is.True); Assert.That(av.StringValue, Is.EqualTo("package")); BoolTrait(ctxt, "isSame, int, int"); BoolTrait(ctxt, "isSame, int, double", false); BoolTrait(ctxt, "isSame, C, D", false); BoolTrait(ctxt, "isSame, D, D"); BoolTrait(ctxt, "compiles", false); BoolTrait(ctxt, "compiles, asd.herp", false); BoolTrait(ctxt, "compiles, i"); BoolTrait(ctxt, "compiles, i + 1"); //BoolTrait(ctxt, "compiles, &i + 1", false); //TODO: Check if both operand types match..is this still efficient? BoolTrait(ctxt, "compiles, typeof(1)"); BoolTrait(ctxt, "compiles, S.nonExistingItem", false); //TODO: Make the resolver not resolve non-static items implicitly (i.e. without explicit resolution option) BoolTrait(ctxt, "compiles, S.statInt"); BoolTrait(ctxt, "compiles, 1,2,3,int,long,std"); BoolTrait(ctxt, "compiles, 1,2,3,int,long,3[1]", false); BoolTrait(ctxt, "compiles, 3[1]", false); BoolTrait(ctxt, "compiles, immutable(S44)(3, &i)", false); }