From 135b0a30da2efeed6f4127bae62b9b410bd4446f Mon Sep 17 00:00:00 2001 From: azyobuzin Date: Tue, 2 Jun 2015 21:13:09 +0900 Subject: [PATCH] Solve errors of test --- HyperTomlProcessor.Test.Cmd/Program.cs | 2 +- .../DeserializeXElementTest.cs | 91 ++++++++++++++++--- HyperTomlProcessor.Test/DynamicTomlTest.cs | 6 +- HyperTomlProcessor.Test/Examples.cs | 4 +- HyperTomlProcessor/TomlParser.cs | 36 ++++---- HyperTomlProcessor/TomlWriter.cs | 2 +- 6 files changed, 102 insertions(+), 39 deletions(-) diff --git a/HyperTomlProcessor.Test.Cmd/Program.cs b/HyperTomlProcessor.Test.Cmd/Program.cs index a939982..473c789 100644 --- a/HyperTomlProcessor.Test.Cmd/Program.cs +++ b/HyperTomlProcessor.Test.Cmd/Program.cs @@ -10,7 +10,7 @@ class Program { static void Main(string[] args) { - Console.WriteLine(Toml.SerializeObject(TestObject.Create())); + Console.WriteLine(Toml.V04.SerializeObject(TestObject.Create())); Console.ReadKey(); } } diff --git a/HyperTomlProcessor.Test/DeserializeXElementTest.cs b/HyperTomlProcessor.Test/DeserializeXElementTest.cs index 524aa46..564e8b4 100644 --- a/HyperTomlProcessor.Test/DeserializeXElementTest.cs +++ b/HyperTomlProcessor.Test/DeserializeXElementTest.cs @@ -8,39 +8,104 @@ public class DeserializeXElementTest { [TestMethod] [ExpectedException(typeof(FormatException))] - public void Error0() + public void Error0V03() { - TomlConvert.DeserializeXElement(Examples.Error0); + Toml.V03.DeserializeXElement(Examples.Error0); } [TestMethod] [ExpectedException(typeof(FormatException))] - public void Error1() + public void Error0V04() { - TomlConvert.DeserializeXElement(Examples.Error1); + Toml.V04.DeserializeXElement(Examples.Error0); } [TestMethod] [ExpectedException(typeof(FormatException))] - public void Error2() + public void Error1V03() { - TomlConvert.DeserializeXElement(Examples.Error2); + Toml.V03.DeserializeXElement(Examples.Error1); } [TestMethod] [ExpectedException(typeof(FormatException))] - public void Error3() + public void Error1V04() { - TomlConvert.DeserializeXElement(Examples.Error3); + Toml.V04.DeserializeXElement(Examples.Error1); } [TestMethod] - public void StartsWith() + [ExpectedException(typeof(FormatException))] + public void Error2V03() + { + Toml.V03.DeserializeXElement(Examples.Error2); + } + + [TestMethod] + [ExpectedException(typeof(FormatException))] + public void Error2V04() + { + Toml.V04.DeserializeXElement(Examples.Error2); + } + + [TestMethod] + [ExpectedException(typeof(FormatException))] + public void Error3V03() + { + Toml.V03.DeserializeXElement(Examples.Error3); + } + + [TestMethod] + [ExpectedException(typeof(FormatException))] + public void Error3V04() + { + Toml.V04.DeserializeXElement(Examples.Error3); + } + + [TestMethod] + public void StartsWithV03() + { + var toml = Toml.V03; + toml.DeserializeXElement("#comment\n[TestTable]"); + toml.DeserializeXElement("Test = 1\n[TestTable]"); + toml.DeserializeXElement("[[Test]]\n[[Test]]\n[TestTable]"); + toml.DeserializeXElement("[TestTable]\nTest = 1"); + } + + [TestMethod] + public void StartsWithV04() + { + var toml = Toml.V04; + toml.DeserializeXElement("#comment\n[TestTable]"); + toml.DeserializeXElement("Test = 1\n[TestTable]"); + toml.DeserializeXElement("[[Test]]\n[[Test]]\n[TestTable]"); + toml.DeserializeXElement("[TestTable]\nTest = 1"); + } + + [TestMethod] + public void NumberWithUnderscores() + { + var dt = DynamicToml.Parse(Toml.V04, "a = 1_2_3_4_5\nb = 9_224_617.445_991\nc = 1e1_00"); + Assert.AreEqual(12345L, dt.a); + Assert.AreEqual(9224617.445991, dt.b); + Assert.AreEqual(1e+100, dt.c); + } + + [TestMethod] + public void InlineTable() { - TomlConvert.DeserializeXElement("#comment\n[TestTable]"); - TomlConvert.DeserializeXElement("Test = 1\n[TestTable]"); - TomlConvert.DeserializeXElement("[[Test]]\n[[Test]]\n[TestTable]"); - TomlConvert.DeserializeXElement("[TestTable]\nTest = 1"); + var dt = DynamicToml.Parse(Toml.V04, @" +name = { first = ""Tom"", last = ""Preston - Werner"" } +point = { x = 1, y = 2 } +arr = [{ x = 1, y = 2 }, { x = 2, y = 3}]"); + Assert.AreEqual("Tom", dt.name.first); + Assert.AreEqual("Preston - Werner", dt.name.last); + Assert.AreEqual(1L, dt.point.x); + Assert.AreEqual(2L, dt.point.y); + Assert.AreEqual(1L, dt.arr[0].x); + Assert.AreEqual(2L, dt.arr[0].y); + Assert.AreEqual(2L, dt.arr[1].x); + Assert.AreEqual(3L, dt.arr[1].y); } } } diff --git a/HyperTomlProcessor.Test/DynamicTomlTest.cs b/HyperTomlProcessor.Test/DynamicTomlTest.cs index e4f5e2e..77072a7 100644 --- a/HyperTomlProcessor.Test/DynamicTomlTest.cs +++ b/HyperTomlProcessor.Test/DynamicTomlTest.cs @@ -12,7 +12,7 @@ public class DynamicTomlTest [TestMethod] public void ParseExample() { - var root = DynamicToml.Parse(Examples.Example); + var root = DynamicToml.Parse(Toml.V03, Examples.Example); Assert.AreEqual("TOML Example", root.title); Assert.AreEqual("Tom Preston-Werner", root.owner.name); Assert.AreEqual("GitHub", root.owner.organization); @@ -42,7 +42,7 @@ public void ParseExample() [TestMethod] public void ParseHardExample() { - var root = DynamicToml.Parse(Examples.HardExample); + var root = DynamicToml.Parse(Toml.V03, Examples.HardExample); Assert.AreEqual("You'll hate me after this - #", root.the.test_string); Utils.SequenceEqual(root.the.hard.test_array, "] ", " # "); Utils.SequenceEqual(root.the.hard.test_array2, "Test #11 ]proved that", "Experiment #9 was a success"); @@ -95,7 +95,7 @@ public void SetAndDeserialize() { var dt = DynamicToml.CreateTable(); dt.root = TestObject.Create(); // 'root' でないとデシリアライズできない - var obj = TomlConvert.DeserializeObject((XElement)dt.root); + var obj = Toml.DeserializeObject((XElement)dt.root); TestObject.Test(obj); } } diff --git a/HyperTomlProcessor.Test/Examples.cs b/HyperTomlProcessor.Test/Examples.cs index 052e133..a43e593 100644 --- a/HyperTomlProcessor.Test/Examples.cs +++ b/HyperTomlProcessor.Test/Examples.cs @@ -87,8 +87,8 @@ public static class Examples harder_test_string = "" And when \""'s are in the string, along with # \"""" # ""and comments are there too"" # Things will get harder - [the.hard.""bit#""] - ""what?"" = ""You don't think some user won't do that?"" + [the.hard.bit#] + what? = ""You don't think some user won't do that?"" multi_line_array = [ ""]"", # ] Oh yes I did diff --git a/HyperTomlProcessor/TomlParser.cs b/HyperTomlProcessor/TomlParser.cs index d2dc5cc..73f39e2 100644 --- a/HyperTomlProcessor/TomlParser.cs +++ b/HyperTomlProcessor/TomlParser.cs @@ -214,7 +214,7 @@ private static void InitializeV03Parser() var sign = Chars.OneOf('+', '-').Optional().Select(o => o.Case(() => "", c => c.ToString())); var digit = Chars.Satisfy(c => c >= '0' && c <= '9'); - var digits = digit.Many1().Map(x => string.Concat(x)); + var digits = digit.Many1().Select(x => string.Concat(x)); var integer = from s in sign from i in digits @@ -224,7 +224,7 @@ private static void InitializeV03Parser() from i in digits from d in Chars.Satisfy('.').Bindr(digits) from e in Chars.OneOf('e', 'E').Bindr(digits) - .Optional().Map(x => x.HasValue ? ("e" + x.Value) : "") + .Optional().Select(x => x.HasValue ? ("e" + x.Value) : "") select new TomlValue(TomlItemType.Float, string.Concat(s, i, ".", d, e)); var boolv = Chars.Sequence("true").Select(_ => true) @@ -249,14 +249,14 @@ private static void InitializeV03Parser() .Or( Chars.Sequence("+").Or(Chars.Sequence("-")) .Append(twoDigits) - .Append(Chars.Satisfy(':').Optional().Map(x => ':')) + .Append(Chars.Satisfy(':').Optional().Select(x => ':')) .Append(twoDigits) ) .Optional() ) .Optional() ) - .Map(x => new TomlValue(TomlItemType.Datetime, XmlConvert.ToDateTimeOffset(string.Concat(x)))); + .Select(x => new TomlValue(TomlItemType.Datetime, XmlConvert.ToDateTimeOffset(string.Concat(x)))); Parser arrayRef = null; var array = Delayed.Return(() => arrayRef); @@ -369,18 +369,16 @@ private static void InitializeV04Parser() var sign = Chars.OneOf('+', '-').Optional().Select(o => o.Case(() => "", c => c.ToString())); var digit = Chars.Satisfy(c => c >= '0' && c <= '9'); var digits = digit.Many1(); - var digitsWithUnderscores = digits.SepBy1(Chars.Satisfy('_').Ignore()).Map(Unfold); + var digitsWithUnderscores = digits.SepBy1(Chars.Satisfy('_').Ignore()).Select(Unfold); + var signedDigitsWithUnderscores = sign.Append(digitsWithUnderscores); - var integer = from s in sign - from i in digitsWithUnderscores - select new TomlValue(TomlItemType.Integer, string.Concat(s, i)); + var integer = signedDigitsWithUnderscores.Select(x => new TomlValue(TomlItemType.Integer, string.Concat(x))); - var floatv = from s in sign - from i in digitsWithUnderscores - from d in Chars.Satisfy('.').Bindr(digitsWithUnderscores) - from e in Chars.OneOf('e', 'E').Bindr(digitsWithUnderscores) - .Optional().Map(x => x.HasValue ? ("e" + x.Value) : "") - select new TomlValue(TomlItemType.Float, string.Concat(s, i, ".", d, e)); + var floatv = Combinator.Choice( + signedDigitsWithUnderscores.Append(Chars.Satisfy('.')).Append(digitsWithUnderscores), + signedDigitsWithUnderscores.Append(Chars.Sequence(".").Append(digitsWithUnderscores).Optional()) + .Append(Chars.OneOf('e', 'E')).Append(signedDigitsWithUnderscores) + ).Select(x => new TomlValue(TomlItemType.Float, string.Concat(x))); var boolv = Chars.Sequence("true").Select(_ => true) .Or(Chars.Sequence("false").Select(_ => false)) @@ -411,7 +409,7 @@ private static void InitializeV04Parser() ) .Optional() ) - .Map(x => new TomlValue(TomlItemType.Datetime, XmlConvert.ToDateTimeOffset(string.Concat(x)))); + .Select(x => new TomlValue(TomlItemType.Datetime, XmlConvert.ToDateTimeOffset(string.Concat(x)))); Parser arrayRef = null; var array = Delayed.Return(() => arrayRef); @@ -419,10 +417,10 @@ private static void InitializeV04Parser() var inlineTable = Delayed.Return(() => inlineTableRef); var key = Chars.Satisfy(c => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-') - .Many1().Or(basicString.Map(x => (string)x.Value)); + .Many1().Or(basicString.Select(x => (string)x.Value)); var value = Combinator.Choice( multilineBasicString, basicString, multilineLiteralString, literalString, // 順番大事 - datetime, integer, floatv, boolv, Combinator.Lazy(array), Combinator.Lazy(inlineTable) + datetime, floatv, integer, boolv, Combinator.Lazy(array), Combinator.Lazy(inlineTable) ); inlineTableRef = key.Between(spaces, spaces) @@ -432,7 +430,7 @@ private static void InitializeV04Parser() ) .SepEndBy0(Chars.Satisfy(',').Ignore()) .Between(Chars.Satisfy('{').Ignore(), spaces.SeqIgnore(Chars.Satisfy('}'))) - .Map(x => new TomlValue(TomlItemType.InlineTable, x)); + .Select(x => new TomlValue(TomlItemType.InlineTable, x)); var comments = comment.Between(spacesOrNewlines, spacesOrNewlines).Many0(); var comma = Chars.Satisfy(',').Between(spacesOrNewlines, spacesOrNewlines).Ignore(); @@ -451,8 +449,8 @@ private static void InitializeV04Parser() createArrayParser(Combinator.Choice( multilineBasicString, basicString, multilineLiteralString, literalString)), // 順番大事 createArrayParser(datetime), - createArrayParser(integer), createArrayParser(floatv), + createArrayParser(integer), createArrayParser(boolv), createArrayParser(Combinator.Lazy(array)), createArrayParser(Combinator.Lazy(inlineTable)) diff --git a/HyperTomlProcessor/TomlWriter.cs b/HyperTomlProcessor/TomlWriter.cs index 654a188..8ed5092 100644 --- a/HyperTomlProcessor/TomlWriter.cs +++ b/HyperTomlProcessor/TomlWriter.cs @@ -61,7 +61,7 @@ private string GetFullName(XElement xe) private static bool IsValidBareKey(string s) { - return !s.All(c => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-'); + return s.All(c => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-'); } private void WriteKey(XElement xe, TextWriter writer)