Skip to content

Commit

Permalink
Fix bug with .cast forcing bad values to NA, not NM.
Browse files Browse the repository at this point in the history
  • Loading branch information
tixxit committed Nov 4, 2014
1 parent 8afecbf commit d9dacb2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions framian/src/main/scala/framian/ColumnTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ final class DefaultColumnTyper[A: ClassTag] extends ColumnTyper[A] {
if (classTag[A].runtimeClass isAssignableFrom col.classTagA.runtimeClass) {
col.column.asInstanceOf[Column[A]]
} else {
Column.empty[A]()
col.column.flatMap { _ => NM }
}
}

Expand All @@ -82,6 +82,6 @@ final class TypeableColumnTyper[A: ClassTag: Typeable] extends ColumnTyper[A] {
if (classTag[A].runtimeClass isAssignableFrom col.classTagA.runtimeClass) {
col.column.asInstanceOf[Column[A]]
} else {
col.column.flatMap { a => Cell.fromOption(a.cast[A]) }
col.column.flatMap { a => Cell.fromOption(a.cast[A], NM) }
}
}
18 changes: 18 additions & 0 deletions framian/src/test/scala/framian/UntypedColumnSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,22 @@ class UntypedColumnSpec extends Specification {
(0 to 4).map(col(_)) must_== Seq(Value(1), Value(2), Value(3), Value(4), NA)
}
}

"cast" should {
"return NMs when values don't make sense" in {
val col0 = TypedColumn(Column(Value(42), NA, NM, Value(32))).cast[String]
col0(0) must_== NM
col0(1) must_== NA
col0(2) must_== NM
col0(3) must_== NM
col0(4) must_== NA

val col1 = TypedColumn(Column(Value("x"), NA, NM, Value("y"))).cast[Double]
col1(0) must_== NM
col1(1) must_== NA
col1(2) must_== NM
col1(3) must_== NM
col1(4) must_== NA
}
}
}

0 comments on commit d9dacb2

Please sign in to comment.