From 8d9efaf0352c0ca86bc01c73f72c8243fdfd44c6 Mon Sep 17 00:00:00 2001 From: Kenneth Gudel Date: Mon, 25 Apr 2016 11:41:20 -0400 Subject: [PATCH 1/4] explicitly handle periods, as toInt fails with decimals --- .../grasshopper/geocoder/search/census/SearchUtils.scala | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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..3c1068c 100644 --- a/geocoder/src/main/scala/grasshopper/geocoder/search/census/SearchUtils.scala +++ b/geocoder/src/main/scala/grasshopper/geocoder/search/census/SearchUtils.scala @@ -12,7 +12,9 @@ object SearchUtils { if (isNumeric(s)) { Some(s.toInt) } else { - if (s.contains("-")) { + if (s.contains(".")) { + Some(s.substring(0, s.indexOf(".")).toInt) + } else if (s.contains("-")) { Some(s.substring(0, s.indexOf("-")).toInt) } else if (s == "None") { None @@ -28,6 +30,6 @@ object SearchUtils { } def isNumeric(str: String): Boolean = { - str.matches("-?\\d+(\\.\\d+)?") + str.matches("-?\\d+") } } From 0378656d0cfb5977d25b5231d6ebb59944c4702d Mon Sep 17 00:00:00 2001 From: Kenneth Gudel Date: Mon, 25 Apr 2016 11:50:07 -0400 Subject: [PATCH 2/4] would currently return None when encountering a negative number with a hyphen the change makes the code ignore a hyphen as the first digit to get around this --- .../scala/grasshopper/geocoder/search/census/SearchUtils.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 3c1068c..f676746 100644 --- a/geocoder/src/main/scala/grasshopper/geocoder/search/census/SearchUtils.scala +++ b/geocoder/src/main/scala/grasshopper/geocoder/search/census/SearchUtils.scala @@ -15,7 +15,7 @@ object SearchUtils { if (s.contains(".")) { Some(s.substring(0, s.indexOf(".")).toInt) } else if (s.contains("-")) { - Some(s.substring(0, s.indexOf("-")).toInt) + Some(s.substring(0, s.indexOf("-", 1)).toInt) } else if (s == "None") { None } else { From e456eac200902e8e32cf2629f68d871067eb418d Mon Sep 17 00:00:00 2001 From: Kenneth Gudel Date: Mon, 25 Apr 2016 11:51:28 -0400 Subject: [PATCH 3/4] reverting to scalaCheck 1.12.5 as property based checks fail with 1.13.0 --- project/Version.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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" From 18bb1ae03355375b6189631297e8a25942f39163 Mon Sep 17 00:00:00 2001 From: Kenneth Gudel Date: Mon, 25 Apr 2016 11:52:54 -0400 Subject: [PATCH 4/4] begin adding property tests for searchutils --- .../search/census/NumericGenerators.scala | 19 +++++++++++++ .../search/census/SearchUtilsSpec.scala | 28 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 geocoder/src/test/scala/grasshopper/geocoder/search/census/NumericGenerators.scala create mode 100644 geocoder/src/test/scala/grasshopper/geocoder/search/census/SearchUtilsSpec.scala 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) + } + } +}