Esempio n. 1
0
 /// <summary>
 /// ¼ì²â×ÖµäÏî±àºÅ²»Ð¡ÓÚ0
 /// </summary>
 /// <param name="id"></param>
 private void CheckIdBiggerZero(int id)
 {
     AssertUtil.AreBigger(id, 0, LanguageUtil.Translate("api_Business_DictionaryItem_CheckIdBiggerZero"));
 }
Esempio n. 2
0
 public void AssertAttrIsType(IPythonProjectEntry module, string variable, string memberName, int index, params PythonMemberType[] types)
 {
     AssertUtil.ContainsExactly(GetMember(module, variable, memberName, index).Select(m => m.MemberType), types);
 }
Esempio n. 3
0
    private void updateWindowsWithHeightRangesSolidRemoved(List <Range1D> heightRanges, Coord pRelCoord,
                                                           SurroundingSurfaceValues ssvs, bool shouldPropagateLight)
    {
        int x = pRelCoord.x, y = pRelCoord.y, z = pRelCoord.z;
        DiscreteDomainRangeList <LightColumn> liCols = m_lightColumnMap[x, z];

        LightColumn colJustBelowYBeforeUpdate        = liCols.rangeContaining(y - 1);  //null if there wasn't

        Range1D highest = heightRanges[heightRanges.Count - 1];
        int     newSurfaceHeightExtentAtXZ           = highest.extent();

        if (colJustBelowYBeforeUpdate != null)
        {
            colJustBelowYBeforeUpdate = colJustBelowYBeforeUpdate.Copy();             // safe keeping
        }

        updateRangeListAtXZ(heightRanges, pRelCoord, true, ssvs);

        //case where y represents a removed top block
        // where either air or solid was underneath the
        // former top block
        if (newSurfaceHeightExtentAtXZ <= y)
        {
            //no column here anymore
            //surface was revealed.
            //add any adjacent blocks to exposed list
            // if they weren't on the list before
            // add them to need update list
            // our work is done...return (put the above in a function)

            b.bug("got new surf less than y");
            Coord surfaceTopCoord = pRelCoord;
            if (newSurfaceHeightExtentAtXZ < y)
            {
                b.bug("is fully less than");
                surfaceTopCoord.y = newSurfaceHeightExtentAtXZ;
            }

            foreach (LightColumn col in  m_lightColumnMap.lightColumnsAdjacentToAndAtleastPartiallyAbove(surfaceTopCoord))
            {
                b.bug("updating ");
                addToSurfaceExposedColumnIfNotContains(col);
                col.lightLevel = Window.LIGHT_LEVEL_MAX_BYTE;
                updateColumnAt(col, col.coord, null, LightDomainAround(col.coord));
            }

            m_lightColumnMap[x, z] = liCols;

            return;
        }

        //REMAINING CASES: Y REMOVED FROM BELOW SURFACE

        int lowestSurroundingSurface = ssvs.lowestValue();

        LightColumn colAtY = liCols.rangeContaining(y);

        AssertUtil.Assert(colAtY != null, "(remove block. didn't get a col at y in lc calc) y was: " + y);


        if (y > lowestSurroundingSurface)               //just update it. anything new will update. anything not new won't.
        {
            //one way or another a newly exposed column here
            addToSurfaceExposedColumnIfNotContains(colAtY);
            updateColumnAt(colAtY, colAtY.coord, null, LightDomainAround(x, z));
            return;
        }

        //REMAINING CASES: Y WAS BELOW THE SURFACE AND NOT EXPOSED BY ADJACENT SURFACE.
        List <LightColumn> justConnectedWithColumns = columnsThatColumnJustConnectedWith(colAtY, y);

        if (justConnectedWithColumns.Count == 0)
        {
            return;
        }

        LightColumn lightest = colAtY;

        bool atYIsLightest = true;

        foreach (LightColumn justConnectedCol in justConnectedWithColumns)
        {
            if (justConnectedCol.lightLevel > lightest.lightLevel)
            {
                lightest      = justConnectedCol;
                atYIsLightest = false;
            }
        }

        if (atYIsLightest)
        {
            foreach (LightColumn adjCol in justConnectedWithColumns)
            {
                updateColumnAt(adjCol, adjCol.coord, colAtY, LightDomainAround(adjCol.coord));
            }
        }
        else
        {
            updateColumnAt(colAtY, colAtY.coord, lightest, LightDomainAround(colAtY.coord));
        }
    }
