Skip to content

Commit

Permalink
make string type identifiers work (type coersion)
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed May 13, 2012
1 parent e3bab35 commit f58189c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/struct.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ function set (buffer, offset, value) {

function defineProperty (name, type) {
debug('defining new struct type field', name)

// allow string types for convenience
type = ref.coerceType(type)

assert(!this._instanceCreated, 'an instance of this Struct type has already '
+ 'been created, cannot add new "fields" anymore')
assert.equal('string', typeof name, 'expected a "string" field name')
Expand Down
16 changes: 16 additions & 0 deletions test/struct.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ describe('Struct', function () {
assert.equal(400, ps.childB.b)
})

describe('string type identifiers', function () {

it('should work with string type identifiers', function () {
var S = Struct({
'int': 'int'
, 'long': 'long'
, 'string': 'string'
})

assert.strictEqual(ref.types.int, S.fields.int.type)
assert.strictEqual(ref.types.long, S.fields.long.type)
assert.strictEqual(ref.types.Utf8String, S.fields.string.type)
})

})

describe('offsets and sizeofs', function () {

function test (structType, testNumber) {
Expand Down

0 comments on commit f58189c

Please sign in to comment.