加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main_test.go 17.85 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
)
var update = flag.Bool("update", false, "update golden (.out) files")
// This is the directory where our test fixtures are.
const fixtureDir = "./test-fixtures"
func TestRewrite(t *testing.T) {
test := []struct {
cfg *config
file string
}{
{
file: "struct_add",
cfg: &config{
add: []string{"json"},
output: "source",
structName: "foo",
transform: "snakecase",
},
},
{
file: "struct_add_existing",
cfg: &config{
add: []string{"json"},
output: "source",
structName: "foo",
transform: "snakecase",
},
},
{
file: "struct_format",
cfg: &config{
add: []string{"gaum"},
output: "source",
structName: "foo",
transform: "snakecase",
valueFormat: "field_name={field}",
},
},
{
file: "struct_format_existing",
cfg: &config{
add: []string{"gaum"},
output: "source",
structName: "foo",
transform: "snakecase",
valueFormat: "field_name={field}",
},
},
{
file: "struct_format_oldstyle",
cfg: &config{
add: []string{"gaum"},
output: "source",
structName: "foo",
transform: "snakecase",
valueFormat: "field_name=$field",
},
},
{
file: "struct_format_existing_oldstyle",
cfg: &config{
add: []string{"gaum"},
output: "source",
structName: "foo",
transform: "snakecase",
valueFormat: "field_name=$field",
},
},
{
file: "struct_remove",
cfg: &config{
remove: []string{"json"},
output: "source",
structName: "foo",
},
},
{
file: "struct_clear_tags",
cfg: &config{
clear: true,
output: "source",
structName: "foo",
},
},
{
file: "struct_clear_options",
cfg: &config{
clearOption: true,
output: "source",
structName: "foo",
},
},
{
file: "line_add",
cfg: &config{
add: []string{"json"},
output: "source",
line: "4",
transform: "snakecase",
},
},
{
file: "line_add_override",
cfg: &config{
add: []string{"json"},
output: "source",
line: "4,5",
transform: "snakecase",
override: true,
},
},
{
file: "line_add_override_column",
cfg: &config{
add: []string{"json:MyBar:bar"},
output: "source",
line: "4,4",
transform: "snakecase",
override: true,
},
},
{
file: "line_add_override_mixed_column_and_equal",
cfg: &config{
add: []string{"json:MyBar:bar:foo=qux"},
output: "source",
line: "4,4",
transform: "snakecase",
override: true,
},
},
{
file: "line_add_override_multi_equal",
cfg: &config{
add: []string{"json:MyBar=bar=foo"},
output: "source",
line: "4,4",
transform: "snakecase",
override: true,
},
},
{
file: "line_add_override_multi_column",
cfg: &config{
add: []string{"json:MyBar:bar:foo"},
output: "source",
line: "4,4",
transform: "snakecase",
override: true,
},
},
{
file: "line_add_no_override",
cfg: &config{
add: []string{"json"},
output: "source",
line: "4,5",
transform: "snakecase",
},
},
{
file: "line_add_outside",
cfg: &config{
add: []string{"json"},
output: "source",
line: "2,8",
transform: "snakecase",
},
},
{
file: "line_add_outside_partial_start",
cfg: &config{
add: []string{"json"},
output: "source",
line: "2,5",
transform: "snakecase",
},
},
{
file: "line_add_outside_partial_end",
cfg: &config{
add: []string{"json"},
output: "source",
line: "5,8",
transform: "snakecase",
},
},
{
file: "line_add_intersect_partial",
cfg: &config{
add: []string{"json"},
output: "source",
line: "5,11",
transform: "snakecase",
},
},
{
file: "line_add_comment",
cfg: &config{
add: []string{"json"},
output: "source",
line: "6,7",
transform: "snakecase",
},
},
{
file: "line_add_option",
cfg: &config{
addOptions: []string{"json=omitempty"},
output: "source",
line: "4,7",
},
},
{
file: "line_add_option_existing",
cfg: &config{
addOptions: []string{"json=omitempty"},
output: "source",
line: "6,8",
},
},
{
file: "line_add_multiple_option",
cfg: &config{
addOptions: []string{"json=omitempty", "hcl=squash"},
add: []string{"hcl"},
output: "source",
line: "4,7",
transform: "snakecase",
},
},
{
file: "line_add_option_with_equal",
cfg: &config{
addOptions: []string{"validate=max=32"},
add: []string{"validate"},
output: "source",
line: "4,7",
transform: "snakecase",
},
},
{
file: "line_remove",
cfg: &config{
remove: []string{"json"},
output: "source",
line: "5,7",
},
},
{
file: "line_remove_option",
cfg: &config{
removeOptions: []string{"hcl=squash"},
output: "source",
line: "4,8",
},
},
{
file: "line_remove_options",
cfg: &config{
removeOptions: []string{"json=omitempty", "hcl=omitnested"},
output: "source",
line: "4,7",
},
},
{
file: "line_remove_option_with_equal",
cfg: &config{
removeOptions: []string{"validate=max=32"},
output: "source",
line: "4,7",
},
},
{
file: "line_multiple_add",
cfg: &config{
add: []string{"json"},
output: "source",
line: "5,6",
transform: "camelcase",
},
},
{
file: "line_lispcase_add",
cfg: &config{
add: []string{"json"},
output: "source",
line: "4,6",
transform: "lispcase",
},
},
{
file: "line_camelcase_add",
cfg: &config{
add: []string{"json"},
output: "source",
line: "4,5",
transform: "camelcase",
},
},
{
file: "line_camelcase_add_embedded",
cfg: &config{
add: []string{"json"},
output: "source",
line: "4,6",
transform: "camelcase",
},
},
{
file: "line_value_add",
cfg: &config{
add: []string{"json:foo"},
output: "source",
line: "4,6",
},
},
{
file: "offset_add",
cfg: &config{
add: []string{"json"},
output: "source",
offset: 32,
transform: "snakecase",
},
},
{
file: "offset_add_composite",
cfg: &config{
add: []string{"json"},
output: "source",
offset: 40,
transform: "snakecase",
},
},
{
file: "offset_add_duplicate",
cfg: &config{
add: []string{"json"},
output: "source",
offset: 209,
transform: "snakecase",
},
},
{
file: "offset_add_literal_in",
cfg: &config{
add: []string{"json"},
output: "source",
offset: 46,
transform: "snakecase",
},
},
{
file: "offset_add_literal_out",
cfg: &config{
add: []string{"json"},
output: "source",
offset: 32,
transform: "snakecase",
},
},
{
file: "errors",
cfg: &config{
add: []string{"json"},
output: "source",
line: "4,7",
transform: "snakecase",
},
},
{
file: "line_pascalcase_add",
cfg: &config{
add: []string{"json"},
output: "source",
line: "4,5",
transform: "pascalcase",
},
},
{
file: "line_pascalcase_add_embedded",
cfg: &config{
add: []string{"json"},
output: "source",
line: "4,6",
transform: "pascalcase",
},
},
{
file: "not_formatted",
cfg: &config{
add: []string{"json"},
output: "source",
line: "3,4",
transform: "snakecase",
},
},
{
file: "skip_private",
cfg: &config{
add: []string{"json"},
output: "source",
structName: "foo",
transform: "snakecase",
skipUnexportedFields: true,
},
},
{
file: "skip_private_multiple_names",
cfg: &config{
add: []string{"json"},
output: "source",
structName: "foo",
transform: "snakecase",
skipUnexportedFields: true,
},
},
{
file: "skip_embedded",
cfg: &config{
add: []string{"json"},
output: "source",
structName: "StationCreated",
transform: "snakecase",
skipUnexportedFields: true,
},
},
{
file: "all_structs",
cfg: &config{
add: []string{"json"},
output: "source",
all: true,
transform: "snakecase",
},
},
{
file: "line_titlecase_add",
cfg: &config{
add: []string{"json"},
output: "source",
line: "4,6",
transform: "titlecase",
},
},
{
file: "line_titlecase_add_embedded",
cfg: &config{
add: []string{"json"},
output: "source",
line: "4,6",
transform: "titlecase",
},
},
{
file: "field_add",
cfg: &config{
add: []string{"json"},
output: "source",
structName: "foo",
fieldName: "bar",
transform: "snakecase",
},
},
{
file: "field_add_same_line",
cfg: &config{
add: []string{"json"},
output: "source",
structName: "foo",
fieldName: "qux",
transform: "snakecase",
},
},
{
file: "field_add_existing",
cfg: &config{
add: []string{"json"},
output: "source",
structName: "foo",
fieldName: "bar",
transform: "snakecase",
},
},
{
file: "field_clear_tags",
cfg: &config{
clear: true,
output: "source",
structName: "foo",
fieldName: "bar",
},
},
{
file: "field_clear_options",
cfg: &config{
clearOption: true,
output: "source",
structName: "foo",
fieldName: "bar",
},
},
{
file: "field_remove",
cfg: &config{
remove: []string{"json"},
output: "source",
structName: "foo",
fieldName: "bar",
},
},
{
file: "offset_anonymous_struct",
cfg: &config{
add: []string{"json"},
output: "source",
offset: 45,
transform: "camelcase",
},
},
{
file: "offset_star_struct",
cfg: &config{
add: []string{"json"},
output: "source",
offset: 35,
transform: "camelcase",
},
},
{
file: "offset_array_struct",
cfg: &config{
add: []string{"json"},
output: "source",
offset: 35,
transform: "camelcase",
},
},
}
for _, ts := range test {
t.Run(ts.file, func(t *testing.T) {
ts.cfg.file = filepath.Join(fixtureDir, fmt.Sprintf("%s.input", ts.file))
node, err := ts.cfg.parse()
if err != nil {
t.Fatal(err)
}
start, end, err := ts.cfg.findSelection(node)
if err != nil {
t.Fatal(err)
}
rewrittenNode, err := ts.cfg.rewrite(node, start, end)
if err != nil {
if _, ok := err.(*rewriteErrors); !ok {
t.Fatal(err)
}
}
out, err := ts.cfg.format(rewrittenNode, err)
if err != nil {
t.Fatal(err)
}
got := []byte(out)
// update golden file if necessary
golden := filepath.Join(fixtureDir, fmt.Sprintf("%s.golden", ts.file))
if *update {
err := ioutil.WriteFile(golden, got, 0644)
if err != nil {
t.Error(err)
}
return
}
// get golden file
want, err := ioutil.ReadFile(golden)
if err != nil {
t.Fatal(err)
}
var from []byte
if ts.cfg.modified != nil {
from, err = ioutil.ReadAll(ts.cfg.modified)
} else {
from, err = ioutil.ReadFile(ts.cfg.file)
}
if err != nil {
t.Fatal(err)
}
// compare
if !bytes.Equal(got, want) {
t.Errorf("case %s\ngot:\n====\n\n%s\nwant:\n=====\n\n%s\nfrom:\n=====\n\n%s\n",
ts.file, got, want, from)
}
})
}
}
func TestJSON(t *testing.T) {
test := []struct {
cfg *config
file string
err error
}{
{
file: "json_single",
cfg: &config{
add: []string{"json"},
line: "5",
},
},
{
file: "json_full",
cfg: &config{
add: []string{"json"},
line: "4,6",
},
},
{
file: "json_intersection",
cfg: &config{
add: []string{"json"},
line: "5,16",
},
},
{
// both small & end range larger than file
file: "json_single",
cfg: &config{
add: []string{"json"},
line: "30,32", // invalid selection
},
err: errors.New("line selection is invalid"),
},
{
// end range larger than file
file: "json_single",
cfg: &config{
add: []string{"json"},
line: "4,50", // invalid selection
},
err: errors.New("line selection is invalid"),
},
{
file: "json_errors",
cfg: &config{
add: []string{"json"},
line: "4,7",
},
},
{
file: "json_not_formatted",
cfg: &config{
add: []string{"json"},
line: "3,4",
},
},
{
file: "json_not_formatted_2",
cfg: &config{
add: []string{"json"},
line: "3,3",
},
},
{
file: "json_not_formatted_3",
cfg: &config{
add: []string{"json"},
offset: 23,
},
},
{
file: "json_not_formatted_4",
cfg: &config{
add: []string{"json"},
offset: 51,
},
},
{
file: "json_not_formatted_5",
cfg: &config{
add: []string{"json"},
offset: 29,
},
},
{
file: "json_not_formatted_6",
cfg: &config{
add: []string{"json"},
line: "2,54",
},
},
{
file: "json_all_structs",
cfg: &config{
add: []string{"json"},
all: true,
},
},
}
for _, ts := range test {
t.Run(ts.file, func(t *testing.T) {
ts.cfg.file = filepath.Join(fixtureDir, fmt.Sprintf("%s.input", ts.file))
// these are explicit and shouldn't be changed for this particular
// main test
ts.cfg.output = "json"
ts.cfg.transform = "camelcase"
node, err := ts.cfg.parse()
if err != nil {
t.Fatal(err)
}
start, end, err := ts.cfg.findSelection(node)
if err != nil {
t.Fatal(err)
}
rewrittenNode, err := ts.cfg.rewrite(node, start, end)
if err != nil {
if _, ok := err.(*rewriteErrors); !ok {
t.Fatal(err)
}
}
out, err := ts.cfg.format(rewrittenNode, err)
if !reflect.DeepEqual(err, ts.err) {
t.Logf("want: %v", ts.err)
t.Logf("got: %v", err)
t.Fatalf("unexpected error")
}
if ts.err != nil {
return
}
got := []byte(out)
// update golden file if necessary
golden := filepath.Join(fixtureDir, fmt.Sprintf("%s.golden", ts.file))
if *update {
err := ioutil.WriteFile(golden, got, 0644)
if err != nil {
t.Error(err)
}
return
}
// get golden file
want, err := ioutil.ReadFile(golden)
if err != nil {
t.Fatal(err)
}
from, err := ioutil.ReadFile(ts.cfg.file)
if err != nil {
t.Fatal(err)
}
// compare
if !bytes.Equal(got, want) {
t.Errorf("case %s\ngot:\n====\n\n%s\nwant:\n=====\n\n%s\nfrom:\n=====\n\n%s\n",
ts.file, got, want, from)
}
})
}
}
func TestModifiedRewrite(t *testing.T) {
cfg := &config{
add: []string{"json"},
output: "source",
structName: "foo",
transform: "snakecase",
file: "struct_add_modified",
modified: strings.NewReader(`struct_add_modified
55
package foo
type foo struct {
bar string
t bool
}
`),
}
node, err := cfg.parse()
if err != nil {
t.Fatal(err)
}
start, end, err := cfg.findSelection(node)
if err != nil {
t.Fatal(err)
}
rewrittenNode, err := cfg.rewrite(node, start, end)
if err != nil {
t.Fatal(err)
}
got, err := cfg.format(rewrittenNode, err)
if err != nil {
t.Fatal(err)
}
golden := filepath.Join(fixtureDir, "struct_add.golden")
want, err := ioutil.ReadFile(golden)
if err != nil {
t.Fatal(err)
}
// compare
if !bytes.Equal([]byte(got), want) {
t.Errorf("got:\n====\n%s\nwant:\n====\n%s\n", got, want)
}
}
func TestModifiedFileMissing(t *testing.T) {
cfg := &config{
add: []string{"json"},
output: "source",
structName: "foo",
transform: "snakecase",
file: "struct_add_modified",
modified: strings.NewReader(`file_that_doesnt_exist
55
package foo
type foo struct {
bar string
t bool
}
`),
}
_, err := cfg.parse()
if err == nil {
t.Fatal("expected error")
}
}
func TestParseLines(t *testing.T) {
var tests = []struct {
file string
}{
{file: "line_directive_unix"},
{file: "line_directive_windows"},
}
for _, ts := range tests {
ts := ts
t.Run(ts.file, func(t *testing.T) {
filePath := filepath.Join(fixtureDir, fmt.Sprintf("%s.input", ts.file))
file, err := os.Open(filePath)
if err != nil {
t.Fatal(err)
}
defer file.Close()
out, err := parseLines(file)
if err != nil {
t.Fatal(err)
}
toBytes := func(lines []string) []byte {
var buf bytes.Buffer
for _, line := range lines {
buf.WriteString(line + "\n")
}
return buf.Bytes()
}
got := toBytes(out)
// update golden file if necessary
golden := filepath.Join(fixtureDir, fmt.Sprintf("%s.golden", ts.file))
if *update {
err := ioutil.WriteFile(golden, got, 0644)
if err != nil {
t.Error(err)
}
return
}
// get golden file
want, err := ioutil.ReadFile(golden)
if err != nil {
t.Fatal(err)
}
from, err := ioutil.ReadFile(filePath)
if err != nil {
t.Fatal(err)
}
// compare
if !bytes.Equal(got, want) {
t.Errorf("case %s\ngot:\n====\n\n%s\nwant:\n=====\n\n%s\nfrom:\n=====\n\n%s\n",
ts.file, got, want, from)
}
})
}
}
func TestParseConfig(t *testing.T) {
// don't output help message during the test
flag.CommandLine.SetOutput(ioutil.Discard)
// The flag.CommandLine.Parse() call fails if there are flags re-defined
// with the same name. If there are duplicates, parseConfig() will return
// an error.
_, err := parseConfig([]string{"test"})
if err != nil {
t.Fatal(err)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化