Skip to content
Browse files

Fixed bugs in operation serialization/deserialization

  • Loading branch information...
1 parent afc5d0c commit 067707cde075b94c373b0e718ed23623f08cfbb8 @djspiewak committed
View
25 server/src/main/scala/com/codecommit/cccp/OpFormat.scala
@@ -61,6 +61,8 @@ object OpFormat {
var back = Vector[Op]()
while (hasNext) {
+ hasNext = false
+
val id = readString(r)
val parent = readInt(r)
val version = readInt(r)
@@ -69,29 +71,40 @@ object OpFormat {
def apply(c: DocOpCursor) {
var next = r.read()
while (next >= 0) {
- next.toChar match {
+ val continue = next.toChar match {
case '+' => {
r.read() // TODO
c.characters(readString(r))
+ true
}
- case 'r' => c.retain(readInt(r))
+ case 'r' => {
+ c.retain(readInt(r))
+ true
+ }
case '-' => {
r.read() // TODO
c.deleteCharacters(readString(r))
+ true
+ }
+
+ case '\n' => {
+ hasNext = true
+ false
}
}
- next = r.read()
+
+ if (continue)
+ next = r.read()
+ else
+ next = -1
}
}
}
val delta = DocOpUtil.buffer(unbuffered)
-
back = back :+ Op(id, parent, version, delta)
-
- hasNext = r.read() == '\n'
}
back
View
67 server/src/test/scala/com/codecommit/cccp/OpFormatSpecs.scala
@@ -0,0 +1,67 @@
+package com.codecommit.cccp
+
+import org.specs2.mutable._
+import java.io.ByteArrayOutputStream
+import java.io.OutputStreamWriter
+import java.io.CharArrayReader
+
+object OpFormatSpecs extends Specification {
+
+ "operation serialization" should {
+ "serialize/deserialize a single operation" in {
+ val op = Op(0).retain(12).chars("test")
+ val str = writeToString(Vector(op))
+ val ops = readFromString(str)
+
+ ops must haveSize(1)
+ ops.head must beLike {
+ case Op(id, 0, 1, delta) if id == op.id => {
+ delta.size mustEqual 2
+ delta.getRetainItemCount(0) mustEqual 12
+ delta.getCharactersString(1) mustEqual "test"
+ }
+ }
+ }
+
+ "serialize/deserialize multiple operations" in {
+ val op1 = Op(0).retain(12).chars("test")
+ val op2 = Op(1).chars("boo!").retain(37).delete("test?")
+
+ val str = writeToString(Vector(op1, op2))
+ val ops = readFromString(str)
+
+ ops must haveSize(2)
+
+ ops(0) must beLike {
+ case Op(id, 0, 1, delta) if id == op1.id => {
+ delta.size mustEqual 2
+ delta.getRetainItemCount(0) mustEqual 12
+ delta.getCharactersString(1) mustEqual "test"
+ }
+ }
+
+ ops(1) must beLike {
+ case Op(id, 1, 2, delta) if id == op2.id => {
+ delta.size mustEqual 3
+ delta.getCharactersString(0) mustEqual "boo!"
+ delta.getRetainItemCount(1) mustEqual 37
+ delta.getDeleteCharactersString(2) mustEqual "test?"
+ }
+ }
+ }
+ }
+
+ def writeToString(ops: Seq[Op]): String = {
+ val os = new ByteArrayOutputStream
+ val writer = new OutputStreamWriter(os)
+ OpFormat.write(ops, writer)
+ writer.close()
+
+ os.toByteArray map { _.toChar } mkString
+ }
+
+ def readFromString(str: String): Seq[Op] = {
+ val reader = new CharArrayReader(str.toArray)
+ OpFormat.read(reader)
+ }
+}

0 comments on commit 067707c

Please sign in to comment.
Something went wrong with that request. Please try again.