diff --git a/geocoder/src/main/scala/grasshopper/geocoder/search/census/SearchUtils.scala b/geocoder/src/main/scala/grasshopper/geocoder/search/census/SearchUtils.scala index de02da8..f676746 100644 --- a/geocoder/src/main/scala/grasshopper/geocoder/search/census/SearchUtils.scala +++ b/geocoder/src/main/scala/grasshopper/geocoder/search/census/SearchUtils.scala @@ -12,8 +12,10 @@ object SearchUtils { if (isNumeric(s)) { Some(s.toInt) } else { - if (s.contains("-")) { - Some(s.substring(0, s.indexOf("-")).toInt) + if (s.contains(".")) { + Some(s.substring(0, s.indexOf(".")).toInt) + } else if (s.contains("-")) { + Some(s.substring(0, s.indexOf("-", 1)).toInt) } else if (s == "None") { None } else { @@ -28,6 +30,6 @@ object SearchUtils { } def isNumeric(str: String): Boolean = { - str.matches("-?\\d+(\\.\\d+)?") + str.matches("-?\\d+") } } diff --git a/geocoder/src/test/scala/grasshopper/geocoder/search/census/NumericGenerators.scala b/geocoder/src/test/scala/grasshopper/geocoder/search/census/NumericGenerators.scala new file mode 100644 index 0000000..a2b0233 --- /dev/null +++ b/geocoder/src/test/scala/grasshopper/geocoder/search/census/NumericGenerators.scala @@ -0,0 +1,19 @@ +package grasshopper.geocoder.search.census + +import org.scalacheck.Gen + +trait NumericGenerators { + + def digits: Gen[Int] = { + for { + n <- Gen.choose(-10000, 10000) + } yield n + } + + def positive: Gen[Int] = { + for { + n <- Gen.choose(0, 10000) + } yield n + } + +} \ No newline at end of file diff --git a/geocoder/src/test/scala/grasshopper/geocoder/search/census/SearchUtilsSpec.scala b/geocoder/src/test/scala/grasshopper/geocoder/search/census/SearchUtilsSpec.scala new file mode 100644 index 0000000..d12ba13 --- /dev/null +++ b/geocoder/src/test/scala/grasshopper/geocoder/search/census/SearchUtilsSpec.scala @@ -0,0 +1,28 @@ +package grasshopper.geocoder.search.census + +import org.scalatest.{ PropSpec, MustMatchers } +import org.scalatest.prop.PropertyChecks +import grasshopper.geocoder.search.census.SearchUtils._ + +class SearchUtilsSpec extends PropSpec with MustMatchers with PropertyChecks with NumericGenerators { + + property("digits are parsed into Ints") { + forAll(digits) { n => + toInt(n.toString) mustBe Some(n) + } + } + + property("decimals are parsed into Ints") { + forAll(digits, positive) { (x, y) => + val numString = x.toString + "." + y.toString + toInt(numString) mustBe Some(x) + } + } + + property("hyphens are parsed into Ints") { + forAll(digits, positive) { (x, y) => + val numString = x.toString + "-" + y.toString + toInt(numString) mustBe Some(x) + } + } +} diff --git a/project/Version.scala b/project/Version.scala index a6ff555..c384942 100644 --- a/project/Version.scala +++ b/project/Version.scala @@ -4,7 +4,7 @@ object Version { val config = "1.3.0" val akka = "2.4.4" val scalaTest = "3.0.0-M15" - val scalaCheck = "1.13.0" + val scalaCheck = "1.12.5" val elasticsearch = "2.2.0" val jts = "1.13" val scale = "0.0.2"