       ]
       anArray: [@ReferenceField 'self', []]
 
+class LocalPost extends Document
+  # Other fields:
+  #   body
+  #   subdocument
+  #     body
+  #   nested
+  #     body
+
+  @Meta
+    name: 'LocalPost'
+    collection: null
+    fields: =>
+      author: @ReferenceField LocalPerson, ['username', 'displayName', 'field1', 'field2'], true, 'posts', ['body', 'subdocument.body', 'nested.body']
+      subscribers: [@ReferenceField LocalPerson]
+      reviewers: [@ReferenceField LocalPerson, [username: 1]]
+      subdocument:
+        person: @ReferenceField LocalPerson, {'username': 1, 'displayName': 1, 'field1': 1, 'field2': 1}, false, 'subdocumentPosts', ['body', 'subdocument.body', 'nested.body']
+        persons: [@ReferenceField LocalPerson, ['username', 'displayName', 'field1', 'field2'], true, 'subdocumentsPosts', ['body', 'subdocument.body', 'nested.body']]
+        slug: @GeneratedField 'self', ['body', 'subdocument.body'], (fields) ->
+          if _.isUndefined(fields.body) or _.isUndefined(fields.subdocument?.body)
+            [fields._id, undefined]
+          else if _.isNull(fields.body) or _.isNull(fields.subdocument.body)
+            [fields._id, null]
+          else
+            [fields._id, "subdocument-prefix-#{ fields.body.toLowerCase() }-#{ fields.subdocument.body.toLowerCase() }-suffix"]
+      nested: [
+        required: @ReferenceField LocalPerson, ['username', 'displayName', 'field1', 'field2'], true, 'nestedPosts', ['body', 'subdocument.body', 'nested.body']
+        optional: @ReferenceField LocalPerson, ['username'], false
+        slug: @GeneratedField 'self', ['body', 'nested.body'], (fields) ->
+          for nested in fields.nested or []
+            if _.isUndefined(fields.body) or _.isUndefined(nested.body)
+              [fields._id, undefined]
+            else if _.isNull(fields.body) or _.isNull(nested.body)
+              [fields._id, null]
+            else
+              [fields._id, "nested-prefix-#{ fields.body.toLowerCase() }-#{ nested.body.toLowerCase() }-suffix"]
+      ]
+      slug: @GeneratedField 'self', ['body', 'subdocument.body'], (fields) ->
+        if _.isUndefined(fields.body) or _.isUndefined(fields.subdocument?.body)
+          [fields._id, undefined]
+        else if _.isNull(fields.body) or _.isNull(fields.subdocument.body)
+          [fields._id, null]
+        else
+          [fields._id, "prefix-#{ fields.body.toLowerCase() }-#{ fields.subdocument.body.toLowerCase() }-suffix"]
+      tags: [
+        @GeneratedField 'self', ['body', 'subdocument.body', 'nested.body'], (fields) ->
+          tags = []
+          if fields.body and fields.subdocument?.body
+            tags.push "tag-#{ tags.length }-prefix-#{ fields.body.toLowerCase() }-#{ fields.subdocument.body.toLowerCase() }-suffix"
+          if fields.body and fields.nested and _.isArray fields.nested
+            for nested in fields.nested when nested.body
+              tags.push "tag-#{ tags.length }-prefix-#{ fields.body.toLowerCase() }-#{ nested.body.toLowerCase() }-suffix"
+          [fields._id, tags]
+      ]
+    triggers: =>
+      testTrigger: @Trigger ['body'], (newDocument, oldDocument) ->
+        return unless newDocument?._id
+        globalTestTriggerCounters[newDocument._id] = (globalTestTriggerCounters[newDocument._id] or 0) + 1
+
+class LocalPerson extends Document
+  # Other fields:
+  #   username
+  #   displayName
+  #   field1
+  #   field2
+
+  @Meta
+    name: 'LocalPerson'
+    collection: null
+    fields: =>
+      count: @GeneratedField 'self', ['posts', 'subdocumentPosts', 'subdocumentsPosts', 'nestedPosts'], (fields) ->
+        [fields._id, (fields.posts?.length or 0) + (fields.nestedPosts?.length or 0) + (fields.subdocumentPosts?.length or 0) + (fields.subdocumentsPosts?.length or 0)]
+
+  formatName: ->
+    "#{ @username }-#{ @displayName or "none" }"
+
 Document.defineAll()
 
 # Just to make sure things are sane

  test.equal Person.Meta._name, 'Person'
  test.equal Person.Meta.parent, _TestPerson.Meta
  test.equal Person.Meta.document, Person
  test.equal Person.Meta._name, 'Person'
  test.equal Person.Meta.collection._name, 'Persons'
  test.equal _.size(Person.Meta.triggers), 0
  test.equal _.size(Person.Meta.fields), 5

  Meteor.call 'wait-for-database', expect (error) ->
    test.isFalse error, error?.toString?() or error

ALL = @ALL = [User, UserLink, CircularFirst, CircularSecond, SpecialPerson, Recursive, IdentityGenerator, SpecialPost, Post, Person, PostLink, SubfieldItem, LocalPost, LocalPerson]

testDocumentList = (test, list) ->
  test.equal Document.list, list, "expected: #{ (d.Meta._name for d in list) } vs. actual: #{ (d.Meta._name for d in Document.list) }"
