Skip to content

Commit

Permalink
only operate on strings
Browse files Browse the repository at this point in the history
  • Loading branch information
brettfo committed Aug 6, 2024
1 parent 02a4282 commit 871c487
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 521 deletions.
19 changes: 19 additions & 0 deletions src/IxMilia.Config.Test/ConfigEscapeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) IxMilia. All Rights Reserved.

using Xunit;

namespace IxMilia.Config.Test
{
public class ConfigEscapeTests
{
[Theory]
[InlineData("abcd", "abcd")]
[InlineData("ab cd", "ab cd")]
[InlineData("ab\"cd", "\"ab\\\"cd\"")]
public void VerifySerialize(string value, string expected)
{
var actual = ConfigExtensions.EscapeString(value);
Assert.Equal(expected, actual);
}
}
}
104 changes: 9 additions & 95 deletions src/IxMilia.Config.Test/ConfigParseTests.cs
Original file line number Diff line number Diff line change
@@ -1,106 +1,20 @@
// Copyright (c) IxMilia. All Rights Reserved.

using System.Collections.Generic;
using Xunit;

namespace IxMilia.Config.Test
{
public class ConfigParseTests
{
private void VerifyParse<T>(T expected, string value)
{
var dict = new Dictionary<string, string>();
dict["key"] = value;
T result;
Assert.True(dict.TryParseValue("key", out result));
Assert.Equal(expected, result);
}

private void VerifyParseFail<T>(string value)
{
var dict = new Dictionary<string, string>();
dict["key"] = value;
T result;
Assert.False(dict.TryParseValue("key", out result));
}

[Fact]
public void ParseDoubleTest()
{
VerifyParse(3.14, "3.14");
}

[Fact]
public void ParseAssignDoubleTest()
{
var dbl = 1.0;
"2.0".TryParseAssign(ref dbl);
Assert.Equal(2.0, dbl);
}

[Fact]
public void AssignDoubleFromDictionaryTest()
{
var dict = new Dictionary<string, string>();
dict["key"] = "2.0";
var dbl = 1.0;
dict.TryParseAssign("key", ref dbl);
Assert.Equal(2.0, dbl);
}

[Fact]
public void ParseStringNotQuotedTest()
{
VerifyParse("some string", "some string");
}

[Fact]
public void ParseStringNotQuotedSameStartAndEndCharacterTest()
{
VerifyParse("abba", "abba");
}

[Fact]
public void ParseQuotedStringTest()
{
VerifyParse("final\nvalue", @"""final\nvalue""");
}

[Fact]
public void ParseDoubleFailTest()
{
VerifyParseFail<double>("three");
}

[Fact]
public void NoParserTest()
{
// System.Object has no Parse() method
VerifyParseFail<object>("foo");
}

[Fact]
public void ParseEnumTest()
{
VerifyParse(Numeros.Dos, "Dos");
}

[Fact]
public void ParseEnumFlagsTest()
{
VerifyParse(Flags.IsAlpha | Flags.IsBeta, "IsAlpha|IsBeta");
}

[Fact]
public void ParseEnumFailTest()
{
VerifyParseFail<Numeros>("Cinco");
}

[Fact]
public void ParseArrayTest()
{
VerifyParse(new[] { 1.0, 2.0 }, "1.0;2.0");
[Theory]
[InlineData("some string", "some string")] // regular string
[InlineData("abba", "abba")] // first and last characters identical, but not quotes
[InlineData(@"'final\nvalue'", "final\nvalue")] // single-quoted string
[InlineData(@"""final\nvalue""", "final\nvalue")] // double-quoted string
public void VerifyParse(string value, string expected)
{
var actual = ConfigExtensions.ParseString(value);
Assert.Equal(expected, actual);
}
}
}
8 changes: 4 additions & 4 deletions src/IxMilia.Config.Test/ConfigReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private IDictionary<string, string> Parse(string data)
[Fact]
public void SimpleParseTest()
{
var dict = Parse(@"
var dict = Parse("""
; comment
rootValue = true
Expand All @@ -28,12 +28,12 @@ public void SimpleParseTest()
key = value
[section.deeperSection]
key = value2
");
key = "quoted\nstring"
""");
Assert.Equal(3, dict.Keys.Count);
Assert.Equal("true", dict["rootValue"]);
Assert.Equal("value", dict["section.key"]);
Assert.Equal("value2", dict["section.deeperSection.key"]);
Assert.Equal("quoted\nstring", dict["section.deeperSection.key"]);
}
}
}
74 changes: 0 additions & 74 deletions src/IxMilia.Config.Test/ConfigSerializeTests.cs

This file was deleted.

45 changes: 0 additions & 45 deletions src/IxMilia.Config.Test/ConfigToStringTests.cs

This file was deleted.

8 changes: 4 additions & 4 deletions src/IxMilia.Config.Test/ConfigWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ public void WriteToEmptyFileTest()
var dict = new Dictionary<string, string>()
{
{ "rootValue", "true" },
{ "section.key2", "value2" },
{ "section.key2", "quoted\nstring" },
{ "section.key1", "value1" },
{ "section.deeperSection.key", "valueDeeper" },
};

AssertWritten(dict, @"
AssertWritten(dict, """
rootValue = true
[section]
key1 = value1
key2 = value2
key2 = "quoted\nstring"
[section.deeperSection]
key = valueDeeper
");
""");
}

[Fact]
Expand Down
22 changes: 0 additions & 22 deletions src/IxMilia.Config.Test/Enums.cs

This file was deleted.

Loading

0 comments on commit 871c487

Please sign in to comment.