Esempio n. 4
0
        public void PydInPackage()
        {
            PythonPaths.Python27.AssertInstalled();

            var outputPath = TestData.GetTempPath(randomSubPath: true);

            Console.WriteLine("Writing to: " + outputPath);

            // run the analyzer
            using (var output = ProcessOutput.RunHiddenAndCapture("Microsoft.PythonTools.Analyzer.exe",
                                                                  "/python", PythonPaths.Python27.InterpreterPath,
                                                                  "/lib", TestData.GetPath(@"TestData\PydStdLib"),
                                                                  "/version", "2.7",
                                                                  "/outdir", outputPath,
                                                                  "/indir", TestData.GetPath("CompletionDB"),
                                                                  "/log", "AnalysisLog.txt")) {
                output.Wait();
                Console.WriteLine("* Stdout *");
                foreach (var line in output.StandardOutputLines)
                {
                    Console.WriteLine(line);
                }
                Console.WriteLine("* Stderr *");
                foreach (var line in output.StandardErrorLines)
                {
                    Console.WriteLine(line);
                }
                Assert.AreEqual(0, output.ExitCode);
            }

            var fact  = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(2, 7));
            var paths = new List <string> {
                outputPath
            };

            paths.AddRange(Directory.EnumerateDirectories(outputPath));
            var typeDb = new PythonTypeDatabase(fact, paths);
            var module = typeDb.GetModule("Package.winsound");

            Assert.IsNotNull(module, "Package.winsound was not analyzed");
            var package = typeDb.GetModule("Package");

            Assert.IsNotNull(package, "Could not import Package");
            var member = package.GetMember(null, "winsound");

            Assert.IsNotNull(member, "Could not get member Package.winsound");
            Assert.AreSame(module, member);

            module = typeDb.GetModule("Package._testcapi");
            Assert.IsNotNull(module, "Package._testcapi was not analyzed");
            package = typeDb.GetModule("Package");
            Assert.IsNotNull(package, "Could not import Package");
            member = package.GetMember(null, "_testcapi");
            Assert.IsNotNull(member, "Could not get member Package._testcapi");
            Assert.IsNotInstanceOfType(member, typeof(CPythonMultipleMembers));
            Assert.AreSame(module, member);

            module = typeDb.GetModule("Package.select");
            Assert.IsNotNull(module, "Package.select was not analyzed");
            package = typeDb.GetModule("Package");
            Assert.IsNotNull(package, "Could not import Package");
            member = package.GetMember(null, "select");
            Assert.IsNotNull(member, "Could not get member Package.select");
            Assert.IsInstanceOfType(member, typeof(CPythonMultipleMembers));
            var mm = (CPythonMultipleMembers)member;

            AssertUtil.ContainsExactly(mm.Members.Select(m => m.MemberType),
                                       PythonMemberType.Module,
                                       PythonMemberType.Constant,
                                       PythonMemberType.Class
                                       );
            Assert.IsNotNull(mm.Members.Contains(module));

            try {
                // Only clean up if the test passed
                Directory.Delete(outputPath, true);
            } catch { }
        }
Esempio n. 5
0
 public void AssertHasAttrExact(IPythonProjectEntry module, string expr, int index, params string[] attrs)
 {
     AssertUtil.ContainsExactly(GetMemberNames(module, expr, index), attrs);
 }
Esempio n. 6
0
        public string Encode(byte[] bytes)
        {
            AssertUtil.ArgumentNotNull(bytes, nameof(bytes));

            return(Encode(bytes, 0, bytes.Length, false));
        }
Esempio n. 7
0
        public void MergeRequirements()
        {
            // Comments should be preserved, only package specs should change.
            AssertUtil.AreEqual(
                PythonProjectNode.MergeRequirements(new[] {
                "a # with a comment",
                "B==0.2",
                "# just a comment B==01234",
                "",
                "x < 1",
                "d==1.0 e==2.0 f==3.0"
            }, new[] {
                "b==0.1",
                "a==0.2",
                "c==0.3",
                "e==4.0",
                "x==0.8"
            }.Select(p => PackageSpec.FromRequirement(p)), false),
                "a==0.2 # with a comment",
                "b==0.1",
                "# just a comment B==01234",
                "",
                "x==0.8",
                "d==1.0 e==4.0 f==3.0"
                );

            // addNew is true, so the c==0.3 should be added.
            AssertUtil.AreEqual(
                PythonProjectNode.MergeRequirements(new[] {
                "a # with a comment",
                "b==0.2",
                "# just a comment B==01234"
            }, new[] {
                "B==0.1",       // case is updated
                "a==0.2",
                "c==0.3"
            }.Select(p => PackageSpec.FromRequirement(p)), true),
                "a==0.2 # with a comment",
                "B==0.1",
                "# just a comment B==01234",
                "c==0.3"
                );

            // No existing entries, so the new ones are sorted and returned.
            AssertUtil.AreEqual(
                PythonProjectNode.MergeRequirements(null, new[] {
                "b==0.2",
                "a==0.1",
                "c==0.3"
            }.Select(p => PackageSpec.FromRequirement(p)), false),
                "a==0.1",
                "b==0.2",
                "c==0.3"
                );

            // Check all the inequalities
            const string inequalities = "<=|>=|<|>|!=|==";

            AssertUtil.AreEqual(
                PythonProjectNode.MergeRequirements(
                    inequalities.Split('|').Select(s => "a " + s + " 1.2.3"),
                    new[] { "a==0" }.Select(p => PackageSpec.FromRequirement(p)),
                    false
                    ),
                inequalities.Split('|').Select(_ => "a==0").ToArray()
                );
        }
Esempio n. 8
0
 public void assertNotNull(string str)
 {
     AssertUtil.Assert(!this.equalTo(Coord.TheErsatzNullCoord()), str);
 }
        public void BasicRequireCompletions()
        {
            using (new OptionHolder("TextEditor", "Node.js", "BraceCompletion", false)) {
                using (var solution = BasicProject.Generate().ToVs()) {
                    var server = solution.OpenItem("Require", "server.js");
                    Keyboard.Type("require(");

                    using (var completionSession = server.WaitForSession <ICompletionSession>()) {
                        Assert.AreEqual(1, completionSession.Session.CompletionSets.Count);

                        // we pick up built-ins, folders w/ package.json, and peers
                        AssertUtil.ContainsAtLeast(
                            completionSession.Session.GetDisplayTexts(),
                            "http",
                            "Foo",
                            "./myapp.js",
                            "./SomeFolder/baz.js",
                            "quox.js"
                            );

                        // we don't show our own file
                        AssertUtil.DoesntContain(completionSession.Session.GetDisplayTexts(), "./server.js");

                        AssertUtil.ContainsAtLeast(
                            completionSession.Session.GetInsertionTexts(),
                            "'http'",
                            "'Foo'",
                            "'./myapp.js'",
                            "'./SomeFolder/baz.js'",
                            "'quox.js'"
                            );

                        Keyboard.Type("htt");
                        server.WaitForText("require(htt");

                        // we should be filtered down
                        AssertUtil.ContainsExactly(
                            completionSession.Session.GetDisplayTexts(),
                            "http",
                            "https"
                            );

                        // this should trigger completion
                        Keyboard.Type(")");
                        server.WaitForText("require('http')");
                    }

                    Keyboard.Backspace(8);
                    server.WaitForText("require");

                    Keyboard.Type("(");
                    using (var completionSession = server.WaitForSession <ICompletionSession>()) {
                        Assert.AreEqual(1, completionSession.Session.CompletionSets.Count);

                        // this should dismiss the session and not complete anything
                        Keyboard.Type("'".ToString());
                        server.WaitForText("require('");

                        Assert.IsTrue(completionSession.Session.IsDismissed);
                    }

                    Keyboard.Backspace(2);
                    server.WaitForText("require");

                    Keyboard.Type("(");
                    using (var completionSession = server.WaitForSession <ICompletionSession>()) {
                        Assert.AreEqual(1, completionSession.Session.CompletionSets.Count);

                        // this should dismiss the session and not complete anything
                        Keyboard.Type("\"".ToString());
                        server.WaitForText("require(\"");

                        Assert.IsTrue(completionSession.Session.IsDismissed);
                    }
                }
            }
        }
