Skip to content

Commit

Permalink
Allow for regex to be unidirectional, and added test to verify its wo…
Browse files Browse the repository at this point in the history
…rking for deserialization
  • Loading branch information
i8beef committed Jul 26, 2020
1 parent 10fcc57 commit d235818
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/HomeAutio.Mqtt.GoogleHome.Tests/GoogleDeviceRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public GoogleDeviceRepositoryTests()
_testFilePath = "TestData/googleDevices.json";
}

[Fact]
public void CanDeserializeValueMaps()
{
// Arrange
var testString = File.ReadAllText("TestData/valueTypeTestData.json");

// Act
var result = new Dictionary<string, Device>(JsonConvert.DeserializeObject<Dictionary<string, Device>>(testString));

// Assert
Assert.NotNull(result);
}

[Fact]
[Trait("Category", "Integration")]
[TestPriority(0)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<None Update="TestData\serviceAccount.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\valueTypeTestData.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class RegexMapTests
{
[Theory]
[InlineData("^mqtt", "mqttResult", "^google$", "googleResult", "google", "mqttResult")]
[InlineData(null, "mqttResult", "^google$", null, "google", "mqttResult")]
[InlineData(null, null, "^google$", null, "google", "google")]
[InlineData("^255$", "255", "^100", "100", 100, "255")]
public void CanMapToMqtt(string mqttSearch, string mqttReplace, string googleSearch, string googleReplace, object value, string expectedResult)
{
Expand All @@ -30,6 +32,8 @@ public void CanMapToMqtt(string mqttSearch, string mqttReplace, string googleSea

[Theory]
[InlineData("^mqtt$", "mqttResult", "^google$", "googleResult", "mqtt", "googleResult")]
[InlineData("^mqtt", null, null, "googleResult", "mqtt", "googleResult")]
[InlineData("^mqtt", null, null, null, "mqtt", "mqtt")]
[InlineData("^255", "255", "^100", "100", "255", "100")]
public void CanMapToGoogle(string mqttSearch, string mqttReplace, string googleSearch, string googleReplace, string value, string expectedResult)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"testy": {
"id": "esplight",
"type": "action.devices.types.OVEN",
"disabled": false,
"willReportState": false,
"roomHint": "Office",
"name": {
"defaultNames": [],
"name": "esplight",
"nicknames": []
},
"deviceInfo": null,
"traits": [
{
"trait": "action.devices.traits.TemperatureControl",
"attributes": {
"queryOnlyTemperatureControl": true,
"temperatureUnitForUX": "C"
},
"commands": {
"action.devices.commands.SetTemperature": {
"temperature": "MQTT_COMMAND_TOPIC"
}
},
"state": {
"temperatureAmbientCelsius": {
"topic": "tele/esplight/SENSOR",
"googleType": "numeric",
"valueMap": [
{
"type": "static",
"google": "test"
},
{
"type": "value",
"mqtt": "on",
"google": "100"
},
{
"mqttMin": 1.0,
"mqttMax": 100.0,
"type": "range",
"google": "true"
},
{
"type": "regex",
"googleSearch": "^googleValueToReplace$",
"googleReplace": "RegExReplaceStringToConvertMqttValueTo",
"mqttSearch": "^mqttValueToReplace$",
"mqttReplace": "RegExReplaceStringToConvertGoogleValueTo"
},
{
"type": "celsius"
}
]
}
}
}
],
"customData": null
}
}
15 changes: 15 additions & 0 deletions src/HomeAutio.Mqtt.GoogleHome/Models/State/ValueMaps/RegexMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,39 @@ public class RegexMap : MapBase
/// <inheritdoc />
public override bool MatchesGoogle(object value)
{
if (GoogleSearch == null)
return false;

return Regex.IsMatch(value.ToString(), GoogleSearch);
}

/// <inheritdoc />
public override string ConvertToGoogle(string value)
{
if (MqttSearch == null || GoogleReplace == null)
return value;

return Regex.Replace(value, MqttSearch, GoogleReplace);
}

/// <inheritdoc />
public override bool MatchesMqtt(string value)
{
if (MqttSearch == null)
return false;

return Regex.IsMatch(value, MqttSearch);
}

/// <inheritdoc />
public override string ConvertToMqtt(object value)
{
if (value == null)
return null;

if (GoogleSearch == null || MqttReplace == null)
return value.ToString();

return Regex.Replace(value.ToString(), GoogleSearch, MqttReplace);
}
}
Expand Down

0 comments on commit d235818

Please sign in to comment.