I am writing a program that reads input from a file, parses the text in that file, and then prints out text to stdout. I have the expected output in a text file. How would I go about using the testing package to compare my program's output to the expected output saved in a text file?
Let's say my code is the following:
package main
import (
"fmt"
"log"
"os"
"bufio"
"strings"
)
func main() {
if len(os.Args) > 1 {
file, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Println(strings.ToUpper(scanner.Text()))
}
} else {
fmt.Println("Need input file as argument")
os.Exit(1)
}
}
This is simply taking each line of the input text file, capitalizing all letters, and then printing each line.
In my input file, I have:
hello, wOrlD!
foo
BAR
And in my expected output file, I have:
HELLO, WORLD!
FOO
BAR
What is the best way to use the testing package to compare my output to the expected output file?
Thanks!