           _readIndex: 0,
         })
 
-        assert.equal("(the_rule)", document.rootNode())
+        assert.equal("(sentence (word) (word))", document.rootNode().toString())
+
+      describe "when the input.read() returns something other than a string", ->
+        it "stops reading", ->
+          input = {
+            read: ->
+              @_readIndex++
+              [
+                "the", "-", "word",
+                {},
+                "the", "-", "word",
+                " "
+              ][@_readIndex - 1]
+
+            seek: (n) ->
+              0
+
+            _readIndex: 0,
+          }
+
+          document.setInput(input)
+
+          assert.equal("(sentence (word))", document.rootNode().toString())
+          assert.equal(4, input._readIndex)
 
     describe "when the supplied object does not implement #seek(n)", ->
       it "throws an exception", ->

treeSitter = require "tree-sitter"
assert = require "assert"
compiler = require ".."

require("segfault-handler").registerHandler()

describe "Document", ->
  document = null
  language = null

  before ->
    grammar = compiler.grammar
      name: "test"
      rules:
        the_rule: -> "stuff"

    language = compiler.compileAndLoad(grammar)

  beforeEach ->
    document = new treeSitter.Document()

        document.setInput({
          read: ->
            @_readIndex++
            [
              "the", "-", "word",
              " ",
              "the", "-", "word",
              ""
            ][@_readIndex - 1]

          seek: (n) ->
            0
