From 7a24970035362040437a9e62abfd9c9aa3eb22e6 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Fri, 3 Apr 2020 23:08:54 +0200 Subject: [PATCH] Fix UnitTests. --- .../Extensions/UserPropertyExtension.cs | 18 +----------------- .../MqttApplicationMessage_Tests.cs | 11 +++++------ 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/Source/MQTTnet/Extensions/UserPropertyExtension.cs b/Source/MQTTnet/Extensions/UserPropertyExtension.cs index 5bb28cf..f861c32 100644 --- a/Source/MQTTnet/Extensions/UserPropertyExtension.cs +++ b/Source/MQTTnet/Extensions/UserPropertyExtension.cs @@ -1,32 +1,16 @@ using System; -using System.ComponentModel; using System.Linq; namespace MQTTnet.Extensions { 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 (propertyName == null) throw new ArgumentNullException(nameof(propertyName)); return message.UserProperties?.SingleOrDefault(up => up.Name.Equals(propertyName, comparisonType))?.Value; } - - public static T GetUserProperty(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); - } - } } } diff --git a/Tests/MQTTnet.Core.Tests/MqttApplicationMessage_Tests.cs b/Tests/MQTTnet.Core.Tests/MqttApplicationMessage_Tests.cs index 6b791dc..05d0907 100644 --- a/Tests/MQTTnet.Core.Tests/MqttApplicationMessage_Tests.cs +++ b/Tests/MQTTnet.Core.Tests/MqttApplicationMessage_Tests.cs @@ -1,8 +1,7 @@ -using System; -using System.Collections.Generic; using Microsoft.VisualStudio.TestTools.UnitTesting; using MQTTnet.Extensions; using MQTTnet.Packets; +using System.Collections.Generic; namespace MQTTnet.Tests { @@ -23,11 +22,11 @@ namespace MQTTnet.Tests }; Assert.AreEqual("bar", message.GetUserProperty("foo")); - Assert.AreEqual(1011, message.GetUserProperty("value")); - Assert.AreEqual("insensitive", message.GetUserProperty("case")); + //Assert.AreEqual(1011, message.GetUserProperty("value")); + Assert.AreEqual(null, message.GetUserProperty("case")); Assert.AreEqual(null, message.GetUserProperty("nonExists")); - Assert.AreEqual(null, message.GetUserProperty("nonExists")); - Assert.ThrowsException(() => message.GetUserProperty("nonExists")); + //Assert.AreEqual(null, message.GetUserProperty("nonExists")); + //Assert.ThrowsException(() => message.GetUserProperty("nonExists")); } } }