/
goextract_test.go
83 lines (69 loc) · 2.34 KB
/
goextract_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main_test
import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
. "github.com/petergtz/goextract"
. "github.com/petergtz/goextract/testutil"
"github.com/petergtz/goextract/util"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
// This is a workaround for the current way of creating test cases.
// A better way could be to actually move the test cases back into this file
// instead of looping automatically through all test files.
var focusedTests = map[string]bool{
// "for_statement_withing_case_block": true,
}
var pendingTests = map[string]bool{
"multiple_simple_statements_taking_parameters_declared_beforehand_changed_within_and_used_afterwards": true,
"comments_in_statements": true,
"comments_outside_extracted_statements": true,
}
var _ = Describe("Goextract", func() {
fileInfos, err := ioutil.ReadDir("test_data")
util.PanicOnError(err)
for _, fileInfo := range fileInfos {
filename := fileInfo.Name()
if !strings.HasSuffix(filename, ".go.input") {
continue
}
prefix := strings.TrimSuffix(filename, ".go.input")
it := It
if pendingTests[filepath.Base(prefix)] {
// TODO ideally this would use PIt to do better reporting, but it actually
// has a differnt method signature.
continue
}
if focusedTests[filepath.Base(prefix)] {
it = FIt
}
it("Can extract a "+strings.Replace(prefix, "_", " ", -1), func() {
selection, extractedFuncName := extractionDataFrom(filepath.Join("test_data", prefix) + ".go.extract")
tmpfile, err := ioutil.TempFile("", "goextract")
util.PanicOnError(err)
defer os.Remove(tmpfile.Name())
ExtractFileToFile(filepath.Join("test_data", filename), selection, extractedFuncName, tmpfile.Name(), true)
Expect(tmpfile.Name()).To(HaveSameContentAs(filepath.Join("test_data", prefix) + ".go.output"))
})
}
})
func extractionDataFrom(filename string) (Selection, string) {
parts := strings.Split(strings.TrimRight(util.ReadFileAsStringOrPanic(filename), "\n"), " ")
Expect(parts).To(HaveLen(5))
return Selection{
Position{toInt(parts[0]), toInt(parts[1])},
Position{toInt(parts[2]), toInt(parts[3])},
},
parts[4]
}
func toInt(s string) int {
i, err := strconv.Atoi(s)
util.PanicOnError(err)
return i
}
func expectedOutputFor(prefix string) string {
return util.ReadFileAsStringOrPanic(filepath.Join("test_data", prefix) + ".output")
}