Skip to content

Commit 9796324

Browse files
committed
fix(delay): keep percentage deviations in the written form
1 parent da666bb commit 9796324

4 files changed

Lines changed: 22 additions & 33 deletions

File tree

internal/delay/delay.go

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ import (
1414
// Spec is one parsed delay expression; the zero value never delays. What base
1515
// and spread hold depends on the kind: the constant delay, the bounds of
1616
// random, or a center and the deviation around it. A deviation written as a
17-
// percentage stays one so the text round-trips.
17+
// percentage is marked relative so the text round-trips.
1818
type Spec struct {
1919
kind kind
2020
base time.Duration
2121
spread time.Duration
2222
pct float64
23+
rel bool
2324
}
2425

2526
func Fixed(d time.Duration) Spec {
@@ -64,7 +65,7 @@ func Parse(raw string) (Spec, error) {
6465
case err != nil:
6566
return Spec{}, err
6667
case relative:
67-
s.pct = pct
68+
s.pct, s.rel = pct, true
6869
default:
6970
if s.spread, err = f.duration(1, args[1]); err != nil {
7071
return Spec{}, err
@@ -105,14 +106,14 @@ func (s Spec) String() string {
105106
}
106107

107108
func (s Spec) argText() string {
108-
if s.pct > 0 {
109-
return strconv.FormatFloat(s.pct, 'f', -1, 64) + "%"
109+
if s.rel {
110+
return strconv.FormatFloat(s.pct, 'g', -1, 64) + "%"
110111
}
111112
return s.spread.String()
112113
}
113114

114115
func (s Spec) deviation() time.Duration {
115-
if s.pct > 0 {
116+
if s.rel {
116117
return clamp(float64(s.base) * s.pct / 100)
117118
}
118119
return s.spread
@@ -139,36 +140,22 @@ func (s *Spec) UnmarshalText(text []byte) error {
139140

140141
// Descriptor is one distribution as documentation and completions see it.
141142
type Descriptor struct {
142-
name string
143-
summary string
144-
args string
145-
}
146-
147-
func (d Descriptor) Name() string {
148-
return d.name
149-
}
150-
151-
func (d Descriptor) Summary() string {
152-
return d.summary
143+
Name string
144+
Summary string
145+
Args string
153146
}
154147

155148
// Usage is the example call, e.g. random(100ms,500ms).
156149
func (d Descriptor) Usage() string {
157-
return call(d.name, d.args)
158-
}
159-
160-
// Args is the example argument list of Usage without the parentheses, so a
161-
// caller offering the usage as a template knows what part of it to fill in.
162-
func (d Descriptor) Args() string {
163-
return d.args
150+
return call(d.Name, d.Args)
164151
}
165152

166153
// Distributions describes the call forms, in documentation order.
167154
func Distributions() []Descriptor {
168155
out := make([]Descriptor, 0, len(forms)-1)
169156
for _, f := range forms {
170157
if f.name != "" {
171-
out = append(out, Descriptor{name: f.name, summary: f.summary, args: f.args})
158+
out = append(out, Descriptor{Name: f.name, Summary: f.summary, Args: f.args})
172159
}
173160
}
174161
return out

internal/delay/delay_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ func TestParseText(t *testing.T) {
4545
{input: "normal(250ms,20%)", want: "normal(250ms,20%)"},
4646
{input: "jitter(200ms,20%)", want: "jitter(200ms,20%)"},
4747
{input: "jitter(200ms,12.5%)", want: "jitter(200ms,12.5%)"},
48+
{input: "normal(250ms,0%)", want: "normal(250ms,0%)"},
49+
{input: "normal(250ms,1e300%)", want: "normal(250ms,1e+300%)"},
4850
{input: "jitter(200ms,20ms)", want: "jitter(200ms,20ms)"},
4951
}
5052

@@ -240,7 +242,7 @@ func TestDistributions(t *testing.T) {
240242
t.Fatal("no distributions described")
241243
}
242244
for _, f := range forms {
243-
if f.Name() == "" || f.Summary() == "" {
245+
if f.Name == "" || f.Summary == "" {
244246
t.Fatalf("incomplete descriptor %+v", f)
245247
}
246248
s := mustParse(t, f.Usage())

internal/intellisense/directives.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ func mockItems() []Item {
7676
}
7777
for _, d := range delay.Distributions() {
7878
items = append(items, Item{
79-
Label: "latency=" + d.Name(),
80-
Summary: d.Summary(),
79+
Label: "latency=" + d.Name,
80+
Summary: d.Summary,
8181
Insert: "latency=" + d.Usage(),
82-
Placeholder: d.Args(),
82+
Placeholder: d.Args,
8383
})
8484
}
8585
return append(items, Item{

internal/intellisense/directives_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func TestMockLatencyArgsCoverEveryDistribution(t *testing.T) {
117117
t.Fatalf("mock args missing the constant latency: %v", mock)
118118
}
119119
for _, d := range delay.Distributions() {
120-
label := "latency=" + d.Name()
120+
label := "latency=" + d.Name
121121
found := false
122122
for _, it := range mock {
123123
if it.Label != label {
@@ -128,8 +128,8 @@ func TestMockLatencyArgsCoverEveryDistribution(t *testing.T) {
128128
t.Fatalf("%s inserts %q, want the %q example", label, it.Insert, d.Usage())
129129
}
130130
// The arguments are typed over, the parentheses around them are not.
131-
if it.Placeholder != d.Args() {
132-
t.Fatalf("%s selects %q, want the %q arguments", label, it.Placeholder, d.Args())
131+
if it.Placeholder != d.Args {
132+
t.Fatalf("%s selects %q, want the %q arguments", label, it.Placeholder, d.Args)
133133
}
134134
start, end, ok := it.PlaceholderRange()
135135
if !ok {
@@ -138,8 +138,8 @@ func TestMockLatencyArgsCoverEveryDistribution(t *testing.T) {
138138
if got := []rune(it.Insert)[end:]; string(got) != ")" {
139139
t.Fatalf("%s selects up to %q, want the closing paren left out", label, string(got))
140140
}
141-
if got := []rune(it.Insert)[start:end]; string(got) != d.Args() {
142-
t.Fatalf("%s selects %q, want %q", label, string(got), d.Args())
141+
if got := []rune(it.Insert)[start:end]; string(got) != d.Args {
142+
t.Fatalf("%s selects %q, want %q", label, string(got), d.Args)
143143
}
144144
}
145145
if !found {

0 commit comments

Comments
 (0)