import { isNull } from "util"; import { G } from '@cafetextual/util' import SourceLocation from "@cafetextual/util/dist/src/source/SourceLocation"; import PObjectMap from "../org/subalternproductions/seepResource/dsl/parser/PObjectMap"; export class CompareResult { constructor(comparisons:any) { this.comparisons = comparisons var errUri:string this.numErrs = 0; for (errUri in this.comparisons) { this.numErrs++; } } numErrs:number hasErrors:boolean comparisons:{[uri:string]:boolean} show():string { if (!this.hasErrors) { return "ok"; } var key:string var out:Array = []; for (key in this.comparisons) { var o:any = this.comparisons[key]; out.push("err: " + key ); out.push(" expected:" + o.expected); out.push(" found :" + o.found); } var outStr:string = out.join('\n') console.log(outStr) return outStr; } } export default class STest { static assertLoc(o:any, startLine:number, startCol:number, endLine:number, endCol:number , map:PObjectMap, includeToken:boolean = false , out:any, path:string):void { this.assertLocation_low(o, null, -1, startLine, startCol, endLine, endCol , map, includeToken, out, path); } static assertItemLoc(o:any, p:any , startLine:number, startCol:number, endLine:number, endCol:number , map:PObjectMap, includeToken:boolean = false , out:any = null, path:string = null):void { if (G.isNum(p)) { this.assertLocation_low(o, null, p as number, startLine, startCol, endLine, endCol , map, includeToken, out, path ) } else { this.assertLocation_low(o, p as string, -1, startLine, startCol, endLine, endCol , map, includeToken , out, path) } } static assertLocation_low(o:any, propertyName:string, index:number, startLine:number, startCol:number, endLine:number, endCol:number, map:PObjectMap, includeToken:boolean = false, out:any, path:string ):void { var loc:SourceLocation = map.objectToLoc(o, false, propertyName, index); console.log(" expected: (" + startLine + "," + startCol + ") - (" + endLine + "-" + endCol + ")" ); console.log(" found : " + loc.show() ) this.assertEquals(" CP ", startLine, loc.start.line, out, path + "startLine"); this.assertEquals(" CP ", startCol, loc.start.column, out, path + "endLine" ); this.assertEquals(" CP ", endLine, loc.end.line , out, path + "endline" ); this.assertEquals(" CP ", endCol, loc.end.column, out, path + "endCol") } // assertLocation_low static assertEquals(label:string, v1:any, v2:any, out:any, path:string):Boolean { if (v1 === v2) { return true; } out[path] = {expecting:v1, found:v2, path:path} } static fail(v:string):void { console.log(v); throw new Error(v); } /** * iterate dynamic objects and test basic equality, includes * - string, boolean literals, numbers * - child objects * - arrays * * note: * - just iterates o1 and makes sure o2 has equivalent properties, not vice-versa. * */ static compareObjects(o1:any, o2:any, out:any = null, path:string = null):CompareResult { var out:any = this.assertObject_low("", o1, o2) return new CompareResult(out); } static assertObject_low(label:string, o1:any, o2:any, out:any = null, path:string = null):any { out = out || {} path = path || "/" console.log(' compareint object ' + label +" : " + path); if (G.isStr(o1)) { this.assertEquals(label, G.isStr(o2), true, out , path); this.assertEquals(label, o1, o2 , out , path); console.log(' ' + o1 + " == " + o2); } else if (G.isBool(o1)) { this.assertEquals(label, o1, o2, out, path); console.log(' ' + o1 + " == " + o2); } else if (G.isNum(o1)) { this.assertEquals(label, G.isNum(o2), true, out, path); this.assertEquals(label , o1, o2, out ,path); console.log(' ' + o1 + " == " + o2); } else if (G.isArr(o1)) { this.assertEquals(label, G.isArr(o2), true, out, path) var arr1:Array = G.aas(o1, Array) var arr2:Array = G.aas(o2 , Array) this.assertEquals(label + "-len", arr1.length, arr2.length, out, path + ".length"); for (var i:number = 0; i < arr1.length; i++) { var v1:any = o1[i]; var v2:any = o2[i]; this.assertObject_low(label, v1, v2, out, path + "[" + i + "]"); } } else if (G.isObj(o1)) { var key:string for (key in o1) { v1 = o1[key] v2 = o2[key] this.assertObject_low(label, v1, v2, out, path + "/" + key); } } return out } /** * strip properties from an object * * @param o1 - he object * @param ignore - hash of properies to removed, defaults to {__uid:true} */ static stripPs( o1:any, ignore:{[name:string]:any} = null):any { ignore = ignore || {"__uid":true}; if (G.isStr(o1)) { // no action } else if (G.isBool(o1)) { // no action } else if (G.isNum(o1)) { } else if (G.isArr(o1)) { var arr1:Array = o1 as Array for (var i:number = 0; i < arr1.length; i++) { var v1:any = o1[i]; STest.stripPs( v1, ignore); } } else if (G.isObj(o1)) { var key:string for (key in o1) { if (key in ignore) { delete o1[key]; } else { var v2:any = o1[key] STest.stripPs(v2, ignore); } } } } }