Esempio n. 10
0
        public ColumnAttribute(string name)
        {
            AssertUtil.ArgumentNotEmpty(name, nameof(name));

            this.Name = name.Trim();
        }
Esempio n. 11
0
 public void assertNotNull(string str)
 {
     AssertUtil.Assert(!Range1D.Equal(this, Range1D.theErsatzNullRange()), str);
 }
Esempio n. 12
0
 // [Category("JaccardSimilarity Test")]
 public void JaccardSimilarity_ShortDescription()
 {
     AssertUtil.Equal("JaccardSimilarity", _myJaccardSimilarity.ShortDescriptionString,
                      "Problem with JaccardSimilarity test short description.");
 }
Esempio n. 13
0
 // [Category("Levenstein Test")]
 public void Levenstein_ShortDescription()
 {
     AssertUtil.Equal("Levenstein", myLevenstein.ShortDescriptionString, "Problem with Levenstein test short description.");
 }
Esempio n. 14
0
 /// <summary>
 /// ¼ì²â×ÖµäÏîÃû³Æ²»Îª¿Õ
 /// </summary>
 /// <param name="name"></param>
 private void CheckNameNotNull(string name)
 {
     AssertUtil.NotNullOrWhiteSpace(name, LanguageUtil.Translate("api_Business_DictionaryItem_CheckNameNotNull"));
 }
Esempio n. 15
0
        public void Update(byte[] bytes)
        {
            AssertUtil.ArgumentNotNull(bytes, nameof(bytes));

            Update(bytes, 0, bytes.Length);
        }
Esempio n. 16
0
 // [Category("EuclideanDistance Test")]
 public void EuclideanDistance_ShortDescription()
 {
     AssertUtil.Equal("EuclideanDistance", _myEuclideanDistance.ShortDescriptionString,
                      "Problem with EuclideanDistance test short description.");
 }
Esempio n. 17
0
        public void Encode(Stream inStream, Stream outStream, bool insertLineBreaks)
        {
            AssertUtil.ArgumentNotNull(inStream, nameof(inStream));
            AssertUtil.ArgumentNotNull(outStream, nameof(outStream));

            if (!inStream.CanRead)
            {
                throw new ArgumentException($"Argument '{nameof(inStream)}' cannot be read.");
            }

            if (!outStream.CanWrite)
            {
                throw new ArgumentException($"Argument '{nameof(outStream)}' not supports writing.");
            }


            var encodePos    = 0;
            var encodeLeft   = 0;
            var bytesReaded  = 0;
            var readBuffer   = new byte[1024];
            var bytesInOut   = 0;
            var outBuffer    = new byte[1024];
            var blocksInLine = 0;

            while ((bytesReaded = inStream.Read(readBuffer, encodeLeft, readBuffer.Length - encodeLeft)) > 0)
            {
                bytesReaded += encodeLeft;

                encodePos  = 0;
                encodeLeft = 0;

                while (encodePos < bytesReaded)
                {
                    if (bytesInOut >= outBuffer.Length - 10)
                    {
                        outStream.Write(outBuffer, 0, bytesInOut);

                        bytesInOut = 0;
                    }

                    if (insertLineBreaks && blocksInLine == 19)
                    {
                        outBuffer[bytesInOut++] = (byte)'\r';
                        outBuffer[bytesInOut++] = (byte)'\n';

                        blocksInLine = 0;
                    }

                    if (encodePos + 3 <= bytesReaded)
                    {
                        outBuffer[bytesInOut++] = (byte)ENCODE_TABLE[(readBuffer[encodePos] & 0xfc) >> 2];
                        outBuffer[bytesInOut++] = (byte)ENCODE_TABLE[((readBuffer[encodePos] & 3) << 4) | ((readBuffer[encodePos + 1] & 240) >> 4)];
                        outBuffer[bytesInOut++] = (byte)ENCODE_TABLE[((readBuffer[encodePos + 1] & 15) << 2) | ((readBuffer[encodePos + 2] & 0xc0) >> 6)];
                        outBuffer[bytesInOut++] = (byte)ENCODE_TABLE[readBuffer[encodePos + 2] & 0x3f];

                        encodePos += 3;
                        blocksInLine++;
                    }
                    else
                    {
                        encodeLeft = bytesReaded - encodePos;
                        for (var i = 0; i < encodeLeft; i++)
                        {
                            readBuffer[i] = readBuffer[encodePos + i];
                        }

                        break;
                    }
                }
            }

            if (encodeLeft == 1)
            {
                outBuffer[bytesInOut++] = (byte)ENCODE_TABLE[(readBuffer[0] & 0xfc) >> 2];
                outBuffer[bytesInOut++] = (byte)ENCODE_TABLE[(readBuffer[0] & 3) << 4];
                //outBuffer[bytesInOut++] = (byte)ENCODE_TABLE[0x40];
                //outBuffer[bytesInOut++] = (byte)ENCODE_TABLE[0x40];
            }
            else if (encodeLeft == 2)
            {
                outBuffer[bytesInOut++] = (byte)ENCODE_TABLE[(readBuffer[0] & 0xfc) >> 2];
                outBuffer[bytesInOut++] = (byte)ENCODE_TABLE[((readBuffer[0] & 3) << 4) | ((readBuffer[1] & 240) >> 4)];
                outBuffer[bytesInOut++] = (byte)ENCODE_TABLE[(readBuffer[1] & 15) << 2];
                //outBuffer[bytesInOut++] = (byte)ENCODE_TABLE[0x40];
            }

            outStream.Write(outBuffer, 0, bytesInOut);
        }
