Skip to content

Commit

Permalink
Merge pull request #118 from greymistcube/refactor/simplify-inspect
Browse files Browse the repository at this point in the history
🧹 Removed unused parameter from Inspect()
  • Loading branch information
greymistcube authored Oct 23, 2023
2 parents 71c480a + 66596bf commit 9f39bed
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 80 deletions.
2 changes: 1 addition & 1 deletion Bencodex.Tests/CodecTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void SpecTestSuite(Spec spec)
_output.WriteLine("Data: {0}", spec.EncodingPath);
Codec codec = new Codec();
IValue decoded = codec.Decode(spec.Encoding);
_output.WriteLine("Value: {0}", decoded.Inspect(false));
_output.WriteLine("Value: {0}", decoded.Inspect());
Assert.Equal(spec.Semantics, decoded);
Assert.Equal(spec.Encoding.LongLength, decoded.EncodingLength);
Assert.Equal(spec.Semantics.EncodingLength, decoded.EncodingLength);
Expand Down
10 changes: 4 additions & 6 deletions Bencodex.Tests/Types/BinaryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,11 @@ public void EncodingLength()
Assert.Equal(13L, new Binary(new byte[10]).EncodingLength);
}

[Theory]
[InlineData(new object[] { false })]
[InlineData(new object[] { true })]
public void Inspect(bool loadAll)
[Fact]
public void Inspect()
{
Assert.Equal("b\"\"", _empty.Inspect(loadAll));
Assert.Equal(@"b""\x68\x65\x6c\x6c\x6f""", _hello.Inspect(loadAll));
Assert.Equal("b\"\"", _empty.Inspect());
Assert.Equal(@"b""\x68\x65\x6c\x6c\x6f""", _hello.Inspect());
}

[Fact]
Expand Down
10 changes: 4 additions & 6 deletions Bencodex.Tests/Types/BooleanTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ public void Encode()
);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void Inspect(bool loadAll)
[Fact]
public void Inspect()
{
Assert.Equal("true", _t.Inspect(loadAll));
Assert.Equal("false", _f.Inspect(loadAll));
Assert.Equal("true", _t.Inspect());
Assert.Equal("false", _f.Inspect());
}

[Fact]
Expand Down
14 changes: 6 additions & 8 deletions Bencodex.Tests/Types/DictionaryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -558,20 +558,18 @@ public void EncodingLength()
Assert.Equal(33L, _mixedKeys.EncodingLength);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void Inspect(bool loadAll)
[Fact]
public void Inspect()
{
Assert.Equal("{}", Dictionary.Empty.Inspect(loadAll));
Assert.Equal("{}", Dictionary.Empty.Inspect());

Assert.Equal(
"{\n \"foo\": \"bar\",\n}",
_textKey.Inspect(loadAll)
_textKey.Inspect()
);
Assert.Equal(
"{\n b\"\\x66\\x6f\\x6f\": \"bar\",\n}",
_binaryKey.Inspect(loadAll)
_binaryKey.Inspect()
);
Assert.Equal(
@"{
Expand All @@ -580,7 +578,7 @@ public void Inspect(bool loadAll)
},
""foo"": ""bar"",
}".NoCr(),
_textKey.SetItem("baz", _textKey).Inspect(loadAll)
_textKey.SetItem("baz", _textKey).Inspect()
);
}

Expand Down
10 changes: 4 additions & 6 deletions Bencodex.Tests/Types/IntegerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,11 @@ public void EncodingLength()
Assert.Equal(6L, new Integer(-456).EncodingLength);
}

[Theory]
[InlineData(new object[] { false })]
[InlineData(new object[] { true })]
public void Inspect(bool loadAll)
[Fact]
public void Inspect()
{
Assert.Equal("123", new Integer(123).Inspect(loadAll));
Assert.Equal("-456", new Integer(-456).Inspect(loadAll));
Assert.Equal("123", new Integer(123).Inspect());
Assert.Equal("-456", new Integer(-456).Inspect());
}

[Fact]
Expand Down
18 changes: 8 additions & 10 deletions Bencodex.Tests/Types/ListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,12 @@ public void EncodingLength()
Assert.Equal(26L, _nest.EncodingLength);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void Inspect(bool loadAll)
[Fact]
public void Inspect()
{
Assert.Equal("[]", _zero.Inspect(loadAll));
Assert.Equal("[null]", _one.Inspect(loadAll));
Assert.Equal("[\n \"hello\",\n \"world\",\n]", _two.Inspect(loadAll));
Assert.Equal("[]", _zero.Inspect());
Assert.Equal("[null]", _one.Inspect());
Assert.Equal("[\n \"hello\",\n \"world\",\n]", _two.Inspect());

var expected = @"[
null,
Expand All @@ -116,11 +114,11 @@ public void Inspect(bool loadAll)
""world"",
],
]".NoCr();
Assert.Equal(expected, _nest.Inspect(loadAll));
Assert.Equal(expected, _nest.Inspect());

