Example #1
0
        public static void Initialise()
        {
            if (Prefixes != null)
            {
                throw new Exception("Do not initialise more than once!");
            }

            var commands = Assembly.GetExecutingAssembly().GetTypes()
                           .SelectMany(t => t.GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
                           .Where(m => m.GetCustomAttributes(typeof(CommandAttribute), false).Any())
                           .Select(m => new
            {
                Attribute = (CommandAttribute)m.GetCustomAttributes(typeof(CommandAttribute), false).First(),
                Action    = (Action <Logger, CommandParameters>)Delegate.CreateDelegate(typeof(Action <Logger, CommandParameters>), m)
            })
#if !DEBUG
                           .Where(c => !(c.Attribute is DebugCommandAttribute))
#endif
                           .ToList();

            dictionary = new Dictionary <string, Command>(commands.Count);
            dictionary.AddRange(commands.Select(
                                    c => new KeyValuePair <string, Command>(c.Attribute.Name, new Command(c.Action, c.Attribute))));

            Prefixes = new PrefixTrie(commands.Select(c => c.Attribute.Name));

            AddParameterCompletion("allAvailableCommands", commands.Select(c => c.Attribute.Name));
        }
Example #2
0
        public void TestIgnoreDouble()
        {
            var trie = new PrefixTrie(
                new[] { "one", "two", "three", "two" }
                );

            trie.Should().HaveCount(3);
        }
Example #3
0
        public void TestExtendPrefix_InvalidPrefix()
        {
            var trie = new PrefixTrie(
                new[] { "one", "two", "three" }
                );

            trie.ExtendPrefix("a").Should().BeNull();
            trie.ExtendPrefix("twofold").Should().BeNull();
        }
Example #4
0
        public void TestAllKeys_InvalidPrefix()
        {
            var words = new[] { "one", "two", "three", "threshing" };

            var trie = new PrefixTrie(words);

            var withPrefixEmptyString = trie.AllKeys("twofold").ToList();

            withPrefixEmptyString.Should().BeEmpty();
        }
Example #5
0
        public void TestAllKeys_EmptyPrefix()
        {
            var words = new[] { "one", "two", "three", "threshing" };

            var trie = new PrefixTrie(words);

            var withPrefixEmptyString = trie.AllKeys("").ToList();

            withPrefixEmptyString.Should().BeEquivalentTo(words);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PrefixLookup"/> class.
        /// </summary>
        /// <param name="prefixList">The dictionary of lookup prefixes.</param>
        /// <param name="caseSensitive">Ttrue if the lookup should be case sensitive.</param>
        public PrefixLookup(IDictionary <string, string> prefixList, bool caseSensitive)
        {
            // Translate the map into a different structure.
            this.prefixPrefixTrie = new PrefixTrie(caseSensitive);

            foreach (var item in prefixList)
            {
                this.prefixPrefixTrie.Add(item.Key, item.Value);
            }
        }
Example #7
0
        public void Tests()
        {
            var trie = new PrefixTrie();

            trie.Insert("apple");
            Assert.True(trie.Search("apple"));   // return True
            Assert.False(trie.Search("app"));    // return False
            Assert.True(trie.StartsWith("app")); // return True
            trie.Insert("app");
            Assert.True(trie.Search("app"));     // return True
        }
Example #8
0
        public void TestContains_DoesNotContainPrefixes()
        {
            var trie = new PrefixTrie(
                new[] { "one", "two", "three" }
                );

            foreach (var notContained in new[] { "", "t", "tw" })
            {
                trie.Contains(notContained).Should().BeFalse();
            }
        }
Example #9
0
        public void TestExtendPrefix()
        {
            var trie = new PrefixTrie(
                new[] { "one", "two", "three", "threshing" }
                );

            trie.ExtendPrefix("o").Should().Be("one");
            trie.ExtendPrefix("one").Should().Be("one");

            trie.ExtendPrefix("t").Should().Be("t");
            trie.ExtendPrefix("th").Should().Be("thre");
        }
Example #10
0
        public void TestCount()
        {
            var trie = new PrefixTrie(
                new[] { "one", "two", "three" }
                );

            trie.Count.Should().Be(3);

            trie = new PrefixTrie(
                new string[0]
                );
            trie.Count.Should().Be(0);
        }
Example #11
0
        public void TestAllKeys()
        {
            var words = new[] { "one", "two", "three", "threshing" };

            var trie = new PrefixTrie(words);

            var withPrefixO = trie.AllKeys("o").ToList();

            withPrefixO.Should().BeEquivalentTo(new[] { "one" });

            var withPrefixTh = trie.AllKeys("th").ToList();

            withPrefixTh.Should().BeEquivalentTo(words.Where(w => w.StartsWith("th")));
        }
Example #12
0
        public void TestContains()
        {
            var trie = new PrefixTrie(
                new[] { "one", "two", "three" }
                );

            foreach (var contained in new[] { "one", "two", "three" })
            {
                trie.Contains(contained).Should().BeTrue();
            }

            foreach (var notContained in new[] { "four", "threeAndSome" })
            {
                trie.Contains(notContained).Should().BeFalse();
            }
        }
Example #13
0
        public void TestGetEnumerator()
        {
            var words = new[] { "one", "two", "three", "threshing" };

            var trie = new PrefixTrie(words);

            var list = new List <string>(words.Length);

            using (var enumerator = trie.GetEnumerator())
            {
                for (int i = 0; i < words.Length; i++)
                {
                    enumerator.MoveNext().Should().BeTrue();
                    list.Add(enumerator.Current);
                }

                enumerator.MoveNext().Should().BeFalse();
            }

            list.Should().Contain(words);
        }