Colin Cross | 8e0c511 | 2015-01-23 14:15:10 -0800 | [diff] [blame] | 1 | // Copyright 2014 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 15 | package blueprint |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | type fooModule struct { |
| 23 | properties struct { |
| 24 | Foo string |
| 25 | } |
| 26 | } |
| 27 | |
Jamie Gennis | 68540da | 2014-10-06 09:10:40 -0700 | [diff] [blame] | 28 | func newFooModule() (Module, []interface{}) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 29 | m := &fooModule{} |
Jamie Gennis | 68540da | 2014-10-06 09:10:40 -0700 | [diff] [blame] | 30 | return m, []interface{}{&m.properties} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | func (f *fooModule) GenerateBuildActions(ModuleContext) { |
| 34 | } |
| 35 | |
| 36 | func (f *fooModule) Foo() string { |
| 37 | return f.properties.Foo |
| 38 | } |
| 39 | |
| 40 | type barModule struct { |
| 41 | properties struct { |
| 42 | Bar bool |
| 43 | } |
| 44 | } |
| 45 | |
Jamie Gennis | 68540da | 2014-10-06 09:10:40 -0700 | [diff] [blame] | 46 | func newBarModule() (Module, []interface{}) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 47 | m := &barModule{} |
Jamie Gennis | 68540da | 2014-10-06 09:10:40 -0700 | [diff] [blame] | 48 | return m, []interface{}{&m.properties} |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | func (b *barModule) GenerateBuildActions(ModuleContext) { |
| 52 | } |
| 53 | |
| 54 | func (b *barModule) Bar() bool { |
| 55 | return b.properties.Bar |
| 56 | } |
| 57 | |
| 58 | func TestContextParse(t *testing.T) { |
| 59 | ctx := NewContext() |
Jamie Gennis | 68540da | 2014-10-06 09:10:40 -0700 | [diff] [blame] | 60 | ctx.RegisterModuleType("foo_module", newFooModule) |
| 61 | ctx.RegisterModuleType("bar_module", newBarModule) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 62 | |
| 63 | r := bytes.NewBufferString(` |
| 64 | foo_module { |
| 65 | name: "MyFooModule", |
| 66 | deps: ["MyBarModule"], |
| 67 | } |
| 68 | |
| 69 | bar_module { |
| 70 | name: "MyBarModule", |
| 71 | } |
| 72 | `) |
| 73 | |
Colin Cross | 7ad621c | 2015-01-07 16:22:45 -0800 | [diff] [blame] | 74 | _, _, errs, _ := ctx.parse(".", "Blueprint", r, nil) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 75 | if len(errs) > 0 { |
| 76 | t.Errorf("unexpected parse errors:") |
| 77 | for _, err := range errs { |
| 78 | t.Errorf(" %s", err) |
| 79 | } |
| 80 | t.FailNow() |
| 81 | } |
| 82 | |
Jamie Gennis | 68540da | 2014-10-06 09:10:40 -0700 | [diff] [blame] | 83 | errs = ctx.resolveDependencies(nil) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 84 | if len(errs) > 0 { |
| 85 | t.Errorf("unexpected dep errors:") |
| 86 | for _, err := range errs { |
| 87 | t.Errorf(" %s", err) |
| 88 | } |
| 89 | t.FailNow() |
| 90 | } |
| 91 | |
Colin Cross | 691a60d | 2015-01-07 18:08:56 -0800 | [diff] [blame] | 92 | errs = ctx.updateDependencies() |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 93 | if len(errs) > 0 { |
| 94 | t.Errorf("unexpected dep cycle errors:") |
| 95 | for _, err := range errs { |
| 96 | t.Errorf(" %s", err) |
| 97 | } |
| 98 | t.FailNow() |
| 99 | } |
| 100 | |
| 101 | } |