go-hep.org/x/hep@v0.38.1/groot/rcmd/rcmd_test.go (about) 1 // Copyright ©2020 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package rcmd // import "go-hep.org/x/hep/groot/rcmd" 6 7 import ( 8 "fmt" 9 "reflect" 10 "testing" 11 ) 12 13 func TestSplitArg(t *testing.T) { 14 for _, tc := range []struct { 15 cmd string 16 fname string 17 sel string 18 err error 19 }{ 20 { 21 cmd: "file.root", 22 fname: "file.root", 23 sel: "/.*", 24 err: nil, 25 }, 26 { 27 cmd: "dir/sub/file.root", 28 fname: "dir/sub/file.root", 29 sel: "/.*", 30 err: nil, 31 }, 32 { 33 cmd: "/dir/sub/file.root", 34 fname: "/dir/sub/file.root", 35 sel: "/.*", 36 err: nil, 37 }, 38 { 39 cmd: "../dir/sub/file.root", 40 fname: "../dir/sub/file.root", 41 sel: "/.*", 42 err: nil, 43 }, 44 { 45 cmd: "dir/sub/file.root:hist", 46 fname: "dir/sub/file.root", 47 sel: "/hist", 48 err: nil, 49 }, 50 { 51 cmd: "dir/sub/file.root:hist*", 52 fname: "dir/sub/file.root", 53 sel: "/hist*", 54 err: nil, 55 }, 56 { 57 cmd: "dir/sub/file.root:", 58 fname: "dir/sub/file.root", 59 sel: "/.*", 60 err: nil, 61 }, 62 { 63 cmd: "file://dir/sub/file.root:", 64 fname: "file://dir/sub/file.root", 65 sel: "/.*", 66 err: nil, 67 }, 68 { 69 cmd: "https://dir/sub/file.root", 70 fname: "https://dir/sub/file.root", 71 sel: "/.*", 72 err: nil, 73 }, 74 { 75 cmd: "http://dir/sub/file.root", 76 fname: "http://dir/sub/file.root", 77 sel: "/.*", 78 err: nil, 79 }, 80 { 81 cmd: "https://dir/sub/file.root:hist*", 82 fname: "https://dir/sub/file.root", 83 sel: "/hist*", 84 err: nil, 85 }, 86 { 87 cmd: "root://dir/sub/file.root:hist*", 88 fname: "root://dir/sub/file.root", 89 sel: "/hist*", 90 err: nil, 91 }, 92 { 93 cmd: "root://dir/sub/file.root:/hist*", 94 fname: "root://dir/sub/file.root", 95 sel: "/hist*", 96 err: nil, 97 }, 98 { 99 cmd: "root://dir/sub/file.root:^/hist*", 100 fname: "root://dir/sub/file.root", 101 sel: "^/hist*", 102 err: nil, 103 }, 104 { 105 cmd: "root://dir/sub/file.root:^hist*", 106 fname: "root://dir/sub/file.root", 107 sel: "^/hist*", 108 err: nil, 109 }, 110 { 111 cmd: "root://dir/sub/file.root:/^hist*", 112 fname: "root://dir/sub/file.root", 113 sel: "/^hist*", 114 err: nil, 115 }, 116 { 117 cmd: "dir/sub/file.root:h:h", 118 err: fmt.Errorf("root-cp: too many ':' in %q", "dir/sub/file.root:h:h"), 119 }, 120 { 121 cmd: "root://dir/sub/file.root:h:h", 122 err: fmt.Errorf("root-cp: too many ':' in %q", "root://dir/sub/file.root:h:h"), 123 }, 124 { 125 cmd: "root://dir/sub/file.root::h:", 126 err: fmt.Errorf("root-cp: too many ':' in %q", "root://dir/sub/file.root::h:"), 127 }, 128 } { 129 t.Run(tc.cmd, func(t *testing.T) { 130 fname, sel, err := splitArg(tc.cmd) 131 switch { 132 case err != nil && tc.err != nil: 133 if !reflect.DeepEqual(err.Error(), tc.err.Error()) { 134 t.Fatalf("got err=%v, want=%v", err, tc.err) 135 } 136 return 137 case err != nil && tc.err == nil: 138 t.Fatalf("got err=%v, want=%v", err, tc.err) 139 case err == nil && tc.err != nil: 140 t.Fatalf("got err=%v, want=%v", err, tc.err) 141 } 142 143 if got, want := fname, tc.fname; got != want { 144 t.Fatalf("fname=%q, want=%q", got, want) 145 } 146 147 if got, want := sel, tc.sel; got != want { 148 t.Fatalf("selection=%q, want=%q", got, want) 149 } 150 }) 151 } 152 }