Commit 9349d39a authored by Jakob Moser's avatar Jakob Moser
Browse files

Add .commandWas(cmd) api

parent e4d1fb0b
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -253,6 +253,35 @@ class ExerciseExecutionContext {
                    // !! converts any value to a boolean (not a special operator, just two ! (boolean not) operators in series)
                    const result = !!predicate(latestEntry.output)

                    verifyHandler(result)
                }, 200)
            },

            /**
             * Verify the command matches a certain condition.
             * 
             * - If param is a string, check that the command is exactly equal to param
             * - If param is a function, the function is called with the actual command as parameter, and we check it returns
             *   a truthy value.
             * 
             * @param {Function|String} param String to exactly match, or predicate function
             */
            commandWas(param) {
                // Wait for 200ms to ensure that the VM has processed the input and written some
                // output to the terminal
                setTimeout(() => {
                    const terminalContents = getTerminalContents()
                    const latestEntry = terminalContents[terminalContents.length - 1]


                    // Poor man's switch-case: depending on the "typeof param", a different verification predicate is chosen
                    const predicate = {
                        "function": param,
                        "string": (actualInput) => actualInput === param,
                    }[typeof param]

                    const result = !!predicate(latestEntry.input)

                    verifyHandler(result)
                }, 200)
            }