diff --git a/test/TestConsole/Program.cs b/test/TestConsole/Program.cs index 364cd9d4..1b5e2f31 100644 --- a/test/TestConsole/Program.cs +++ b/test/TestConsole/Program.cs @@ -18,7 +18,6 @@ using TestConsole.ObjectSizes; using TestConsole.PresentationModels; using TestConsole.Threading; -using TestConsole.Tokens; using TestConsole.DispatcherExceptions; using TestConsole.Asyncing; @@ -28,9 +27,6 @@ class Program { private static void Main(string[] args) { - //TestTokens.Test(); - //TestTokenWriter.Test(); - //TestEntity.Test(); //TestPresentationModels.Test(); //TestXmlModelDefinition.Test(); //TestFieldMetadataValidatorKey.Test(); diff --git a/test/TestConsole/Tokens/TestTokenWriter.cs b/test/TestConsole/Tokens/TestTokenWriter.cs deleted file mode 100644 index d950f76d..00000000 --- a/test/TestConsole/Tokens/TestTokenWriter.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Neptuo.Collections.Specialized; -using Neptuo.Text.Tokens; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TestConsole.Tokens -{ - static class TestTokenWriter - { - public static void Test() - { - TokenWriter writer = new TokenWriter("Hello, my name is {Name} and I am from {City}."); - string result = writer.Format(new KeyValueCollection().Add("Name", "Peter").Add("City", "Prague")); - Console.WriteLine(result); - } - } -} diff --git a/test/TestConsole/Tokens/TestTokens.cs b/test/TestConsole/Tokens/TestTokens.cs deleted file mode 100644 index e9529e15..00000000 --- a/test/TestConsole/Tokens/TestTokens.cs +++ /dev/null @@ -1,51 +0,0 @@ -using Neptuo.Text.Positions; -using Neptuo.Text.Tokens; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TestConsole.Tokens -{ - static class TestTokens - { - public static void Test() - { - //string template = "{ProductID ID=5, Converter={x:State NullToBool, AppendOnly}}x{DestinationID}"; - //string template = "{ProductID ID=5, Converter={x:State NullToBool, AppendOnly}}{DestinationID}"; - string template = @"asdkja sldkjas ldkajsd lkajsd -as -as{ProductID ID=5} askdh aksjdh alskdj asldkj alskdj."; - //string template = "http://localhost:60000/{Locale}/{DestinationID}/products/{ProductID}/info"; - - TokenParser parser = new TokenParser(); - parser.Configuration.AllowTextContent = true; - parser.Configuration.AllowMultipleTokens = true; - parser.Configuration.AllowAttributes = true; - parser.Configuration.AllowDefaultAttributes = true; - parser.Configuration.AllowEscapeSequence = false; - - parser.OnParsedToken += OnToken; - if (parser.Parse(template)) - Console.WriteLine("=> Parsed"); - else - Console.WriteLine("=> Not parsed"); - } - - static void OnToken(object sender, TokenEventArgs e) - { - Console.WriteLine("Starting at {0}, to {1}, source.substring: {2}", e.StartPosition, e.EndPosition, e.OriginalContent.Substring(e.StartPosition, e.EndPosition - e.StartPosition + 1)); - Console.WriteLine(e.Token.Fullname); - PrintLineInfo(e.Token); - Console.WriteLine(String.Join(";", e.Token.DefaultAttributes)); - Console.WriteLine(String.Join(";", e.Token.Attributes.Select(a => String.Format("{0}:{1}", a.Name, a.Value)))); - Console.WriteLine("---"); - } - - static void PrintLineInfo(IDocumentSpan lineInfo) - { - Console.WriteLine("{0}:{1} -> {2}:{3}", lineInfo.LineIndex, lineInfo.ColumnIndex, lineInfo.EndLineIndex, lineInfo.EndColumnIndex); - } - } -} diff --git a/test/UnitTest/Text/Tokens/TestTokenParser.cs b/test/UnitTest/Text/Tokens/TestTokenParser.cs new file mode 100644 index 00000000..858a5fc5 --- /dev/null +++ b/test/UnitTest/Text/Tokens/TestTokenParser.cs @@ -0,0 +1,89 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neptuo.Text.Positions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Neptuo.Text.Tokens +{ + [TestClass] + public class TestTokenParser + { + [TestMethod] + public void WithParameter_Single() + { + ParseTemplate("{ProductID ID=5}", true); + } + + [TestMethod] + public void WithParameter_Inner() + { + ParseTemplate("{ProductID ID=5, Converter={x:State NullToBool, AppendOnly}}", true); + } + + [TestMethod] + public void WithParameter_Multi() + { + ParseTemplate("{ProductID ID=5}{DestinationID}", true); + } + + [TestMethod] + public void WithParameter_Multi_WithSeparator() + { + ParseTemplate("{ProductID ID=5}xx{DestinationID}", true); + } + + [TestMethod] + public void SuperComplex() + { + ParseTemplate("{ProductID ID=5, Converter={x:State NullToBool, AppendOnly}}x{DestinationID}", true); + } + + [TestMethod] + public void MultiLine() + { + string template = @"asdkja sldkjas ldkajsd lkajsd +as +as{ProductID ID=5} askdh aksjdh alskdj asldkj alskdj."; + + ParseTemplate(template, true); + } + + [TestMethod] + public void Url() + { + ParseTemplate("http://localhost:60000/{Locale}/{DestinationID}/products/{ProductID}/info", true); + } + + private static void ParseTemplate(string template, bool result) + { + TokenParser parser = new TokenParser(); + parser.Configuration.AllowTextContent = true; + parser.Configuration.AllowMultipleTokens = true; + parser.Configuration.AllowAttributes = true; + parser.Configuration.AllowDefaultAttributes = true; + parser.Configuration.AllowEscapeSequence = false; + + parser.OnParsedToken += OnToken; + + Assert.AreEqual(result, parser.Parse(template)); + } + + static void OnToken(object sender, TokenEventArgs e) + { + Console.WriteLine("Starting at {0}, to {1}, source.substring: {2}", e.StartPosition, e.EndPosition, e.OriginalContent.Substring(e.StartPosition, e.EndPosition - e.StartPosition)); + Console.WriteLine(e.Token.Fullname); + PrintLineInfo(e.Token); + Console.WriteLine(String.Join(";", e.Token.DefaultAttributes)); + Console.WriteLine(String.Join(";", e.Token.Attributes.Select(a => String.Format("{0}:{1}", a.Name, a.Value)))); + Console.WriteLine("---"); + } + + static void PrintLineInfo(IDocumentSpan lineInfo) + { + Console.WriteLine("{0}:{1} -> {2}:{3}", lineInfo.LineIndex, lineInfo.ColumnIndex, lineInfo.EndLineIndex, lineInfo.EndColumnIndex); + } + } +}