| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package main |
| 6 |
| 7 import ( |
| 8 "encoding/json" |
| 9 "testing" |
| 10 |
| 11 "golang.org/x/net/context" |
| 12 "google.golang.org/grpc" |
| 13 |
| 14 . "github.com/smartystreets/goconvey/convey" |
| 15 |
| 16 "github.com/golang/protobuf/proto" |
| 17 google_protobuf2 "github.com/luci/luci-go/common/proto/google" |
| 18 dm "github.com/luci/luci-go/dm/api/service/v1" |
| 19 ) |
| 20 |
| 21 type event struct { |
| 22 name string |
| 23 msg proto.Message |
| 24 } |
| 25 |
| 26 type recordingClient struct { |
| 27 deps []*EditParams |
| 28 result *EditResult |
| 29 |
| 30 walkGraphRsp *dm.GraphData |
| 31 ensureGraphDataRsp *dm.EnsureGraphDataRsp |
| 32 } |
| 33 |
| 34 func (r *recordingClient) EnsureGraphData(ctx context.Context, in *dm.EnsureGrap
hDataReq, opts ...grpc.CallOption) (*dm.EnsureGraphDataRsp, error) { |
| 35 r.deps = nil |
| 36 for _, q := range in.Quest { |
| 37 ep := &EditParams{} |
| 38 if err := json.Unmarshal([]byte(q.Parameters), ep); err != nil { |
| 39 panic(err) |
| 40 } |
| 41 r.deps = append(r.deps, ep) |
| 42 } |
| 43 |
| 44 rsp := r.ensureGraphDataRsp |
| 45 r.ensureGraphDataRsp = nil |
| 46 return rsp, nil |
| 47 } |
| 48 |
| 49 func (r *recordingClient) ActivateExecution(ctx context.Context, in *dm.Activate
ExecutionReq, opts ...grpc.CallOption) (*google_protobuf2.Empty, error) { |
| 50 panic("never happens") |
| 51 } |
| 52 |
| 53 func (r *recordingClient) FinishAttempt(ctx context.Context, in *dm.FinishAttemp
tReq, opts ...grpc.CallOption) (*google_protobuf2.Empty, error) { |
| 54 r.result = &EditResult{} |
| 55 if err := json.Unmarshal([]byte(in.Data.Object), r.result); err != nil { |
| 56 panic(err) |
| 57 } |
| 58 return nil, nil |
| 59 } |
| 60 |
| 61 func (r *recordingClient) WalkGraph(ctx context.Context, in *dm.WalkGraphReq, op
ts ...grpc.CallOption) (*dm.GraphData, error) { |
| 62 rsp := r.walkGraphRsp |
| 63 r.walkGraphRsp = nil |
| 64 return rsp, nil |
| 65 } |
| 66 |
| 67 var _ dm.DepsClient = (*recordingClient)(nil) |
| 68 |
| 69 func newEDRunner() (*editDistanceRun, *recordingClient) { |
| 70 recorder := &recordingClient{} |
| 71 return &editDistanceRun{cmdRun: cmdRun{ |
| 72 Context: context.Background(), |
| 73 client: recorder, |
| 74 questDesc: &dm.Quest_Desc{ |
| 75 DistributorParameters: "{}", |
| 76 }, |
| 77 }}, recorder |
| 78 } |
| 79 |
| 80 func TestEditDistance(t *testing.T) { |
| 81 Convey("edit distance", t, func() { |
| 82 edr, recorder := newEDRunner() |
| 83 |
| 84 Convey("base cases", func() { |
| 85 Convey("empty", func() { |
| 86 er, stop := edr.compute(&EditParams{}) |
| 87 So(stop, ShouldBeFalse) |
| 88 So(er, ShouldResemble, EditResult{0, "", ""}) |
| 89 }) |
| 90 |
| 91 Convey("all sub", func() { |
| 92 er, stop := edr.compute(&EditParams{"hello", ""}
) |
| 93 So(stop, ShouldBeFalse) |
| 94 So(er, ShouldResemble, EditResult{5, "-----", ""
}) |
| 95 }) |
| 96 |
| 97 Convey("all add", func() { |
| 98 er, stop := edr.compute(&EditParams{"", "hello"}
) |
| 99 So(stop, ShouldBeFalse) |
| 100 So(er, ShouldResemble, EditResult{5, "+++++", ""
}) |
| 101 }) |
| 102 }) |
| 103 |
| 104 Convey("single", func() { |
| 105 Convey("equal", func() { |
| 106 er, stop := edr.compute(&EditParams{"a", "a"}) |
| 107 So(stop, ShouldBeFalse) |
| 108 So(er, ShouldResemble, EditResult{0, "=", ""}) |
| 109 }) |
| 110 |
| 111 Convey("different", func() { |
| 112 |
| 113 Convey("trigger", func() { |
| 114 recorder.ensureGraphDataRsp = &dm.Ensure
GraphDataRsp{ |
| 115 Accepted: true, ShouldHalt: true
} |
| 116 _, stop := edr.compute(&EditParams{"a",
"b"}) |
| 117 So(stop, ShouldBeTrue) |
| 118 So(recorder.deps, ShouldResemble, []*Edi
tParams{ |
| 119 {"", ""}, |
| 120 {"", "b"}, |
| 121 {"a", ""}, |
| 122 }) |
| 123 }) |
| 124 |
| 125 Convey("already done", func() { |
| 126 recorder.ensureGraphDataRsp = &dm.Ensure
GraphDataRsp{ |
| 127 Accepted: true, |
| 128 QuestIds: []*dm.Quest_ID{{Id: "1
"}, {Id: "2"}, {Id: "3"}}, |
| 129 Result: &dm.GraphData{ |
| 130 Quests: map[string]*dm.Q
uest{ |
| 131 "1": {Attempts:
map[uint32]*dm.Attempt{ |
| 132 1: dm.Ne
wAttemptFinished(dm.NewJsonResult("{}")), |
| 133 }}, |
| 134 "2": {Attempts:
map[uint32]*dm.Attempt{ |
| 135 1: dm.Ne
wAttemptFinished(dm.NewJsonResult("{}")), |
| 136 }}, |
| 137 "3": {Attempts:
map[uint32]*dm.Attempt{ |
| 138 1: dm.Ne
wAttemptFinished(dm.NewJsonResult("{}")), |
| 139 }}, |
| 140 }, |
| 141 }, |
| 142 } |
| 143 er, stop := edr.compute(&EditParams{"a",
"b"}) |
| 144 So(stop, ShouldBeFalse) |
| 145 So(er, ShouldResemble, EditResult{1, "~"
, ""}) |
| 146 }) |
| 147 }) |
| 148 }) |
| 149 }) |
| 150 } |
| OLD | NEW |