github.com/dahs81/otto@v0.2.1-0.20160126165905-6400716cf085/appfile/detect/parse_test.go (about) 1 package detect 2 3 import ( 4 "path/filepath" 5 "reflect" 6 "testing" 7 ) 8 9 func TestParse(t *testing.T) { 10 cases := []struct { 11 File string 12 Result *Config 13 Err bool 14 }{ 15 { 16 "basic.hcl", 17 &Config{ 18 Detectors: []*Detector{ 19 &Detector{ 20 Type: "go", 21 File: []string{"*.go"}, 22 }, 23 }, 24 }, 25 false, 26 }, 27 28 { 29 "priority.hcl", 30 &Config{ 31 Detectors: []*Detector{ 32 &Detector{ 33 Type: "go", 34 File: []string{"*.go"}, 35 Priority: 50, 36 }, 37 }, 38 }, 39 false, 40 }, 41 } 42 43 for _, tc := range cases { 44 path, err := filepath.Abs(filepath.Join("./test-fixtures", "parse", tc.File)) 45 if err != nil { 46 t.Fatalf("file: %s\n\n%s", tc.File, err) 47 continue 48 } 49 50 actual, err := ParseFile(path) 51 if (err != nil) != tc.Err { 52 t.Fatalf("file: %s\n\n%s", tc.File, err) 53 continue 54 } 55 56 if !reflect.DeepEqual(actual, tc.Result) { 57 t.Fatalf("file: %s\n\n%#v\n\n%#v", tc.File, actual, tc.Result) 58 } 59 } 60 } 61 62 func TestParseDir(t *testing.T) { 63 cases := []struct { 64 Dir string 65 Result *Config 66 Err bool 67 }{ 68 { 69 "basic", 70 &Config{ 71 Detectors: []*Detector{ 72 &Detector{ 73 Type: "go", 74 File: []string{"*.go"}, 75 }, 76 &Detector{ 77 Type: "ruby", 78 File: []string{"*.rb"}, 79 }, 80 }, 81 }, 82 false, 83 }, 84 85 { 86 "no-exist", 87 nil, 88 false, 89 }, 90 } 91 92 for _, tc := range cases { 93 path, err := filepath.Abs(filepath.Join("./test-fixtures", "parse-dir", tc.Dir)) 94 if err != nil { 95 t.Fatalf("file: %s\n\n%s", tc.Dir, err) 96 continue 97 } 98 99 actual, err := ParseDir(path) 100 if (err != nil) != tc.Err { 101 t.Fatalf("file: %s\n\n%s", tc.Dir, err) 102 continue 103 } 104 105 if !reflect.DeepEqual(actual, tc.Result) { 106 t.Fatalf("file: %s\n\n%#v\n\n%#v", tc.Dir, actual, tc.Result) 107 } 108 } 109 }