Home > Backend Development > Golang > How to Unit Test Command Line Flag Values Against an Enumeration in Go?

How to Unit Test Command Line Flag Values Against an Enumeration in Go?

Mary-Kate Olsen
Release: 2024-11-05 09:59:02
Original
891 people have browsed it

How to Unit Test Command Line Flag Values Against an Enumeration in Go?

Testing Command Line Flags in Go

This article explores testing techniques for command line flags in Golang. Specifically, we will examine how to unit test flag values against an enumeration.

Problem Statement

Given the following code:

<code class="go">// Define flag for output format
var formatType string

// Constants representing valid format types
const (
    text = "text"
    json = "json"
    hash = "hash"
)

// Initialize flags
func init() {
    flag.StringVar(&formatType, "format", "text", "Desired output format")
}

// Main function
func main() {
    flag.Parse()
}</code>
Copy after login

We wish to write a unit test to verify that the -format flag value matches one of the predefined constants.

Solution Using Custom Flag Type

To test flags in a more flexible manner, we can utilize the flag.Var function with a custom type implementing the Value interface.

<code class="go">package main

import (
    "errors"
    "flag"
    "fmt"
)

// Custom type representing format type
type formatType string

// String() method for Value interface
func (f *formatType) String() string {
    return fmt.Sprint(*f)
}

// Set() method for Value interface
func (f *formatType) Set(value string) error {
    if len(*f) > 0 && *f != "text" {
        return errors.New("format flag already set")
    }
    if value != "text" && value != "json" && value != "hash" {
        return errors.New("Invalid Format Type")
    }
    *f = formatType(value)
    return nil
}

// Initialize flag with custom type
func init() {
    typeFlag := "text" // Default value
    usage := `Format type. Must be "text", "json" or "hash". Defaults to "text".`
    flag.Var(&typeFlag, "format", usage)
}

// Main function
func main() {
    flag.Parse()
    fmt.Println("Format type is", typeFlag)
}
</code>
Copy after login

In this solution, flag.Var takes a pointer to a custom type that satisfies the Value interface, allowing us to define our own validation logic within the Set method.

Unit Testing Custom Flag Type

Unit tests for the custom flag type can be written as follows:

<code class="go">// Test unit validates that the format flag is within the enumeration
func TestFormatFlag(t *testing.T) {
    testCases := []struct {
        input       string
        expectedErr string
    }{
        {"text", ""},
        {"json", ""},
        {"hash", ""},
        {"", "Invalid Format Type"},
        {"xml", "Invalid Format Type"},
    }

    for _, tc := range testCases {
        t.Run(tc.input, func(t *testing.T) {
            args := []string{"-format", tc.input}
            flag.CommandLine = flag.NewFlagSet("test", flag.PanicOnError)
            err := flag.CommandLine.Parse(args)

            if err != nil && err.Error() != tc.expectedErr {
                t.Errorf("Unexpected error: %v", err)
                return
            }
        })
    }
}</code>
Copy after login

The above is the detailed content of How to Unit Test Command Line Flag Values Against an Enumeration in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template