Exemplo n.º 1
0
        public void TestGetResult_2()
        {
            var list = new List <TreeDto> {
                GetNode1(), GetNode2()
            };
            var result = new TreeResult(list).GetResult();

            //根节点
            Assert.Single(result.Nodes);
            var root = result.Nodes.First();

            Assert.Equal(Id, root.Key);
            Assert.Equal(Title, root.Title);
            Assert.True(root.Disabled);
            Assert.True(root.Expanded);
            Assert.True(root.Checked);
            Assert.True(root.DisableCheckbox);
            Assert.False(root.Selectable);
            Assert.True(root.Selected);
            Assert.False(root.IsLeaf);

            //子节点
            Assert.Single(root.Children);
            var child = root.Children[0];

            Assert.Equal(Id2, child.Key);
            Assert.Equal(Title2, child.Title);
            Assert.True(child.Disabled);
            Assert.True(child.Expanded);
            Assert.True(child.Checked);
            Assert.True(child.DisableCheckbox);
            Assert.False(child.Selectable);
            Assert.True(child.Selected);
            Assert.True(child.IsLeaf);
        }
Exemplo n.º 2
0
        public void TestGetResult_4()
        {
            var list = new List <TreeDto> {
                GetNode1(), GetNode2(), GetNode3(), GetNode4()
            };
            var result = new TreeResult(list).GetResult();

            //根节点
            Assert.Equal(2, result.Nodes.Count);
            var root = result.Nodes.First();

            Assert.Equal(Id, root.Key);
            Assert.False(root.IsLeaf);

            //根节点2
            var root2 = result.Nodes[1];

            Assert.Equal(Id3, root2.Key);
            Assert.True(root2.IsLeaf);

            //子节点
            Assert.Equal(2, root.Children.Count);
            var child = root.Children[0];

            Assert.Equal(Id2, child.Key);
            Assert.True(child.IsLeaf);

            //子节点2
            Assert.Equal(2, root.Children.Count);
            var child2 = root.Children[1];

            Assert.Equal(Id4, child2.Key);
            Assert.True(child2.IsLeaf);
        }
Exemplo n.º 3
0
        public async Task <ActionResult> GetAsync([FromRoute] string path)
        {
            var allParams      = PartitionByKey(HttpContext.Request.Query.ToDictionary(x => x.Key, x => x.Value), x => x.StartsWith("$"));
            var modifiers      = allParams.Item1;
            var isFlatten      = modifiers.TryGetValue("$flatten").Select(x => bool.Parse(x.First())).IfNone(false);
            var includeErrors  = modifiers.TryGetValue("$includeErrors").Select(x => bool.Parse(x.First())).IfNone(false);
            var ignoreKeyTypes = modifiers.TryGetValue("$ignoreKeyTypes").Select(x => bool.Parse(x.First())).IfNone(false);
            var includePaths   = modifiers.TryGetValue("$include").Select(x => x.ToArray()).IfNone(new string[] {});

            var translateValue = ignoreKeyTypes ? (TranslateValue)TranslateValueToString : (x => x.Value);

            IReadOnlyDictionary <string, JsonValue> contextParams = allParams.Item2.ToDictionary(x => x.Key,
                                                                                                 x => x.Value.Count == 1 ? JsonValue.NewString(x.Value.ToString()) : JsonValue.NewArray(x.Value.Map(t => JsonValue.NewString(t)).ToArray()), StringComparer.OrdinalIgnoreCase);

            var identities = new IdentityHashSet(contextParams.Where(x => !x.Key.Contains(".")).Select(x => new Identity(x.Key, x.Value.AsString())));

            if (!_checkAccess(User, path, identities))
            {
                return(Forbid());
            }
            GetLoadedContextByIdentityType contextProps =
                identityType => key => contextParams.TryGetValue($"{identityType}.{key}");

            var root = ConfigurationPath.New(path);

            var query = GetQuery(root, includePaths);

            var values = await _tweek.GetContextAndCalculate(query, identities, _contextDriver, contextProps);

            var errors = values.Where(x => x.Value.Exception != null).ToDictionary(x => x.Key, x => x.Value.Exception.Message);

            Response.Headers.Add("X-Error-Count", errors.Count.ToString());

            object result = null;

            if (root.IsScan)
            {
                var relativeData = values.Where(x => x.Value.Exception == null).ToDictionary(x => x.Key.ToRelative(root), x => x.Value);
                result = !isFlatten
                    ? TreeResult.From(relativeData, translateValue)
                    : relativeData.ToDictionary(x => x.Key.ToString(), x => translateValue(x.Value));
            }
            else if (values.TryGetValue(root, out var value) && value.Exception == null)
            {
                result = ignoreKeyTypes ? TranslateValueToString(value) : value.Value;
            }

            if (!includeErrors)
            {
                return(Json(result));
            }


            return(Json(new Dictionary <string, object> {
                { "data", result }, { "errors", errors }
            }));
        }
