Skip to content

Commit 1db1fc4

Browse files
authored
Turn eqeq in doc comments to asserts for testing (#7531)
* Turn eqeq in doc comments to asserts for testing * Update completion file
1 parent 56c60ee commit 1db1fc4

File tree

7 files changed

+169
-26
lines changed

7 files changed

+169
-26
lines changed

runtime/Belt_HashMap.resi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ let s1 = Belt.HashMap.copy(s0)
174174

175175
Belt.HashMap.set(s0, 2, "3")
176176

177-
assertEqual(Belt.HashMap.get(s0, 2) == Belt.HashMap.get(s1, 2), false)
177+
assertEqual(Belt.HashMap.get(s0, 2) != Belt.HashMap.get(s1, 2), true)
178178
```
179179
*/
180180
let copy: t<'key, 'value, 'id> => t<'key, 'value, 'id>

runtime/Stdlib_Console.resi

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ on MDN.
1414

1515
```rescript
1616
Console.assert_(false, "Hello World!")
17-
Console.assert_(42 == 42, "The answer")
17+
Console.assert_(42 === 42, "The answer")
1818
```
1919
*/
2020
@val
@@ -27,7 +27,7 @@ external assert_: (bool, 'a) => unit = "console.assert"
2727

2828
```rescript
2929
Console.assert2(false, "Hello", "World")
30-
Console.assert2(42 == 42, [1, 2, 3], '4')
30+
Console.assert2(42 === 42, [1, 2, 3], '4')
3131
```
3232
*/
3333
@val
@@ -40,7 +40,7 @@ external assert2: (bool, 'a, 'b) => unit = "console.assert"
4040

4141
```rescript
4242
Console.assert3(false, "Hello", "World", "ReScript")
43-
Console.assert3(42 == 42, "One", 2, #3)
43+
Console.assert3(42 === 42, "One", 2, #3)
4444
```
4545
*/
4646
@val
@@ -54,7 +54,7 @@ external assert3: (bool, 'a, 'b, 'c) => unit = "console.assert"
5454
```rescript
5555
let value = 42
5656
Console.assert4(false, "Hello", "World", "ReScript", "!!!")
57-
Console.assert4(value == 42, [1, 2], (3, 4), [#5, #6], #"polyvar")
57+
Console.assert4(value === 42, [1, 2], (3, 4), [#5, #6], #"polyvar")
5858
```
5959
*/
6060
@val
@@ -68,7 +68,7 @@ external assert4: (bool, 'a, 'b, 'c, 'd) => unit = "console.assert"
6868
```rescript
6969
let value = 42
7070
Console.assert5(false, "Hello", "World", "JS", '!', '!')
71-
Console.assert5(value == 42, [1, 2], (3, 4), [#5, #6], #"polyvar", {"name": "ReScript"})
71+
Console.assert5(value === 42, [1, 2], (3, 4), [#5, #6], #"polyvar", {"name": "ReScript"})
7272
```
7373
*/
7474
@val
@@ -82,7 +82,7 @@ external assert5: (bool, 'a, 'b, 'c, 'd, 'e) => unit = "console.assert"
8282
```rescript
8383
let value = 42
8484
Console.assert6(false, "Hello", "World", "JS", '!', '!', '?')
85-
Console.assert6(value == 42, [1, 2], (3, 4), [#5, #6], #"polyvar", {"name": "ReScript"}, 42)
85+
Console.assert6(value === 42, [1, 2], (3, 4), [#5, #6], #"polyvar", {"name": "ReScript"}, 42)
8686
```
8787
*/
8888
@val
@@ -96,7 +96,7 @@ external assert6: (bool, 'a, 'b, 'c, 'd, 'e, 'f) => unit = "console.assert"
9696
```rescript
9797
let value = 42
9898
Console.assertMany(false, ["Hello", "World"])
99-
Console.assertMany(value == 42, [1, 2, 3])
99+
Console.assertMany(value === 42, [1, 2, 3])
100100
```
101101
*/
102102
@val

runtime/Stdlib_String.resi

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -891,11 +891,8 @@ See [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref
891891
## Examples
892892

893893
```rescript
894-
String.splitByRegExpAtMost("Hello World. How are you doing?", %re("/ /"), ~limit=3) == [
895-
Some("Hello"),
896-
Some("World."),
897-
Some("How"),
898-
]
894+
String.splitByRegExpAtMost("Hello World. How are you doing?", / /, ~limit=3) ==
895+
[Some("Hello"), Some("World."), Some("How")]
899896
```
900897
*/
901898
@send

scripts/format_check.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ case "$(uname -s)" in
2121
if ./cli/rescript.js format -check $files; then
2222
printf "${successGreen}✅ ReScript code formatting ok.${reset}\n"
2323
else
24-
printf "${warningYellow}⚠️ ReScript code formatting issues found.${reset}\n"
24+
printf "${warningYellow}⚠️ ReScript code formatting issues found. Run 'make format' to fix.${reset}\n"
2525
exit 1
2626
fi
2727
;;

tests/analysis_tests/tests/src/expected/CompletionJsx.res.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ Path s
802802
"kind": 12,
803803
"tags": [],
804804
"detail": "(string, RegExp.t, ~limit: int) => array<option<string>>",
805-
"documentation": {"kind": "markdown", "value": "\n`splitByRegExpAtMost(str, regexp, ~limit)` splits the given `str` at every\noccurrence of `regexp` and returns an array of the first `limit` resulting\nsubstrings. If `limit` is negative or greater than the number of substrings, the\narray will contain all the substrings.\nSee [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) on MDN.\n\n## Examples\n\n```rescript\nString.splitByRegExpAtMost(\"Hello World. How are you doing?\", %re(\"/ /\"), ~limit=3) == [\n Some(\"Hello\"),\n Some(\"World.\"),\n Some(\"How\"),\n]\n```\n"},
805+
"documentation": {"kind": "markdown", "value": "\n`splitByRegExpAtMost(str, regexp, ~limit)` splits the given `str` at every\noccurrence of `regexp` and returns an array of the first `limit` resulting\nsubstrings. If `limit` is negative or greater than the number of substrings, the\narray will contain all the substrings.\nSee [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) on MDN.\n\n## Examples\n\n```rescript\nString.splitByRegExpAtMost(\"Hello World. How are you doing?\", / /, ~limit=3) ==\n [Some(\"Hello\"), Some(\"World.\"), Some(\"How\")]\n```\n"},
806806
"sortText": "splitByRegExpAtMost",
807807
"insertText": "->String.splitByRegExpAtMost",
808808
"additionalTextEdits": [{

tests/docstring_tests/DocTest.res

Lines changed: 97 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,96 @@ let getCodeBlocks = example => {
112112
}
113113
}
114114

115+
// Transform lines that contain == patterns to use assertEqual
116+
let transformEqualityAssertions = code => {
117+
let lines = code->String.split("\n")
118+
lines
119+
->Array.mapWithIndex((line, idx) => {
120+
let trimmedLine = line->String.trim
121+
122+
// Check if the line contains == and is not inside a comment
123+
if (
124+
trimmedLine->String.includes("==") &&
125+
!(trimmedLine->String.startsWith("//")) &&
126+
!(trimmedLine->String.startsWith("/*")) &&
127+
// Not an expression line
128+
!(trimmedLine->String.startsWith("if")) &&
129+
!(trimmedLine->String.startsWith("|")) &&
130+
!(trimmedLine->String.startsWith("let")) &&
131+
!(trimmedLine->String.startsWith("~")) &&
132+
!(trimmedLine->String.startsWith("module")) &&
133+
!(trimmedLine->String.startsWith("->")) &&
134+
!(trimmedLine->String.endsWith(","))
135+
) {
136+
// Split string at == (not ===) and transform to assertEqual
137+
let parts = {
138+
let rec searchFrom = (currentLine: string, startIndex: int): option<(string, string)> => {
139+
if startIndex >= currentLine->String.length {
140+
// Base case: reached end of string without finding a suitable "=="
141+
None
142+
} else {
143+
let lineSuffix = currentLine->String.sliceToEnd(~start=startIndex)
144+
let idxEqEqInSuffix = lineSuffix->String.indexOfOpt("==")
145+
let idxEqEqEqInSuffix = lineSuffix->String.indexOfOpt("===")
146+
147+
switch (idxEqEqInSuffix, idxEqEqEqInSuffix) {
148+
| (None, _) =>
149+
// No "==" found in the rest of the string.
150+
None
151+
| (Some(iEqEq), None) =>
152+
// Found "==" but no "===" in the suffix.
153+
// This "==" must be standalone.
154+
// Calculate its absolute index in the original `currentLine`.
155+
let actualIdx = startIndex + iEqEq
156+
let left = currentLine->String.slice(~start=0, ~end=actualIdx)
157+
let right = currentLine->String.sliceToEnd(~start=actualIdx + 2)
158+
Some((left, right))
159+
| (Some(iEqEq), Some(iEqEqEq)) =>
160+
// Found both "==" and "===" in the suffix.
161+
if iEqEq < iEqEqEq {
162+
// The "==" occurs before "===". This "==" is standalone.
163+
// Example: "a == b === c". In suffix "a == b === c", iEqEq is for "a ==".
164+
let actualIdx = startIndex + iEqEq
165+
let left = currentLine->String.slice(~start=0, ~end=actualIdx)
166+
let right = currentLine->String.sliceToEnd(~start=actualIdx + 2)
167+
Some((left, right))
168+
} else {
169+
// iEqEq >= iEqEqEq
170+
// This means the first "==" encountered (at iEqEq relative to suffix)
171+
// is part of or comes after the first "===" encountered (at iEqEqEq relative to suffix).
172+
// Example: "a === b". In suffix "a === b", iEqEqEq is for "a ===", iEqEq is also for "a ==".
173+
// We must skip over the "===" found at iEqEqEq and search again.
174+
// The next search should start after this "===".
175+
// The "===" starts at (startIndex + iEqEqEq) in the original line. It has length 3.
176+
searchFrom(currentLine, startIndex + iEqEqEq + 3)
177+
}
178+
}
179+
}
180+
}
181+
searchFrom(line, 0)
182+
}
183+
let parts = switch parts {
184+
| Some((left, right)) if right->String.trim->String.length === 0 =>
185+
Some((
186+
left,
187+
lines
188+
->Array.get(idx + 1)
189+
->Option.getExn(~message="Expected to have an expected expression on the next line"),
190+
))
191+
| _ => parts
192+
}
193+
switch parts {
194+
| Some((left, right)) if !(right->String.includes(")") || right->String.includes("//")) =>
195+
`(${left->String.trim})->assertEqual(${right->String.trim})`
196+
| _ => line
197+
}
198+
} else {
199+
line
200+
}
201+
})
202+
->Array.join("\n")
203+
}
204+
115205
let rec loop = (lines: list<string>, acc: list<string>) => {
116206
switch lines {
117207
| list{hd, ...rest} =>
@@ -120,16 +210,13 @@ let getCodeBlocks = example => {
120210
->String.startsWith("```res") {
121211
| true =>
122212
let code = loopEndCodeBlock(rest, list{})
123-
loop(
124-
rest,
125-
list{
126-
code
127-
->List.reverse
128-
->List.toArray
129-
->Array.join("\n"),
130-
...acc,
131-
},
132-
)
213+
let codeString =
214+
code
215+
->List.reverse
216+
->List.toArray
217+
->Array.join("\n")
218+
->transformEqualityAssertions
219+
loop(rest, list{codeString, ...acc})
133220
| false => loop(rest, acc)
134221
}
135222
| list{} => acc

tests/docstring_tests/DocTest.res.js

Lines changed: 60 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)