Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rst test check messages (fix #17280) #17338

Merged
merged 6 commits into from
Mar 17, 2021

Conversation

a-mr
Copy link
Contributor

@a-mr a-mr commented Mar 11, 2021

Check warnings and errors in RST test. Fixes #17280 using symbolName implemented in #17281.

Depends on #17257 since printing of line numbers / columns were changed there. So review only the latest commit: f9c427a

arg: string) =
let mc = msgkind.whichMsgClass
let a = $msgkind % arg
let message = "$1($2, $3) $4: $5" % [$filename, $line, $col, $mc, a]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use toLocation std/private/miscdollars
rationale: DRY, and enables #690 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed though it's weird that toLocation omits column if it's 0. So I had to change starting column to 1, which would be worth doing anyway.

Copy link
Member

@timotheecour timotheecour Mar 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wrote it this way to avoid spurious file.nim(1, 0) messages when column is not generated in the 1st place (eg depending on nim's flags)

IMO it's fine because valid columns are assumed to be >= 1

result = rstToHtml(input, rstOptions, defaultConfig(), testMsgHandler)
except EParseError:
discard
template assertEqual(s1, s2: untyped) =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use unittest.check or unittest.require, don't roll your own (and assertEqual has the multiple template evaluation bug)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I switched to check, OK.

However for code

    check(warnings8[] == @["input(4, 0) Warning: unknown substitution " &
            "\'citation-som\'h"])

it seems very verbose:

    /home/amakarov/tmp/Nim/tests/stdlib/trstgen.nim(941, 22): Check failed: warnings8[] ==
    @["input(4, 0) Warning: unknown substitution " & "\'citation-som\'h"]
    warnings8[] was @["input(4, 0) Warning: unknown substitution \'citation-som\'"]
    @["input(4, 0) Warning: unknown substitution " & "\'citation-som\'h"] was @["input(4, 0) Warning: unknown substitution \'citation-som\'h"]

The constant in @[...] is repeated 3 times. Do you think check had better be improved?

Copy link
Member

@timotheecour timotheecour Mar 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are 2 things going on here:

but just assume this will eventually be fixed, and use the shorter code as you did here (it only matters when things go wrong anyway, and just hurts a bit debug experience so it's fine)

@a-mr a-mr force-pushed the rst-test-check-messages branch from 76fb6b9 to 67a436c Compare March 12, 2021 16:40
# TODO: find out hot to configure proper exception instead of defect
expect(AssertionDefect):
let output5 = input5.toHtml
var error5 = new string
Copy link
Member

@timotheecour timotheecour Mar 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of having to bump up a counter, use blocks so each test is independent in terms of variables in scope (makes it easier to move them around, add some without messing re-ordering, etc)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. I suggest to do a follow-up PR for trstgen.nim:

  • split to separate tests as you said
  • use dedent in this test everywhere

wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, in a different PR is ok, to make progress faster and reviewing easier :)

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
doAssert error != nil, "unexpected RST error '" & message & "'"
error[] = message
# we check only first error because subsequent ones may be meaningless
raise newException(EParseError, message)
Copy link
Member

@timotheecour timotheecour Mar 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional: why raise only to catch later? is this to stop on 1st error? can't we instead:

  • either keep track of number of errors seen here and not overwrite error[] after it's been written to once
  • or add an option roStopOnFirstError

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, just to stop on 1st error.

We can do all of that but there is little sense. In the 1st commit all errors were checked but I saw that they add little value to 1st error. Check for number of errors is better but that would be difficult to develop: if you broke tests around this number then you don't know immediately what those errors were :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what i meant was keeping track of number of errors so you only set error[] for just the 1st emitted error, and then ignoring subsequent ones. but ok, it's a minor detail.

@a-mr
Copy link
Contributor Author

a-mr commented Mar 13, 2021

Also observed that columns in nim doc (on *.nim files) are shifted by 3.

It appears that a start column of doc comment ## is passed. So I added "## ".len to get the correct column in a previous commit.

There is a little problem here: automatic cropping logic determines number of spaces after ##, which may not be 1, and AFAIK this number is not saved anywhere. So if anyone chooses to not follow the convention 1 space after ## then he'll get a shifted number of columns again.

@@ -25,6 +25,7 @@ from std/private/globs import nativeToUnixPath
const
exportSection = skField
docCmdSkip = "skip"
DocColOffset = "## ".len # assuming that a space was added after ##
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in reply to

Also observed that columns in nim doc (on *.nim files) are shifted by 3.

(i prefer code comments to maintain threading)
take a look at https://gist.github.com/timotheecour/e53f06241914fa1e20a40f631edfae10

run nim doc -d:case1 main (and -d:case2, -d:case3)

before this PR, line and column number is bad
after this PR, line and column number is also bad (maybe a bit worse?)

IMO we should report line and column numbers wrt source file, and not offset it by "## ".len; unless I'm missing something it should be both simpler and more robust.

But I suggest merging this PR to make progress and addressing this inconsistent line/col numbering in followup PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the case1 is just incorrectly adjusted line in parseHeadline. We can fix it with an appropriate test now.

The multiline case2 and case3 are more difficult, some investigation will be needed.

@@ -497,7 +499,9 @@ proc defaultMsgHandler*(filename: string, line, col: int, msgkind: MsgKind,
arg: string) =
let mc = msgkind.whichMsgClass
let a = $msgkind % arg
let message = "$1($2, $3) $4: $5" % [filename, $line, $col, $mc, a]
var message: string
toLocation(message, filename, line, col + ColRstOffset)
Copy link
Member

@timotheecour timotheecour Mar 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in future PR we can add an outplace version of toLocation to make life simpler in here and elsewhere, useful wherever performance is not critical (like here). Or you could also use sugar.dup

@@ -1476,7 +1476,7 @@ $content
# ---------- forum ---------------------------------------------------------

proc rstToHtml*(s: string, options: RstParseOptions,
config: StringTableRef, line=1, column=1,
config: StringTableRef,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of specifying line, col in all callers (and doing so incorrectly, see below with (1,1) instead of (LineRstInit,ColRstInit) which is (1,0), how about add an overload of rstParse:

proc rstParse*(text, filename: string, hasToc: var bool, options: RstParseOptions, findFile: FindFileHandler = nil, msgHandler: MsgHandler = nil): PRstNode =
  rstParse(line=LineRstInit,column=ColRstInit,...)

will simplify and make these correct:

compiler/docgen.nim:161:12:  result = rstParse(text, filename, line, column, hasToc, rstOptions,
lib/packages/docutils/rst.nim:2593:6:proc rstParse*(text, filename: string,
lib/packages/docutils/rstgen.nim:1509:13:  var rst = rstParse(s, filen, line=LineRstInit, column=ColRstInit,
lib/packages/docutils/rstgen.nim:1523:7:      rstParse(rstSource, "", line=LineRstInit, column=ColRstInit,
tests/stdlib/trstgen.nim:425:30:    rstGenera.renderRstToOut(rstParse(input10, "", 1, 1, option, {}), output10)
tests/stdlib/trstgen.nim:451:32:    rstGenera11.renderRstToOut(rstParse(input11, "", 1, 1, option11, {}), output11)
tests/stdlib/trstgen.nim:480:32:    rstGenera12.renderRstToOut(rstParse(input12, "", 1, 1, option12, {roSupportMarkdown}), output12)
tests/stdlib/trstgen.nim:577:30:    rstGenera.renderRstToOut(rstParse(input1, "", 1, 1, option, {}), output1)

can be done in cleanup PR though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will be overloaded in the future.

@a-mr
Copy link
Contributor Author

a-mr commented Mar 17, 2021

@Araq @narimiran

Pure RST part should be already correct here.

The problem with columns in genComment is not fixed correctly but the proper fix is hard since it would require to modify the lexer: currently TLineInfo does not preserve columns, it contains only beginning column of ## or ##[, not column of the doc comment itself:

##[ Doc comment
^   ^
|   |
cur |
    should be

I tried to put the line and column of Doc comment itself to n.info but immediately got a problem with nimpretty.

@timotheecour
Copy link
Member

@a-mr

The problem with columns in genComment is not fixed correctly

let's followup here: timotheecour#658

ringabout pushed a commit to ringabout/Nim that referenced this pull request Mar 22, 2021
ardek66 pushed a commit to ardek66/Nim that referenced this pull request Mar 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rst.whichMsgClass is buggy: relies on $enum instead of $ast(enum)
4 participants