public void Match_TestSimpleObjectWithList()
        {
            var regex = @"^(?<Id>\d+)"
                        + @",(?<Name>.+)"
                        + @",(?<Value>\d+(\.\d+)?)"
                        + @",(?<Enabled>(true|1|false|0))"
                        + @"(,(?<ExternalIds>\d+))*"
                        + @"$";
            var mapper   = new RegexMap <TestSimpleModel>(regex);
            var value    = "123,Bonjour,3.1415,true,4,5,6";
            var expected = new TestSimpleModel
            {
                Id          = 123,
                Name        = "Bonjour",
                Value       = 3.1415m,
                Enabled     = true,
                ExternalIds = new List <int>
                {
                    4, 5, 6
                }
            };
            var result = mapper.Match(value);

            expected.ShouldDeepEqual(result);
        }
        public void Matches_WithGroupNamesDefinedInRegex()
        {
            var mapper = new RegexMap <TestModel>(@"{Id:(?<Id>\d*),Name:""(?<Name>[^""]*)""}");

            var result =
                mapper.Matches(
                    @"{Id:1,Name:""Test1""},{Id:12,Name:""Test12""},{Id:123,Name:""Test123""},{Id:1234,Name:""Test1234""}");

            Assert.AreEqual(
                new[]
            {
                new TestModel {
                    Id = 1, Name = "Test1"
                },
                new TestModel {
                    Id = 12, Name = "Test12"
                },
                new TestModel {
                    Id = 123, Name = "Test123"
                },
                new TestModel {
                    Id = 1234, Name = "Test1234"
                }
            },
                result);
        }
Example #3
0
        public Day20()
        {
            RegexMap regex     = new RegexMap(File.ReadAllText("Inputs/Day20.txt"));
            int      nextIndex = 0;
            Dictionary <IntVec2, int> nodes = new Dictionary <IntVec2, int>();
            List <HashSet <int> >     graph = new List <HashSet <int> >();

            int GetNodeIndex(IntVec2 p)
            {
                if (!nodes.TryGetValue(p, out int index))
                {
                    nodes.Add(p, index = nextIndex++);
                }
                return(index);
            }

            void AddEdge(IntVec2 source, IntVec2 sink)
            {
                int sourceIndex = GetNodeIndex(source);
                int sinkIndex   = GetNodeIndex(sink);

                int maxIndex = Math.Max(sourceIndex, sinkIndex);

                while (graph.Count <= maxIndex)
                {
                    graph.Add(new HashSet <int>());
                }

                graph[sourceIndex].Add(sinkIndex);
                graph[sinkIndex].Add(sourceIndex);
            }

            IntVec2 origin = IntVec2.Zero;

            regex.Traverse(origin, (current, directions) =>
            {
                foreach (char c in directions)
                {
                    IntVec2 newCoord = current + c switch
                    {
                        'N' => IntVec2.UnitY,
                        'S' => - IntVec2.UnitY,
                        'W' => - IntVec2.UnitX,
                        'E' => IntVec2.UnitX,
                        _ => throw new InvalidOperationException()
                    };

                    AddEdge(current, newCoord);

                    current = newCoord;
                }
        public void Match_TestSimpleObject()
        {
            var mapper =
                new RegexMap <TestSimpleModel>(
                    @"^(?<Id>\d+),(?<Name>.+),(?<Value>\d+(\.\d+)?),(?<Enabled>(true|false))$");
            var value    = "123,Bonjour,3.1415,true";
            var expected = new TestSimpleModel
            {
                Id          = 123,
                Name        = "Bonjour",
                Value       = 3.1415m,
                Enabled     = true,
                ExternalIds = null,
            };
            var result = mapper.Match(value);

            expected.ShouldDeepEqual(result);
        }
        public void CanMapToGoogle(string mqttSearch, string mqttReplace, string googleSearch, string googleReplace, string value, string expectedResult)
        {
            // Arrange
            var mapper = new RegexMap
            {
                MqttSearch    = mqttSearch,
                MqttReplace   = mqttReplace,
                GoogleSearch  = googleSearch,
                GoogleReplace = googleReplace
            };

            // Act
            var matches = mapper.MatchesMqtt(value);
            var result  = mapper.ConvertToGoogle(value);

            // Assert
            Assert.True(matches);
            Assert.Equal(expectedResult, result);
        }
Example #6
0
        /// <summary>
        /// Analyzes the specified logical expression and extracts the array of tokens.
        /// </summary>
        /// <param name="expression">The logical expression.</param>
        /// <returns>
        /// Array of extracted tokens.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">expression;Expression not provided.</exception>
        public IEnumerable <Token> Analyze(string expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression", "Expression not provided.");
            }

            Expression       = expression;
            CompiledRegexMap = RegexMap.ToDictionary(kvp => kvp.Key, kvp => new Regex(string.Format("^{0}", kvp.Value), RegexOptions.Compiled));

            var tokens = new List <Token>();

            while (Next())
            {
                tokens.Add(Token);
            }

            // once we've reached the end of the string, EOF token is returned - thus, parser's lookahead does not have to worry about running out of tokens
            tokens.Add(new Token(TokenType.EOF, string.Empty));

            return(tokens);
        }