| // 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 TestDuplicateState(t *testing.T) { |
| ctx := assert.Context(t) |
| _, err := fsm.Compile(fsm.FSM{ |
| States: []fsm.State{ |
| {Name: "A"}, |
| {Name: "A"}, |
| }, |
| }) |
| assert.With(ctx).ThatError(err).HasMessage("Duplicate state name A") |
| } |
| |
| func TestNoState(t *testing.T) { |
| ctx := assert.Context(t) |
| _, err := fsm.Compile(fsm.FSM{}) |
| assert.With(ctx).ThatError(err).HasMessage("No states specified") |
| } |
| |
| func TestCorrectName(t *testing.T) { |
| ctx := assert.Context(t) |
| newState := func(name string) fsm.State { |
| return fsm.Do(name, func(ctx log.Context) error { |
| assert.For(ctx, "state").That(fsm.Get(ctx).State()).Equals(name) |
| return nil |
| }) |
| } |
| fsm.Run(ctx, fsm.MustCompile(fsm.FSM{ |
| States: []fsm.State{ |
| newState("A"), |
| newState("B"), |
| newState("C"), |
| }, |
| Transitions: []fsm.Transition{ |
| {From: "A", To: "B"}, |
| {From: "B", Event: "doc", To: "C"}, |
| }, |
| })) |
| } |
| |
| func TestBadInitialState(t *testing.T) { |
| ctx := assert.Context(t) |
| _, err := fsm.Compile(fsm.FSM{ |
| Initial: "B", |
| States: []fsm.State{ |
| fsm.Do("A", nil), |
| }, |
| }) |
| assert.With(ctx).ThatError(err).HasMessage("Invalid initial state B") |
| } |
| |
| type stateMethodTester struct{ got interface{} } |
| |
| func (m *stateMethodTester) A(ctx log.Context) error { m.got = fsm.Get(ctx).State(); return nil } |
| func (m *stateMethodTester) Bad() {} |
| |
| func TestStateMethod(t *testing.T) { |
| ctx := assert.Context(t) |
| m := &stateMethodTester{} |
| err := fsm.RunWith(ctx, m, fsm.MustCompile(fsm.FSM{ |
| States: []fsm.State{ |
| fsm.Method("A"), |
| fsm.Exit("Done"), |
| }, |
| Transitions: []fsm.Transition{ |
| {From: "A", To: "Done"}, |
| }, |
| })) |
| assert.With(ctx).ThatError(err).Succeeded() |
| assert.For(ctx, "result").That(m.got).Equals("A") |
| } |
| |
| func TestInvalidStateMethodName(t *testing.T) { |
| ctx := assert.Context(t) |
| m := &stateMethodTester{} |
| err := fsm.RunWith(ctx, m, fsm.MustCompile(fsm.FSM{ |
| States: []fsm.State{ |
| fsm.Method("B"), |
| }, |
| })) |
| assert.With(ctx).ThatError(err).HasMessage("Error:No such method") |
| } |
| |
| func TestInvalidStateMethodSignature(t *testing.T) { |
| ctx := assert.Context(t) |
| m := &stateMethodTester{} |
| err := fsm.RunWith(ctx, m, fsm.MustCompile(fsm.FSM{ |
| States: []fsm.State{ |
| fsm.Method("Bad"), |
| }, |
| })) |
| assert.With(ctx).ThatError(err).HasMessage("Error:Invalid signature") |
| } |