Exemplo n.º 4
0
        public void Test_1Node()
        {
            var node = new TreeNode {
                Id = "1", Text = "a", Attributes = new { url = "b" }
            };

            _result = new TreeResult(new[] { node });
            Assert.AreEqual("[{\"id\":\"1\",\"text\":\"a\",\"attributes\":{\"url\":\"b\"}}]", _result.ToString());
        }
Exemplo n.º 5
0
        public void Test_Async_State()
        {
            var node = new TreeNode {
                Id = "1", Text = "a"
            };

            _result = new TreeResult(new[] { node }, true);
            Assert.AreEqual("[{\"id\":\"1\",\"text\":\"a\",\"state\":\"closed\"}]", _result.ToString());
        }
Exemplo n.º 6
0
        public IActionResult GetTreeNodes(QueryParameter parameter)
        {
            var list = new List <TreeDto> {
                GetNode1(), GetNode2(), GetNode3(), GetNode4(), GetNode5()
            };
            var result = new TreeResult(list).GetResult();

            return(Success(result));
        }
Exemplo n.º 7
0
        public void Test_2Node()
        {
            var node = new TreeNode {
                Id = "1", Text = "a"
            };
            var node2 = new TreeNode {
                Id = "2", Text = "b"
            };

            _result = new TreeResult(new[] { node, node2 });
            Assert.AreEqual("[{\"id\":\"1\",\"text\":\"a\"},{\"id\":\"2\",\"text\":\"b\"}]", _result.ToString());
        }
Exemplo n.º 8
0
        public void TestSelectedKeys()
        {
            var node2 = GetNode2();

            node2.Selected = false;
            var list = new List <TreeDto> {
                GetNode1(), node2, GetNode3(), GetNode4(), GetNode5()
            };
            var result = new TreeResult(list).GetResult();

            Assert.Equal(Id, result.SelectedKeys[0]);
            Assert.Equal(Id3, result.SelectedKeys[1]);
            Assert.Equal(Id4, result.SelectedKeys[2]);
        }
Exemplo n.º 9
0
        public AggregatePingTracker(PingConfiguration config, GameAddressDetector addressDetector, GameNetwork network) : base(config, addressDetector, PingTrackerKind.Aggregate)
        {
            // Define trackers
            this.trackerInfos = new Dictionary <string, TrackerInfo>();

            RegisterTracker(COMTrackerKey, new ComponentModelPingTracker(config, addressDetector)
            {
                Verbose = false
            });
            RegisterTracker(IpHlpApiTrackerKey, new IpHlpApiPingTracker(config, addressDetector)
            {
                Verbose = false
            });
            RegisterTracker(PacketTrackerKey, new PacketPingTracker(config, addressDetector, network)
            {
                Verbose = false
            });

            // Create decision tree to solve tracker selection problem
            this.decisionTree = new DecisionTree <string>(
                // If COM is errored
                () => TrackerIsErrored(COMTrackerKey),
                // Just use IpHlpApi
                pass: new DecisionTree <string>(() => TreeResult.Resolve(IpHlpApiTrackerKey)),
                fail: new DecisionTree <string>(
                    // If difference between pings is more than 30
                    () => Math.Abs((long)GetTrackerRTT(COMTrackerKey) - (long)GetTrackerRTT(IpHlpApiTrackerKey)) > 30,
                    pass: new DecisionTree <string>(
                        // Use greater ping value, something's probably subtly broken
                        () => GetTrackerRTT(COMTrackerKey) < GetTrackerRTT(IpHlpApiTrackerKey),
                        pass: new DecisionTree <string>(() => TreeResult.Resolve(IpHlpApiTrackerKey)),
                        fail: new DecisionTree <string>(() => TreeResult.Resolve(COMTrackerKey))
                        ),
                    fail: new DecisionTree <string>(
                        // If both of these trackers report a ping of 0
                        () => GetTrackerRTT(COMTrackerKey) == 0 && GetTrackerRTT(IpHlpApiTrackerKey) == 0,
                        // Just use packets (inaccurate)
                        pass: new DecisionTree <string>(() => TreeResult.Resolve(PacketTrackerKey)),
                        fail: new DecisionTree <string>(
                            // Otherwise use the lower ping value, we'll assume it's more accurate
                            () => GetTrackerRTT(COMTrackerKey) < GetTrackerRTT(IpHlpApiTrackerKey),
                            pass: new DecisionTree <string>(() => TreeResult.Resolve(COMTrackerKey)),
                            fail: new DecisionTree <string>(() => TreeResult.Resolve(IpHlpApiTrackerKey))
                            )
                        )
                    )
                );
        }
