Skip to content

Commit

Permalink
add helpful unit test class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robey Pointer committed Jul 13, 2009
1 parent e4b1f3d commit 6f91dae
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ivy/ivy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<info
organisation="net.lag"
module="naggati"
revision="0.7"
revision="0.7.1"
e:testclass="net.lag.naggati.TestRunner"
e:buildpackage="net/lag/naggati"
/>
Expand Down
27 changes: 26 additions & 1 deletion src/main/scala/net/lag/naggati/Decoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package net.lag.naggati

import scala.collection.mutable
import org.apache.mina.core.buffer.IoBuffer
import org.apache.mina.core.session.IoSession
import org.apache.mina.core.filterchain.IoFilter
import org.apache.mina.core.session.{DummySession, IoSession}
import org.apache.mina.filter.codec._


Expand Down Expand Up @@ -96,3 +98,26 @@ class Decoder(private val firstStep: Step) extends ProtocolDecoder {
state.buffer.limit(state.buffer.position)
}
}


// for use in unit tests
class TestDecoder(firstStep: Step) {
private val fakeDecoderOutput = new ProtocolDecoderOutput {
override def flush(nextFilter: IoFilter.NextFilter, s: IoSession) = {}
override def write(obj: AnyRef) = written += obj
}

private val fakeSession: IoSession = new DummySession
val written = new mutable.ListBuffer[AnyRef]
val decoder = new Decoder(firstStep)

def apply(buffer: IoBuffer): List[AnyRef] = {
decoder.decode(fakeSession, buffer, fakeDecoderOutput)
written.toList
}

def apply(s: String): List[AnyRef] = apply(s.getBytes)
def apply(x: Array[Byte]): List[AnyRef] = apply(IoBuffer.wrap(x))

def write(obj: AnyRef) = fakeDecoderOutput.write(obj)
}
11 changes: 6 additions & 5 deletions src/test/scala/net/lag/naggati/CodecSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,16 @@ object CodecSpec extends Specification {
End
}

val decoder = new Decoder(step)
quickDecode(decoder, "xx")
written mustEqual Nil
val decoder = new TestDecoder(step)
decoder("xx") mustEqual Nil
scored mustEqual false
quickDecode(decoder, "y")
written mustEqual Nil
decoder("y") mustEqual Nil
scored mustEqual true
}


// FIXME: convert these other tests to use TestDecoder too.

"read a variable number of bytes" in {
var n = 2
var rv: List[String] = Nil
Expand Down

0 comments on commit 6f91dae

Please sign in to comment.