| new file mode 100644 |
| index 000000000..224cc59bf |
| --- /dev/null |
| +++ b/internal/regionlabel/move_test.go |
| @@ -0,0 +1,126 @@ |
| +package regionlabel |
| + |
| +import ( |
| + "go/ast" |
| + "go/parser" |
| + "go/token" |
| + "path/filepath" |
| + "strconv" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/provision" |
| +) |
| + |
| +// stateConsts returns every string constant in the const block of file that |
| +// declares anchor: the Phase 1 states (provision.go, anchor MovePreparing) |
| +// and the offline states (offline_move.go, anchor OfflineStaging). A state |
| +// added to either block without a class fails TestEveryMoveStateHasAPhrase. |
| +func stateConsts(t *testing.T, file, anchor string) map[string]string { |
| + t.Helper() |
| + f, err := parser.ParseFile(token.NewFileSet(), filepath.Join("..", "provision", file), nil, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, d := range f.Decls { |
| + g, ok := d.(*ast.GenDecl) |
| + if !ok || g.Tok != token.CONST { |
| + continue |
| + } |
| + out, hasAnchor := map[string]string{}, false |
| + for _, s := range g.Specs { |
| + vs := s.(*ast.ValueSpec) |
| + for i, name := range vs.Names { |
| + hasAnchor = hasAnchor || name.Name == anchor |
| + if i < len(vs.Values) { |
| + if lit, ok := vs.Values[i].(*ast.BasicLit); ok && lit.Kind == token.STRING { |
| + v, _ := strconv.Unquote(lit.Value) |
| + out[name.Name] = v |
| + } |
| + } |
| + } |
| + } |
| + if hasAnchor { |
| + return out |
| + } |
| + } |
| + t.Fatalf("no const block in %s declares %s", file, anchor) |
| + return nil |
| +} |
| + |
| +func TestEveryMoveStateHasAPhrase(t *testing.T) { |
| + t.Parallel() |
| + for _, block := range []struct{ file, anchor string }{ |
| + {"provision.go", "MovePreparing"}, {"offline_move.go", "OfflineStaging"}, |
| + } { |
| + for name, state := range stateConsts(t, block.file, block.anchor) { |
| + if moveClasses[state] == moveUnknown { |
| + t.Errorf("%s (%q) has no move class: add it to moveClasses", name, state) |
| + } |
| + } |
| + } |
| +} |
| + |
| +func TestMovePhraseByClass(t *testing.T) { |
| + t.Parallel() |
| + n := names() |
| + at := func(state string) provision.Record { |
| + r := rec("uk", "", "") |
| + r.Move = &provision.MoveState{ID: "mv_abcdefghijklmnopqrstuvwxyz", From: "uk", To: "au", State: state} |
| + return r |
| + } |
| + for state, want := range map[string]string{ |
| + provision.MovePreparing: "moving to Sydney (au)", |
| + provision.MoveReady: "moving to Sydney (au)", |
| + provision.MoveCutting: "moving to Sydney (au)", |
| + provision.OfflineStaging: "moving to Sydney (au)", |
| + provision.OfflineStopping: "moving to Sydney (au)", |
| + provision.OfflineSealing: "moving to Sydney (au)", |
| + provision.OfflineSealed: "moving to Sydney (au)", |
| + provision.MoveLanding: "arriving from London (uk)", |
| + provision.MoveCommitted: "arriving from London (uk)", |
| + provision.OfflineLanding: "arriving from London (uk)", |
| + provision.OfflineLanded: "arriving from London (uk)", |
| + provision.OfflineOpen: "arriving from London (uk)", |
| + provision.OfflineFinishing: "arriving from London (uk)", |
| + provision.MoveAborting: "returning from Sydney (au)", |
| + provision.MoveReverting: "returning from Sydney (au)", |
| + provision.MoveRollingBack: "returning from Sydney (au)", |
| + provision.MoveStuck: "returning from Sydney (au)", |
| + provision.OfflineReverting: "returning from Sydney (au)", |
| + "a-state-from-a-newer-box": "", |
| + } { |
| + if got := MovePhrase(at(state), "ie", n); got != want { |
| + t.Errorf("%s: %q, want %q", state, got, want) |
| + } |
| + } |
| + if got := MovePhrase(rec("uk", "", ""), "ie", n); got != "" { |
| + t.Fatalf("no move: %q", got) |
| + } |
| +} |
| + |
| +// TestMovePhraseFollowsTheFlip: Review Focus 4, through the real offline |
| +// transitions. Before the flip the record is still in uk; the flip moves |
| +// the record to au and the phrase turns round; an abort of a landing puts |
| +// the record back and says it is returning. |
| +func TestMovePhraseFollowsTheFlip(t *testing.T) { |
| + t.Parallel() |
| + n := names() |
| + r := rec("uk", "", "") |
| + const id = "mv_abcdefghijklmnopqrstuvwxyz" |
| + r.Move = &provision.MoveState{Kind: provision.MoveKindOffline, ID: id, From: "uk", To: "au", State: provision.OfflineSealed} |
| + if got := ForRecord(r, "ie", n).Text + ", " + MovePhrase(r, "ie", n); got != "London (uk), moving to Sydney (au)" { |
| + t.Fatalf("sealed: %q", got) |
| + } |
| + if err := provision.OfflineFlip(id)(&r); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := ForRecord(r, "ie", n).Text + ", " + MovePhrase(r, "ie", n); got != "Sydney (au), arriving from London (uk)" { |
| + t.Fatalf("flipped: %q", got) |
| + } |
| + if err := provision.OfflineAbort(id)(&r); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := ForRecord(r, "ie", n).Text + ", " + MovePhrase(r, "ie", n); got != "London (uk), returning from Sydney (au)" { |
| + t.Fatalf("aborted landing: %q", got) |
| + } |
| +} |