<commit_before>var Struct = require('../src/Struct')
  , assert = require('assert')

describe('Creating structs', function () {

  var type = Struct.types.int8
    , buf = Buffer([ 1, 2 ])

  it('supports objects of name→type pairs', function () {
    var struct = Struct({
      name: type
    , otherName: type
    })
    assert.deepEqual(struct(buf), { name: 1, otherName: 2 })
  })

  it('supports .field(name, type)', function () {
    var struct = Struct().field('name', type)
    assert.deepEqual(struct(buf), { name: 1 })

    struct.field('otherName', type)
    assert.deepEqual(struct(buf), { name: 1, otherName: 2 })
  })

})

describe('Struct types', function () {

  var buf = Buffer([ 10, 20 ])
    , write = Buffer(1)
    , opts = { buf: buf, offset: 0 }

  var byte = Struct.types.uint8

  function resetOffset() { opts.offset = 0 }

  beforeEach(resetOffset)

  it('can read a thing', function () {
    assert.equal(byte.read(opts), 10)
    assert.equal(opts.offset, 1)
  })<commit_msg>add test for accessing parent structs<commit_after>var Struct = require('../src/Struct')
  , assert = require('assert')

describe('Creating structs', function () {

  var type = Struct.types.int8
    , buf = Buffer([ 1, 2 ])

  it('supports objects of name→type pairs', function () {
    var struct = Struct({
      name: type
    , otherName: type
    })
    assert.deepEqual(struct(buf), { name: 1, otherName: 2 })
  })

  it('supports .field(name, type)', function () {
    var struct = Struct().field('name', type)
    assert.deepEqual(struct(buf), { name: 1 })

    struct.field('otherName', type)
    assert.deepEqual(struct(buf), { name: 1, otherName: 2 })
  })

})

describe('Value paths', function () {
  var int8 = Struct.types.int8
    , char = Struct.types.char

  it('supports accessing parent structs', function () {
    var struct = Struct({
      size: int8,
      b: Struct({
        text1: char('../size'),
        text2: char('../size')
      })
    })

    assert.deepEqual(
      struct(Buffer([ 2, 0x20, 0x20, 0x68, 0x69 ])),
      { size: 2, b: { text1: '  ', text2: 'hi' } }
    )
  })
})

describe('Struct types', function () {

  var buf = Buffer([ 10, 20 ])
    , write = Buffer(1)
    , opts = { buf: buf, offset: 0 }

  var byte = Struct.types.uint8

  function resetOffset() { opts.offset = 0 }

  beforeEach(resetOffset)

  it('can read a thing', function () {
    assert.equal(byte.read(opts), 10)
    assert.equal(opts.offset, 1)
  })
{'fraction_non_alphanumeric': 0.1708502024291498, 'fraction_numerical': 0.02388663967611336, 'mean_word_length': 2.9472843450479234, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4780076', 'n_tokens_mistral': 883, 'n_tokens_neox': 811, 'n_words': 208}