World Conquest Chronicles

World Conquest Chronicles

go-code-runner, v1.1

The library that implements the compiling and running of a code written in the Go programming language and running of a test case set for the compiled code (i.e. the executable file).

Running of a test case set for the compiled code (i.e. the executable file).

Change Log

  • running of a test case set for the compiled code (i.e. the executable file):
    • representation of a test case:
      • input;
      • expected output;
    • checking of an actual output in each test case:
      • returning of the sentinel errors:
        • failed running — it returns on a running error;
        • unexpected output — it returns when the expected and actual outputs do not match.

Features

  • saving of a code to a temporary file:
    • storing of the temporary file with the code to an individual temporary directory;
  • compiling of a code written in the Go programming language:
    • automatic importing of the packages used in the code;
    • enriching of an error of the external command running by an output from the stderr stream;
  • running of the compiled code (i.e. the executable file):
    • passing of a custom input as the stdin stream;
    • returning of an output from the stdout stream;
    • enriching of an error of the external command running by an output from the stderr stream;
  • running of a test case set for the compiled code (i.e. the executable file):
    • representation of a test case:
      • input;
      • expected output;
    • checking of an actual output in each test case:
      • returning of the sentinel errors:
        • failed running — it returns on a running error;
        • unexpected output — it returns when the expected and actual outputs do not match.

Examples

testrunner.RunCode (success):

package main

import (
    "fmt"
    "log"
    "os"
    "path/filepath"

    coderunner "github.com/thewizardplusplus/go-code-runner"
    testrunner "github.com/thewizardplusplus/go-code-runner/test-runner"
)

func main() {
    const code = `
        package main

        func main() {
            var x, y int
            fmt.Scan(&x, &y)

            fmt.Println(x + y)
        }
    `

    pathToCode, err := coderunner.SaveTemporaryCode(code)
    if err != nil {
        log.Fatal(err)
    }
    defer os.RemoveAll(filepath.Dir(pathToCode)) // nolint: errcheck

    pathToExecutable, err := coderunner.CompileCode(pathToCode)
    if err != nil {
        log.Fatal(err)
    }

    err = testrunner.RunCode(pathToExecutable, []testrunner.TestCase{
        {Input: "5 12", ExpectedOutput: "17\n"},
        {Input: "23 42", ExpectedOutput: "65\n"},
    })
    fmt.Printf("%v\n", err)

    // Output:
    // <nil>
}

testrunner.RunCode (error):

package main

import (
    "fmt"
    "log"
    "os"
    "path/filepath"

    coderunner "github.com/thewizardplusplus/go-code-runner"
    testrunner "github.com/thewizardplusplus/go-code-runner/test-runner"
)

func main() {
    const code = `
        package main

        func main() {
            var x, y int
            fmt.Scan(&x, &y)

            fmt.Println(x + y)
        }
    `

    pathToCode, err := coderunner.SaveTemporaryCode(code)
    if err != nil {
        log.Fatal(err)
    }
    defer os.RemoveAll(filepath.Dir(pathToCode)) // nolint: errcheck

    pathToExecutable, err := coderunner.CompileCode(pathToCode)
    if err != nil {
        log.Fatal(err)
    }

    err = testrunner.RunCode(pathToExecutable, []testrunner.TestCase{
        {Input: "5 12", ExpectedOutput: "17\n"},
        {Input: "23 42", ExpectedOutput: "100\n"},
    })
    fmt.Printf("%v\n", err)

    // Output:
    // unexpected output (input - "23 42"): expected - "100\n", actual - "65\n"
}

Repository

Link: https://github.com/thewizardplusplus/go-code-runner/tree/v1.1.

Content: code.

License: MIT.