Exemplo n.º 10
0
        public void Test_2Node_Children()
        {
            var node = new TreeNode {
                Id = "1", Text = "a", Level = 1
            };
            var node2 = new TreeNode {
                Id = "2", ParentId = "1", Text = "b", Level = 2
            };

            _result = new TreeResult(new[] { node, node2 });
            var expected = new Str();

            expected.Add("[{\"id\":\"1\",\"text\":\"a\",");
            expected.Add("\"children\":[{\"id\":\"2\",\"ParentId\":\"1\",\"text\":\"b\"}]");
            expected.Add("}]");
            Assert.AreEqual(expected.ToString(), _result.ToString());
        }
Exemplo n.º 11
0
        private static async Task <List <TreeResult> > RewriteSourcesAsync(IEnumerable <Document> documents)
        {
            var results = new List <TreeResult>();

            foreach (var document in documents)
            {
                var semanticModel = await document.GetSemanticModelAsync();

                var tree = await document.GetSyntaxTreeAsync();

                var rw         = new NUnitToMSTestRewriter(semanticModel);
                var result     = rw.Visit(tree.GetRoot());
                var treeResult = new TreeResult(tree.FilePath, tree.Encoding, result, rw.Diagnostics, rw.Changed);
                results.Add(treeResult);
            }

            return(results);
        }
Exemplo n.º 12
0
        private static List <TreeResult> RewriteSources(Compilation compilation)
        {
            var resultsLock = new object();
            var results     = new List <TreeResult>();

            var p = Parallel.ForEach(
                compilation.SyntaxTrees,
                () => new List <TreeResult>(),
                (tree, state, local) =>
            {
                if (!state.ShouldExitCurrentIteration)
                {
                    try
                    {
                        var semanticModel = compilation.GetSemanticModel(tree);
                        var rw            = new NUnitToMSTestRewriter(semanticModel);
                        var result        = rw.Visit(tree.GetRoot());
                        var treeResult    = new TreeResult(tree.FilePath, tree.Encoding, result, rw.Diagnostics, rw.Changed);
                        local.Add(treeResult);
                    }
                    catch (Exception ex)
                    {
                        Error(ex.ToString());
                        state.Stop();
                    }
                }

                return(local);
            },
                local =>
            {
                lock (resultsLock)
                {
                    results.AddRange(local);
                }
            });

            if (DumpDiagnostics(results) || !p.IsCompleted)
            {
                return(null);
            }

            return(results);
        }
        public async Task <TreeResult> GetWholeTree(int termClassId, string term, string cc, string sr, int deepLevel)
        {
            var termTreeModel = await GetCcTermTree(termClassId, term, cc, sr, deepLevel);

            var treeItems = termTreeModel.Select(t => new TreeItem
            {
                Id         = t.TermClassId.ToString(),
                Pid        = t.PId.ToString(),
                Term       = t.Term,
                PathTerm   = t.PathTerm,
                Source     = t.Source,
                OrderIndex = t.OrderIndex
            }).ToList();
            var results = new TreeResult {
                TreeItems = treeItems
            };

            return(results);
        }
Exemplo n.º 14
0
        public void TestGetResult_1()
        {
            var list = new List <TreeDto> {
                GetNode1()
            };
            var result = new TreeResult(list).GetResult();

            Assert.Single(result.Nodes);
            var node = result.Nodes.First();

            Assert.Equal(Id, node.Key);
            Assert.Equal(Title, node.Title);
            Assert.True(node.Disabled, "node.IsDisabled");
            Assert.True(node.Expanded, "node.IsExpanded");
            Assert.True(node.Checked, "node.IsChecked");
            Assert.True(node.DisableCheckbox, "node.IsDisableCheckbox");
            Assert.False(node.Selectable, "node.IsSelectable");
            Assert.True(node.Selected, "node.IsSelected");
            Assert.True(node.IsLeaf, "node.IsLeaf");
        }
