godog/example/ls_test.go
gedi c6d00dd6d5 refactor to use cocumber gherkin3 parser library
* bdde4c4 fix test suite and migration changes
* a3b6e01 refactor pretty formatter
* 2c0c7ba fix outline scenario handling
* f6b411d add a different language test feature

add a different language test feature
2015-06-25 21:19:02 +03:00

65 строки
1,7 КиБ
Go

package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/DATA-DOG/godog"
)
type lsFeature struct {
dir string
buf *bytes.Buffer
}
func lsFeatureContext(s godog.Suite) {
c := &lsFeature{buf: bytes.NewBuffer(make([]byte, 1024))}
s.Step(`^I am in a directory "([^"]*)"$`, c.iAmInDirectory)
s.Step(`^I have a (file|directory) named "([^"]*)"$`, c.iHaveFileOrDirectoryNamed)
s.Step(`^I run ls$`, c.iRunLs)
s.Step(`^I should get output:$`, c.iShouldGetOutput)
}
func (f *lsFeature) iAmInDirectory(args ...*godog.Arg) error {
f.dir = os.TempDir() + "/" + args[0].String()
if err := os.RemoveAll(f.dir); err != nil && !os.IsNotExist(err) {
return err
}
return os.Mkdir(f.dir, 0775)
}
func (f *lsFeature) iHaveFileOrDirectoryNamed(args ...*godog.Arg) (err error) {
if len(f.dir) == 0 {
return fmt.Errorf("the directory was not chosen yet")
}
switch args[0].String() {
case "file":
err = ioutil.WriteFile(f.dir+"/"+args[1].String(), []byte{}, 0664)
case "directory":
err = os.Mkdir(f.dir+"/"+args[1].String(), 0775)
}
return err
}
func (f *lsFeature) iShouldGetOutput(args ...*godog.Arg) error {
expected := strings.Split(args[0].DocString().Content, "\n")
actual := strings.Split(strings.TrimSpace(f.buf.String()), "\n")
if len(expected) != len(actual) {
return fmt.Errorf("number of expected output lines %d, does not match actual: %d", len(expected), len(actual))
}
for i, line := range actual {
if line != expected[i] {
return fmt.Errorf(`expected line "%s" at position: %d to match "%s", but it did not`, expected[i], i, line)
}
}
return nil
}
func (f *lsFeature) iRunLs(args ...*godog.Arg) error {
f.buf.Reset()
return ls(f.dir, f.buf)
}