Skip to content

Commit

Permalink
Add TrinaryType
Browse files Browse the repository at this point in the history
  • Loading branch information
excelwebzone committed Sep 9, 2016
1 parent beac75a commit a56a7b9
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/enum/type-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import SystemUtils from 'gdbots/common/util/system-utils';
* @method static TypeName TIME_UUID()
* @method static TypeName TIMESTAMP()
* @method static TypeName TINY_INT()
* @method static TypeName TRINARY()
* @method static TypeName UUID()
*/
export default class TypeName extends SystemUtils.mixinClass(Enum) {}
Expand Down Expand Up @@ -71,5 +72,6 @@ TypeName.initEnum({
TIME_UUID: 'time-uuid',
TIMESTAMP: 'timestamp',
TINY_INT: 'tiny-int',
TRINARY: 'trinary',
UUID: 'uuid'
});
73 changes: 73 additions & 0 deletions src/type/trinary-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';

import SystemUtils from 'gdbots/common/util/system-utils';
import AbstractIntType from 'gdbots/pbj/type/abstract-int-type';

/**
* @link https://en.wikipedia.org/wiki/Three-valued_logic
* 0 = unknown
* 1 = true
* 2 = false
*/
export default class TrinaryType extends SystemUtils.mixinClass(AbstractIntType)
{
/**
* {@inheritdoc}
*/
guard(value, field) {
if ([0, 1, 2].indexOf(value) === -1) {
throw new Error('Field [' + field.getName() + '] value [' + value + '] is not a valid. Must be 0, 1, or 2.');
}
}

/**
* {@inheritdoc}
*/
encode(value, field, codec = null) {
let tmp = parseInt(value);
return isNaN(tmp) || !isFinite(tmp) ? 0 : tmp;
}

/**
* {@inheritdoc}
*/
decode(value, field, codec = null) {
let tmp = parseInt(value);
return isNaN(tmp) || !isFinite(tmp) ? 0 : tmp;
}

/**
* {@inheritdoc}
*/
getDefault() {
return 0;
}

/**
* {@inheritdoc}
*/
isNumeric() {
return true;
}

/**
* {@inheritdoc}
*/
getMin() {
return 0;
}

/**
* {@inheritdoc}
*/
getMax() {
return 2;
}

/**
* {@inheritdoc}
*/
allowedInSet() {
return false;
}
}
73 changes: 73 additions & 0 deletions tests/type/trinary-type-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';

import TrinaryType from 'gdbots/pbj/type/trinary-type';
import FieldBuilder from 'gdbots/pbj/field-builder';

describe('trinary-type-test', function() {
it('validate encoding', function(done) {
let field = FieldBuilder.create('trinary_unknown', TrinaryType.create()).build();
let type = field.getType();

type.encode(0, field).should.eql(0);
type.encode(1, field).should.eql(1);
type.encode(2, field).should.eql(2);

done();
});

it('validate decoding', function(done) {
let field = FieldBuilder.create('trinary_unknown', TrinaryType.create()).build();
let type = field.getType();

type.decode(null, field).should.eql(0);
type.decode(0, field).should.eql(0);
type.decode(1, field).should.eql(1);
type.decode(2, field).should.eql(2);

type.decode('0', field).should.eql(0);
type.decode('1', field).should.eql(1);
type.decode('2', field).should.eql(2);

done();
});

it('validate values', function(done) {
let field = FieldBuilder.create('trinary_unknown', TrinaryType.create()).build();
let type = field.getType();

type.guard(0, field);
type.guard(1, field);
type.guard(2, field);

done();
});

it('invalid values validation', function(done) {
let field = FieldBuilder.create('trinary_unknown', TrinaryType.create()).build();
let type = field.getType();
let thrown = false;

let invalid = [
'a',
[],
3,
-1,
false,
true,
];

invalid.forEach(function(val) {
try {
type.guard(val, field);
} catch (e) {
thrown = true;
}

if (false === thrown) {
console.log('TrinaryType field accepted invalid value [' + val + '].');
}
});

done();
});
});
2 changes: 0 additions & 2 deletions tests/type/type-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ describe('type-test', function() {
thrown = true;
}

thrown.should.eql(true);

if (false === thrown) {
console.log('[' + TypeName.name + '] accepted more than [' + field.getType().getMaxBytes() + '] bytes.');
}
Expand Down

0 comments on commit a56a7b9

Please sign in to comment.