| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
# Copyright (c) 2018-2026, Maxime Soulé |
| 4 |
# All rights reserved. |
| 5 |
# |
| 6 |
# This source code is licensed under the BSD-style license found in the |
| 7 |
# LICENSE file in the root directory of this source tree. |
| 8 |
|
| 9 |
use strict; |
| 10 |
use warnings; |
| 11 |
use autodie; |
| 12 |
use 5.010; |
| 13 |
|
| 14 |
use IPC::Open2; |
| 15 |
|
| 16 |
die "usage $0 [-h]\n" if @ARGV != 0; |
| 17 |
|
| 18 |
(my $REPO_DIR = $0) =~ s,/[^/]+\z,/..,; |
| 19 |
-d $REPO_DIR or die "Cannot find repository directory ($REPO_DIR)\n"; |
| 20 |
|
| 21 |
# Check .golangci.yml vs .github/workflows/ci.yml |
| 22 |
if (open(my $fh, '<', "$REPO_DIR/.github/workflows/ci.yml")) |
| 23 |
{ |
| 24 |
my($ci_min, $linter_min); |
| 25 |
while (defined(my $line = <$fh>)) |
| 26 |
{ |
| 27 |
if ($line =~ /^\s+go-version: \[(\d+\.\d+)/) |
| 28 |
{ |
| 29 |
$ci_min = $1; |
| 30 |
last; |
| 31 |
} |
| 32 |
} |
| 33 |
close $fh; |
| 34 |
$ci_min // die "*** Cannot extract min go version from .github/workflows/ci.yml\n"; |
| 35 |
|
| 36 |
undef $fh; |
| 37 |
open($fh, '<', "$REPO_DIR/.golangci.yml"); |
| 38 |
while (defined(my $line = <$fh>)) |
| 39 |
{ |
| 40 |
if ($line =~ /^\s+go: '([\d.]+)'/) |
| 41 |
{ |
| 42 |
$linter_min = $1; |
| 43 |
last; |
| 44 |
} |
| 45 |
} |
| 46 |
close $fh; |
| 47 |
$linter_min // die "*** Cannot extract min go version from .golangci.yml\n"; |
| 48 |
|
| 49 |
if ($ci_min ne $linter_min) |
| 50 |
{ |
| 51 |
die "*** min go versions mismatch: ci=$ci_min linter=$linter_min\n"; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
my $SITE_REPO_DIR = "$REPO_DIR/../go-testdeep-site"; |
| 56 |
unless (-d $SITE_REPO_DIR) |
| 57 |
{ |
| 58 |
if ($ENV{PROD_SITE}) |
| 59 |
{ |
| 60 |
die "*** Cannot PROD_SITE as $SITE_REPO_DIR not found!\n"; |
| 61 |
} |
| 62 |
|
| 63 |
warn "*** WARNING: cannot find $SITE_REPO_DIR. Disabling site upgrade.\n"; |
| 64 |
undef $SITE_REPO_DIR; |
| 65 |
} |
| 66 |
|
| 67 |
my $DIR = "$REPO_DIR/td"; |
| 68 |
-d $DIR or die "Cannot find td/ directory ($DIR)\n"; |
| 69 |
|
| 70 |
my $URL_ZETTA = 'https://go-testdeep.zetta.rocks'; |
| 71 |
my $URL_GODEV = 'https://pkg.go.dev'; |
| 72 |
my $URL_GODOC = "$URL_GODEV/github.com/maxatome/go-testdeep"; |
| 73 |
|
| 74 |
my $HEADER = <<'EOH'; |
| 75 |
// Copyright (c) 2018-2026, Maxime Soulé |
| 76 |
// All rights reserved. |
| 77 |
// |
| 78 |
// This source code is licensed under the BSD-style license found in the |
| 79 |
// LICENSE file in the root directory of this source tree. |
| 80 |
// |
| 81 |
// DO NOT EDIT!!! AUTOMATICALLY GENERATED!!! |
| 82 |
EOH |
| 83 |
|
| 84 |
my $args_comment_src = <<'EOC'; |
| 85 |
|
| 86 |
%arg{args...} are optional and allow to name the test. This name is |
| 87 |
used in case of failure to qualify the test. If %code{len(args) > 1} and |
| 88 |
the first item of %arg{args} is a string and contains a '%' rune then |
| 89 |
[fmt.Fprintf] is used to compose the name, else %arg{args} are passed to |
| 90 |
[fmt.Fprint]. Do not forget it is the name of the test, not the |
| 91 |
reason of a potential failure. |
| 92 |
EOC |
| 93 |
|
| 94 |
my $ARGS_COMMENT_GD = doc2godoc($args_comment_src); |
| 95 |
my $ARGS_COMMENT_MD = doc2md($args_comment_src); |
| 96 |
|
| 97 |
|
| 98 |
# These operators are variadics. The value describes how the variadic |
| 99 |
# param is handled in T.Operator and CmpOperator functions: |
| 100 |
# - if value is -discard, variadic param is completely ignored; |
| 101 |
# - if value is -smuggle, special case for Smuggle, variadic param is |
| 102 |
# changed to expectedValue only; |
| 103 |
# - otherwise, it is the value to use to mimic empty variadic param in Operator. |
| 104 |
my %IGNORE_VARIADIC = (Between => 'td.BoundsInIn', |
| 105 |
N => 0, |
| 106 |
Re => 'nil', |
| 107 |
Recv => 0, |
| 108 |
TruncTime => 0, |
| 109 |
Sorted => 'nil', |
| 110 |
# These operators accept several StructFields, |
| 111 |
# but we want only one here |
| 112 |
Struct => 'nil', |
| 113 |
SStruct => 'nil', |
| 114 |
# These operators accept several MapEntries, |
| 115 |
# but we want only one here |
| 116 |
Map => 'nil', |
| 117 |
SubMapOf => 'nil', |
| 118 |
SuperMapOf => 'nil', |
| 119 |
# These operators accept several ArrayEntries, |
| 120 |
# but we want only one here |
| 121 |
Array => 'nil', |
| 122 |
Slice => 'nil', |
| 123 |
SuperSliceOf => 'nil', |
| 124 |
# Other cases |
| 125 |
JSONPointer => -discard, |
| 126 |
Smuggle => -smuggle); |
| 127 |
|
| 128 |
# Smuggler operators (automatically filled) |
| 129 |
my %SMUGGLER_OPERATORS; |
| 130 |
|
| 131 |
# These operators should be renamed when used as *T method |
| 132 |
my %RENAME_METHOD = (Lax => 'CmpLax', |
| 133 |
ErrorIs => 'CmpErrorIs'); |
| 134 |
|
| 135 |
# These operators do not have *T method nor Cmp shortcut |
| 136 |
my %ONLY_OPERATORS = map { $_ => 1 } qw(Catch Delay Ignore Tag); |
| 137 |
|
| 138 |
my @INPUT_LABELS = qw(nil bool str int float cplx |
| 139 |
array slice map struct ptr if chan func); |
| 140 |
my %INPUTS; |
| 141 |
@INPUTS{@INPUT_LABELS} = (); |
| 142 |
|
| 143 |
opendir(my $dh, $DIR); |
| 144 |
|
| 145 |
my(%funcs, %operators, %consts, %forbiddenOpsInJSON); |
| 146 |
|
| 147 |
while (readdir $dh) |
| 148 |
{ |
| 149 |
if (/^td_.*\.go\z/ and not /_test.go\z/) |
| 150 |
{ |
| 151 |
my $contents = slurp("$DIR/$_"); |
| 152 |
|
| 153 |
# Load the operators forbidden inside JSON() |
| 154 |
if ($_ eq 'td_json.go') |
| 155 |
{ |
| 156 |
$contents =~ /^var forbiddenOpsInJSON = map\[string\]string\{(.*?)^\}/ms |
| 157 |
or die "$_: forbiddenOpsInJSON map not found\n"; |
| 158 |
@forbiddenOpsInJSON{$1 =~ /"([^"]+)":/g} = (); |
| 159 |
} |
| 160 |
|
| 161 |
while ($contents =~ /^const \(\n(.+)^\)\n/gms) |
| 162 |
{ |
| 163 |
@consts{$1 =~ /^\t([A-Z]\w+)/mg} = (); |
| 164 |
} |
| 165 |
|
| 166 |
my %imports = map { ($_ => $_) } qw(fmt io os reflect testing); |
| 167 |
if ($contents =~ /^import \(\n(.+?)\s*\)\n/ms) |
| 168 |
{ |
| 169 |
foreach my $pkg (split(/\n+/, $1)) |
| 170 |
{ |
| 171 |
if ($pkg =~ /^\s*(\w+)\s+\"([^"]+)/) |
| 172 |
{ |
| 173 |
$imports{$1} = $2; |
| 174 |
$imports{$2} = $2; |
| 175 |
} |
| 176 |
elsif ($pkg =~ m,^\s*"((?:.+/)?([^/"]+)),) |
| 177 |
{ |
| 178 |
$imports{$2} = $1; |
| 179 |
$imports{$1} = $1; |
| 180 |
} |
| 181 |
else |
| 182 |
{ |
| 183 |
die "$_: cannot parse import line <$pkg>\n"; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
my %ops; |
| 189 |
while ($contents =~ m,^// summary\((\w+)\): (.*\n(?://.*\n)*),gm) |
| 190 |
{ |
| 191 |
my($op, $summary) = ($1, $2); |
| 192 |
$summary =~ s,^// input\(.*,,sm; |
| 193 |
$ops{$op} = process_summary($summary =~ s,\n(?://|\z),,gr); |
| 194 |
} |
| 195 |
|
| 196 |
my %inputs; |
| 197 |
while ($contents =~ m,^// input\((\w+)\): (.*\n(?://.*\n)*),gm) |
| 198 |
{ |
| 199 |
my $op = $1; |
| 200 |
foreach my $in (split(/\s*,\s*/, $2 =~ s,\n(?://|\z),,gr)) |
| 201 |
{ |
| 202 |
if ($in eq 'all') |
| 203 |
{ |
| 204 |
@{$inputs{$op}}{keys %INPUTS} = ('✓') x keys %INPUTS; |
| 205 |
next; |
| 206 |
} |
| 207 |
if ($in =~ /^(\w+)\((.*)\)\z/) |
| 208 |
{ |
| 209 |
$inputs{$op}{$1} = process_summary($2); |
| 210 |
$in = $1; |
| 211 |
} |
| 212 |
else |
| 213 |
{ |
| 214 |
$inputs{$op}{$in} = '✓'; |
| 215 |
} |
| 216 |
exists $INPUTS{$in} or die "$_: input($op) unknown input '$in'\n"; |
| 217 |
$inputs{$op}{if} //= '✓'; # interface |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
my $num_smugglers = keys %SMUGGLER_OPERATORS; |
| 222 |
|
| 223 |
while ($contents =~ m,^(// ([A-Z]\w*) .*\n(?://.*\n)*)func \2\((.*?)\) TestDeep \{\n,gm) |
| 224 |
{ |
| 225 |
exists $ops{$2} or die "$_: no summary($2) found\n"; |
| 226 |
exists $inputs{$2} or die "$_: no input($2) found\n"; |
| 227 |
|
| 228 |
my($doc, $func, $params) = ($1, $2, $3); |
| 229 |
|
| 230 |
if ($doc =~ /is a smuggler operator/) |
| 231 |
{ |
| 232 |
$SMUGGLER_OPERATORS{$func} = 1; |
| 233 |
} |
| 234 |
|
| 235 |
my @args; |
| 236 |
foreach my $arg (split(/, /, $params)) |
| 237 |
{ |
| 238 |
my %arg; |
| 239 |
@arg{qw(name type)} = split(/ /, $arg, 2); |
| 240 |
if (defined $arg{type} |
| 241 |
and $arg{variadic} = $arg{type} =~ s/^\.{3}//) |
| 242 |
{ |
| 243 |
if (defined(my $default = $IGNORE_VARIADIC{$func})) |
| 244 |
{ |
| 245 |
next if $default eq -discard; |
| 246 |
|
| 247 |
delete $arg{variadic}; |
| 248 |
if ($default eq -smuggle) |
| 249 |
{ |
| 250 |
# Smuggle: rename variadic param from |
| 251 |
# fnsThenExpectedValue to expectedValue |
| 252 |
$arg{name} = 'expectedValue'; |
| 253 |
} |
| 254 |
else |
| 255 |
{ |
| 256 |
$arg{default} = $default; |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
push(@args, \%arg); |
| 262 |
} |
| 263 |
my $last_type; |
| 264 |
foreach my $arg (reverse @args) |
| 265 |
{ |
| 266 |
if (defined(my $arg_type = $arg->{type}) and not $arg->{variadic}) |
| 267 |
{ |
| 268 |
if (defined $last_type and $arg_type eq $last_type) |
| 269 |
{ |
| 270 |
delete $arg->{type}; |
| 271 |
} |
| 272 |
$last_type = $arg_type; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
$funcs{$func}{args} = \@args unless $ONLY_OPERATORS{$func}; |
| 277 |
|
| 278 |
# "//<TAB>" is OK, otherwise TAB is not allowed |
| 279 |
die "TAB detected in $func operator documentation\n" if $doc =~ m,(?<!^//)\t,m; |
| 280 |
|
| 281 |
$operators{$func} = { |
| 282 |
name => $func, |
| 283 |
summary => delete $ops{$func}, |
| 284 |
input => delete $inputs{$func}, |
| 285 |
doc => $doc, |
| 286 |
signature => "func $func($params) TestDeep", |
| 287 |
args => \@args, |
| 288 |
imports => \%imports, |
| 289 |
}; |
| 290 |
} |
| 291 |
|
| 292 |
if (%ops) |
| 293 |
{ |
| 294 |
die "$_: summary found without operator definition: " |
| 295 |
. join(', ', keys %ops) . "\n"; |
| 296 |
} |
| 297 |
|
| 298 |
if (%inputs) |
| 299 |
{ |
| 300 |
die "$_: input found without operator definition: " |
| 301 |
. join(', ', keys %inputs) . "\n"; |
| 302 |
} |
| 303 |
|
| 304 |
if ($contents =~ m,^\ttdSmugglerBase(?! // ignored),m |
| 305 |
and $num_smugglers == keys %SMUGGLER_OPERATORS) |
| 306 |
{ |
| 307 |
die "$_: this file should contain at least one smuggler operator\n"; |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
closedir($dh); |
| 313 |
|
| 314 |
%funcs or die "No TestDeep golang source file found!\n"; |
| 315 |
|
| 316 |
my $funcs_contents = my $t_contents = <<EOH; |
| 317 |
$HEADER |
| 318 |
package td |
| 319 |
|
| 320 |
import ( |
| 321 |
\t"time" |
| 322 |
) |
| 323 |
|
| 324 |
EOH |
| 325 |
|
| 326 |
$funcs_contents .= <<EOV; |
| 327 |
// allOperators lists the ${\scalar(keys(%funcs) + keys %ONLY_OPERATORS)} operators. |
| 328 |
// nil means not usable in JSON(). |
| 329 |
var allOperators = map[string]any{ |
| 330 |
${\join('', map |
| 331 |
{ qq("$_": ) . (exists $forbiddenOpsInJSON{$_} ? 'nil' : $_) . ",\n" } |
| 332 |
sort keys(%funcs), keys %ONLY_OPERATORS)} |
| 333 |
} |
| 334 |
|
| 335 |
EOV |
| 336 |
|
| 337 |
my @sorted_funcs = sort { lc($a) cmp lc($b) } keys %funcs; |
| 338 |
|
| 339 |
foreach my $func (@sorted_funcs) |
| 340 |
{ |
| 341 |
my $func_name = "Cmp$func"; |
| 342 |
my $method_name = $RENAME_METHOD{$func} // $func; |
| 343 |
|
| 344 |
my $cmp_args = 'got'; |
| 345 |
if (@{$funcs{$func}{args}}) |
| 346 |
{ |
| 347 |
foreach my $arg (@{$funcs{$func}{args}}) |
| 348 |
{ |
| 349 |
if (defined $arg->{type}) |
| 350 |
{ |
| 351 |
if ($arg->{type} ne 'any' or $arg->{variadic}) |
| 352 |
{ |
| 353 |
$cmp_args .= ' any'; |
| 354 |
} |
| 355 |
last |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
else |
| 360 |
{ |
| 361 |
$cmp_args .= ' any'; |
| 362 |
} |
| 363 |
|
| 364 |
my $call_args = ''; |
| 365 |
my @cmpt_args; |
| 366 |
|
| 367 |
foreach my $arg (@{$funcs{$func}{args}}) |
| 368 |
{ |
| 369 |
$call_args .= ', ' unless $call_args eq ''; |
| 370 |
$call_args .= $arg->{name}; |
| 371 |
push(@cmpt_args, { name => $arg->{name} }); |
| 372 |
|
| 373 |
$cmp_args .= ", $arg->{name} "; |
| 374 |
|
| 375 |
if ($arg->{variadic}) |
| 376 |
{ |
| 377 |
$call_args .= '...'; |
| 378 |
$cmp_args .= '[]'; |
| 379 |
} |
| 380 |
|
| 381 |
$cmp_args .= $arg->{type} if defined $arg->{type}; |
| 382 |
} |
| 383 |
|
| 384 |
my $cmp_doc = <<EOF; |
| 385 |
Cmp$func is a shortcut for: |
| 386 |
|
| 387 |
\ttd.Cmp(t, got, td.$func($call_args), args...) |
| 388 |
|
| 389 |
EOF |
| 390 |
|
| 391 |
$funcs_contents .= "\n" . go_comment($cmp_doc) . <<EOF; |
| 392 |
// See [$func] for details. |
| 393 |
EOF |
| 394 |
$cmp_doc .= <<EOF; # operator doc |
| 395 |
See above for details. |
| 396 |
EOF |
| 397 |
|
| 398 |
my $t_doc = <<EOF; |
| 399 |
$method_name is a shortcut for: |
| 400 |
|
| 401 |
\tt.Cmp(got, td.$func($call_args), args...) |
| 402 |
|
| 403 |
EOF |
| 404 |
$t_contents .= "\n" . go_comment($t_doc) . <<EOF; |
| 405 |
// See [$func] for details. |
| 406 |
EOF |
| 407 |
$t_doc .= <<EOF; # operator doc |
| 408 |
See above for details. |
| 409 |
EOF |
| 410 |
|
| 411 |
my $func_comment; |
| 412 |
my $last_arg = $funcs{$func}{args}[-1]; |
| 413 |
if (defined $last_arg->{default}) |
| 414 |
{ |
| 415 |
my $default = $last_arg->{default}; |
| 416 |
$default = "[$1]" if $default =~ /^td\.(.+)/ and exists $consts{$1}; |
| 417 |
$func_comment .= <<EOF; |
| 418 |
|
| 419 |
[$func] optional parameter $last_arg->{name} is here mandatory. |
| 420 |
$default value should be passed to mimic its absence in |
| 421 |
original [$func] call. |
| 422 |
EOF |
| 423 |
} |
| 424 |
|
| 425 |
$func_comment .= <<EOF; |
| 426 |
|
| 427 |
Returns true if the test is OK, false if it fails. |
| 428 |
EOF |
| 429 |
my $method_comment = $func_comment; |
| 430 |
$func_comment .= <<EOF; |
| 431 |
|
| 432 |
If t is a [*T] then its Config field is inherited. |
| 433 |
EOF |
| 434 |
$operators{$func}{cmp}{name} = "Cmp$func"; |
| 435 |
$operators{$func}{cmp}{doc} = $cmp_doc . $func_comment . $ARGS_COMMENT_MD; |
| 436 |
$operators{$func}{cmp}{signature} = my $cmp_sig = |
| 437 |
"func Cmp$func(t TestingT, $cmp_args, args ...any) bool"; |
| 438 |
$operators{$func}{cmp}{args} = \@cmpt_args; |
| 439 |
$funcs_contents .= go_comment($func_comment . $ARGS_COMMENT_GD) . <<EOF; |
| 440 |
$cmp_sig { |
| 441 |
\tt.Helper() |
| 442 |
\treturn Cmp(t, got, $func($call_args), args...) |
| 443 |
} |
| 444 |
EOF |
| 445 |
|
| 446 |
$operators{$func}{t}{name} = $method_name; |
| 447 |
$operators{$func}{t}{doc} = $t_doc . $method_comment . $ARGS_COMMENT_MD; |
| 448 |
$operators{$func}{t}{signature} = my $t_sig = |
| 449 |
"func (t *T) $method_name($cmp_args, args ...any) bool"; |
| 450 |
$operators{$func}{t}{args} = \@cmpt_args; |
| 451 |
$t_contents .= go_comment($method_comment . $ARGS_COMMENT_GD) . <<EOF; |
| 452 |
$t_sig { |
| 453 |
\tt.Helper() |
| 454 |
\treturn t.Cmp(got, $func($call_args), args...) |
| 455 |
} |
| 456 |
EOF |
| 457 |
} |
| 458 |
|
| 459 |
my $examples = slurp("$DIR/example_test.go"); |
| 460 |
my $funcs_reg = join('|', @sorted_funcs); |
| 461 |
|
| 462 |
my($imports) = ($examples =~ /^(import \(.+?^\))$/ms); |
| 463 |
|
| 464 |
while ($examples =~ /^func Example($funcs_reg)(_\w+)?\(\) \{\n(.*?)^\}$/gms) |
| 465 |
{ |
| 466 |
my($op, $name, $code) = ($1, $2, $3); |
| 467 |
if ($code =~ /\btd\.$op\(/) |
| 468 |
{ |
| 469 |
# Only copy example for which an operator use is found |
| 470 |
push(@{$funcs{$op}{examples}}, { name => $name // '', code => $code }); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
{ |
| 475 |
open(my $fh, "| gofmt -s > '$DIR/cmp_funcs.go'"); |
| 476 |
print $fh $funcs_contents; |
| 477 |
close $fh; |
| 478 |
say "$DIR/cmp_funcs.go generated"; |
| 479 |
} |
| 480 |
|
| 481 |
{ |
| 482 |
open(my $fh, "| gofmt -s > '$DIR/t.go'"); |
| 483 |
print $fh $t_contents; |
| 484 |
close $fh; |
| 485 |
say "$DIR/t.go generated"; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
my $funcs_test_contents = <<EOH; |
| 490 |
$HEADER |
| 491 |
package td_test |
| 492 |
|
| 493 |
$imports |
| 494 |
EOH |
| 495 |
|
| 496 |
my $t_test_contents = $funcs_test_contents; |
| 497 |
|
| 498 |
my($rep, $reb, $rec); |
| 499 |
$rep = qr/\( [^()]* (?:(??{ $rep }) [^()]* )* \)/x; # recursively matches (...) |
| 500 |
$reb = qr/\[ [^][]* (?:(??{ $reb }) [^][]* )* \]/x; # recursively matches [...] |
| 501 |
$rec = qr/\{ [^{}]* (?:(??{ $rec }) [^{}]* )* \}/x; # recursively matches {...} |
| 502 |
|
| 503 |
my $rparam =qr/"(?:\\.|[^"]+)*" # "string" |
| 504 |
|'(?:\\.|[^']+)*' # 'char' |
| 505 |
|`[^`]*` # `string` |
| 506 |
|&[a-zA-Z_]\w*(?:\.\w+)?(?:$rec)? # &Struct{...}, &variable |
| 507 |
|&?\[[^][]*\](?:any|\w+)$rec # []Array{...} |
| 508 |
|\[\]byte\("[^"]+"\) # []byte("...") |
| 509 |
|map${reb}\w+$rec # map[...]Type{...} |
| 510 |
|func\([^)]*\)[^{]+$rec # func fn (...) ... { ... } |
| 511 |
|[a-zA-Z_]\w*(?:\.\w+)?(?:$rec|$rep)? # Str{...}, Fn(...), pkg.var |
| 512 |
|[\w.*+-\/]+ # 123*pkg.var... |
| 513 |
|$rep$rep # (type)(value) |
| 514 |
/x; |
| 515 |
|
| 516 |
sub extract_params |
| 517 |
{ |
| 518 |
my($func, $params_str) = @_; |
| 519 |
my $str = substr($params_str, 1, -1); |
| 520 |
|
| 521 |
$str ne '' or return; |
| 522 |
|
| 523 |
my @params; |
| 524 |
for (;;) |
| 525 |
{ |
| 526 |
if ($str =~ /\G\s*($rparam)\s*(,|\z)/omsgx) |
| 527 |
{ |
| 528 |
push(@params, $1); |
| 529 |
$2 or return @params; |
| 530 |
} |
| 531 |
else |
| 532 |
{ |
| 533 |
die "Cannot extract params from $func: $params_str\n" |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
foreach my $func (@sorted_funcs) |
| 539 |
{ |
| 540 |
my $args = $funcs{$func}{args}; |
| 541 |
my $method = $RENAME_METHOD{$func} // $func; |
| 542 |
|
| 543 |
foreach my $example (@{$funcs{$func}{examples}}) |
| 544 |
{ |
| 545 |
my $name = $example->{name}; |
| 546 |
|
| 547 |
foreach my $info ([ "td.Cmp$func(t, ", "Cmp$func", \$funcs_test_contents ], |
| 548 |
[ "t.$method(", "T_$method",\$t_test_contents ]) |
| 549 |
{ |
| 550 |
(my $code = $example->{code}) =~ |
| 551 |
s%td\.Cmp\(t,\s+($rparam),\s+td\.$func($rep)% |
| 552 |
my @params = extract_params("$func$name", $2); |
| 553 |
my $repl = $info->[0] . $1; |
| 554 |
for (my $i = 0; $i < @$args; $i++) |
| 555 |
{ |
| 556 |
$repl .= ', '; |
| 557 |
if ($func eq 'Sorted') # very special case |
| 558 |
{ |
| 559 |
my $num = @params - $i; |
| 560 |
if ($num == 0) { $repl .= 'nil' } |
| 561 |
elsif ($num == 1) { $repl .= $params[$i] } |
| 562 |
else |
| 563 |
{ |
| 564 |
$repl .= '[]string{' |
| 565 |
. join(', ', @params[$i .. $#params]) |
| 566 |
. '}' |
| 567 |
} |
| 568 |
last |
| 569 |
} |
| 570 |
if ($args->[$i]{variadic}) |
| 571 |
{ |
| 572 |
if (defined $params[$i]) |
| 573 |
{ |
| 574 |
$repl .= '[]' . $args->[$i]{type} . '{' |
| 575 |
. join(', ', @params[$i .. $#params]) |
| 576 |
. '}'; |
| 577 |
last |
| 578 |
} |
| 579 |
$repl .= 'nil'; |
| 580 |
last |
| 581 |
} |
| 582 |
$repl .= $params[$i] |
| 583 |
// $args->[$i]{default} |
| 584 |
// die("Internal error, no param: " |
| 585 |
. "$func$name -> #$i/$args->[$i]{name}!\n"); |
| 586 |
} |
| 587 |
$repl |
| 588 |
%egs; |
| 589 |
|
| 590 |
${$info->[2]} .= <<EOF; |
| 591 |
|
| 592 |
func Example$info->[1]$name() { |
| 593 |
$code} |
| 594 |
EOF |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
{ |
| 600 |
# Cmp* examples |
| 601 |
open(my $fh, "| gofmt -s > '$DIR/example_cmp_test.go'"); |
| 602 |
print $fh $funcs_test_contents; |
| 603 |
close $fh; |
| 604 |
say "$DIR/example_cmp_test.go generated"; |
| 605 |
} |
| 606 |
|
| 607 |
{ |
| 608 |
# T.* examples |
| 609 |
$t_test_contents =~ s/t := &testing\.T\{\}/t := td.NewT(&testing\.T\{\})/g; |
| 610 |
$t_test_contents =~ s/td\.Cmp\(t,/t.Cmp(/g; |
| 611 |
|
| 612 |
open(my $fh, "| gofmt -s > '$DIR/example_t_test.go'"); |
| 613 |
print $fh $t_test_contents; |
| 614 |
close $fh; |
| 615 |
say "$DIR/example_t_test.go generated"; |
| 616 |
} |
| 617 |
|
| 618 |
# |
| 619 |
# Check "args..." comment is the same everywhere it needs to be |
| 620 |
my @args_errors; |
| 621 |
$ARGS_COMMENT_GD = go_comment($ARGS_COMMENT_GD); |
| 622 |
foreach my $go_file (do { opendir(my $dh, $DIR); |
| 623 |
grep /(?<!_test)\.go\z/, readdir $dh }) |
| 624 |
{ |
| 625 |
my $contents = slurp("$DIR/$go_file"); |
| 626 |
|
| 627 |
while ($contents =~ m,\n((?://[^\n]*\n)*) |
| 628 |
func\ ([A-Z]\w+|\(t\ \*T\)\ [A-Z]\w+)($rep),xg) |
| 629 |
{ |
| 630 |
my($comment, $func, $params) = ($1, $2, $3); |
| 631 |
|
| 632 |
next if ($func eq '(t *T) CmpDeeply' |
| 633 |
or $func eq 'CmpDeeply' |
| 634 |
or $func =~ /^\(t \*T\) (?:Log|Error|Fatal)Trace\z/); |
| 635 |
|
| 636 |
if ($params =~ /\Qargs ...any)\E\z/ |
| 637 |
and $comment !~ /\Q$ARGS_COMMENT_GD/) |
| 638 |
{ |
| 639 |
push(@args_errors, "$go_file: $func"); |
| 640 |
} |
| 641 |
} |
| 642 |
} |
| 643 |
if (@args_errors) |
| 644 |
{ |
| 645 |
die "*** At least one args... comment is missing or not conform:\n- " |
| 646 |
. join("\n- ", @args_errors) |
| 647 |
. "\n"; |
| 648 |
} |
| 649 |
|
| 650 |
my $common_links = do |
| 651 |
{ |
| 652 |
my $td_url = "$URL_GODOC/td"; |
| 653 |
|
| 654 |
# Specific types and functions |
| 655 |
join("\n", map "[`$_`]: $td_url#$_", qw(T TestDeep Cmp)) |
| 656 |
. "\n\n" |
| 657 |
# Helpers |
| 658 |
. join("\n", map "[`$_`]: $URL_GODOC/helpers/$_", |
| 659 |
qw(tdhttp tdsuite tdsynctest tdutil)) |
| 660 |
. "\n\n" |
| 661 |
# Specific links |
| 662 |
. "[`BeLax` config flag]: $td_url#ContextConfig.BeLax\n" |
| 663 |
. "[`error`]: https://pkg.go.dev/builtin#error\n" |
| 664 |
. "\n\n" |
| 665 |
# Foreign types |
| 666 |
. join("\n", map "[`$_->[0]`]: https://pkg.go.dev/$_->[1]", |
| 667 |
[ 'fmt.Stringer' => 'fmt/#Stringer' ], |
| 668 |
[ 'time.Time' => 'time/#Time' ], |
| 669 |
[ 'math.NaN' => 'math/#NaN' ]) |
| 670 |
. "\n"; |
| 671 |
}; |
| 672 |
|
| 673 |
my @sorted_operators = sort { lc($a) cmp lc($b) } keys %operators; |
| 674 |
|
| 675 |
my $md_links = do |
| 676 |
{ |
| 677 |
$common_links |
| 678 |
. join("\n", map qq([`$_`]: {{% ref "$_" %}}), @sorted_operators) |
| 679 |
. "\n\n" |
| 680 |
# Cmp* functions |
| 681 |
. join("\n", map qq([`Cmp$_`]: {{% ref "$_#cmp\L$_\E-shortcut" %}}), |
| 682 |
@sorted_funcs) |
| 683 |
. "\n\n" |
| 684 |
# T.Cmp* methods |
| 685 |
. join("\n", map |
| 686 |
{ |
| 687 |
my $m = $RENAME_METHOD{$_} // $_; |
| 688 |
qq([`T.$m`]: {{% ref "$_#t\L$m\E-shortcut" %}}) |
| 689 |
} |
| 690 |
@sorted_funcs); |
| 691 |
}; |
| 692 |
|
| 693 |
my $gh_links = do |
| 694 |
{ |
| 695 |
my $td_url = "$URL_ZETTA/operators/"; |
| 696 |
$common_links |
| 697 |
. join("\n", map qq([`$_`]: $td_url\L$_/), @sorted_operators) |
| 698 |
. "\n\n" |
| 699 |
# Cmp* functions |
| 700 |
. join("\n", map qq([`Cmp$_`]: $td_url\L$_/#cmp$_-shortcut), @sorted_funcs) |
| 701 |
. "\n\n" |
| 702 |
# T.Cmp* methods |
| 703 |
. join("\n", map |
| 704 |
{ |
| 705 |
my $m = $RENAME_METHOD{$_} // $_; |
| 706 |
qq([`T.$m`]: $td_url\L$_/#t$m-shortcut) |
| 707 |
} |
| 708 |
@sorted_funcs); |
| 709 |
}; |
| 710 |
|
| 711 |
# README.md |
| 712 |
{ |
| 713 |
my $readme = slurp("$REPO_DIR/README.md"); |
| 714 |
|
| 715 |
# Links |
| 716 |
$readme =~ s{(<!-- links:begin -->).*(<!-- links:end -->)} |
| 717 |
{$1\n$gh_links\n$2}s; |
| 718 |
|
| 719 |
open(my $fh, '>', "$REPO_DIR/README.md.new"); |
| 720 |
print $fh $readme; |
| 721 |
close $fh; |
| 722 |
rename "$REPO_DIR/README.md.new", "$REPO_DIR/README.md"; |
| 723 |
say "$REPO_DIR/README.md modified"; |
| 724 |
} |
| 725 |
|
| 726 |
# Hugo |
| 727 |
if (defined $SITE_REPO_DIR) |
| 728 |
{ |
| 729 |
my $op_examples = slurp("$DIR/example_test.go"); |
| 730 |
|
| 731 |
# Reload generated examples so they are properly gofmt'ed |
| 732 |
my $cmp_examples = slurp("$DIR/example_cmp_test.go"); |
| 733 |
my $t_examples = slurp("$DIR/example_t_test.go"); |
| 734 |
|
| 735 |
foreach my $operator (@sorted_operators) |
| 736 |
{ |
| 737 |
# Rework each operator doc |
| 738 |
my $doc = process_doc($operators{$operator}); |
| 739 |
|
| 740 |
open(my $fh, '>', "$SITE_REPO_DIR/docs_src/content/operators/$operator.md"); |
| 741 |
print $fh <<EOM; |
| 742 |
--- |
| 743 |
title: "$operator" |
| 744 |
weight: 10 |
| 745 |
--- |
| 746 |
|
| 747 |
```go |
| 748 |
$operators{$operator}{signature} |
| 749 |
``` |
| 750 |
|
| 751 |
$doc |
| 752 |
|
| 753 |
> See also [<i class='fas fa-book'></i> $operator godoc](https://pkg.go.dev/github.com/maxatome/go-testdeep/td#$operator). |
| 754 |
|
| 755 |
EOM |
| 756 |
|
| 757 |
my @examples; |
| 758 |
my $re = qr/^func Example${operator}(?:_(\w+))?\(\) \{\n(.+?)^\}$/ms; |
| 759 |
while ($op_examples =~ /$re/g) |
| 760 |
{ |
| 761 |
my $name = ucfirst($1 // 'Base'); |
| 762 |
|
| 763 |
push(@examples, <<EOE); |
| 764 |
{{%expand "$name example" %}}```go |
| 765 |
${2} |
| 766 |
```{{% /expand%}} |
| 767 |
EOE |
| 768 |
} |
| 769 |
if (@examples) |
| 770 |
{ |
| 771 |
printf $fh "### Example%s\n\n", @examples > 1 ? 's' : ''; |
| 772 |
print $fh @examples; |
| 773 |
} |
| 774 |
|
| 775 |
if (my $cmp = $operators{$operator}{cmp}) |
| 776 |
{ |
| 777 |
$cmp->{imports} = $operators{$operator}{imports}; |
| 778 |
unshift(@{$cmp->{args}}, { name => 't' }); |
| 779 |
my $doc = process_doc($cmp); |
| 780 |
shift @{$cmp->{args}}; |
| 781 |
|
| 782 |
print $fh <<EOM; |
| 783 |
## $cmp->{name} shortcut |
| 784 |
|
| 785 |
```go |
| 786 |
$cmp->{signature} |
| 787 |
``` |
| 788 |
|
| 789 |
$doc |
| 790 |
|
| 791 |
> See also [<i class='fas fa-book'></i> $cmp->{name} godoc](https://pkg.go.dev/github.com/maxatome/go-testdeep/td#$cmp->{name}). |
| 792 |
|
| 793 |
EOM |
| 794 |
|
| 795 |
@examples = (); |
| 796 |
my $re = qr/func ExampleCmp${operator}(?:_(\w+))?\(\) \{\n(.+?)^\}$/ms; |
| 797 |
while ($cmp_examples =~ /$re/g) |
| 798 |
{ |
| 799 |
my $name = ucfirst($1 // 'Base'); |
| 800 |
|
| 801 |
push(@examples, <<EOE); |
| 802 |
{{%expand "$name example" %}}```go |
| 803 |
${2} |
| 804 |
```{{% /expand%}} |
| 805 |
EOE |
| 806 |
} |
| 807 |
if (@examples) |
| 808 |
{ |
| 809 |
printf $fh "### Example%s\n\n", @examples > 1 ? 's' : ''; |
| 810 |
print $fh @examples; |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
if (my $t = $operators{$operator}{t}) |
| 815 |
{ |
| 816 |
$t->{imports} = $operators{$operator}{imports}; |
| 817 |
unshift(@{$t->{args}}, { name => 't' }); |
| 818 |
my $doc = process_doc($t); |
| 819 |
shift @{$t->{args}}; |
| 820 |
|
| 821 |
print $fh <<EOM; |
| 822 |
## T.$t->{name} shortcut |
| 823 |
|
| 824 |
```go |
| 825 |
$t->{signature} |
| 826 |
``` |
| 827 |
|
| 828 |
$doc |
| 829 |
|
| 830 |
> See also [<i class='fas fa-book'></i> T.$t->{name} godoc](https://pkg.go.dev/github.com/maxatome/go-testdeep/td#T.$t->{name}). |
| 831 |
|
| 832 |
EOM |
| 833 |
|
| 834 |
@examples = (); |
| 835 |
my $re = qr/func ExampleT_$t->{name}(?:_(\w+))?\(\) \{\n(.+?)^\}$/ms; |
| 836 |
while ($t_examples =~ /$re/g) |
| 837 |
{ |
| 838 |
my $name = ucfirst($1 // 'Base'); |
| 839 |
|
| 840 |
push(@examples, <<EOE); |
| 841 |
{{%expand "$name example" %}}```go |
| 842 |
${2} |
| 843 |
```{{% /expand%}} |
| 844 |
EOE |
| 845 |
} |
| 846 |
if (@examples) |
| 847 |
{ |
| 848 |
printf $fh "### Example%s\n\n", @examples > 1 ? 's' : ''; |
| 849 |
print $fh @examples; |
| 850 |
} |
| 851 |
} |
| 852 |
close $fh; |
| 853 |
} |
| 854 |
|
| 855 |
# Dump operators |
| 856 |
{ |
| 857 |
my $op_list_file = "$SITE_REPO_DIR/docs_src/content/operators/_index.md"; |
| 858 |
my $op_list = slurp($op_list_file); |
| 859 |
|
| 860 |
$op_list =~ s{(<!-- operators:begin -->).*(<!-- operators:end -->)} |
| 861 |
{ |
| 862 |
"$1\n" |
| 863 |
. join('', |
| 864 |
map qq\n: $operators{$_}{summary}\n\n!, |
| 865 |
@sorted_operators) |
| 866 |
. $2 |
| 867 |
}se or die "operators tags not found in $op_list_file\n"; |
| 868 |
|
| 869 |
$op_list =~ s{(<!-- smugglers:begin -->).*(<!-- smugglers:end -->)} |
| 870 |
{ |
| 871 |
"$1\n" |
| 872 |
. join('', |
| 873 |
map qq\n: $operators{$_}{summary}\n\n!, |
| 874 |
sort { lc($a) cmp lc($b) } |
| 875 |
keys %SMUGGLER_OPERATORS) |
| 876 |
. "$md_links\n$2" |
| 877 |
}se or die "smugglers tags not found in $op_list_file\n"; |
| 878 |
|
| 879 |
open(my $fh, '>', "$op_list_file.new"); |
| 880 |
print $fh $op_list; |
| 881 |
close $fh; |
| 882 |
rename "$op_list_file.new", $op_list_file; |
| 883 |
} |
| 884 |
|
| 885 |
# Dump matrices |
| 886 |
{ |
| 887 |
my $matrix_file = "$SITE_REPO_DIR/docs_src/content/operators/matrix.md"; |
| 888 |
my $matrix = slurp($matrix_file); |
| 889 |
|
| 890 |
my $header = <<'EOH'; |
| 891 |
|
| 892 |
| Operator vs go type | nil | bool | string | {u,}int* | float* | complex* | array | slice | map | struct | pointer | interface¹ | chan | func | operator | |
| 893 |
| ------------------- | --- | ---- | ------ | -------- | ------ | -------- | ----- | ----- | --- | ------ | ------- | ---------- | ---- | ---- | -------- | |
| 894 |
EOH |
| 895 |
|
| 896 |
$matrix =~ s{(<!-- op-go-matrix:begin -->).*(<!-- op-go-matrix:end -->)} |
| 897 |
{ |
| 898 |
my $repl = "$1\n"; |
| 899 |
my $num = 0; |
| 900 |
foreach my $op (@sorted_operators) |
| 901 |
{ |
| 902 |
$repl .= $header if $num++ % 10 == 0; |
| 903 |
$repl .= "| [`$op`]"; |
| 904 |
for my $label (@INPUT_LABELS) |
| 905 |
{ |
| 906 |
$repl .= " | " |
| 907 |
. ($operators{$op}{input}{$label} // '✗'); |
| 908 |
} |
| 909 |
$repl .= " | [`$op`] |\n"; |
| 910 |
} |
| 911 |
"$repl\n$md_links\n$2" |
| 912 |
}se or die "op-go-matrix tags not found in $matrix_file\n"; |
| 913 |
|
| 914 |
my %by_input; |
| 915 |
while (my($op, $info) = each %operators) |
| 916 |
{ |
| 917 |
while (my($label, $comment) = each %{$operators{$op}{input}}) |
| 918 |
{ |
| 919 |
$by_input{$label}{$op} = $comment; |
| 920 |
} |
| 921 |
} |
| 922 |
$matrix =~ s{(<!-- go-(\w+)-matrix:begin -->).*(<!-- go-\2-matrix:end -->)} |
| 923 |
{ |
| 924 |
my $repl = "$1\n"; |
| 925 |
foreach my $op (sort keys %{$by_input{$2}}) |
| 926 |
{ |
| 927 |
my $comment = $by_input{$2}{$op}; |
| 928 |
next if $comment eq 'todo'; |
| 929 |
if ($comment eq '✓') |
| 930 |
{ |
| 931 |
next if $2 eq 'if'; |
| 932 |
$comment = ''; |
| 933 |
} |
| 934 |
elsif ($2 eq 'if') |
| 935 |
{ |
| 936 |
$comment =~ s/^✓ \+/ →/; |
| 937 |
} |
| 938 |
else |
| 939 |
{ |
| 940 |
substr($comment, 0, 0, ' only '); |
| 941 |
} |
| 942 |
$repl .= "- [`$op`]$comment\n"; |
| 943 |
} |
| 944 |
$repl . $3 |
| 945 |
}gse or die "go-op-matrix tags not found in $matrix_file\n"; |
| 946 |
|
| 947 |
open(my $fh, '>', "$matrix_file.new"); |
| 948 |
print $fh $matrix; |
| 949 |
close $fh; |
| 950 |
rename "$matrix_file.new", $matrix_file; |
| 951 |
} |
| 952 |
|
| 953 |
# tdhttp example |
| 954 |
{ |
| 955 |
my $example = slurp("$REPO_DIR/helpers/tdhttp/example_test.go"); |
| 956 |
|
| 957 |
my($import) = $example =~ /^(import \(.*?^\))$/ms; |
| 958 |
$import or die "tdhttp example, import not found!\n"; |
| 959 |
$example =~ s/.*^func Example\(\) \{\n\tt := &testing.T\{\}\n\n//ms |
| 960 |
or die "tdhttp example, func Example() not found!\n"; |
| 961 |
|
| 962 |
$example =~ s/fmt\.Printf/t.Logf/g |
| 963 |
or die "tdhttp example, fmt.Printf not found\n"; |
| 964 |
$example =~ s/fmt\.Println/t.Log/g |
| 965 |
or die "tdhttp example, fmt.Println not found\n"; |
| 966 |
$example =~ s,\n\t// Output:\n.*,},s |
| 967 |
or die "tdhttp example, Output: not found\n"; |
| 968 |
|
| 969 |
my $md_file = "$SITE_REPO_DIR/docs_src/content/helpers/_index.md"; |
| 970 |
|
| 971 |
my $final = slurp($md_file) =~ |
| 972 |
s{(<!-- tdhttp:begin -->).*(<!-- tdhttp:end -->)} |
| 973 |
<$1 |
| 974 |
{{%expand "Main example" %}}```go |
| 975 |
package myapi |
| 976 |
|
| 977 |
$import |
| 978 |
|
| 979 |
func TestMyAPI(t *testing.T) { |
| 980 |
$example |
| 981 |
```{{% /expand%}} |
| 982 |
$2>rs or die "tdhttp example not found in $md_file!"; |
| 983 |
|
| 984 |
open(my $out, '>', "$md_file.new"); |
| 985 |
print $out $final; |
| 986 |
close $out; |
| 987 |
rename "$md_file.new", $md_file; |
| 988 |
} |
| 989 |
|
| 990 |
# Update _index.md go lines |
| 991 |
{ |
| 992 |
my $lines = cloc_go(".") |
| 993 |
- cloc_go("internal/json/parser.go", "internal/spew"); |
| 994 |
|
| 995 |
my $index = "$SITE_REPO_DIR/docs_src/content/_index.md"; |
| 996 |
my $contents = slurp($index); |
| 997 |
$contents =~ s,/\d+-lines_of_go-,/$lines-lines_of_go-, |
| 998 |
or die "Cannot update 'lines of go' badge in $index\n"; |
| 999 |
|
| 1000 |
open(my $out, '>', "$index.new"); |
| 1001 |
print $out $contents; |
| 1002 |
close $out; |
| 1003 |
rename "$index.new", $index; |
| 1004 |
} |
| 1005 |
|
| 1006 |
# Final publish |
| 1007 |
if ($ENV{PROD_SITE}) |
| 1008 |
{ |
| 1009 |
# Delegate to go-testdeep-site repository |
| 1010 |
chdir $SITE_REPO_DIR; |
| 1011 |
exec './publish.sh'; |
| 1012 |
} |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
# "" → "//" |
| 1017 |
# "\txxx" → "//\txxx" |
| 1018 |
# "xxx" → "// xxx" |
| 1019 |
sub go_comment |
| 1020 |
{ |
| 1021 |
shift =~ s{^(.?)} |
| 1022 |
{ |
| 1023 |
$1 eq '' |
| 1024 |
? '//' |
| 1025 |
: substr($1, 0, 1) eq "\t" ? "//$1" : "// $1" |
| 1026 |
}egmr |
| 1027 |
} |
| 1028 |
|
| 1029 |
sub doc2godoc |
| 1030 |
{ |
| 1031 |
my $doc = shift; |
| 1032 |
|
| 1033 |
state $repl = { arg => sub { $_[0] }, |
| 1034 |
code => sub { $_[0] }, |
| 1035 |
godoc => sub { $_[0] } }; |
| 1036 |
|
| 1037 |
$doc =~ s/%([a-z]+)\{([^}]+)\}/($repl->{$1} or die $1)->($2)/egr; |
| 1038 |
} |
| 1039 |
|
| 1040 |
sub doc2md |
| 1041 |
{ |
| 1042 |
my $doc = shift; |
| 1043 |
|
| 1044 |
state $repl = { arg => sub { "*$_[0]*" }, |
| 1045 |
code => sub { "`$_[0]`" }, |
| 1046 |
godoc => sub |
| 1047 |
{ |
| 1048 |
my($pkg, $fn) = split('\.', $_[0], 2); |
| 1049 |
"[`$_[0]`](https://pkg.go.dev/$pkg/#$fn)" |
| 1050 |
} }; |
| 1051 |
|
| 1052 |
$doc =~ s/%([a-z]+)\{([^}]+)\}/($repl->{$1} or die $1)->($2)/egr; |
| 1053 |
} |
| 1054 |
|
| 1055 |
sub process_summary |
| 1056 |
{ |
| 1057 |
my $text = shift; |
| 1058 |
|
| 1059 |
$text =~ s/(time\.Time|fmt.Stringer|error)/[`$1`]/g; |
| 1060 |
$text =~ s/BeLax config flag/[`BeLax` config flag]/; |
| 1061 |
$text =~ s/(\[\]byte|\bnil\b)/`$1`/g; |
| 1062 |
|
| 1063 |
return $text; |
| 1064 |
} |
| 1065 |
|
| 1066 |
sub process_doc |
| 1067 |
{ |
| 1068 |
my $op = shift; |
| 1069 |
|
| 1070 |
my $doc = $op->{doc}; |
| 1071 |
|
| 1072 |
$doc =~ s,^// ?,,mg if $doc =~ m,^//,; |
| 1073 |
|
| 1074 |
$doc =~ s/\n{3,}/\n\n/g; |
| 1075 |
|
| 1076 |
my($inEx, $inBul); |
| 1077 |
$doc =~ s{^(?:(\n?\S) |
| 1078 |
|(\n?)(\s+)(\S+))} |
| 1079 |
< |
| 1080 |
if (defined $1) |
| 1081 |
{ |
| 1082 |
if ($inEx) { $inEx = ''; "```\n$1" } |
| 1083 |
elsif ($inBul) { $inBul = ''; "\n$1" } |
| 1084 |
else { $1 } |
| 1085 |
} |
| 1086 |
else |
| 1087 |
{ |
| 1088 |
my($nl, $indent, $beg) = ($2, $3, $4); |
| 1089 |
if ($inEx) { $nl . substr($indent, length($inEx)) . $beg } |
| 1090 |
elsif ($inBul) { $nl . substr($indent, length($inBul)) . $beg } |
| 1091 |
elsif ($beg =~ /^---/) { $inEx = $indent; "$nl```\n$beg" } |
| 1092 |
elsif ($beg =~ /^-/) { $inBul = $indent; "\n-" } |
| 1093 |
else { $inEx = $indent; "$nl```go\n$beg" } |
| 1094 |
} |
| 1095 |
>gemx; |
| 1096 |
# Special case for: "=~" or "!~" |
| 1097 |
$doc =~ s/"=~" or "!~"/"=\\~" or "!\\~"/g; |
| 1098 |
$doc .= "```\n" if $inEx; |
| 1099 |
|
| 1100 |
# Get & remove links at the end of comment |
| 1101 |
my %links; |
| 1102 |
while ($doc =~ s/^\[([^]\n]+)\]: (.+)\n\z//m) |
| 1103 |
{ |
| 1104 |
$links{$1} = $2; |
| 1105 |
} |
| 1106 |
|
| 1107 |
my @codes; |
| 1108 |
$doc =~ s/^(```go\n.*?^```\n)/push(@codes, $1); "CODE<$#codes>"/gems; |
| 1109 |
|
| 1110 |
$doc =~ s{ |
| 1111 |
(?<placeholder> \$\^[A-Za-z]+) # placeholder |
| 1112 |
| \[TestDeep\](?<operator>\s+operators?) # operator |
| 1113 |
| (?<!\w) \[ (?<link> [^]\n]+) \] (?!\w) # link |
| 1114 |
| (?<native_type> (?:(?:\[\])+|\*+|\b) |
| 1115 |
(?:bool\b |
| 1116 |
|u?int(?:\*|(?:8|16|32|64)?\b) |
| 1117 |
|float(?:\*|(?:32|64)\b) |
| 1118 |
|complex(?:\*|(?:64|128)\b) |
| 1119 |
|string\b |
| 1120 |
|rune\b |
| 1121 |
|byte\b |
| 1122 |
|any(?!\s+(?:numeric|of|placeholder))) |
| 1123 |
|\(\*byte\)\(nil\) |
| 1124 |
|\bmap\[string\]any |
| 1125 |
|\b(?:len|cap)\(\) |
| 1126 |
|\bnil\b |
| 1127 |
|\$(?:\d+|[a-zA-Z_]\w*)) # native_type |
| 1128 |
| (?<error> \berror\b) # error |
| 1129 |
| (?<type_behind> \bTypeBehind(?:\(\)|\b)) # type_behind |
| 1130 |
| \b(?<smuggler> smuggler\s+operator)\b # smuggler |
| 1131 |
| (?<belax> BeLax\s+config\s+flag) # belax |
| 1132 |
}{ |
| 1133 |
if ($+{placeholder}) |
| 1134 |
{ |
| 1135 |
"`$+{placeholder}`" |
| 1136 |
} |
| 1137 |
elsif ($+{operator}) |
| 1138 |
{ |
| 1139 |
qq!; |
| 1140 |
} |
| 1141 |
elsif (my $inner = $+{link}) |
| 1142 |
{ |
| 1143 |
if ($links{$inner}) |
| 1144 |
{ |
| 1145 |
qq!; |
| 1146 |
} |
| 1147 |
elsif ($operators{$inner}) |
| 1148 |
{ |
| 1149 |
qq!; |
| 1150 |
} |
| 1151 |
# local exported identifier |
| 1152 |
elsif ($inner =~ /^\*?([A-Z]\w*(?:\.[A-Z]\w*)?)\z/) |
| 1153 |
{ |
| 1154 |
qq!; |
| 1155 |
} |
| 1156 |
# imported package |
| 1157 |
elsif ($inner =~ m,^\*?([a-z][\w/]*)(?:\.([A-Z]\w*(?:\.[A-Z]\w*)?))?\z, |
| 1158 |
and my $full = $op->{imports}{$1}) |
| 1159 |
{ |
| 1160 |
qq . ')'; |
| 1161 |
} |
| 1162 |
else |
| 1163 |
{ |
| 1164 |
qq![$inner]!; |
| 1165 |
} |
| 1166 |
} |
| 1167 |
elsif ($+{native_type}) |
| 1168 |
{ |
| 1169 |
"`$+{native_type}`" |
| 1170 |
} |
| 1171 |
elsif ($+{error}) |
| 1172 |
{ |
| 1173 |
"[`error`]($URL_GODEV/builtin#error)" |
| 1174 |
} |
| 1175 |
elsif ($+{type_behind}) |
| 1176 |
{ |
| 1177 |
qq! |
| 1178 |
} |
| 1179 |
elsif ($+{smuggler}) |
| 1180 |
{ |
| 1181 |
qq! |
| 1182 |
} |
| 1183 |
elsif ($+{belax}) |
| 1184 |
{ |
| 1185 |
qq!; |
| 1186 |
} |
| 1187 |
}geox; |
| 1188 |
|
| 1189 |
$doc =~ s/^See also /> See also /m; |
| 1190 |
|
| 1191 |
if ($op->{args} and @{$op->{args}}) |
| 1192 |
{ |
| 1193 |
$doc =~ s/(?<!\w) |
| 1194 |
(${\join('|', map quotemeta($_->{name}), @{$op->{args}})}) |
| 1195 |
(?!\w)/*$1*/gx; |
| 1196 |
} |
| 1197 |
|
| 1198 |
return $doc =~ s/^CODE<(\d+)>/go_format($op, $codes[$1])/egmr; |
| 1199 |
} |
| 1200 |
|
| 1201 |
sub go_format |
| 1202 |
{ |
| 1203 |
my($operator, $code) = @_; |
| 1204 |
|
| 1205 |
$code =~ s/^```go\n// or return $code; |
| 1206 |
$code =~ s/\n```\n\z//; |
| 1207 |
|
| 1208 |
my $pid = open2(my $fmt_out, my $fmt_in, 'gofmt', '-s'); |
| 1209 |
|
| 1210 |
my $root; |
| 1211 |
if ($code =~ /^func/) |
| 1212 |
{ |
| 1213 |
$root = 1; |
| 1214 |
print $fmt_in <<EOM; |
| 1215 |
package x |
| 1216 |
|
| 1217 |
//line $operator->{name}.go:1 |
| 1218 |
$code |
| 1219 |
EOM |
| 1220 |
} |
| 1221 |
else |
| 1222 |
{ |
| 1223 |
print $fmt_in <<EOM; |
| 1224 |
package x |
| 1225 |
|
| 1226 |
//line $operator->{name}.go:1 |
| 1227 |
func x() { |
| 1228 |
$code |
| 1229 |
} |
| 1230 |
EOM |
| 1231 |
} |
| 1232 |
close $fmt_in; |
| 1233 |
|
| 1234 |
my $new_code = do { <$fmt_out>; <$fmt_out>; <$fmt_out>; # skip 1st 3 lines |
| 1235 |
local $/; <$fmt_out> }; |
| 1236 |
chomp($new_code); |
| 1237 |
unless ($root) |
| 1238 |
{ |
| 1239 |
$new_code =~ s/[^\t]+//; |
| 1240 |
$new_code =~ s/\n\}\z//; |
| 1241 |
$new_code =~ s/^\t//gm; |
| 1242 |
} |
| 1243 |
|
| 1244 |
waitpid $pid, 0; |
| 1245 |
if ($? != 0) |
| 1246 |
{ |
| 1247 |
die <<EOD |
| 1248 |
gofmt of following example for function $operator->{name} failed: |
| 1249 |
$code |
| 1250 |
EOD |
| 1251 |
} |
| 1252 |
|
| 1253 |
$new_code =~ s/^(\t+)/" " x length $1/gme; |
| 1254 |
|
| 1255 |
if ($new_code ne $code) |
| 1256 |
{ |
| 1257 |
die <<EOD; |
| 1258 |
Code example function $operator->{name} is not correctly indented: |
| 1259 |
$code |
| 1260 |
------------------ should be ------------------ |
| 1261 |
$new_code |
| 1262 |
EOD |
| 1263 |
} |
| 1264 |
|
| 1265 |
return "```go\n$new_code\n```\n"; |
| 1266 |
} |
| 1267 |
|
| 1268 |
sub slurp |
| 1269 |
{ |
| 1270 |
local $/; |
| 1271 |
open(my $fh, '<', shift); |
| 1272 |
<$fh> |
| 1273 |
} |
| 1274 |
|
| 1275 |
sub cloc_go |
| 1276 |
{ |
| 1277 |
open(my $fh, '-|', 'cloc', @_); |
| 1278 |
while (defined(my $line = <$fh>)) |
| 1279 |
{ |
| 1280 |
# Language files blank comment code |
| 1281 |
# -------------------------------------------------------------- |
| 1282 |
# Go 3 77 149 1019 |
| 1283 |
if ($line =~ /^Go\s+(?:\d+\s+){3}(\d+)$/) |
| 1284 |
{ |
| 1285 |
return $1; |
| 1286 |
} |
| 1287 |
} |
| 1288 |
close $fh; |
| 1289 |
die "`cloc @_` doesn't find any Go file\n"; |
| 1290 |
} |