mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-11-05 14:02:55 +08:00
Fix UnitTests.
This commit is contained in:
@@ -1,32 +1,16 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.ComponentModel;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace MQTTnet.Extensions
|
namespace MQTTnet.Extensions
|
||||||
{
|
{
|
||||||
public static class UserPropertyExtension
|
public static class UserPropertyExtension
|
||||||
{
|
{
|
||||||
public static string GetUserProperty(this MqttApplicationMessage message, string propertyName, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase)
|
public static string GetUserProperty(this MqttApplicationMessage message, string propertyName, StringComparison comparisonType = StringComparison.Ordinal)
|
||||||
{
|
{
|
||||||
if (message == null) throw new ArgumentNullException(nameof(message));
|
if (message == null) throw new ArgumentNullException(nameof(message));
|
||||||
if (propertyName == null) throw new ArgumentNullException(nameof(propertyName));
|
if (propertyName == null) throw new ArgumentNullException(nameof(propertyName));
|
||||||
|
|
||||||
return message.UserProperties?.SingleOrDefault(up => up.Name.Equals(propertyName, comparisonType))?.Value;
|
return message.UserProperties?.SingleOrDefault(up => up.Name.Equals(propertyName, comparisonType))?.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static T GetUserProperty<T>(this MqttApplicationMessage message, string propertyName, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase)
|
|
||||||
{
|
|
||||||
var value = GetUserProperty(message, propertyName, comparisonType);
|
|
||||||
|
|
||||||
var typeDescriptor = TypeDescriptor.GetConverter(typeof(T));
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return (T) typeDescriptor.ConvertFromString(value);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException($"Cannot convert value({value ?? "null"}) of UserProperty({propertyName}) to {typeof(T).FullName}.", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
using MQTTnet.Extensions;
|
using MQTTnet.Extensions;
|
||||||
using MQTTnet.Packets;
|
using MQTTnet.Packets;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace MQTTnet.Tests
|
namespace MQTTnet.Tests
|
||||||
{
|
{
|
||||||
@@ -23,11 +22,11 @@ namespace MQTTnet.Tests
|
|||||||
};
|
};
|
||||||
|
|
||||||
Assert.AreEqual("bar", message.GetUserProperty("foo"));
|
Assert.AreEqual("bar", message.GetUserProperty("foo"));
|
||||||
Assert.AreEqual(1011, message.GetUserProperty<int>("value"));
|
//Assert.AreEqual(1011, message.GetUserProperty<int>("value"));
|
||||||
Assert.AreEqual("insensitive", message.GetUserProperty("case"));
|
Assert.AreEqual(null, message.GetUserProperty("case"));
|
||||||
Assert.AreEqual(null, message.GetUserProperty("nonExists"));
|
Assert.AreEqual(null, message.GetUserProperty("nonExists"));
|
||||||
Assert.AreEqual(null, message.GetUserProperty<int?>("nonExists"));
|
//Assert.AreEqual(null, message.GetUserProperty<int?>("nonExists"));
|
||||||
Assert.ThrowsException<InvalidOperationException>(() => message.GetUserProperty<int>("nonExists"));
|
//Assert.ThrowsException<InvalidOperationException>(() => message.GetUserProperty<int>("nonExists"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user