blob: dbc166e2bbe765e0e8a91a9eedd27c0298548ed4 [file] [log] [blame]
Shinichiro Hamaji0439a3e2015-04-01 02:01:05 +09001package main
2
3import (
4 "reflect"
5 "testing"
6)
7
8func TestRuleParser(t *testing.T) {
9 for _, tc := range []struct {
10 in string
11 want Rule
Shinichiro Hamaji21a9b5f2015-04-01 02:42:59 +090012 err string
Shinichiro Hamaji0439a3e2015-04-01 02:01:05 +090013 } {
14 {
15 in: "foo: bar",
16 want: Rule{
17 outputs: []string{"foo"},
18 inputs: []string{"bar"},
19 },
20 },
21 {
22 in: "foo: bar baz",
23 want: Rule{
24 outputs: []string{"foo"},
25 inputs: []string{"bar", "baz"},
26 },
27 },
Shinichiro Hamaji21a9b5f2015-04-01 02:42:59 +090028 {
29 in: "foo:: bar",
30 want: Rule{
31 outputs: []string{"foo"},
32 inputs: []string{"bar"},
33 isDoubleColon: true,
34 },
35 },
36 {
37 in: "foo",
38 err: "*** missing separator.",
39 },
40 {
41 in: "%.o: %.c",
42 want: Rule{
43 outputPatterns: []string{"%.o"},
44 inputs: []string{"%.c"},
45 },
46 },
47 {
48 in: "foo %.o: %.c",
49 err: "*** mixed implicit and normal rules: deprecated syntax",
50 },
51 {
52 in: "foo.o: %.o: %.c %.h",
53 want: Rule{
54 outputs: []string{"foo.o"},
55 outputPatterns: []string{"%.o"},
56 inputs: []string{"%.c", "%.h"},
57 },
58 },
59 {
60 in: "%.x: %.y: %.z",
61 err: "*** mixed implicit and normal rules: deprecated syntax",
62 },
63 {
64 in: "foo.o: : %.c",
65 err: "*** missing target pattern.",
66 },
67 {
68 in: "foo.o: %.o %.o: %.c",
69 err: "*** multiple target patterns.",
70 },
71 {
72 in: "foo.o: foo.o: %.c",
73 err: "*** target pattern contains no '%'.",
74 },
75 /* TODO
76 {
77 in: "foo.o: %.c: %.c",
78 err: "*** target 'foo.o' doesn't match the target pattern",
79 },
80 */
Shinichiro Hamaji0439a3e2015-04-01 02:01:05 +090081 } {
82 got := &Rule{}
Shinichiro Hamaji21a9b5f2015-04-01 02:42:59 +090083 err := got.parse(tc.in)
84 if err != tc.err {
85 t.Errorf(`r.parse(%q)=%s, want %s`, tc.in, err, tc.err)
86 }
87 if err == "" && !reflect.DeepEqual(*got, tc.want) {
88 t.Errorf(`r.parse(%q); r=%q, want %q`, tc.in, *got, tc.want)
Shinichiro Hamaji0439a3e2015-04-01 02:01:05 +090089 }
90 }
91}