Beispiel #1
0
        public void Parse_PassingEmptyArray_ReturnsEmptyObject()
        {
            CustomT result = Parser.Parse("[]");

            Assert.Equal(CustomTDef.tObject, result.Type);
            Assert.Equal(0, result.Children.Count());
        }
Beispiel #2
0
        public void Parse_PassingSimmpleArray_ReturnsObject()
        {
            CustomT result = Parser.Parse("[Bob:s, Nancy:n]");

            Assert.Equal(CustomTDef.tObject, result.Type);
            Assert.Equal(2, result.Children.Count);
            Assert.True(result.Children.Any(c => c.Name == "Bob"));
        }
Beispiel #3
0
        public static CustomT Parse(string input, string name = null)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return(null);
            }
            else if (input == "n")
            {
                return new CustomT()
                       {
                           Name = name, Type = CustomTDef.number
                       }
            }
            ;
            else if (input == "s")
            {
                return new CustomT()
                       {
                           Name = name, Type = CustomTDef.text
                       }
            }
            ;
            else if (input == "b")
            {
                return new CustomT()
                       {
                           Name = name, Type = CustomTDef.boolean
                       }
            }
            ;
            else if (input.StartsWith("[") && input.EndsWith("]"))
            {
                input = input.Substring(1, input.Length - 2);

                string[] strArray = input.Split(new char[] { ',', ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);

                var custT = new CustomT()
                {
                    Type = CustomTDef.tObject, Children = new List <CustomT>()
                };
                for (int i = 0; i < strArray.Length; i++)
                {
                    string[] objArray = strArray[i].Split(new char[] { ':', ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);
                    if (objArray.Length == 1)
                    {
                        var subChild = new CustomT()
                        {
                            Type     = CustomTDef.tObject,
                            Name     = objArray[0],
                            Children = new List <CustomT>()
                        };

                        subChild.Children.Add(Parse(strArray[++i]));
                        custT.Children.Add(subChild);
                    }
                    else
                    {
                        custT.Children.Add(Parse(objArray[1], objArray[0]));
                    }
                }

                return(custT);
            }
            else
            {
                throw new FormatException();
            }
        }
Beispiel #4
0
        public void Parse_PassingN_ReturnsNumber()
        {
            CustomT result = Parser.Parse("n");

            Assert.Equal(CustomTDef.number, result.Type);
        }
Beispiel #5
0
        [Fact] void Parse_PassingEmptyString_ReturnsNull()
        {
            CustomT result = Parser.Parse("");

            Assert.Null(result);
        }
Beispiel #6
0
        public void Parse_PassingComplexArray_ReturnsObject()
        {
            CustomT result = Parser.Parse("[Name: [Location:s, Phone:n]]");

            Assert.Equal(CustomTDef.tObject, result.Type);
        }
Beispiel #7
0
        public void Parse_PassingS_ReturnsBoolean()
        {
            CustomT result = Parser.Parse("b");

            Assert.Equal(CustomTDef.boolean, result.Type);
        }
Beispiel #8
0
        public void Parse_PassingS_ReturnsText()
        {
            CustomT result = Parser.Parse("s");

            Assert.Equal(CustomTDef.text, result.Type);
        }