Repository navigation
Expand file tree
/
Copy pathreadme_test.go
More file actions
119 lines (112 loc) · 3.21 KB
/
Copy pathreadme_test.go
File metadata and controls
119 lines (112 loc) · 3.21 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
package ruby
import (
"os"
"sort"
"strings"
"testing"
rb "github.com/go-embedded-ruby/ruby"
)
// bindings lists every Ruby name the bindings define: "Module.function",
// "Class.class_method" and "Class#method", for the modules and classes
// Register makes.
func bindings(t *testing.T) []string {
// The names rbgo defines itself in the same modules (its own Stats,
// Special, Optimize and Integrate) are not this package's.
own := map[string]bool{}
for _, n := range walkNames(t, rb.New()) {
own[n] = true
}
var names []string
for _, n := range walkNames(t, newInterp(t)) {
if !own[n] {
names = append(names, n)
}
}
return names
}
// walkNames lists the names of the six modules in an interpreter.
func walkNames(t *testing.T, in *rb.Interp) []string {
src := `
out = []
walk = lambda do |mod|
mod.singleton_methods(false).each { |m| out << "#{mod}.#{m}" }
mod.instance_methods(false).each { |m| out << "#{mod}##{m}" } if mod.is_a?(Class)
mod.constants(false).each do |c|
v = mod.const_get(c)
walk.(v) if v.is_a?(Module) && v.name.start_with?("#{mod}::")
end
end
[:Stats, :Special, :Optimize, :Integrate, :DataFrame, :Plot].each { |m| walk.(Object.const_get(m)) if Object.const_defined?(m) }
out`
v, err := in.Eval(src, "names.rb", 1)
if err != nil {
t.Fatal(err)
}
var names []string
for _, n := range v.Go().([]any) {
names = append(names, n.(string))
}
sort.Strings(names)
return names
}
// The README lists what is bound, every name of it: a binding added without
// its line in the README fails here.
func TestREADMEListsEveryBinding(t *testing.T) {
readme, err := os.ReadFile("README.md")
if err != nil {
t.Fatal(err)
}
text := strings.ReplaceAll(string(readme), "\r\n", "\n") // a Windows checkout
missing := 0
for _, n := range bindings(t) {
// The README groups names by class: "Stats::Normal#pdf" is listed as
// `pdf` in the Stats::Normal entry, so look for the bare name in it.
owner, name := n, n
if i := strings.LastIndexAny(n, ".#"); i >= 0 {
owner, name = n[:i], n[i+1:]
}
section := sectionOf(text, owner)
if !strings.Contains(section, "`"+name+"`") {
if missing < 40 {
t.Errorf("README does not list %s", n)
}
missing++
}
}
if missing > 0 {
t.Errorf("%d names missing in all", missing)
}
}
// sectionOf is the README's entry for a module or class: the lines from its
// "#### Owner" heading to the next heading.
func sectionOf(text, owner string) string {
h := "#### " + owner + "\n"
i := strings.Index(text, h)
if i < 0 {
return ""
}
rest := text[i+len(h):]
if j := strings.Index(rest, "\n#"); j >= 0 {
rest = rest[:j]
}
return rest
}
// TestPrintBindings prints the names grouped as the README lists them; run
// it with -v -run TestPrintBindings when the bindings change.
func TestPrintBindings(t *testing.T) {
groups := map[string][]string{}
var owners []string
for _, n := range bindings(t) {
i := strings.LastIndexAny(n, ".#")
owner := n[:i]
if _, ok := groups[owner]; !ok {
owners = append(owners, owner)
}
groups[owner] = append(groups[owner], "`"+n[i+1:]+"`")
}
var b strings.Builder
for _, o := range owners {
b.WriteString("#### " + o + "\n\n" + strings.Join(groups[o], " ") + "\n\n")
}
t.Log("\n" + b.String())
}