// If any element is a list/dict it should be indented
Assert.Equal("[\n [],\n]", new List(new IValue[] { _zero }).Inspect(loadAll));
Assert.Equal("[\n {},\n]", new List(Dictionary.Empty).Inspect(loadAll));
Assert.Equal("[\n [],\n]", new List(new IValue[] { _zero }).Inspect());
Assert.Equal("[\n {},\n]", new List(Dictionary.Empty).Inspect());
}

[Fact]
Expand Down
8 changes: 3 additions & 5 deletions Bencodex.Tests/Types/NullTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ public void Kind()
Assert.Equal(ValueKind.Null, Null.Value.Kind);
}

[Theory]
[InlineData(new object[] { false })]
[InlineData(new object[] { true })]
public void Inspect(bool loadAll)
[Fact]
public void Inspect()
{
Assert.Equal("null", Null.Value.Inspect(loadAll));
Assert.Equal("null", Null.Value.Inspect());
}

[Fact]
Expand Down
12 changes: 5 additions & 7 deletions Bencodex.Tests/Types/TextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,14 @@ public void Encode()
);
}

[Theory]
[InlineData(new object[] { false })]
[InlineData(new object[] { true })]
public void Inspect(bool loadAll)
[Fact]
public void Inspect()
{
Assert.Equal("\"\"", _empty.Inspect(loadAll));
Assert.Equal("\"\u4f60\u597d\"", _nihao.Inspect(loadAll));
Assert.Equal("\"\"", _empty.Inspect());
Assert.Equal("\"\u4f60\u597d\"", _nihao.Inspect());
Assert.Equal(
"\"new lines and\\n\\\"quotes\\\" become escaped to \\\\\"",
_complex.Inspect(loadAll)
_complex.Inspect()
);
}

Expand Down
6 changes: 3 additions & 3 deletions Bencodex/Types/Binary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,15 @@ public string ToBase64()
return Convert.ToBase64String(Unsafe.As<ImmutableArray<byte>, byte[]>(ref bytes));
}

/// <inheritdoc cref="IValue.Inspect(bool)"/>
public string Inspect(bool loadAll)
/// <inheritdoc cref="IValue.Inspect()"/>
public string Inspect()
{
IEnumerable<string> contents = this.Select(b => $"\\x{b:x2}");
return $"b\"{string.Join(string.Empty, contents)}\"";
}

/// <inheritdoc cref="object.ToString()"/>
public override string ToString() =>
$"{nameof(Bencodex)}.{nameof(Bencodex.Types)}.{nameof(Binary)} {Inspect(false)}";
$"{nameof(Bencodex)}.{nameof(Bencodex.Types)}.{nameof(Binary)} {Inspect()}";
}
}
7 changes: 3 additions & 4 deletions Bencodex/Types/Boolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,12 @@ public override int GetHashCode()
return Value.GetHashCode();
}

/// <inheritdoc cref="IValue.Inspect(bool)"/>
public string Inspect(bool loadAll) =>
Value ? "true" : "false";
/// <inheritdoc cref="IValue.Inspect()"/>
public string Inspect() => Value ? "true" : "false";

/// <inheritdoc cref="object.ToString()"/>
[Pure]
public override string ToString() =>
$"{nameof(Bencodex)}.{nameof(Types)}.{nameof(Boolean)} {Inspect(false)}";
$"{nameof(Bencodex)}.{nameof(Types)}.{nameof(Boolean)} {Inspect()}";
}
}
8 changes: 4 additions & 4 deletions Bencodex/Types/Dictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1676,23 +1676,23 @@ public override int GetHashCode()
=> unchecked(_dict.Aggregate(GetType().GetHashCode(), (accum, next)
=> (accum * 397) ^ ((next.Key.GetHashCode() * 397) ^ next.Value.GetHashCode())));

/// <inheritdoc cref="IValue.Inspect(bool)"/>
public string Inspect(bool loadAll)
/// <inheritdoc cref="IValue.Inspect()"/>
public string Inspect()
{
if (_dict.Count < 1)
{
return "{}";
}

IEnumerable<string> pairs = this.Select(kv =>
$" {kv.Key.Inspect(loadAll)}: {kv.Value.Inspect(loadAll).Replace("\n", "\n ")},\n"
$" {kv.Key.Inspect()}: {kv.Value.Inspect().Replace("\n", "\n ")},\n"
).OrderBy(s => s);
string pairsString = string.Join(string.Empty, pairs);
return $"{{\n{pairsString}}}";
}

/// <inheritdoc cref="object.ToString()"/>
public override string ToString() =>
$"{nameof(Bencodex)}.{nameof(Types)}.{nameof(Dictionary)} {Inspect(false)}";
$"{nameof(Bencodex)}.{nameof(Types)}.{nameof(Dictionary)} {Inspect()}";
}
}
8 changes: 2 additions & 6 deletions Bencodex/Types/IValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ public interface IValue : IEquatable<IValue>

/// <summary>
/// Gets a human-readable representation for debugging.
/// <para>Unloaded values may be omitted.</para>
/// </summary>
/// <param name="loadAll">Load all unloaded values before showing them. This option
/// is applied to subtrees recursively.</param>
/// <returns>A human-readable representation for debugging, which looks similar to Python's
/// literal syntax. However, if a value is a complex tree and contains any unloaded
/// subvalues, these are omitted and their fingerprints are shown instead.</returns>
string Inspect(bool loadAll);
/// literal syntax.</returns>
string Inspect();
}
}
6 changes: 3 additions & 3 deletions Bencodex/Types/Integer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@ public override int GetHashCode()
return Value.GetHashCode();
}

/// <inheritdoc cref="IValue.Inspect(bool)"/>
public string Inspect(bool loadAll) =>
/// <inheritdoc cref="IValue.Inspect()"/>
public string Inspect() =>
Value.ToString(CultureInfo.InvariantCulture);

/// <inheritdoc cref="object.ToString()"/>
public override string ToString() =>
$"{nameof(Bencodex)}.{nameof(Types)}.{nameof(Integer)} {Inspect(false)}";
$"{nameof(Bencodex)}.{nameof(Types)}.{nameof(Integer)} {Inspect()}";

internal long CountDecimalDigits() =>
Value.Sign switch
Expand Down
12 changes: 6 additions & 6 deletions Bencodex/Types/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,10 @@ IEqualityComparer<IValue> equalityComparer
IImmutableList<IValue> IImmutableList<IValue>.SetItem(int index, IValue value) =>
new List(_values.SetItem(index, value));

/// <inheritdoc cref="IValue.Inspect(bool)"/>
public string Inspect(bool loadAll)
/// <inheritdoc cref="IValue.Inspect()"/>
public string Inspect()
{
string InspectItem(IValue value, bool load) => value.Inspect(load);
string InspectItem(IValue value) => value.Inspect();

string inspection;
switch (_values.Length)
Expand All @@ -483,12 +483,12 @@ public string Inspect(bool loadAll)
goto default;
}

inspection = $"[{InspectItem(first, loadAll)}]";
inspection = $"[{InspectItem(first)}]";
break;

default:
IEnumerable<string> elements = _values.Select(v =>
$" {InspectItem(v, loadAll).Replace("\n", "\n ")},\n"
$" {InspectItem(v).Replace("\n", "\n ")},\n"
);
inspection = $"[\n{string.Join(string.Empty, elements)}]";
break;
Expand All @@ -499,6 +499,6 @@ public string Inspect(bool loadAll)

/// <inheritdoc cref="object.ToString()"/>
public override string ToString() =>
$"{nameof(Bencodex)}.{nameof(Types)}.{nameof(List)} {Inspect(false)}";
$"{nameof(Bencodex)}.{nameof(Types)}.{nameof(List)} {Inspect()}";
}
}
4 changes: 2 additions & 2 deletions Bencodex/Types/Null.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ bool IEquatable<IValue>.Equals(IValue other) =>

bool IEquatable<Null>.Equals(Null other) => true;

/// <inheritdoc cref="IValue.Inspect(bool)"/>
public string Inspect(bool loadAll) => "null";
/// <inheritdoc cref="IValue.Inspect()"/>
public string Inspect() => "null";

/// <inheritdoc cref="object.ToString()"/>
public override string ToString() =>
Expand Down
6 changes: 3 additions & 3 deletions Bencodex/Types/Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public int CompareTo(object? obj)
throw new ArgumentException($"Object must be of type {nameof(Text)}");
}

/// <inheritdoc cref="IValue.Inspect(bool)"/>
public string Inspect(bool loadAll)
/// <inheritdoc cref="IValue.Inspect()"/>
public string Inspect()
{
string contents = Value
.Replace("\\", "\\\\")
Expand All @@ -107,6 +107,6 @@ public string Inspect(bool loadAll)

/// <inheritdoc cref="object.ToString()"/>
public override string ToString() =>
$"{nameof(Bencodex)}.{nameof(Types)}.{nameof(Text)} {Inspect(false)}";
$"{nameof(Bencodex)}.{nameof(Types)}.{nameof(Text)} {Inspect()}";
}
}
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ Version 0.16.0
To be released.

- Removed `IValue.Inspection` property. [[#117]]
- Changed `IValue.Inspect(bool)` to `IValue.Inspect()`. [[#118]]

[#117]: https://github.com/planetarium/bencodex.net/pull/117
[#118]: https://github.com/planetarium/bencodex.net/pull/118


Version 0.15.0
Expand Down

0 comments on commit 9f39bed

Please sign in to comment.