| 1 | // Copyright 2019 The Go 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 | package main |
| 5 | |
| 6 | import ( |
| 7 | "bytes" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "reflect" |
| 11 | "sort" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | ) |
| 15 | |
| 16 | func TestDigraph(t *testing.T) { |
| 17 | const g1 = ` |
| 18 | socks shoes |
| 19 | shorts pants |
| 20 | pants belt shoes |
| 21 | shirt tie sweater |
| 22 | sweater jacket |
| 23 | hat |
| 24 | ` |
| 25 | |
| 26 | const g2 = ` |
| 27 | a b c |
| 28 | b d |
| 29 | c d |
| 30 | d c |
| 31 | e e |
| 32 | ` |
| 33 | |
| 34 | for _, test := range []struct { |
| 35 | name string |
| 36 | input string |
| 37 | cmd string |
| 38 | args []string |
| 39 | want string |
| 40 | }{ |
| 41 | {"nodes", g1, "nodes", nil, "belt\nhat\njacket\npants\nshirt\nshoes\nshorts\nsocks\nsweater\ntie\n"}, |
| 42 | {"reverse", g1, "reverse", []string{"jacket"}, "jacket\nshirt\nsweater\n"}, |
| 43 | {"transpose", g1, "transpose", nil, "belt pants\njacket sweater\npants shorts\nshoes pants\nshoes socks\nsweater shirt\ntie shirt\n"}, |
| 44 | {"forward", g1, "forward", []string{"socks"}, "shoes\nsocks\n"}, |
| 45 | {"forward multiple args", g1, "forward", []string{"socks", "sweater"}, "jacket\nshoes\nsocks\nsweater\n"}, |
| 46 | {"scss", g2, "sccs", nil, "c d\ne\n"}, |
| 47 | {"scc", g2, "scc", []string{"d"}, "c\nd\n"}, |
| 48 | {"succs", g2, "succs", []string{"a"}, "b\nc\n"}, |
| 49 | {"succs-long-token", g2 + "x " + strings.Repeat("x", 96*1024), "succs", []string{"x"}, strings.Repeat("x", 96*1024) + "\n"}, |
| 50 | {"preds", g2, "preds", []string{"c"}, "a\nd\n"}, |
| 51 | {"preds multiple args", g2, "preds", []string{"c", "d"}, "a\nb\nc\nd\n"}, |
| 52 | } { |
| 53 | t.Run(test.name, func(t *testing.T) { |
| 54 | stdin = strings.NewReader(test.input) |
| 55 | stdout = new(bytes.Buffer) |
| 56 | if err := doDigraph(test.cmd, test.args); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | |
| 60 | got := stdout.(fmt.Stringer).String() |
| 61 | if got != test.want { |
| 62 | t.Errorf("digraph(%s, %s) = got %q, want %q", test.cmd, test.args, got, test.want) |
| 63 | } |
| 64 | }) |
| 65 | } |
| 66 | |
| 67 | // TODO(adonovan): |
| 68 | // - test errors |
| 69 | } |
| 70 | |
| 71 | func TestAllpaths(t *testing.T) { |
| 72 | for _, test := range []struct { |
| 73 | name string |
| 74 | in string |
| 75 | to string // from is always "A" |
| 76 | want string |
| 77 | }{ |
| 78 | { |
| 79 | name: "Basic", |
| 80 | in: "A B\nB C", |
| 81 | to: "B", |
| 82 | want: "A B\n", |
| 83 | }, |
| 84 | { |
| 85 | name: "Long", |
| 86 | in: "A B\nB C\n", |
| 87 | to: "C", |
| 88 | want: "A B\nB C\n", |
| 89 | }, |
| 90 | { |
| 91 | name: "Cycle Basic", |
| 92 | in: "A B\nB A", |
| 93 | to: "B", |
| 94 | want: "A B\nB A\n", |
| 95 | }, |
| 96 | { |
| 97 | name: "Cycle Path Out", |
| 98 | // A <-> B -> C -> D |
| 99 | in: "A B\nB A\nB C\nC D", |
| 100 | to: "C", |
| 101 | want: "A B\nB A\nB C\n", |
| 102 | }, |
| 103 | { |
| 104 | name: "Cycle Path Out Further Out", |
| 105 | // A -> B <-> C -> D -> E |
| 106 | in: "A B\nB C\nC D\nC B\nD E", |
| 107 | to: "D", |
| 108 | want: "A B\nB C\nC B\nC D\n", |
| 109 | }, |
| 110 | { |
| 111 | name: "Two Paths Basic", |
| 112 | // /-> C --\ |
| 113 | // A -> B -- -> E -> F |
| 114 | // \-> D --/ |
| 115 | in: "A B\nB C\nC E\nB D\nD E\nE F", |
| 116 | to: "E", |
| 117 | want: "A B\nB C\nB D\nC E\nD E\n", |
| 118 | }, |
| 119 | { |
| 120 | name: "Two Paths With One Immediately From Start", |
| 121 | // /-> B -+ -> D |
| 122 | // A -- | |
| 123 | // \-> C <+ |
| 124 | in: "A B\nA C\nB C\nB D", |
| 125 | to: "C", |
| 126 | want: "A B\nA C\nB C\n", |
| 127 | }, |
| 128 | { |
| 129 | name: "Two Paths Further Up", |
| 130 | // /-> B --\ |
| 131 | // A -- -> D -> E -> F |
| 132 | // \-> C --/ |
| 133 | in: "A B\nA C\nB D\nC D\nD E\nE F", |
| 134 | to: "E", |
| 135 | want: "A B\nA C\nB D\nC D\nD E\n", |
| 136 | }, |
| 137 | { |
| 138 | // We should include A - C - D even though it's further up the |
| 139 | // second path than D (which would already be in the graph by |
| 140 | // the time we get around to integrating the second path). |
| 141 | name: "Two Splits", |
| 142 | // /-> B --\ /-> E --\ |
| 143 | // A -- -> D -- -> G -> H |
| 144 | // \-> C --/ \-> F --/ |
| 145 | in: "A B\nA C\nB D\nC D\nD E\nD F\nE G\nF G\nG H", |
| 146 | to: "G", |
| 147 | want: "A B\nA C\nB D\nC D\nD E\nD F\nE G\nF G\n", |
| 148 | }, |
| 149 | { |
| 150 | // D - E should not be duplicated. |
| 151 | name: "Two Paths - Two Splits With Gap", |
| 152 | // /-> B --\ /-> F --\ |
| 153 | // A -- -> D -> E -- -> H -> I |
| 154 | // \-> C --/ \-> G --/ |
| 155 | in: "A B\nA C\nB D\nC D\nD E\nE F\nE G\nF H\nG H\nH I", |
| 156 | to: "H", |
| 157 | want: "A B\nA C\nB D\nC D\nD E\nE F\nE G\nF H\nG H\n", |
| 158 | }, |
| 159 | { |
| 160 | // C <--> B --> A --> D <--> E |
| 161 | // ⋃ |
| 162 | name: "non-regression test for #74842", |
| 163 | in: "A D\nB A\nB B\nB C\nC B\nD E\nE D", |
| 164 | to: "D", |
| 165 | want: "A D\nD E\nE D\n", |
| 166 | }, |
| 167 | { |
| 168 | // A --> B --> D |
| 169 | // ^ |
| 170 | // v |
| 171 | // C[123] |
| 172 | name: "regression test for #74842", |
| 173 | in: "A B\nB C1\nB C2\nB C3\nB D\nC1 B\nC2 B\nC3 B\n", |
| 174 | to: "D", |
| 175 | want: "A B\nB C1\nB C2\nB C3\nB D\nC1 B\nC2 B\nC3 B\n", |
| 176 | }, |
| 177 | { |
| 178 | // A -------> B --> D |
| 179 | // \--> C ---^ | |
| 180 | // ^----------+ |
| 181 | name: "another regression test for #74842", |
| 182 | in: "A B\nA C\nB D\nC B\nD C\n", |
| 183 | to: "D", |
| 184 | want: "A B\nA C\nB D\nC B\nD C\n", |
| 185 | }, |
| 186 | } { |
| 187 | t.Run(test.name, func(t *testing.T) { |
| 188 | stdin = strings.NewReader(test.in) |
| 189 | stdout = new(bytes.Buffer) |
| 190 | if err := doDigraph("allpaths", []string{"A", test.to}); err != nil { |
| 191 | t.Fatal(err) |
| 192 | } |
| 193 | |
| 194 | got := stdout.(fmt.Stringer).String() |
| 195 | if got != test.want { |
| 196 | t.Errorf("digraph(allpaths, A, %s) = got %q, want %q", test.to, got, test.want) |
| 197 | } |
| 198 | }) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | func TestSomepath(t *testing.T) { |
| 203 | for _, test := range []struct { |
| 204 | name string |
| 205 | in string |
| 206 | to string |
| 207 | // somepath is non-deterministic, so we have to provide all the |
| 208 | // possible options. Each option is separated with |. |
| 209 | wantAnyOf string |
| 210 | }{ |
| 211 | { |
| 212 | name: "Basic", |
| 213 | in: "A B\n", |
| 214 | to: "B", |
| 215 | wantAnyOf: "A B", |
| 216 | }, |
| 217 | { |
| 218 | name: "Basic With Cycle", |
| 219 | in: "A B\nB A", |
| 220 | to: "B", |
| 221 | wantAnyOf: "A B", |
| 222 | }, |
| 223 | { |
| 224 | name: "Two Paths", |
| 225 | // /-> B --\ |
| 226 | // A -- -> D |
| 227 | // \-> C --/ |
| 228 | in: "A B\nA C\nB D\nC D", |
| 229 | to: "D", |
| 230 | wantAnyOf: "A B\nB D|A C\nC D", |
| 231 | }, |
| 232 | { |
| 233 | name: "Printed path is minimal", |
| 234 | // A -> B1->B2->B3 -> E |
| 235 | // A -> C1->C2 -> E |
| 236 | // A -> D -> E |
| 237 | in: "A D C1 B1\nD E\nC1 C2\nC2 E\nB1 B2\nB2 B3\nB3 E", |
| 238 | to: "E", |
| 239 | wantAnyOf: "A D\nD E", |
| 240 | }, |
| 241 | } { |
| 242 | t.Run(test.name, func(t *testing.T) { |
| 243 | stdin = strings.NewReader(test.in) |
| 244 | stdout = new(bytes.Buffer) |
| 245 | if err := doDigraph("somepath", []string{"A", test.to}); err != nil { |
| 246 | t.Fatal(err) |
| 247 | } |
| 248 | |
| 249 | got := stdout.(fmt.Stringer).String() |
| 250 | lines := strings.Split(got, "\n") |
| 251 | sort.Strings(lines) |
| 252 | got = strings.Join(lines[1:], "\n") |
| 253 | |
| 254 | var oneMatch bool |
| 255 | for want := range strings.SplitSeq(test.wantAnyOf, "|") { |
| 256 | if got == want { |
| 257 | oneMatch = true |
| 258 | } |
| 259 | } |
| 260 | if !oneMatch { |
| 261 | t.Errorf("digraph(somepath, A, %s) = got %q, want any of\n%s", test.to, got, test.wantAnyOf) |
| 262 | } |
| 263 | }) |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | func TestSplit(t *testing.T) { |
| 268 | for _, test := range []struct { |
| 269 | line string |
| 270 | want []string |
| 271 | }{ |
| 272 | {`one "2a 2b" three`, []string{"one", "2a 2b", "three"}}, |
| 273 | {`one tw"\n\x0a\u000a\012"o three`, []string{"one", "tw\n\n\n\no", "three"}}, |
| 274 | } { |
| 275 | got, err := split(test.line) |
| 276 | if err != nil { |
| 277 | t.Errorf("split(%s) failed: %v", test.line, err) |
| 278 | } |
| 279 | if !reflect.DeepEqual(got, test.want) { |
| 280 | t.Errorf("split(%s) = %v, want %v", test.line, got, test.want) |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | func TestQuotedLength(t *testing.T) { |
| 286 | for _, test := range []struct { |
| 287 | input string |
| 288 | want int |
| 289 | }{ |
| 290 | {`"abc"`, 5}, |
| 291 | {`"abc"def`, 5}, |
| 292 | {`"abc\"d"ef`, 8}, // "abc\"d" is consumed, ef is residue |
| 293 | {`"\012\n\x0a\u000a\U0000000a"`, 28}, |
| 294 | {"\"\xff\"", 3}, // bad UTF-8 is ok |
| 295 | {`"\xff"`, 6}, // hex escape for bad UTF-8 is ok |
| 296 | } { |
| 297 | got, ok := quotedLength(test.input) |
| 298 | if !ok { |
| 299 | got = 0 |
| 300 | } |
| 301 | if got != test.want { |
| 302 | t.Errorf("quotedLength(%s) = %d, want %d", test.input, got, test.want) |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // errors |
| 307 | for _, input := range []string{ |
| 308 | ``, // not a quotation |
| 309 | `a`, // not a quotation |
| 310 | `'a'`, // not a quotation |
| 311 | `"a`, // not terminated |
| 312 | `"\0"`, // short octal escape |
| 313 | `"\x1"`, // short hex escape |
| 314 | `"\u000"`, // short \u escape |
| 315 | `"\U0000000"`, // short \U escape |
| 316 | `"\k"`, // invalid escape |
| 317 | "\"ab\nc\"", // newline |
| 318 | } { |
| 319 | if n, ok := quotedLength(input); ok { |
| 320 | t.Errorf("quotedLength(%s) = %d, want !ok", input, n) |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | func TestFocus(t *testing.T) { |
| 326 | for _, test := range []struct { |
| 327 | name string |
| 328 | in string |
| 329 | focus string |
| 330 | want string |
| 331 | }{ |
| 332 | { |
| 333 | name: "Basic", |
| 334 | in: "A B", |
| 335 | focus: "B", |
| 336 | want: "A B\n", |
| 337 | }, |
| 338 | { |
| 339 | name: "Some Nodes Not Included", |
| 340 | // C does not have a path involving B, and should not be included |
| 341 | // in the output. |
| 342 | in: "A B\nA C", |
| 343 | focus: "B", |
| 344 | want: "A B\n", |
| 345 | }, |
| 346 | { |
| 347 | name: "Cycle In Path", |
| 348 | // A <-> B -> C |
| 349 | in: "A B\nB A\nB C", |
| 350 | focus: "C", |
| 351 | want: "A B\nB A\nB C\n", |
| 352 | }, |
| 353 | { |
| 354 | name: "Cycle Out Of Path", |
| 355 | // C <- A <->B |
| 356 | in: "A B\nB A\nB C", |
| 357 | focus: "C", |
| 358 | want: "A B\nB A\nB C\n", |
| 359 | }, |
| 360 | { |
| 361 | name: "Complex", |
| 362 | // Paths in and out from focus. |
| 363 | // /-> F |
| 364 | // /-> B -> D -- |
| 365 | // A -- \-> E |
| 366 | // \-> C |
| 367 | in: "A B\nA C\nB D\nD F\nD E", |
| 368 | focus: "D", |
| 369 | want: "A B\nB D\nD E\nD F\n", |
| 370 | }, |
| 371 | } { |
| 372 | t.Run(test.name, func(t *testing.T) { |
| 373 | stdin = strings.NewReader(test.in) |
| 374 | stdout = new(bytes.Buffer) |
| 375 | if err := doDigraph("focus", []string{test.focus}); err != nil { |
| 376 | t.Fatal(err) |
| 377 | } |
| 378 | got := stdout.(fmt.Stringer).String() |
| 379 | if got != test.want { |
| 380 | t.Errorf("digraph(focus, %s) = got %q, want %q", test.focus, got, test.want) |
| 381 | } |
| 382 | }) |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | func TestToDot(t *testing.T) { |
| 387 | in := `a b c |
| 388 | b "d\"\\d" |
| 389 | c "d\"\\d"` |
| 390 | want := `digraph "" { |
| 391 | n0 [label="a"]; |
| 392 | n1 [label="b"]; |
| 393 | n2 [label="c"]; |
| 394 | n3 [label="d\"\\d"]; |
| 395 | n0 -> n1; |
| 396 | n0 -> n2; |
| 397 | n1 -> n3; |
| 398 | n2 -> n3; |
| 399 | } |
| 400 | ` |
| 401 | defer func(in io.Reader, out io.Writer) { stdin, stdout = in, out }(stdin, stdout) |
| 402 | stdin = strings.NewReader(in) |
| 403 | stdout = new(bytes.Buffer) |
| 404 | if err := doDigraph("to", []string{"dot"}); err != nil { |
| 405 | t.Fatal(err) |
| 406 | } |
| 407 | got := stdout.(fmt.Stringer).String() |
| 408 | if got != want { |
| 409 | t.Errorf("digraph(to, dot) = got\n %q\nwant\n %q", got, want) |
| 410 | } |
| 411 | } |