github.com/vugu/vugu@v0.3.5/gen/merge_test.go (about) 1 package gen 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "regexp" 8 "testing" 9 ) 10 11 func TestMerge(t *testing.T) { 12 13 debug := true 14 15 type tcase struct { 16 name string 17 infiles map[string]string // file structure to start with 18 out map[string][]string // regexps to match in output files 19 outNot map[string][]string // regexps to NOT match in output files 20 } 21 22 tcList := []tcase{ 23 { 24 name: "simple", 25 infiles: map[string]string{ 26 "file1.go": "package main\nfunc main(){}", 27 "file2.go": "package main\nvar a string", 28 }, 29 out: map[string][]string{ 30 "out.go": {`func main`, `var a string`}, 31 }, 32 }, 33 { 34 name: "comments", 35 infiles: map[string]string{ 36 "file1.go": "package main\n// main comment here\nfunc main(){}", 37 "file2.go": "package main\nvar a string // a comment here\n", 38 }, 39 out: map[string][]string{ 40 "out.go": {`func main`, `// main comment here`, `var a string`, `// a comment here`}, 41 }, 42 }, 43 { 44 name: "import-dedup", 45 infiles: map[string]string{ 46 "file1.go": "package main\nimport \"fmt\"\n// main comment here\nfunc main(){}", 47 "file2.go": "package main\nimport \"fmt\"\nvar a string // a comment here\n", 48 }, 49 out: map[string][]string{ 50 "out.go": {`import "fmt"`}, 51 }, 52 outNot: map[string][]string{ 53 "out.go": {`(?ms)import "fmt".*import "fmt"`}, 54 }, 55 }, 56 { 57 name: "import-dedup-2", 58 infiles: map[string]string{ 59 "file1.go": "package main\nimport \"fmt\"\n// main comment here\nfunc main(){}", 60 "file2.go": "package main\nimport \"fmt\"\nimport \"log\"\nvar a string // a comment here\n", 61 }, 62 out: map[string][]string{ 63 "out.go": {`import "fmt"`, `import "log"`}, 64 }, 65 outNot: map[string][]string{ 66 "out.go": {`(?ms)\}.*import "log"`}, 67 }, 68 }, 69 } 70 71 for _, tc := range tcList { 72 tc := tc 73 t.Run(tc.name, func(t *testing.T) { 74 75 tmpDir, err := ioutil.TempDir("", "TestMerge") 76 if err != nil { 77 t.Fatal(err) 78 } 79 80 if debug { 81 t.Logf("Test %q using tmpDir: %s", tc.name, tmpDir) 82 } else { 83 defer os.RemoveAll(tmpDir) 84 t.Parallel() 85 } 86 87 tstWriteFiles(tmpDir, tc.infiles) 88 var in []string 89 for k := range tc.infiles { 90 // in = append(in, filepath.Join(tmpDir, k)) 91 in = append(in, k) 92 } 93 94 err = mergeGoFiles(tmpDir, "out.go", in...) 95 if err != nil { 96 t.Fatal(err) 97 } 98 99 for fname, patterns := range tc.out { 100 b, err := ioutil.ReadFile(filepath.Join(tmpDir, fname)) 101 if err != nil { 102 t.Errorf("failed to read file %q after Run: %v", fname, err) 103 continue 104 } 105 for _, pattern := range patterns { 106 re := regexp.MustCompile(pattern) 107 if !re.Match(b) { 108 t.Errorf("failed to match regexp on file %q: %s", fname, pattern) 109 } 110 } 111 } 112 113 for fname, patterns := range tc.outNot { 114 b, err := ioutil.ReadFile(filepath.Join(tmpDir, fname)) 115 if err != nil { 116 t.Errorf("failed to read file %q after Run: %v", fname, err) 117 continue 118 } 119 for _, pattern := range patterns { 120 re := regexp.MustCompile(pattern) 121 if re.Match(b) { 122 t.Errorf("incorrectly matched regexp on file %q: %s", fname, pattern) 123 } 124 } 125 } 126 127 if debug { 128 outb, _ := ioutil.ReadFile(filepath.Join(tmpDir, "out.go")) 129 t.Logf("OUTPUT:\n%s", outb) 130 } 131 132 }) 133 } 134 135 }