Shinichiro Hamaji | 0439a3e | 2015-04-01 02:01:05 +0900 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestRuleParser(t *testing.T) { |
| 9 | for _, tc := range []struct { |
| 10 | in string |
| 11 | want Rule |
Shinichiro Hamaji | 21a9b5f | 2015-04-01 02:42:59 +0900 | [diff] [blame] | 12 | err string |
Shinichiro Hamaji | 0439a3e | 2015-04-01 02:01:05 +0900 | [diff] [blame] | 13 | } { |
| 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 Hamaji | 21a9b5f | 2015-04-01 02:42:59 +0900 | [diff] [blame] | 28 | { |
| 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 Hamaji | 0439a3e | 2015-04-01 02:01:05 +0900 | [diff] [blame] | 81 | } { |
| 82 | got := &Rule{} |
Shinichiro Hamaji | 21a9b5f | 2015-04-01 02:42:59 +0900 | [diff] [blame] | 83 | 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 Hamaji | 0439a3e | 2015-04-01 02:01:05 +0900 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |