better test support
This commit is contained in:
		@@ -67,8 +67,8 @@ namespace console.screen {
 | 
			
		||||
        if (!lines) {
 | 
			
		||||
            lines = [];
 | 
			
		||||
            console.addListener(log);
 | 
			
		||||
            brick.buttonUp.onEvent(ButtonEvent.Click, () => scroll(1))
 | 
			
		||||
            brick.buttonDown.onEvent(ButtonEvent.Click, () => scroll(-1))                
 | 
			
		||||
            brick.buttonUp.onEvent(ButtonEvent.Click, () => scroll(-1))
 | 
			
		||||
            brick.buttonDown.onEvent(ButtonEvent.Click, () => scroll(1))                
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -75,9 +75,9 @@ namespace motors {
 | 
			
		||||
     * @param buf message buffer
 | 
			
		||||
     */
 | 
			
		||||
    //%
 | 
			
		||||
    export function readPWM(buf: Buffer): number {
 | 
			
		||||
    export function readPWM(buf: Buffer): void {
 | 
			
		||||
        init()
 | 
			
		||||
        return pwmMM.read(buf);
 | 
			
		||||
        pwmMM.read(buf);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -261,9 +261,16 @@ namespace motors {
 | 
			
		||||
        //%
 | 
			
		||||
        isReady(): boolean {
 | 
			
		||||
            this.init();
 | 
			
		||||
            const r = readPWM(mkCmd(this._port, DAL.opOutputTest, 0))
 | 
			
		||||
            // 0 = ready, 1 = busy
 | 
			
		||||
            return r == 0;
 | 
			
		||||
            const buf = mkCmd(this._port, DAL.opOutputTest, 2);
 | 
			
		||||
            readPWM(buf)
 | 
			
		||||
            const flags = buf.getNumber(NumberFormat.UInt8LE, 2);
 | 
			
		||||
            // TODO: FIX with ~ support
 | 
			
		||||
            for(let i = 0; i < DAL.NUM_OUTPUTS; ++i) {
 | 
			
		||||
                const flag = 1 << i;
 | 
			
		||||
                if ((this._port & flag) && (flags & flag))
 | 
			
		||||
                    return false;
 | 
			
		||||
            }
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
@@ -529,8 +536,8 @@ namespace motors {
 | 
			
		||||
    export const largeCD = new SynchedMotorPair(Output.CD);
 | 
			
		||||
 | 
			
		||||
    function reset(out: Output) {
 | 
			
		||||
        let b = mkCmd(out, DAL.opOutputReset, 0)
 | 
			
		||||
        writePWM(b)
 | 
			
		||||
        writePWM(mkCmd(out, DAL.opOutputReset, 0))
 | 
			
		||||
        writePWM(mkCmd(out, DAL.opOutputClearCount, 0))
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function outOffset(out: Output) {
 | 
			
		||||
 
 | 
			
		||||
@@ -23,9 +23,12 @@ namespace tests {
 | 
			
		||||
            // clear state
 | 
			
		||||
            this.reset();
 | 
			
		||||
 | 
			
		||||
            console.log(`# ${this.name}`)
 | 
			
		||||
            console.log(`> ${this.name}`)
 | 
			
		||||
            this.handler()
 | 
			
		||||
 | 
			
		||||
            if (this.errors.length)
 | 
			
		||||
                console.log('')
 | 
			
		||||
 | 
			
		||||
            // ensure clean state after test
 | 
			
		||||
            this.reset();
 | 
			
		||||
        }
 | 
			
		||||
@@ -39,13 +42,15 @@ namespace tests {
 | 
			
		||||
 | 
			
		||||
        const start = control.millis();
 | 
			
		||||
        console.sendToScreen();
 | 
			
		||||
        console.log(`${_tests.length} tests`)
 | 
			
		||||
        console.log(`${_tests.length} tests found`)
 | 
			
		||||
        console.log(` `)
 | 
			
		||||
        for (let i = 0; i < _tests.length; ++i) {
 | 
			
		||||
            const t = _currentTest = _tests[i];
 | 
			
		||||
            t.run();
 | 
			
		||||
            _currentTest = undefined;
 | 
			
		||||
        }
 | 
			
		||||
        console.log(`${_tests.map(t => t.errors.length).reduce((p, c) => p + c, 0)} X, ${Math.ceil((control.millis() - start) / 1000)}s`)
 | 
			
		||||
        console.log(` `)
 | 
			
		||||
        console.log(`${_tests.length} tests, ${_tests.map(t => t.errors.length).reduce((p, c) => p + c, 0)} errs in ${Math.ceil((control.millis() - start) / 1000)}s`)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -71,7 +76,7 @@ namespace tests {
 | 
			
		||||
    //% blockId=testAssert block="assert %message|%condition"
 | 
			
		||||
    export function assert(message: string, condition: boolean) {
 | 
			
		||||
        if (!condition) {
 | 
			
		||||
            console.log(` X ${message || ''}`)
 | 
			
		||||
            console.log(`!!! ${message || ''}`)
 | 
			
		||||
            if (_currentTest)
 | 
			
		||||
                _currentTest.errors.push(message);
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,47 +1,42 @@
 | 
			
		||||
```typescript
 | 
			
		||||
// add tests package
 | 
			
		||||
tests.test("lgB set speed 10", () => {
 | 
			
		||||
    motors.largeB.setSpeed(10);
 | 
			
		||||
    loops.pause(100)
 | 
			
		||||
    loops.pause(500)
 | 
			
		||||
    tests.assertClose("speedB", 10, motors.largeB.speed(), 2)
 | 
			
		||||
});
 | 
			
		||||
tests.test("lgB set speed 25 (reversed)", () => {
 | 
			
		||||
    motors.largeB.setReversed(true)
 | 
			
		||||
    motors.largeB.setSpeed(25)
 | 
			
		||||
    loops.pause(100)
 | 
			
		||||
    loops.pause(500)
 | 
			
		||||
    tests.assertClose("speedB", -25, motors.largeB.speed(), 2)
 | 
			
		||||
});
 | 
			
		||||
tests.test("lgBC set speed 5", () => {
 | 
			
		||||
    motors.largeBC.setSpeed(5)
 | 
			
		||||
    loops.pause(100)
 | 
			
		||||
    loops.pause(500)
 | 
			
		||||
    tests.assertClose("speedB", 5, motors.largeB.speed(), 1);
 | 
			
		||||
    tests.assertClose("speedC", 5, motors.largeC.speed(), 1);
 | 
			
		||||
});
 | 
			
		||||
tests.test("lgBC steer 50% 2x", () => {
 | 
			
		||||
    motors.largeBC.setBrake(true)
 | 
			
		||||
    motors.largeBC.steer(50, 50, 1, MoveUnit.Rotations)
 | 
			
		||||
    loops.pause(1000)
 | 
			
		||||
    loops.pause(2000)
 | 
			
		||||
    tests.assertClose("largeB", 360, motors.largeB.angle(), 5)
 | 
			
		||||
    motors.largeBC.setBrake(false)
 | 
			
		||||
})
 | 
			
		||||
tests.test("lgBC steer 50% 500deg", () => {
 | 
			
		||||
tests.test("lgBC steer 360deg", () => {
 | 
			
		||||
    motors.largeBC.setBrake(true)
 | 
			
		||||
    motors.largeBC.steer(50, 50, 135, MoveUnit.Degrees)
 | 
			
		||||
    loops.pause(1000)
 | 
			
		||||
    tests.assertClose("largeB", 135, motors.largeB.angle(), 5)
 | 
			
		||||
    motors.largeBC.steer(50, 50, 360, MoveUnit.Degrees)
 | 
			
		||||
    loops.pause(2000)
 | 
			
		||||
    tests.assertClose("largeB", 360, motors.largeB.angle(), 5)
 | 
			
		||||
});
 | 
			
		||||
tests.test("lgBC steer 50% 2s", () => {
 | 
			
		||||
tests.test("lgBC steer 50% 1s", () => {
 | 
			
		||||
    motors.largeBC.setBrake(true)
 | 
			
		||||
    motors.largeBC.steer(50, 50, 500, MoveUnit.MilliSeconds)
 | 
			
		||||
    loops.pause(1000)
 | 
			
		||||
    motors.largeBC.steer(10, 50, 1000, MoveUnit.MilliSeconds)
 | 
			
		||||
    loops.pause(2000)
 | 
			
		||||
})
 | 
			
		||||
tests.test("lgBC tank 50% 720deg", () => {
 | 
			
		||||
tests.test("lgBC tank 50% 180deg", () => {
 | 
			
		||||
    motors.largeBC.setBrake(true)
 | 
			
		||||
    motors.largeBC.tank(50, 50, 180, MoveUnit.Degrees)
 | 
			
		||||
    loops.pause(1000)
 | 
			
		||||
    tests.assertClose("largeB", 180, motors.largeB.angle(), 5)
 | 
			
		||||
});
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
```package
 | 
			
		||||
tests
 | 
			
		||||
```
 | 
			
		||||
		Reference in New Issue
	
	Block a user