-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from lechu445/lb_add_readproperty_method
Add IIonReader.ReadProperty() method
- Loading branch information
Showing
14 changed files
with
277 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using NUnit.Framework; | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace Anixe.Ion.UnitTests | ||
{ | ||
public class IonExtensionsTest | ||
{ | ||
[Test] | ||
public void ReadProperty_Test() | ||
{ | ||
var reader = CreateReaderForInput("\"some_key\" = \"some_value\""); | ||
var ionProperty = reader.ReadProperty(); | ||
|
||
ReadOnlySpan<char> key = ionProperty.Key; | ||
ReadOnlySpan<char> value = ionProperty.Value; | ||
|
||
Assert.AreEqual("some_key", key.ToString()); | ||
Assert.AreEqual("some_value", value.ToString()); | ||
|
||
// getting more than one time | ||
Assert.AreEqual("some_key", ionProperty.Key.ToString()); | ||
|
||
// reading property one more time | ||
var newIonProperty = reader.ReadProperty(); | ||
Assert.AreEqual("some_key", newIonProperty.Key.ToString()); | ||
} | ||
|
||
[Test] | ||
public void ReadProperty_Empty_Property_Test() | ||
{ | ||
var reader = CreateReaderForInput("\"\" = \"\""); | ||
var ionProperty = reader.ReadProperty(); | ||
Assert.AreEqual("", ionProperty.Key.ToString()); | ||
Assert.AreEqual("", ionProperty.Value.ToString()); | ||
|
||
var secondReader = CreateReaderForInput(" = "); | ||
Assert.AreEqual("", ionProperty.Key.ToString()); | ||
Assert.AreEqual("", ionProperty.Value.ToString()); | ||
} | ||
|
||
private static IIonReader CreateReaderForInput(string input) | ||
{ | ||
var reader = IonReaderFactory.Create(new MemoryStream(Encoding.UTF8.GetBytes(input))); | ||
reader.Read(); | ||
return reader; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using NUnit.Framework; | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace Anixe.Ion.UnitTests | ||
{ | ||
public class IonPropertyTest | ||
{ | ||
[TestCase("some_key = some_value", "some_key", "some_value")] | ||
[TestCase("\"some_key\" = \"some_value\"", "some_key", "some_value")] | ||
[TestCase("\"some_key\" = 1", "some_key", "1")] | ||
[TestCase(" \"some_key\" = \"some_value\" ", "some_key", "some_value")] | ||
[TestCase("\"some_key\": \"some_value\"", "", "\"some_key\": \"some_value\"")] | ||
[TestCase("\"\" = \"\"", "", "")] | ||
[TestCase(" = ", "", "")] | ||
[TestCase("some key = some value", "some key", "some value")] | ||
[TestCase("key=some=value", "key", "some=value")] | ||
[TestCase("some_setting = \"true\"", "some_setting", "true")] | ||
[TestCase("some_setting =", "some_setting", "")] | ||
public void IonProperty_Test(string input, string key, string value) | ||
{ | ||
using var reader = IonReaderFactory.Create(new MemoryStream(Encoding.UTF8.GetBytes(input))); | ||
reader.Read(); | ||
Assert.True(reader.IsProperty); | ||
|
||
var property = new IonProperty(reader); | ||
Assert.AreEqual(key, property.Key.ToString()); | ||
Assert.AreEqual(value, property.Value.ToString()); | ||
} | ||
|
||
[TestCase("")] | ||
[TestCase("[AAA]")] | ||
[TestCase("|--|--|")] | ||
public void IonProperty_Throws_InvalidOperationException(string input) | ||
{ | ||
using var reader = IonReaderFactory.Create(new MemoryStream(Encoding.UTF8.GetBytes(input))); | ||
reader.Read(); | ||
Assert.False(reader.IsProperty); | ||
Assert.Throws<InvalidOperationException>(() => new IonProperty(reader)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
|
||
namespace Anixe.Ion | ||
{ | ||
public static class IonExtensions | ||
{ | ||
/// <summary> | ||
/// Creates ref-struct that represents key-value property. | ||
/// </summary> | ||
/// <param name="reader">Instance of <see cref="IIonReader"/> with current state <see cref="IIonReader.IsProperty"/>=<see langword="true"/>.</param> | ||
/// <param name="propertySeparator">Character that separates key from value. By default it is used = character.</param> | ||
/// <returns>An instance of <see cref="IonProperty"/> that has access to key and value spans in current line.</returns> | ||
/// <exception cref="InvalidOperationException">Reader is in not correct state. That means <see cref="IIonReader.IsProperty"/> is <see langword="false"/>.</exception> | ||
public static IonProperty ReadProperty(this IIonReader reader, char propertySeparator = '=') | ||
{ | ||
return new IonProperty(reader, propertySeparator); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
|
||
namespace Anixe.Ion | ||
{ | ||
/// <summary> | ||
/// Represents a ref-struct that accesses key and value of property of <see cref="IIonReader"/>. | ||
/// </summary> | ||
public ref struct IonProperty | ||
{ | ||
public readonly ReadOnlySpan<char> Key; | ||
public readonly ReadOnlySpan<char> Value; | ||
|
||
/// <summary> | ||
/// Creates a new instance of <see cref="IonProperty"/>. | ||
/// </summary> | ||
/// <param name="reader">Instance of <see cref="IIonReader"/> with current state <see cref="IIonReader.IsProperty"/>=<see langword="true"/>.</param> | ||
/// <param name="propertySeparator">Character that separates key from value. By default it is used = character.</param> | ||
/// <exception cref="InvalidOperationException">Reader is in not correct state. That means <see cref="IIonReader.IsProperty"/> is <see langword="false"/>.</exception> | ||
public IonProperty(IIonReader reader, char propertySeparator = '=') | ||
{ | ||
if (!reader.IsProperty) | ||
{ | ||
throw new InvalidOperationException("Cannot read property when reader in not on property line."); | ||
} | ||
|
||
var span = (ReadOnlySpan<char>)reader.CurrentRawLine.AsSpan(); | ||
var sepIdx = span.IndexOf(propertySeparator); | ||
if (sepIdx == -1) | ||
{ | ||
this.Key = ReadOnlySpan<char>.Empty; | ||
this.Value = span; | ||
return; | ||
} | ||
|
||
this.Key = span.Slice(0, sepIdx).Trim().Trim('"'); | ||
this.Value = span.Slice(sepIdx + 1, span.Length - sepIdx - 1).Trim().Trim('"'); | ||
} | ||
} | ||
} |
Oops, something went wrong.