Esempio n. 18
0
        private IEnumerable <Task> StartCrossThreadAnalysisCalls(
            CancellationToken cancel,
            PythonLanguageVersion version
            )
        {
            var fact  = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
            var state = PythonAnalyzer.CreateSynchronously(fact);

            const string testCode = @"from mod{0:000} import test_func as other_test_func, MyClass as other_mc

c = None
def test_func(a, b={1}()):
    '''My test function'''
    globals c
    a = b
    a = {1}(a)
    b = other_test_func(a)
    c = other_mc.fn(b)
    return b

class MyClass:
    fn = test_func

my_test_func = test_func
my_test_func = other_test_func
my_test_func('abc')

mc = MyClass()
mc.fn([])
";

            var entries = Enumerable.Range(0, 100)
                          .Select(i => {
                var entry  = state.AddModule(string.Format("mod{0:000}", i), string.Format("mod{0:000}.py", i));
                var parser = Parser.CreateParser(new StringReader(testCode.FormatInvariant(i + 1, PythonTypes[i % PythonTypes.Count])), PythonLanguageVersion.V34);
                using (var p = entry.BeginParse()) {
                    p.Tree = parser.ParseFile();
                    p.Complete();
                }
                return(entry);
            })
                          .ToList();

            // One analysis before we start
            foreach (var e in entries)
            {
                e.Analyze(cancel, true);
            }
            state.AnalyzeQueuedEntries(cancel);

            // Repeatedly re-analyse the code
            yield return(Task.Run(() => {
                var rnd = new Random();
                while (!cancel.IsCancellationRequested)
                {
                    var shufEntries = entries
                                      .Select(e => Tuple.Create(rnd.Next(), e))
                                      .OrderBy(t => t.Item1)
                                      .Take(entries.Count / 2)
                                      .Select(t => t.Item2)
                                      .ToList();
                    foreach (var e in shufEntries)
                    {
                        e.Analyze(cancel, true);
                    }

                    state.AnalyzeQueuedEntries(cancel);
                    Console.WriteLine("Analysis complete");
                    Thread.Sleep(1000);
                }
            }, cancel));

            // Repeatedly re-parse the code
            yield return(Task.Run(() => {
                var rnd = new Random();
                while (!cancel.IsCancellationRequested)
                {
                    var shufEntries = entries
                                      .Select((e, i) => Tuple.Create(rnd.Next(), e, i))
                                      .OrderBy(t => t.Item1)
                                      .Take(entries.Count / 4)
                                      .ToList();
                    foreach (var t in shufEntries)
                    {
                        var i = t.Item3;
                        var parser = Parser.CreateParser(new StringReader(testCode.FormatInvariant(i + 1, PythonTypes[i % PythonTypes.Count])), PythonLanguageVersion.V34);
                        using (var p = t.Item2.BeginParse()) {
                            p.Tree = parser.ParseFile();
                            p.Complete();
                        }
                    }
                    Thread.Sleep(1000);
                }
            }, cancel));

            // Repeatedly request signatures
            yield return(Task.Run(() => {
                var entry = entries[1];
                while (!cancel.IsCancellationRequested)
                {
                    var sigs = entry.Analysis.GetSignaturesByIndex("my_test_func", 0).ToList();

                    if (sigs.Any())
                    {
                        AssertUtil.ContainsExactly(
                            sigs.Select(s => s.Name),
                            "test_func"
                            );

                        foreach (var s in sigs)
                        {
                            AssertUtil.ContainsExactly(s.Parameters.Select(p => p.Name), "a", "b");
                        }
                    }
                }
            }, cancel));

            // Repeated request variables and descriptions
            yield return(Task.Run(() => {
                var entry = entries[1];
                while (!cancel.IsCancellationRequested)
                {
                    var descriptions = entry.Analysis.GetDescriptionsByIndex("my_test_func", 0).ToList();
                    descriptions = entry.Analysis.GetDescriptionsByIndex("c", 0).ToList();
                }
            }, cancel));

            // Repeated request members and documentation
            yield return(Task.Run(() => {
                var entry = entries[1];
                while (!cancel.IsCancellationRequested)
                {
                    var descriptions = entry.Analysis.GetCompletionDocumentationByIndex("mc", "fn", 0).ToList();
                }
            }, cancel));
        }
