| // Copyright (C) 2016 The Android Open Source Project |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package fsm_test |
| |
| import ( |
| "testing" |
| |
| "android.googlesource.com/platform/tools/gpu/framework/assert" |
| "android.googlesource.com/platform/tools/gpu/framework/fsm" |
| "android.googlesource.com/platform/tools/gpu/framework/log" |
| ) |
| |
| func TestInvalidHook(t *testing.T) { |
| ctx := assert.Context(t) |
| _, err := fsm.Compile(fsm.FSM{ |
| States: []fsm.State{ |
| {Name: "A"}, |
| }, |
| Hooks: []fsm.Hook{ |
| {From: "B"}, |
| }, |
| }) |
| assert.With(ctx).ThatError(err).HasMessage("Hook from B to <nil> on <nil> matched no transitions") |
| } |
| |
| type hookMethodTester struct { |
| from fsm.StateID |
| event fsm.StateID |
| to fsm.StateID |
| } |
| |
| func (m *hookMethodTester) Hook(ctx log.Context, fsm *fsm.Instance, from fsm.StateID, event fsm.Event) { |
| m.from = from |
| m.event = event |
| m.to = fsm.State() |
| } |
| func (m *hookMethodTester) Bad() {} |
| |
| func TestMethodHook(t *testing.T) { |
| ctx := assert.Context(t) |
| m := &hookMethodTester{} |
| err := fsm.RunWith(ctx, m, fsm.MustCompile(fsm.FSM{ |
| States: []fsm.State{ |
| fsm.Do("A", nil), |
| fsm.Exit("Done"), |
| }, |
| Transitions: []fsm.Transition{ |
| {From: "A", To: "Done"}, |
| }, |
| Hooks: []fsm.Hook{ |
| fsm.OnMethod(nil, nil, nil, "Hook"), |
| }, |
| })) |
| assert.With(ctx).ThatError(err).Succeeded() |
| assert.For(ctx, "from").ThatString(m.from).Equals("A") |
| assert.For(ctx, "event").That(m.event).Equals(fsm.Next) |
| assert.For(ctx, "to").ThatString(m.to).Equals("Done") |
| } |
| |
| func TestInvalidHookMethodName(t *testing.T) { |
| ctx := assert.Context(t) |
| m := &hookMethodTester{} |
| err := fsm.RunWith(ctx, m, fsm.MustCompile(fsm.FSM{ |
| States: []fsm.State{ |
| fsm.Do("A", nil), |
| fsm.Exit("Done"), |
| }, |
| Transitions: []fsm.Transition{ |
| {From: "A", To: "Done"}, |
| }, |
| Hooks: []fsm.Hook{ |
| fsm.OnMethod(nil, nil, nil, "MissingHook"), |
| }, |
| })) |
| assert.With(ctx).ThatError(err).HasMessage("Error:No such method") |
| } |
| |
| func TestInvalidHookMethodSignature(t *testing.T) { |
| ctx := assert.Context(t) |
| m := &hookMethodTester{} |
| err := fsm.RunWith(ctx, m, fsm.MustCompile(fsm.FSM{ |
| States: []fsm.State{ |
| fsm.Do("A", nil), |
| fsm.Exit("Done"), |
| }, |
| Transitions: []fsm.Transition{ |
| {From: "A", To: "Done"}, |
| }, |
| Hooks: []fsm.Hook{ |
| fsm.OnMethod(nil, nil, nil, "Bad"), |
| }, |
| })) |
| assert.With(ctx).ThatError(err).HasMessage("Error:Invalid signature") |
| } |