-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
beac75a
commit a56a7b9
Showing
4 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters