-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from ExcuseMi/master
Extract code IV Parser + Wrote tests/ Fix Farfetch'd Error
- Loading branch information
Showing
7 changed files
with
120 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %")); | ||
|
||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters