Implement a unit test case for parsing trans output
This commit is contained in:
@ -7,29 +7,30 @@ import (
|
||||
"log"
|
||||
)
|
||||
|
||||
type Translator interface {
|
||||
Translate() []string
|
||||
|
||||
type Translation struct {
|
||||
job TransJob
|
||||
Executor func(TransJob) ([]byte, error)
|
||||
}
|
||||
|
||||
type transJob struct {
|
||||
type TransJob struct {
|
||||
word string
|
||||
fromLang string
|
||||
toLang string
|
||||
}
|
||||
|
||||
func New(word, fromLang, toLang string) Translator {
|
||||
return transJob{word: word, fromLang: fromLang, toLang: toLang}
|
||||
}
|
||||
|
||||
func (tj transJob) Translate() []string {
|
||||
outBytes, err := tj.execTrans()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to execute command 'trans': %v", err)
|
||||
func New(word, fromLang, toLang string) Translation {
|
||||
return Translation{
|
||||
job: TransJob{
|
||||
word: word,
|
||||
fromLang: fromLang,
|
||||
toLang: toLang,
|
||||
},
|
||||
Executor: executeTransShell,
|
||||
}
|
||||
return uniqueSlice(parseTransOutput(string(outBytes)))
|
||||
}
|
||||
|
||||
func (tj transJob) execTrans() ([]byte, error) {
|
||||
func executeTransShell(tj TransJob) ([]byte, error) {
|
||||
config := []string{
|
||||
"-no-ansi",
|
||||
"-show-original", "n",
|
||||
@ -43,6 +44,14 @@ func (tj transJob) execTrans() ([]byte, error) {
|
||||
return exec.Command("trans", config...).Output()
|
||||
}
|
||||
|
||||
func (t Translation) Translate() []string {
|
||||
outBytes, err := t.Executor(t.job)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to execute command 'trans': %v", err)
|
||||
}
|
||||
return uniqueSlice(parseTransOutput(string(outBytes)))
|
||||
}
|
||||
|
||||
func parseTransOutput(out string) []string {
|
||||
outLines := strings.Split(out, "\n")
|
||||
translation := outLines[0]
|
||||
|
Reference in New Issue
Block a user