-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoracle_test.go
More file actions
284 lines (272 loc) · 7.68 KB
/
Copy pathoracle_test.go
File metadata and controls
284 lines (272 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package optparse
import (
"encoding/json"
"fmt"
"math/big"
"os/exec"
"strings"
"testing"
)
// The differential oracle drives the *same* pure-Ruby OptionParser that
// go-embedded-ruby ships (testdata/optionparser.rb, extracted from rbgo's
// prelude). Each case is run by both this Go engine and the Ruby reference and
// the serialized result (matches + coerced values + leftovers, or the error
// class/reason/args/message) is compared byte-for-byte. This guarantees rbgo
// byte-compatibility; the deterministic ruby-free tests below independently lock
// the same expectations so the no-ruby / qemu lanes still reach 100% coverage.
// rubyHarness mirrors the Go engine via the reference OptionParser. It self-skips
// when ruby is absent and binds $stdout to binary so Windows CRLF never leaks.
const rubyHarness = `
$stdout.binmode
require File.expand_path('testdata/optionparser.rb')
require 'json'
def serialize(v)
case v
when true then {t:"bool", v:true}
when false then {t:"bool", v:false}
when nil then {t:"nil"}
when Integer then {t:"int", v:v.to_s}
when Float then {t:"float", v:v}
when Array then {t:"array", v:v}
when String then {t:"str", v:v}
else {t:"other", v:v.to_s}
end
end
def coerce_arg(c, list, values)
case c
when "Integer" then Integer
when "Float" then Float
when "Array" then Array
when "String" then String
when "List"
if values && !values.empty?
h = {}; list.each_with_index { |k,i| h[k] = values[i] }; h
else
list
end
else nil
end
end
inp = JSON.parse($stdin.read)
op = OptionParser.new
op.banner = inp["banner"] if inp["banner"]
mode = inp["mode"]
begin
if mode == "getopts"
res = op.getopts(inp["argv"], *inp["getopts_specs"])
puts JSON.generate({ok: true, getopts: res})
else
matches = []
(inp["specs"] || []).each_with_index do |sp, idx|
args = sp["opts"].dup
conv = coerce_arg(sp["coerce"], sp["list"], sp["values"])
args << conv if conv
op.on(*args) { |v| matches << {spec: idx, value: serialize(v)} }
end
argv = inp["argv"].dup
case mode
when "order" then rest = op.order!(argv)
when "permute" then rest = op.permute!(argv)
else rest = op.parse!(argv)
end
if inp["help"]
puts JSON.generate({ok: true, help: op.help})
else
puts JSON.generate({ok: true, matches: matches, rest: rest})
end
end
rescue OptionParser::ParseError => e
puts JSON.generate({ok: false, klass: e.class.name, reason: e.reason, args: e.args, message: e.message})
end
`
type oracleSpec struct {
Opts []string `json:"opts"`
Coerce string `json:"coerce,omitempty"`
List []string `json:"list,omitempty"`
Values []string `json:"values,omitempty"`
}
type oracleInput struct {
Mode string `json:"mode"`
Specs []oracleSpec `json:"specs,omitempty"`
Argv []string `json:"argv"`
Banner string `json:"banner,omitempty"`
Help bool `json:"help,omitempty"`
GetoptsSpecs []string `json:"getopts_specs,omitempty"`
}
type serValue struct {
T string `json:"t"`
V any `json:"v,omitempty"`
}
type oracleOutput struct {
OK bool `json:"ok"`
Matches []struct {
Spec int `json:"spec"`
Value serValue `json:"value"`
} `json:"matches"`
Rest []string `json:"rest"`
Help string `json:"help"`
Getopts map[string]json.RawMessage `json:"getopts"`
Klass string `json:"klass"`
Reason string `json:"reason"`
Args []string `json:"args"`
Message string `json:"message"`
}
func runRuby(t *testing.T, in oracleInput) oracleOutput {
t.Helper()
if _, err := exec.LookPath("ruby"); err != nil {
t.Skip("ruby not on PATH; skipping differential test")
}
data, err := json.Marshal(in)
if err != nil {
t.Fatalf("marshal: %v", err)
}
cmd := exec.Command("ruby", "-e", rubyHarness)
cmd.Stdin = strings.NewReader(string(data))
out, err := cmd.Output()
if err != nil {
t.Fatalf("ruby: %v\n%s", err, out)
}
var o oracleOutput
if err := json.Unmarshal(out, &o); err != nil {
t.Fatalf("unmarshal %q: %v", out, err)
}
return o
}
// serializeGo renders a Go Match value the same way the Ruby harness serializes.
func serializeGo(v any) serValue {
switch x := v.(type) {
case bool:
return serValue{T: "bool", V: x}
case nil:
return serValue{T: "nil"}
case int64:
return serValue{T: "int", V: fmt.Sprintf("%d", x)}
case *big.Int:
return serValue{T: "int", V: x.String()}
case float64:
return serValue{T: "float", V: x}
case []string:
arr := make([]any, len(x))
for i, s := range x {
arr[i] = s
}
return serValue{T: "array", V: arr}
case string:
return serValue{T: "str", V: x}
default:
return serValue{T: "other", V: fmt.Sprintf("%v", x)}
}
}
// buildGo registers the input's specs on a fresh Parser and parses argv with the
// requested mode, returning the same shape the oracle compares.
func buildGo(t *testing.T, in oracleInput) oracleOutput {
t.Helper()
p := New()
p.ProgramName = "optparse"
if in.Banner != "" {
p.Banner = in.Banner
}
if in.Mode == "getopts" {
res, err := p.Getopts(in.Argv, in.GetoptsSpecs...)
var o oracleOutput
if err != nil {
pe := err.(*ParseError)
return oracleOutput{Klass: pe.Class(), Reason: pe.Reason(), Args: pe.Args, Message: pe.Error()}
}
o.OK = true
o.Getopts = map[string]json.RawMessage{}
for name, r := range res {
var raw json.RawMessage
switch {
case !r.TakesArg:
raw, _ = json.Marshal(r.Flag)
case r.Str == nil:
raw = json.RawMessage("null")
default:
raw, _ = json.Marshal(*r.Str)
}
o.Getopts[name] = raw
}
return o
}
for _, s := range in.Specs {
p.Define(s.Opts, s.Coerce, s.List, s.Values)
}
var matches []Match
var rest []string
var err error
switch in.Mode {
case "order":
matches, rest, err = p.Order(in.Argv, nil)
case "permute":
matches, rest, err = p.Permute(in.Argv)
default:
matches, rest, err = p.ParseBang(in.Argv)
}
if err != nil {
pe := err.(*ParseError)
return oracleOutput{Klass: pe.Class(), Reason: pe.Reason(), Args: pe.Args, Message: pe.Error()}
}
if in.Help {
// The Ruby harness emits only the help text in this mode (no matches/rest).
return oracleOutput{OK: true, Help: p.Help()}
}
o := oracleOutput{OK: true, Rest: rest}
for _, m := range matches {
o.Matches = append(o.Matches, struct {
Spec int `json:"spec"`
Value serValue `json:"value"`
}{Spec: m.SpecIndex, Value: serializeGo(m.Value)})
}
return o
}
func canon(t *testing.T, o oracleOutput) string {
t.Helper()
// Normalize: drop the OK flag (we only compare meaningful fields) and re-encode
// canonically so map ordering / nil-vs-empty don't cause spurious diffs.
type norm struct {
Matches []struct {
Spec int
T string
V string
}
Rest []string
Help string
Getopts map[string]string
Klass string
Reason string
Args []string
Message string
}
var n norm
for _, m := range o.Matches {
n.Matches = append(n.Matches, struct {
Spec int
T string
V string
}{m.Spec, m.Value.T, fmt.Sprintf("%v", m.Value.V)})
}
n.Rest = o.Rest
n.Help = o.Help
if o.Getopts != nil {
n.Getopts = map[string]string{}
for k, v := range o.Getopts {
n.Getopts[k] = string(v)
}
}
n.Klass, n.Reason, n.Args, n.Message = o.Klass, o.Reason, o.Args, o.Message
b, _ := json.Marshal(n)
return string(b)
}
func TestDifferentialAgainstReference(t *testing.T) {
for i, c := range corpus {
c := c
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
want := runRuby(t, c) // skips if no ruby
got := buildGo(t, c)
if g, w := canon(t, got), canon(t, want); g != w {
t.Errorf("MISMATCH input=%+v\n got=%s\nwant=%s", c, g, w)
}
})
}
}