Exemplo n.º 15
0
        public void Test_3Node_Children()
        {
            var node = new TreeNode {
                Id = "1"
            };
            var node2 = new TreeNode {
                Id = "2", ParentId = "1"
            };
            var node3 = new TreeNode {
                Id = "3", ParentId = "2"
            };

            _result = new TreeResult(new[] { node2, node3, node });
            var expected = new Str();

            expected.Add("[{\"id\":\"1\",\"children\":[");
            expected.Add("{\"id\":\"2\",\"ParentId\":\"1\",\"children\":[");
            expected.Add("{\"id\":\"3\",\"ParentId\":\"2\"}]");
            expected.Add("}]}]");
            Assert.AreEqual(expected.ToString(), _result.ToString());
        }
Exemplo n.º 16
0
        public TreeResult <MenuView> GetMenuTreeGrid()
        {
            var menus     = base.Set();
            var queryable = menus.Where(o => o.ParentId > 0)
                            .Select(m => new MenuView
            {
                Id          = m.Id,
                Description = m.Description,
                Name        = m.Name,
                Url         = m.Url,
                Sort        = m.Sort,
                State       = m.State,
                MenuLevel   = m.MenuLevel,
                ParentId    = m.ParentId,
                SourcePath  = m.SourcePath,
                _parentId   = m.ParentId
            }).Union(menus.Where(o => o.ParentId == 0)
                     .Select(m => new MenuView
            {
                Id          = m.Id,
                Description = m.Description,
                Name        = m.Name,
                Url         = m.Url,
                Sort        = m.Sort,
                State       = m.State,
                MenuLevel   = m.MenuLevel,
                ParentId    = m.ParentId,
                SourcePath  = m.SourcePath,
                _parentId   = null
            }));
            TreeResult <MenuView> treeResult = new TreeResult <MenuView>()
            {
                total = menus.Count(),
                rows  = queryable
            };

            return(treeResult);
        }
Exemplo n.º 17
0
        public async Task <ActionResult> GetAsync([FromRoute] string path)
        {
            var allParams      = PartitionByKey(HttpContext.Request.Query.ToDictionary(x => x.Key, x => x.Value), x => x.StartsWith("$"));
            var modifiers      = allParams.Item1;
            var isFlatten      = modifiers.TryGetValue("$flatten").Select(x => bool.Parse(x.First())).IfNone(false);
            var ignoreKeyTypes = modifiers.TryGetValue("$ignoreKeyTypes").Select(x => bool.Parse(x.First())).IfNone(false);
            var includePaths   = modifiers.TryGetValue("$include").Select(x => x.ToArray()).IfNone(new string[] {});

            var translateValue = ignoreKeyTypes ? (TranslateValue)TranslateValueToString : (x => x.Value);

            IReadOnlyDictionary <string, JsonValue> contextParams = allParams.Item2.ToDictionary(x => x.Key,
                                                                                                 x => JsonValue.NewString(x.Value.ToString()), StringComparer.OrdinalIgnoreCase);

            var identities = new IdentityHashSet(contextParams.Where(x => !x.Key.Contains(".")).Select(x => new Identity(x.Key, x.Value.AsString())));

            if (!_checkAccess(User, path, identities))
            {
                return(Forbid());
            }
            GetLoadedContextByIdentityType contextProps =
                identityType => key => contextParams.TryGetValue($"{identityType}.{key}");

            var root = ConfigurationPath.New(path);

            var query = GetQuery(root, includePaths);

            var data = await _tweek.GetContextAndCalculate(query, identities, _contextDriver, contextProps);

            if (root.IsScan)
            {
                var relativeData = data.ToDictionary(x => x.Key.ToRelative(root), x => x.Value);
                return(Json(!isFlatten ? (TreeResult.From(relativeData, translateValue)) : relativeData.ToDictionary(x => x.Key.ToString(), x => translateValue(x.Value))));
            }

            return(data.Select(x => ignoreKeyTypes ? (object)x.Value.Value.AsString() : x.Value.Value)
                   .FirstOrNone()
                   .Match(x => Json(x), () => Json(null)));
        }
Exemplo n.º 18
0
 public void TestNull()
 {
     _result = new TreeResult(null);
     Assert.AreEqual("[]", _result.ToString());
 }
Exemplo n.º 19
0
 public static TreeResult <T> Resolve <T>(T result)
 {
     return(TreeResult <T> .Resolve(result));
 }