Skip to content

Commit

Permalink
Merge pull request #27 from ExcuseMi/master
Browse files Browse the repository at this point in the history
Extract code IV Parser + Wrote tests/ Fix Farfetch'd Error
  • Loading branch information
ExcuseMi authored Jul 31, 2016
2 parents 7e92f8b + 947c698 commit 77b8b64
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 20 deletions.
37 changes: 37 additions & 0 deletions PogoLocationFeeder/Helper/IVParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace PogoLocationFeeder.Helper
{
public class IVParser
{
public static double parseIV(string input)
{
double iv;
iv = parseRegexDouble(input, @"(?i)\b(1?\d{1,3}[,.]?\d{0,3})\W*\%?\W*IV"); // 52 IV 52% IV 52IV 52.5 IV
if (iv == default(double))
iv = parseRegexDouble(input, @"(?i)\bIV\W?(1?\d{1,2}[,.]?\d{0,3})");
if (iv == default(double))
iv = parseRegexDouble(input, @"\b(1?\d{1,3}[,.]?\d{0,3})\W*\%?"); // 52% 52 %

return iv;
}

private static double parseRegexDouble(string input, string regex)
{
Match match = Regex.Match(input, regex);
if (match.Success)
{
return Convert.ToDouble(match.Groups[1].Value.Replace(',', '.'), CultureInfo.InvariantCulture);
}
else
return default(double);
}
}

}
22 changes: 3 additions & 19 deletions PogoLocationFeeder/Helper/MessageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public List<SniperInfo> parseMessage(string message)
sniperInfo.latitude = geoCoordinates.latitude;
sniperInfo.longitude = geoCoordinates.longitude;
}
parseIV(input);
double iv = IVParser.parseIV(input);
sniperInfo.iv = iv;
parseTimestamp(input);
PokemonId pokemon = PokemonParser.parsePokemon(input);
sniperInfo.id = pokemon;
Expand All @@ -37,25 +38,8 @@ public List<SniperInfo> parseMessage(string message)
return snipeList;
}

private double parseRegexDouble(string input, string regex)
{
Match match = Regex.Match(input, regex);
if (match.Success)
{
return Convert.ToDouble(match.Groups[1].Value.Replace(',', '.'), CultureInfo.InvariantCulture);
}
else
return default(double);
}

private void parseIV(string input)
{
sniperInfo.iv = parseRegexDouble(input, @"(?i)\s(1?\d{1,2}[,.]?\d{0,3})\s?\%?\s?IV"); // 52 IV 52% IV 52IV 52.5 IV
if (sniperInfo.iv == default(double))
sniperInfo.iv = parseRegexDouble(input, @"(1?\d{1,2}[,.]?\d{0,3})\s?\%"); // 52% 52 %
if (sniperInfo.iv == default(double))
sniperInfo.iv = parseRegexDouble(input, @"(?i)IV\s?(1?\d{1,2}[,.]?\d{0,3})");
}


private void parseTimestamp(string input)
{
Expand Down
1 change: 1 addition & 0 deletions PogoLocationFeeder/PogoLocationFeeder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Helper\IVParser.cs" />
<Compile Include="Helper\MessageCache.cs" />
<Compile Include="Helper\GeoCoordinatesParser.cs" />
<Compile Include="Helper\PokemonParser.cs" />
Expand Down
1 change: 0 additions & 1 deletion PogoLocationFeeder/PokeSniperReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public List<SniperInfo> readAll()
private SniperInfo map(Result result)
{
SniperInfo sniperInfo = new SniperInfo();
sniperInfo.id = (PokemonId)Enum.Parse(typeof(PokemonId), result.name, true);
PokemonId pokemonId = PokemonParser.parsePokemon(result.name);
sniperInfo.id = pokemonId;
GeoCoordinates geoCoordinates = GeoCoordinatesParser.parseGeoCoordinates(result.coords);
Expand Down
30 changes: 30 additions & 0 deletions PogoLocationFeederTests/Helper/IVParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PogoLocationFeeder.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PogoLocationFeeder.Helper.Tests
{
[TestClass()]
public class IVParserTests
{
[TestMethod()]
public void parseIVTest()
{
Assert.AreEqual(100, IVParser.parseIV("100 IV"));
Assert.AreEqual(88.01, IVParser.parseIV("88,01 IV"));
Assert.AreEqual(5.2, IVParser.parseIV("5.2 % IV"));

Assert.AreEqual(15.0, IVParser.parseIV("15 IV"));
Assert.AreEqual(85.11, IVParser.parseIV("IV 85.11 %"));
Assert.AreEqual(85.11, IVParser.parseIV("IV 85,11"));

Assert.AreEqual(100, IVParser.parseIV("100.00 %"));


}
}
}
47 changes: 47 additions & 0 deletions PogoLocationFeederTests/Helper/MessageParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PogoLocationFeeder.Helper;
using POGOProtos.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PogoLocationFeeder.Helper.Tests
{
[TestClass()]
public class MessageParserTests
{
MessageParser messageParser = new MessageParser();

[TestMethod()]
public void parseMessageTest()
{

verifyParsing("[239 seconds remaining] 52% IV - Jolteon at 42.877637631245,74.620142194759 [ Moveset: ThunderShockFast/Thunderbolt ]",
42.877637631245, 74.620142194759, PokemonId.Jolteon, 52, DateTime.Now.AddSeconds(239));


}

private void verifyParsing(String text, double latitude, double longitude, PokemonId pokemonId, double iv, DateTime expiration)
{
List<SniperInfo> sniperInfo = messageParser.parseMessage(text);
Assert.IsNotNull(sniperInfo);
Assert.AreEqual(pokemonId, sniperInfo[0].id);
Assert.AreEqual(latitude, sniperInfo[0].latitude);
Assert.AreEqual(longitude, sniperInfo[0].longitude);
Assert.AreEqual(iv, sniperInfo[0].iv);
Assert.AreEqual(Truncate(expiration, TimeSpan.FromSeconds(1)), Truncate(sniperInfo[0].timeStamp, TimeSpan.FromSeconds(1)));

}

private static DateTime Truncate(DateTime dateTime, TimeSpan timeSpan)
{
if (timeSpan == TimeSpan.Zero) return dateTime; // Or could throw an ArgumentException
return dateTime.AddTicks(-(dateTime.Ticks % timeSpan.Ticks));
}

}

}
2 changes: 2 additions & 0 deletions PogoLocationFeederTests/PogoLocationFeederTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@
</Choose>
<ItemGroup>
<Compile Include="GeoCoordinatesParserTest.cs" />
<Compile Include="Helper\IVParserTests.cs" />
<Compile Include="Helper\MessageCacheTests.cs" />
<Compile Include="Helper\MessageParserTests.cs" />
<Compile Include="Helper\PokemonParserTests.cs" />
<Compile Include="PokeSniperReaderTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down

0 comments on commit 77b8b64

Please sign in to comment.