Esempio n. 19
0
        public void Decode(Stream inStream, Stream outStream, bool ignoreInvalidChars)
        {
            AssertUtil.ArgumentNotNull(inStream, nameof(inStream));
            AssertUtil.ArgumentNotNull(outStream, nameof(outStream));

            if (!inStream.CanRead)
            {
                throw new ArgumentException($"Argument '{nameof(inStream)}' cannot be read.");
            }

            if (!outStream.CanWrite)
            {
                throw new ArgumentException($"Argument '{nameof(outStream)}' not supports writing.");
            }


            var decodePos    = 0;
            var bytesReaded  = 0;
            var readBuffer   = new byte[1024];
            var bytesInOut   = 0;
            var outBuffer    = new byte[1024];
            var bytesInBlock = 0;
            var decodeBlock  = new byte[4];
            var waitForEnd   = false;

            while ((bytesReaded = inStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
            {
                decodePos = 0;

                while (decodePos < bytesReaded)
                {
                    if (bytesInOut >= outBuffer.Length - 10)
                    {
                        outStream.Write(outBuffer, 0, bytesInOut);

                        bytesInOut = 0;
                    }

                    for (; bytesInBlock < 4 && decodePos < bytesReaded; decodePos++)
                    {
                        switch (readBuffer[decodePos])
                        {
                        case (byte)' ':
                        case (byte)'\r':
                        case (byte)'\n':
                        case (byte)'\t':
                            continue;

                        case (byte)'=':
                            // Padding may appear only in last two chars of 4-char block.
                            // ab==
                            // abc=
                            if (bytesInBlock < 2)
                            {
                                if (ignoreInvalidChars)
                                {
                                    bytesInBlock = 0;
                                    continue;
                                }

                                if (bytesInBlock == 1)
                                {
                                    throw new FormatException($"Invalid base64 padding '{(char)decodeBlock[0]}='.");
                                }
                                else
                                {
                                    throw new FormatException("Invalid base64 padding '='.");
                                }
                            }
                            else if (bytesInBlock == 2)
                            {
                                if (waitForEnd)
                                {
                                    goto CONTINUE;
                                }

                                waitForEnd = true;
                                continue;
                            }
                            else
                            {
                                goto CONTINUE;
                            }

                        default:
                            if (readBuffer[decodePos] > 127 || DECODE_TABLE[readBuffer[decodePos]] == -1)
                            {
                                if (ignoreInvalidChars)
                                {
                                    continue;
                                }

                                if (waitForEnd)
                                {
                                    throw new FormatException($"Invalid base64 padding '{(char)decodeBlock[0]}{(char)decodeBlock[1]}={(char)readBuffer[decodePos]}'.");
                                }

                                throw new FormatException($"Invalid base64 char '{(char)readBuffer[decodePos]}'.");
                            }

                            if (waitForEnd && !ignoreInvalidChars)
                            {
                                throw new FormatException($"Invalid base64 padding '{(char)decodeBlock[0]}{(char)decodeBlock[1]}={(char)readBuffer[decodePos]}'.");
                            }
                            else if (waitForEnd)
                            {
                                waitForEnd = false;

                                bytesInBlock = 0;
                                decodeBlock[bytesInBlock++] = (byte)DECODE_TABLE[readBuffer[decodePos]];
                            }
                            else
                            {
                                decodeBlock[bytesInBlock++] = (byte)DECODE_TABLE[readBuffer[decodePos]];
                            }

                            continue;
                        }
                    }

                    if (bytesInBlock == 4)
                    {
                        outBuffer[bytesInOut++] = (byte)((decodeBlock[0] << 2) | (decodeBlock[1] >> 4));
                        outBuffer[bytesInOut++] = (byte)(((decodeBlock[1] & 0xF) << 4) | (decodeBlock[2] >> 2));
                        outBuffer[bytesInOut++] = (byte)(((decodeBlock[2] & 0x3) << 6) | decodeBlock[3]);

                        bytesInBlock = 0;
                    }
                }
            }

CONTINUE:
            if (bytesInBlock > 0)
            {
                // Incomplete 4-byte base64 data block.
                if (bytesInBlock == 1 && !ignoreInvalidChars)
                {
                    throw new FormatException($"Invalid incomplete base64 4-char block '{(char)decodeBlock[0]}'.");
                }

                if (bytesInBlock > 1)
                {
                    outBuffer[bytesInOut++] = (byte)((decodeBlock[0] << 2) | (decodeBlock[1] >> 4));
                }

                if (bytesInBlock > 2)
                {
                    outBuffer[bytesInOut++] = (byte)(((decodeBlock[1] & 0xF) << 4) | (decodeBlock[2] >> 2));
                }

                if (bytesInBlock > 3)
                {
                    outBuffer[bytesInOut++] = (byte)(((decodeBlock[2] & 0x3) << 6) | decodeBlock[3]);
                }
            }

            outStream.Write(outBuffer, 0, bytesInOut);
        }
Esempio n. 20
0
        public void AttachPtvsd()
        {
            var expectedOutput = new[] { "stdout", "stderr" };

            string script = TestData.GetPath(@"TestData\DebuggerProject\AttachPtvsd.py");
            var    psi    = new ProcessStartInfo(Version.InterpreterPath, PtvsdInterpreterArguments + " \"" + script + "\"")
            {
                WorkingDirectory       = TestData.GetPath(),
                UseShellExecute        = false,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                CreateNoWindow         = true
            };

            var p = Process.Start(psi);

            try {
                PythonProcess proc = null;
                for (int i = 0; ; ++i)
                {
                    Thread.Sleep(1000);
                    try {
                        proc = PythonRemoteProcess.Attach(
                            new Uri("tcp://secret@localhost?opt=" + PythonDebugOptions.RedirectOutput),
                            warnAboutAuthenticationErrors: false);
                        break;
                    } catch (SocketException) {
                        // Failed to connect - the process might have not started yet, so keep trying a few more times.
                        if (i >= 5 || p.HasExited)
                        {
                            throw;
                        }
                    }
                }

                try {
                    var attached = new AutoResetEvent(false);
                    proc.ProcessLoaded += (sender, e) => {
                        Console.WriteLine("Process loaded");

                        var bp = proc.AddBreakPoint(script, 10);
                        bp.Add();

                        proc.Resume();
                        attached.Set();
                    };

                    var actualOutput = new List <string>();
                    proc.DebuggerOutput += (sender, e) => {
                        actualOutput.Add(e.Output);
                    };

                    var bpHit = new AutoResetEvent(false);
                    proc.BreakpointHit += (sender, args) => {
                        Console.WriteLine("Breakpoint hit");
                        bpHit.Set();
                        proc.Continue();
                    };

                    proc.StartListening();
                    Assert.IsTrue(attached.WaitOne(20000), "Failed to attach within 20s");
                    Assert.IsTrue(bpHit.WaitOne(20000), "Failed to hit breakpoint within 20s");

                    p.WaitForExit(DefaultWaitForExitTimeout);
                    AssertUtil.ArrayEquals(expectedOutput, actualOutput);
                } finally {
                    DetachProcess(proc);
                }
            } finally {
                Console.WriteLine(p.StandardOutput.ReadToEnd());
                Console.WriteLine(p.StandardError.ReadToEnd());
                DisposeProcess(p);
            }
        }
Esempio n. 21
0
        public void General()
        {
            string code = @"def f(a, *b, **c): pass

def f1(x = 42): pass
def f2(x = []): pass

class C(object): 
    @property
    def f(self):
        return 42
    def g(self):
        return 100

class D(C): 
    x = C().g

abc = 42
fob = int

class X(object): pass
class Y(object): pass

union = X()
union = Y()

list_of_int = [1, 2, 3]
tuple_of_str = 'a', 'b', 'c'

m = max

class Aliased(object):
    def f(self):
        pass

def Aliased(fob):
    pass
";

            using (var newPs = SaveLoad(PythonLanguageVersion.V27, new AnalysisModule("test", "test.py", code))) {
                AssertUtil.Contains(newPs.Analyzer.Analyzer.GetModules().Select(x => x.Name), "test");

                string codeText = @"
import test
abc = test.abc
fob = test.fob
a = test.C()
cf = a.f
cg = a.g()
dx = test.D().x
scg = test.C.g
f1 = test.f1
union = test.union
list_of_int = test.list_of_int
tuple_of_str = test.tuple_of_str
f1 = test.f1
f2 = test.f2
m = test.m
Aliased = test.Aliased
";
                var    newMod   = newPs.NewModule("baz", codeText);
                int    pos      = codeText.LastIndexOf('\n');

                AssertUtil.ContainsExactly(newMod.Analysis.GetTypeIdsByIndex("abc", pos), BuiltinTypeId.Int);
                AssertUtil.ContainsExactly(newMod.Analysis.GetTypeIdsByIndex("cf", pos), BuiltinTypeId.Int);
                AssertUtil.ContainsExactly(newMod.Analysis.GetTypeIdsByIndex("cg", pos), BuiltinTypeId.Int);
                Assert.AreEqual("function f1(x = 42)", newMod.Analysis.GetValuesByIndex("f1", pos).First().Description);
                Assert.AreEqual("bound method x", newMod.Analysis.GetValuesByIndex("dx", pos).First().Description);
                Assert.AreEqual("function test.C.g(self)", newMod.Analysis.GetValuesByIndex("scg", pos).First().Description);
                var unionMembers = new List <AnalysisValue>(newMod.Analysis.GetValuesByIndex("union", pos));
                Assert.AreEqual(unionMembers.Count, 2);
                AssertUtil.ContainsExactly(unionMembers.Select(x => x.PythonType.Name), "X", "Y");

                var list = newMod.Analysis.GetValuesByIndex("list_of_int", pos).First();
                AssertUtil.ContainsExactly(newMod.Analysis.GetShortDescriptionsByIndex("list_of_int", pos), "list of int");
                AssertUtil.ContainsExactly(newMod.Analysis.GetShortDescriptionsByIndex("tuple_of_str", pos), "tuple of str");

                AssertUtil.ContainsExactly(newMod.Analysis.GetShortDescriptionsByIndex("fob", pos), "type int");

                var result = newMod.Analysis.GetSignaturesByIndex("f1", pos).ToArray();
                Assert.AreEqual(1, result.Length);
                Assert.AreEqual(1, result[0].Parameters.Length);
                Assert.AreEqual("int", result[0].Parameters[0].Type);

                result = newMod.Analysis.GetSignaturesByIndex("m", pos).ToArray();
                Assert.AreEqual(6, result.Length);

                var members = newMod.Analysis.GetMembersByIndex("Aliased", pos, GetMemberOptions.None);
                AssertUtil.Contains(members.Select(x => x.Name), "f");
                AssertUtil.Contains(members.Select(x => x.Name), "__self__");
            }
        }
Esempio n. 22
0
        public void ReplSplitCodeTest()
        {
            // http://pytools.codeplex.com/workitem/606

            var testCases = new[] {
                new {
                    Code     = @"def f():
    pass

def g():
    pass

f()
g()",
                    Expected = new[] { "def f():\r\n    pass\r\n", "def g():\r\n    pass\r\n", "f()", "g()" }
                },
                new {
                    Code     = @"def f():
    pass

f()

def g():
    pass

f()
g()",
                    Expected = new[] { "def f():\r\n    pass\r\n", "f()", "def g():\r\n    pass\r\n", "f()", "g()" }
                },
                new {
                    Code     = @"def f():
    pass

f()
f()

def g():
    pass

f()
g()",
                    Expected = new[] { "def f():\r\n    pass\r\n", "f()", "f()", "def g():\r\n    pass\r\n", "f()", "g()" }
                },
                new {
                    Code     = @"    def f():
        pass

    f()
    f()

    def g():
        pass

    f()
    g()",
                    Expected = new[] { "def f():\r\n    pass\r\n", "f()", "f()", "def g():\r\n    pass\r\n", "f()", "g()" }
                },
                new {
                    Code     = @"# Comment

f()
f()",
                    Expected = new[] { "# Comment\r\n\r\nf()\r\n", "f()" }
                }
            };

            using (var evaluator = MakeEvaluator()) {
                int counter = 0;
                foreach (var testCase in testCases)
                {
                    Console.WriteLine("Test case {0}", ++counter);
                    AssertUtil.AreEqual(ReplEditFilter.JoinToCompleteStatements(ReplEditFilter.SplitAndDedent(testCase.Code), Microsoft.PythonTools.Parsing.PythonLanguageVersion.V35), testCase.Expected);
                }
            }
        }
Esempio n. 23
0
 public void AssertNotHasAttr(IPythonProjectEntry module, string expr, int index, params string[] attrs)
 {
     AssertUtil.DoesntContain(GetMemberNames(module, expr, index), attrs);
 }
Esempio n. 24
0
        public async Task LoadAndUnloadModule()
        {
            var services = PythonToolsTestUtilities.CreateMockServiceProvider().GetEditorServices();

            using (var are = new AutoResetEvent(false))
                using (var analyzer = await VsProjectAnalyzer.CreateForTestsAsync(services, InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(3, 6)))) {
                    var m1Path = TestData.GetPath("TestData\\SimpleImport\\module1.py");
                    var m2Path = TestData.GetPath("TestData\\SimpleImport\\module2.py");

                    var toAnalyze = new HashSet <string>(StringComparer.OrdinalIgnoreCase)
                    {
                        m1Path, m2Path
                    };
                    analyzer.AnalysisComplete += (s, e) => {
                        lock (toAnalyze) {
                            toAnalyze.Remove(e.Path);
                        }
                        are.Set();
                    };
                    var entry1 = await analyzer.AnalyzeFileAsync(m1Path);

                    var entry2 = await analyzer.AnalyzeFileAsync(m2Path);

                    WaitForEmptySet(are, toAnalyze, CancellationTokens.After60s);

                    var loc = new Microsoft.PythonTools.SourceLocation(1, 1);
                    AssertUtil.ContainsExactly(
                        analyzer.GetEntriesThatImportModuleAsync("module1", true).Result.Select(m => m.moduleName),
                        "module2"
                        );

                    AssertUtil.ContainsExactly(
                        analyzer.GetValueDescriptions(entry2, "x", loc),
                        "int"
                        );

                    toAnalyze.Add(m2Path);
                    await analyzer.UnloadFileAsync(entry1);

                    WaitForEmptySet(are, toAnalyze, CancellationTokens.After15s);

                    // Even though module1 has been unloaded, we still know that
                    // module2 imports it.
                    AssertUtil.ContainsExactly(
                        analyzer.GetEntriesThatImportModuleAsync("module1", true).Result.Select(m => m.moduleName),
                        "module2"
                        );

                    AssertUtil.ContainsExactly(
                        analyzer.GetValueDescriptions(entry2, "x", loc)
                        );

                    toAnalyze.Add(m1Path);
                    toAnalyze.Add(m2Path);
                    await analyzer.AnalyzeFileAsync(m1Path);

                    WaitForEmptySet(are, toAnalyze, CancellationTokens.After5s);

                    AssertUtil.ContainsExactly(
                        analyzer.GetEntriesThatImportModuleAsync("module1", true).Result.Select(m => m.moduleName),
                        "module2"
                        );

                    AssertUtil.ContainsExactly(
                        analyzer.GetValueDescriptions(entry2, "x", loc),
                        "int"
                        );
                }
        }
Esempio n. 25
0
 public void AssertHasParameters(IPythonProjectEntry module, string expr, int index, params string[] paramNames)
 {
     AssertUtil.AreEqual(module.Analysis.GetSignaturesByIndex(expr, index).Single().Parameters.Select(p => p.Name), paramNames);
 }
Esempio n. 26
0
        private static void AreXPathNavigatorsEqual(XPathNavigator nav1, XPathNavigator nav2, string message)
        {
            while (true)
            {
                if (nav1.Name != nav2.Name)
                {
                    Assert.Fail("Expected element <{0}>. Actual element <{1}>.{2}", nav1.Name, nav2.Name, message);
                }
                var anav1 = nav1.CreateNavigator();
                var anav2 = nav2.CreateNavigator();
                var attr1 = new List <string>();
                var attr2 = new List <string>();

                if (anav1.MoveToFirstAttribute())
                {
                    do
                    {
                        attr1.Add(string.Format("{0}=\"{1}\"", anav1.Name, anav1.Value));
                    } while (anav1.MoveToNextAttribute());
                }
                if (anav2.MoveToFirstAttribute())
                {
                    do
                    {
                        attr2.Add(string.Format("{0}=\"{1}\"", anav2.Name, anav2.Value));
                    } while (anav2.MoveToNextAttribute());
                }

                AssertUtil.ContainsExactly(attr2, attr1);

                var cnav1 = nav1.CreateNavigator();
                var cnav2 = nav2.CreateNavigator();
                if (cnav1.MoveToFirstChild())
                {
                    if (cnav2.MoveToFirstChild())
                    {
                        AreXPathNavigatorsEqual(cnav1, cnav2, message);
                    }
                    else
                    {
                        Assert.Fail("Expected element {0}.{1}", GetFullPath(cnav1), message);
                    }
                }
                else if (cnav2.MoveToFirstChild())
                {
                    Assert.Fail("Unexpected element {0}.{1}", GetFullPath(cnav2), message);
                }

                if (nav1.MoveToNext())
                {
                    if (nav2.MoveToNext())
                    {
                        continue;
                    }
                    else
                    {
                        Assert.Fail("Expected element {0}.{1}", GetFullPath(nav1), message);
                    }
                }
                else if (nav2.MoveToNext())
                {
                    Assert.Fail("Unexpected element {0}.{1}", GetFullPath(nav2), message);
                }
                break;
            }
        }
Esempio n. 27
0
        public void AssertDescriptionContains(IPythonProjectEntry module, string expr, int index, params string[] description)
        {
            var val = GetValue <AnalysisValue>(module, expr, index);

            AssertUtil.Contains(val?.Description, description);
        }
        public void SendUpdates()
        {
            Dictionary <string, AnalysisProgress> results = null;

            using (var ready = new AutoResetEvent(false))
                using (var listener = new AnalyzerStatusListener(r => { results = r; ready.Set(); })) {
                    ready.Reset();
                    listener.RequestUpdate();
                    ready.WaitOne();
                    Assert.IsNotNull(results);
                    if (results.Count > 0)
                    {
                        ready.Reset();
                        listener.RequestUpdate();
                        ready.WaitOne();
                        Assert.IsNotNull(results);
                        if (results.Count > 0)
                        {
                            Console.WriteLine("WARNING: {0} results received from a previous test", results.Count);
                            Console.WriteLine("Keys are:");
                            foreach (var key in results.Keys)
                            {
                                Console.WriteLine("    {0}", key);
                            }
                        }
                    }

                    using (var sender1 = new AnalyzerStatusUpdater("SendUpdates1"))
                        using (var sender2 = new AnalyzerStatusUpdater("SendUpdates2")) {
                            // Block until workers have started
                            sender1.WaitForWorkerStarted();
                            sender1.ThrowPendingExceptions();
                            sender2.WaitForWorkerStarted();
                            sender2.ThrowPendingExceptions();

                            ready.Reset();
                            listener.RequestUpdate();
                            ready.WaitOne();
                            Assert.IsNotNull(results);
                            AssertUtil.ContainsAtLeast(results.Keys, "SendUpdates1", "SendUpdates2");
                            Assert.AreEqual(int.MaxValue, results["SendUpdates1"].Progress, "SendUpdates1.Progress not initialized to MaxValue");
                            Assert.AreEqual(0, results["SendUpdates1"].Maximum, "SendUpdates1.Maximum not initialized to 0");
                            Assert.AreEqual(string.Empty, results["SendUpdates1"].Message, "SendUpdates1.Message not initialized to empty");
                            Assert.AreEqual(int.MaxValue, results["SendUpdates2"].Progress, "SendUpdates2.Progress not initialized to MaxValue");
                            Assert.AreEqual(0, results["SendUpdates2"].Maximum, "SendUpdates2.Maximum not initialized to 0");
                            Assert.AreEqual(string.Empty, results["SendUpdates2"].Message, "SendUpdates2.Message not initialized to empty");

                            sender1.UpdateStatus(100, 200, "Message1");
                            sender1.FlushQueue(TimeSpan.FromSeconds(1.0));

                            ready.Reset();
                            listener.RequestUpdate();
                            ready.WaitOne();
                            Assert.AreEqual(100, results["SendUpdates1"].Progress, "SendUpdates1.Progress not set to 100");
                            Assert.AreEqual(200, results["SendUpdates1"].Maximum, "SendUpdates1.Maximum not set to 200");
                            Assert.AreEqual("Message1", results["SendUpdates1"].Message, "SendUpdates1.Message not set");
                            Assert.AreEqual(int.MaxValue, results["SendUpdates2"].Progress, "SendUpdates2.Progress changed from MaxValue");
                            Assert.AreEqual(0, results["SendUpdates2"].Maximum, "SendUpdates2.Maximum changed from 0");
                            Assert.AreEqual(string.Empty, results["SendUpdates2"].Message, "SendUpdates2.Message changed from empty");

                            sender2.UpdateStatus(1000, 2000, "Message2");
                            sender2.FlushQueue(TimeSpan.FromSeconds(1.0));

                            ready.Reset();
                            listener.RequestUpdate();
                            ready.WaitOne();
                            Assert.AreEqual(100, results["SendUpdates1"].Progress, "SendUpdates1.Progress changed from 100");
                            Assert.AreEqual(200, results["SendUpdates1"].Maximum, "SendUpdates1.Maximum changed from 200");
                            Assert.AreEqual("Message1", results["SendUpdates1"].Message, "SendUpdates1.Message changed");
                            Assert.AreEqual(1000, results["SendUpdates2"].Progress, "SendUpdates2.Progress not set to 1000");
                            Assert.AreEqual(2000, results["SendUpdates2"].Maximum, "SendUpdates2.Maximum not set to 2000");
                            Assert.AreEqual("Message2", results["SendUpdates2"].Message, "SendUpdates2.Message not set");
                        }

                    Thread.Sleep(100); // allow updaters to terminate
                    ready.Reset();
                    listener.RequestUpdate();
                    ready.WaitOne();
                    Assert.IsNotNull(results);
                    if (results.Count > 0)
                    {
                        Console.WriteLine("WARNING: {0} results exist at end of test", results.Count);
                        Console.WriteLine("Keys are:");
                        foreach (var key in results.Keys)
                        {
                            Console.WriteLine("    {0}", key);
                        }
                    }
                    Assert.IsFalse(results.ContainsKey("SendUpdates1"), "results were not cleaned up");
                    Assert.IsFalse(results.ContainsKey("SendUpdates2"), "results were not cleaned up");
                }
        }
Esempio n. 29
0
 /// <summary>
 /// 检测用户是否登录
 /// </summary>
 private void CheckManageId()
 {
     AssertUtil.AreBigger(UserId, 0, LanguageUtil.Translate("api_Business_Tags_CheckManageId"));
 }
Esempio n. 30
0
        public void ReplSplitCodeTest()
        {
            // http://pytools.codeplex.com/workitem/606

            var testCases = new[] {
                new {
                    Code     = @"def f():
    pass

def g():
    pass

f()
g()",
                    Expected = new[] { "def f():\r\n    pass\r\n", "def g():\r\n    pass\r\n", "f()", "g()" }
                },
                new {
                    Code     = @"def f():
    pass

f()

def g():
    pass

f()
g()",
                    Expected = new[] { "def f():\r\n    pass\r\n", "f()", "def g():\r\n    pass\r\n", "f()", "g()" }
                },
                new {
                    Code     = @"def f():
    pass

f()
f()

def g():
    pass

f()
g()",
                    Expected = new[] { "def f():\r\n    pass\r\n", "f()", "f()", "def g():\r\n    pass\r\n", "f()", "g()" }
                },
                new {
                    Code     = @"    def f():
        pass

    f()
    f()

    def g():
        pass

    f()
    g()",
                    Expected = new[] { "def f():\r\n    pass\r\n", "f()", "f()", "def g():\r\n    pass\r\n", "f()", "g()" }
                }
            };

            using (var evaluator = MakeEvaluator()) {
                foreach (var testCase in testCases)
                {
                    AssertUtil.AreEqual(evaluator.JoinCode(evaluator.SplitCode(testCase.Code)), testCase.Expected);
                }
            }
        }