Implement warning emitter (Closes #327)
This commit is contained in:
parent
f4c67993f6
commit
851d8edfdd
|
@ -16,6 +16,8 @@ class Session {
|
||||||
this.passes = options.passes || [];
|
this.passes = options.passes || [];
|
||||||
this.visitor = options.visitor || ast.visitor;
|
this.visitor = options.visitor || ast.visitor;
|
||||||
|
|
||||||
|
if ( typeof options.warn === "function" ) this.warn = options.warn;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
parse( input, options ) {
|
parse( input, options ) {
|
||||||
|
@ -30,6 +32,8 @@ class Session {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
warn( message, details ) {}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Session;
|
module.exports = Session;
|
||||||
|
|
|
@ -9,12 +9,8 @@ module.exports = function ( chai, utils ) {
|
||||||
|
|
||||||
const Assertion = chai.Assertion;
|
const Assertion = chai.Assertion;
|
||||||
|
|
||||||
Assertion.addMethod( "changeAST", function ( grammar, props, options, additionalRuleProps ) {
|
function parse( grammar, session, options ) {
|
||||||
|
|
||||||
options = typeof options !== "undefined" ? options : {};
|
|
||||||
additionalRuleProps = typeof additionalRuleProps !== "undefined" ? additionalRuleProps : { reportFailures: true };
|
|
||||||
|
|
||||||
const session = new Session( { grammar } );
|
|
||||||
const ast = session.parse( grammar );
|
const ast = session.parse( grammar );
|
||||||
|
|
||||||
if ( ! options.allowedStartRules ) {
|
if ( ! options.allowedStartRules ) {
|
||||||
|
@ -25,6 +21,18 @@ module.exports = function ( chai, utils ) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ast;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Assertion.addMethod( "changeAST", function ( grammar, props, options, additionalRuleProps ) {
|
||||||
|
|
||||||
|
options = typeof options !== "undefined" ? options : {};
|
||||||
|
additionalRuleProps = typeof additionalRuleProps !== "undefined" ? additionalRuleProps : { reportFailures: true };
|
||||||
|
|
||||||
|
const session = new Session( { grammar } );
|
||||||
|
const ast = parse( grammar, session, options );
|
||||||
|
|
||||||
ast.rules = ast.rules.map( rule => Object.assign( rule, additionalRuleProps ) );
|
ast.rules = ast.rules.map( rule => Object.assign( rule, additionalRuleProps ) );
|
||||||
|
|
||||||
utils.flag( this, "object" )( ast, session, options );
|
utils.flag( this, "object" )( ast, session, options );
|
||||||
|
@ -38,15 +46,7 @@ module.exports = function ( chai, utils ) {
|
||||||
options = typeof options !== "undefined" ? options : {};
|
options = typeof options !== "undefined" ? options : {};
|
||||||
|
|
||||||
const session = new Session( { grammar } );
|
const session = new Session( { grammar } );
|
||||||
const ast = session.parse( grammar );
|
const ast = parse( grammar, session, options );
|
||||||
|
|
||||||
if ( ! options.allowedStartRules ) {
|
|
||||||
|
|
||||||
options.allowedStartRules = ast.rules.length > 0
|
|
||||||
? [ ast.rules[ 0 ].name ]
|
|
||||||
: [];
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
let passed, result;
|
let passed, result;
|
||||||
|
|
||||||
|
@ -84,4 +84,52 @@ module.exports = function ( chai, utils ) {
|
||||||
|
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
Assertion.addMethod( "reportWarning", function ( grammar, warnings, options ) {
|
||||||
|
|
||||||
|
warnings = Array.isArray( warnings )
|
||||||
|
? warnings
|
||||||
|
: warnings == null
|
||||||
|
? []
|
||||||
|
: [ warnings ];
|
||||||
|
|
||||||
|
options = typeof options !== "undefined" ? options : {};
|
||||||
|
|
||||||
|
const messages = [];
|
||||||
|
function warn( message ) {
|
||||||
|
|
||||||
|
messages.push( message );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const session = new Session( { grammar, warn } );
|
||||||
|
const ast = parse( grammar, session, options );
|
||||||
|
|
||||||
|
utils.flag( this, "object" )( ast, session, options );
|
||||||
|
|
||||||
|
const messagesCount = messages.length;
|
||||||
|
const warningsCount = warnings.length;
|
||||||
|
|
||||||
|
if ( warnings.length )
|
||||||
|
|
||||||
|
this.assert(
|
||||||
|
messagesCount === warningsCount,
|
||||||
|
`expected #{this} to report ${ warningsCount } warnings, but it reported ${ messagesCount } warnings`,
|
||||||
|
`expected #{this} to not report ${ warningsCount } warnings`,
|
||||||
|
warnings,
|
||||||
|
messages
|
||||||
|
);
|
||||||
|
|
||||||
|
warnings.forEach( warning => {
|
||||||
|
|
||||||
|
this.assert(
|
||||||
|
messages.indexOf( warning ) !== -1,
|
||||||
|
"expected #{this} to report the warning #{exp}, but it didn't",
|
||||||
|
"expected #{this} to not report the warning #{exp}",
|
||||||
|
warning
|
||||||
|
);
|
||||||
|
|
||||||
|
} );
|
||||||
|
|
||||||
|
} );
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue