Skip to content
This repository was archived by the owner on Jan 8, 2020. It is now read-only.
Open

Utils #217

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -28,6 +30,6 @@ object SearchUtils {
}

def isNumeric(str: String): Boolean = {
str.matches("-?\\d+(\\.\\d+)?")
str.matches("-?\\d+")
}
}
Original file line number Diff line number Diff line change
@@ -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
}

}
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
2 changes: 1 addition & 1 deletion project/Version.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down