Skip to content

Commit

Permalink
Added converters for bytes, longs, shorts and floats
Browse files Browse the repository at this point in the history
  • Loading branch information
paradigmatic committed Jul 5, 2011
1 parent 11bc5b0 commit 1527a16
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/ValueConverter.scala
Expand Up @@ -51,26 +51,54 @@ object ValueConverter {
object ValueConverters {

/**
* Converts a string, into itself... Well just an identity converter.
* Converts a string, to itself... Well just an identity converter.
*/
implicit val stringConverter = ValueConverter[String]( s => s )

/**
* Convert strings into ints.
* Convert strings to bytes.
*/
implicit val byteConverter = ValueConverter[Byte](
s => java.lang.Byte.parseByte(s)
)

/**
* Convert strings to shorts.
*/
implicit val shortConverter = ValueConverter[Short](
s => java.lang.Short.parseShort(s)
)

/**
* Convert strings to ints.
*/
implicit val intConverter = ValueConverter[Int](
s => java.lang.Integer.parseInt(s)
)

/**
* Convert strings into doubles.
* Convert strings to longs.
*/
implicit val longConverter = ValueConverter[Long](
s => java.lang.Long.parseLong(s)
)

/**
* Convert strings to floats.
*/
implicit val floatConverter = ValueConverter[Float](
s => java.lang.Float.parseFloat(s)
)

/**
* Convert strings to doubles.
*/
implicit val doubleConverter = ValueConverter[Double](
s => java.lang.Double.parseDouble(s)
)

/**
* Convert strings into Booleans. The strings values: "T", "true" and "on"
* Convert strings to Booleans. The strings values: "T", "true" and "on"
* will be converted to true and the strings: "F", "false" and "off" will
* be converted to false.
*/
Expand All @@ -84,7 +112,7 @@ object ValueConverters {
if( trues contains s ) true
else if ( falses contains s ) false
else throw new IllegalArgumentException(
s + " could not be converted in into a Boolean"
s + " could not be converted in to a Boolean"
)
}
}
Expand Down
47 changes: 47 additions & 0 deletions test-src/ValueConverterSpec.scala
Expand Up @@ -17,6 +17,28 @@ class ValueConverterSpec extends WordSpec with ShouldMatchers{
}
}

"The byte converter" should {
"parse a string in Byte" in {
convert[Byte](Option("12")) should be (Option(12:Byte))
}
"return a exception when the string cannot be parsed" in {
intercept[Exception] {
convert[Byte](Option("129"))
}
}
}

"The short converter" should {
"parse a string in short" in {
convert[Short](Option("1234")) should be (Option(1234:Short))
}
"return a exception when the string cannot be parsed" in {
intercept[Exception] {
convert[Short](Option("12341234"))
}
}
}

"The int converter" should {
"parse a string in Int" in {
convert[Int](Option("1234")) should be (Option(1234))
Expand All @@ -28,6 +50,31 @@ class ValueConverterSpec extends WordSpec with ShouldMatchers{
}
}

"The long converter" should {
"parse a string in Long" in {
convert[Long](Option("1234")) should be (Option(1234:Long))
}
"return a exception when the string cannot be parsed" in {
intercept[Exception] {
convert[Long](Option("12.34"))
}
}
}

"The float converter" should {
"parse a string into a Float" in {
convert[Float](Option("1234")) should be (Option(1234.0f))
convert[Float](Option("1e-9")) should be (Option(1e-9f))
convert[Float](Option(".1")) should be (Option(.1f))
}
"return a exception when the string cannot be parsed" in {
intercept[Exception] {
convert[Float](Option("1ef-9"))
}
}
}


"The double converter" should {
"parse a string into a Double" in {
convert[Double](Option("1234")) should be (Option(1234.0))
Expand Down

0 comments on commit 1527a16

Please sign in to comment.