mercredi 23 octobre 2019

Testing CLI arguments in go

I want to test some CLI routines in go that are run in the main() function purely, they are just exercises that I'm doing, but I want to make tests on them! So for example how can I pass arguments to table test this king of algorithm?

package main


import (
    "bufio"
    "fmt"
    "os"
    "strconv"
)

func main() {
    var f *os.File
    f = os.Stdin
    defer f.Close()

    scanner := bufio.NewScanner(f)

    for scanner.Scan() {
        if scanner.Text() == "STOP" || scanner.Text() == "stop" {
            break
        }

        n, err := strconv.ParseInt(scanner.Text(), 10, 64)

        if err == nil {
            fmt.Printf("Number formatted: %d\n", n)
        } else {
            fmt.Println(err.Error())
        }
    }
}

I put the code on playground too for better help! https://play.golang.org/p/JgrQ2yFogNs

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire