Commit b2bff9c3 authored by Jakob Moser's avatar Jakob Moser
Browse files

Add retry() api to exercises

parent 911dbd8e
Loading
Loading
Loading
Loading
+37 −0
Original line number Diff line number Diff line
@@ -42,6 +42,18 @@ export class Exercise {
        this.#execute.bind(this.#context)()
    }

    /**
     * Allow the user to retry the exercise after they failed, reusing their
     * current terminal session etc.
     * 
     * This is accomplished by restarting the exercise in a context that
     * disables e.g. prepareWith, but normally allows awaiting manual confirmation.
     */
    retry() {
        this.#context = new ExerciseRetryExecutionContext(this.#context)
        this.start()
    }

    /**
     * Inform the execute function that the user has manually confirmed that
     * they are done with the exercise.
@@ -290,6 +302,31 @@ class ExerciseExecutionContext {
    }
}

/**
 * Exercise execution contexts for retry attempts. Disables
 * - describe()
 * - prepareWith()
 * - clear()
 */
class ExerciseRetryExecutionContext extends ExerciseExecutionContext {

    /**
     * Create a ExerciseRetryExecutionContext
     * @param {ExerciseExecutionContext} oldContext An old context from which handlers are inherited
     */
    constructor(oldContext) {
        super()
        this._describeHandler = oldContext._describeHandler
        this._verifyHandler = oldContext._verifyHandler
    }

    describe() {}

    async prepareWith() {}

    async clear() {}
}

/**
 * A test. It has a human-readable name and a list of exercises.
 */