Skip to content

Commit

Permalink
Solve errors of test
Browse files Browse the repository at this point in the history
  • Loading branch information
azyobuzin committed Jun 2, 2015
1 parent 65d3bc6 commit 135b0a3
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 39 deletions.
2 changes: 1 addition & 1 deletion HyperTomlProcessor.Test.Cmd/Program.cs
Expand Up @@ -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();
}
}
Expand Down
91 changes: 78 additions & 13 deletions HyperTomlProcessor.Test/DeserializeXElementTest.cs
Expand Up @@ -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);
}
}
}
6 changes: 3 additions & 3 deletions HyperTomlProcessor.Test/DynamicTomlTest.cs
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -95,7 +95,7 @@ public void SetAndDeserialize()
{
var dt = DynamicToml.CreateTable();
dt.root = TestObject.Create(); // 'root' でないとデシリアライズできない
var obj = TomlConvert.DeserializeObject<TestObject>((XElement)dt.root);
var obj = Toml.DeserializeObject<TestObject>((XElement)dt.root);
TestObject.Test(obj);
}
}
Expand Down
4 changes: 2 additions & 2 deletions HyperTomlProcessor.Test/Examples.cs
Expand Up @@ -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
Expand Down
36 changes: 17 additions & 19 deletions HyperTomlProcessor/TomlParser.cs
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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<char, TomlValue> arrayRef = null;
var array = Delayed.Return(() => arrayRef);
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -411,18 +409,18 @@ 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<char, TomlValue> arrayRef = null;
var array = Delayed.Return(() => arrayRef);
Parser<char, TomlValue> inlineTableRef = null;
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)
Expand All @@ -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();
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion HyperTomlProcessor/TomlWriter.cs
Expand Up @@ -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)
Expand Down

0 comments on commit 135b0a3

Please sign in to comment.