File tree Expand file tree Collapse file tree 4 files changed +78
-3
lines changed
Expand file tree Collapse file tree 4 files changed +78
-3
lines changed Original file line number Diff line number Diff line change @@ -35,15 +35,19 @@ import {
3535 * var moddle = new Moddle([pkg]);
3636 *
3737 * @param {Array<Package> } packages the packages to contain
38+ *
39+ * @param { { strict?: boolean } } [config] moddle configuration
3840 */
39- export default function Moddle ( packages ) {
41+ export default function Moddle ( packages , config = { } ) {
4042
4143 this . properties = new Properties ( this ) ;
4244
4345 this . factory = new Factory ( this , this . properties ) ;
4446 this . registry = new Registry ( packages , this . properties ) ;
4547
4648 this . typeCache = { } ;
49+
50+ this . config = config ;
4751}
4852
4953
Original file line number Diff line number Diff line change @@ -145,6 +145,12 @@ Properties.prototype.getProperty = function(target, name) {
145145 return null ;
146146 }
147147
148+ const strict = model . config . strict === true ;
149+
150+ if ( strict ) {
151+ throw new TypeError ( `unknown property <${ name } > on <${ target . $type } >` ) ;
152+ }
153+
148154 // eslint-disable-next-line no-undef
149155 typeof console !== 'undefined' && console . warn ( `unknown property <${ name } > on <${ target . $type } >` ) ;
150156
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ export function createModelBuilder(base) {
2121 throw new Error ( '[test-util] must specify a base directory' ) ;
2222 }
2323
24- function createModel ( packageNames ) {
24+ function createModel ( packageNames , config = { } ) {
2525
2626 var packages = map ( packageNames , function ( f ) {
2727 var pkg = cache [ f ] ;
@@ -38,7 +38,7 @@ export function createModelBuilder(base) {
3838 return pkg ;
3939 } ) ;
4040
41- return new Moddle ( packages ) ;
41+ return new Moddle ( packages , config ) ;
4242 }
4343
4444 return createModel ;
Original file line number Diff line number Diff line change @@ -400,4 +400,69 @@ describe('moddle', function() {
400400
401401 } ) ;
402402
403+
404+ describe ( 'property access (strict)' , function ( ) {
405+
406+ const moddle = createModel ( [
407+ 'properties'
408+ ] , {
409+ strict : true
410+ } ) ;
411+
412+
413+ it ( 'should configure in strict mode' , function ( ) {
414+
415+ // then
416+ expect ( moddle . config . strict ) . to . be . true ;
417+ } ) ;
418+
419+
420+ describe ( 'typed' , function ( ) {
421+
422+ it ( 'should access basic' , function ( ) {
423+
424+ // when
425+ const element = moddle . create ( 'props:ComplexCount' , {
426+ count : 10
427+ } ) ;
428+
429+ // then
430+ expect ( element . get ( 'count' ) ) . to . eql ( 10 ) ;
431+ expect ( element . get ( 'props:count' ) ) . to . eql ( 10 ) ;
432+
433+ // available under base name
434+ expect ( element . count ) . to . exist ;
435+ } ) ;
436+
437+
438+ it ( 'fail accessing unknown property' , function ( ) {
439+
440+ // when
441+ const element = moddle . create ( 'props:ComplexCount' ) ;
442+
443+ // then
444+ expect ( ( ) => {
445+ element . get ( 'foo' ) ;
446+ } ) . to . throw ( / u n k n o w n p r o p e r t y < f o o > o n < p r o p s : C o m p l e x C o u n t > / ) ;
447+
448+ expect ( ( ) => {
449+ element . set ( 'foo' , 10 ) ;
450+ } ) . to . throw ( / u n k n o w n p r o p e r t y < f o o > o n < p r o p s : C o m p l e x C o u n t > / ) ;
451+ } ) ;
452+
453+
454+ it ( 'fail instantiating with unknown property' , function ( ) {
455+
456+ // then
457+ expect ( ( ) => {
458+ moddle . create ( 'props:ComplexCount' , {
459+ foo : 10
460+ } ) ;
461+ } ) . to . throw ( / u n k n o w n p r o p e r t y < f o o > o n < p r o p s : C o m p l e x C o u n t > / ) ;
462+ } ) ;
463+
464+ } ) ;
465+
466+ } ) ;
467+
403468} ) ;
You can’t perform that action at this time.
0 commit comments