@@ -2431,14 +2431,13 @@ SQLActiveCode.prototype.runProg = function() {
24312431 }
24322432 $ ( this . output ) . text ( "" )
24332433 // Run this query
2434- let query = this . buildProg ( ) ;
2434+ let query = this . buildProg ( false ) ; // false --> Do not include suffix
24352435 try {
24362436 var res = this . db . exec ( query ) ;
24372437 } catch ( error ) {
24382438 result_mess = error . toString ( ) ;
24392439 $ ( this . output ) . text ( error ) ;
24402440 $ ( this . outDiv ) . show ( ) ;
2441-
24422441 }
24432442 this . logRunEvent ( {
24442443 'div_id' : this . divid ,
@@ -2479,8 +2478,71 @@ SQLActiveCode.prototype.runProg = function() {
24792478 respDiv . appendChild ( table )
24802479 $ ( this . outDiv ) . show ( )
24812480
2481+ // Now handle autograding
2482+ if ( this . suffix ) {
2483+ result = this . autograde ( res [ 0 ] ) ;
2484+ $ ( this . output ) . text ( result ) ;
2485+ }
2486+
2487+ }
2488+
2489+ SQLActiveCode . prototype . autograde = function ( result_table ) {
2490+ tests = this . suffix . split ( / \n / ) ;
2491+ this . passed = 0 ;
2492+ this . failed = 0 ;
2493+ // Tests should be of the form
2494+ // assert row,col oper value for example
2495+ // assert 4,4 == 3
2496+ result = ""
2497+ tests = tests . filter ( function ( s ) {
2498+ return s . indexOf ( 'assert' ) > - 1
2499+ } )
2500+ for ( let test of tests ) {
2501+ [ assert , loc , oper , expected ] = test . split ( / \s + / ) ;
2502+ [ row , col ] = loc . split ( ',' ) ;
2503+ result += this . testOneAssert ( row , col , oper , expected , result_table ) ;
2504+ result += "\n"
2505+ }
2506+ pct = 100 * this . passed / ( this . passed + this . failed ) ;
2507+ pct = pct . toLocaleString ( undefined , { maximumFractionDigits : 2 } ) ;
2508+ result += `You passed ${ this . passed } out of ${ this . passed + this . failed } tests for ${ pct } %`
2509+ this . logBookEvent ( { event : 'unittest' ,
2510+ div_id : this . divid ,
2511+ course : eBookConfig . course ,
2512+ act : `percent:${ pct } :passed:${ this . passed } :failed:${ this . failed } `
2513+ } ) ;
2514+ return result ;
2515+ }
2516+
2517+ SQLActiveCode . prototype . testOneAssert = function ( row , col , oper , expected , result_table ) {
2518+ let actual = result_table . values [ row ] [ col ]
2519+ const operators = {
2520+ "==" : function ( operand1 , operand2 ) {
2521+ return operand1 == operand2 ;
2522+ } ,
2523+ "!=" : function ( operand1 , operand2 ) {
2524+ return operand1 != operand2 ;
2525+ } ,
2526+ ">" : function ( operand1 , operand2 ) {
2527+ return operand1 > operand2 ;
2528+ } ,
2529+ "<" : function ( operand1 , operand2 ) {
2530+ return operand1 > operand2 ;
2531+ }
2532+ } ;
2533+
2534+ res = operators [ oper ] ( actual , expected )
2535+ if ( res ) {
2536+ output = `Pass: ${ actual } ${ oper } ${ expected } in row ${ row } column ${ result_table . columns [ col ] } ` ;
2537+ this . passed ++ ;
2538+ } else {
2539+ output = `Failed ${ actual } ${ oper } ${ expected } in row ${ row } column ${ result_table . columns [ col ] } ` ;
2540+ this . failed ++
2541+ }
2542+ return output ;
24822543}
24832544
2545+
24842546function createTable ( tableData ) {
24852547 var table = document . createElement ( 'table' ) ;
24862548 var head = document . createElement ( 'thead' ) ;
0 commit comments