Commit 8cc645dc authored by Marc Gravell's avatar Marc Gravell

improve RedisFeatures text output

parent a4d087f6
using System;
using Xunit;
namespace StackExchange.Redis.Tests
{
public class RedisFeaturesTests
{
[Fact]
public void ExecAbort() // a random one because it is fun
{
var features = new RedisFeatures(new Version(2, 9));
var s = features.ToString();
Assert.True(features.ExecAbort);
Assert.StartsWith("Features in 2.9\r\n", s);
Assert.Contains("ExecAbort: True\r\n", s);
features = new RedisFeatures(new Version(2, 9, 5));
s = features.ToString();
Assert.False(features.ExecAbort);
Assert.StartsWith("Features in 2.9.5\r\n", s);
Assert.Contains("ExecAbort: False\r\n", s);
features = new RedisFeatures(new Version(3, 0));
s = features.ToString();
Assert.True(features.ExecAbort);
Assert.StartsWith("Features in 3.0\r\n", s);
Assert.Contains("ExecAbort: True\r\n", s);
}
}
}
using System; using System;
using System.Linq;
using System.Reflection;
using System.Text; using System.Text;
namespace StackExchange.Redis namespace StackExchange.Redis
...@@ -32,6 +34,7 @@ public struct RedisFeatures ...@@ -32,6 +34,7 @@ public struct RedisFeatures
v4_9_1 = new Version(4, 9, 1); // 5.0 RC1 is version 4.9.1 v4_9_1 = new Version(4, 9, 1); // 5.0 RC1 is version 4.9.1
private readonly Version version; private readonly Version version;
/// <summary> /// <summary>
/// Create a new RedisFeatures instance for the given version /// Create a new RedisFeatures instance for the given version
/// </summary> /// </summary>
...@@ -166,13 +169,33 @@ public RedisFeatures(Version version) ...@@ -166,13 +169,33 @@ public RedisFeatures(Version version)
/// </summary> /// </summary>
public override string ToString() public override string ToString()
{ {
var sb = new StringBuilder().Append("Features in ").Append(Version).AppendLine() var v = Version; // the docs lie: Version.ToString(fieldCount) only supports 0-2 fields
.Append("ExpireOverwrite: ").Append(ExpireOverwrite).AppendLine() var sb = new StringBuilder().Append("Features in ").Append(v.Major).Append('.').Append(v.Minor);
.Append("Persist: ").Append(Persist).AppendLine(); if (v.Revision >= 0) sb.Append('.').Append(v.Revision);
if (v.Build >= 0) sb.Append('.').Append(v.Build);
sb.AppendLine();
object boxed = this;
foreach(var prop in s_props)
{
sb.Append(prop.Name).Append(": ").Append(prop.GetValue(boxed)).AppendLine();
}
return sb.ToString(); return sb.ToString();
} }
// 2.9.5 (cluster beta 1) has a bug in EXECABORT re MOVED and ASK; it only affects cluster, but
// frankly if you aren't playing with cluster, why are you using 2.9.5 in the first place? private static readonly PropertyInfo[] s_props = (
from prop in typeof(RedisFeatures).GetProperties(BindingFlags.Instance | BindingFlags.Public)
where prop.PropertyType == typeof(bool)
let indexers = prop.GetIndexParameters()
where indexers == null || indexers.Length == 0
orderby prop.Name
select prop).ToArray();
/// <summary>Returns the hash code for this instance.</summary>
/// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
public override int GetHashCode() => Version.GetHashCode();
/// <summary>Indicates whether this instance and a specified object are equal.</summary>
/// <returns>true if <paramref name="obj" /> and this instance are the same type and represent the same value; otherwise, false. </returns>
/// <param name="obj">The object to compare with the current instance. </param>
public override bool Equals(object obj) => obj is RedisFeatures f && f.Version == Version;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment