From 89d30c1b981a803f2dfa077a5cbc45c5f8f9888b Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Fri, 15 Sep 2023 21:57:37 -0500 Subject: [PATCH] Improve help grammar for field hints (#51178) --- stdlib/REPL/src/docview.jl | 5 ++-- stdlib/REPL/test/docview.jl | 50 ++++++++++++++++++++++++------------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/stdlib/REPL/src/docview.jl b/stdlib/REPL/src/docview.jl index 9b35765c0c511..377f63b5148d2 100644 --- a/stdlib/REPL/src/docview.jl +++ b/stdlib/REPL/src/docview.jl @@ -643,8 +643,9 @@ function fielddoc(binding::Binding, field::Symbol) end end end - fields = join(["`$f`" for f in fieldnames(resolve(binding))], ", ", ", and ") - fields = isempty(fields) ? "no fields" : "fields $fields" + fs = fieldnames(resolve(binding)) + fields = isempty(fs) ? "no fields" : (length(fs) == 1 ? "field " : "fields ") * + join(("`$f`" for f in fs), ", ", ", and ") Markdown.parse("`$(resolve(binding))` has $fields.") end diff --git a/stdlib/REPL/test/docview.jl b/stdlib/REPL/test/docview.jl index a8c7955b6952f..c967b11274953 100644 --- a/stdlib/REPL/test/docview.jl +++ b/stdlib/REPL/test/docview.jl @@ -4,21 +4,17 @@ using Test import REPL, REPL.REPLCompletions import Markdown -@testset "symbol completion" begin - @test startswith(let buf = IOBuffer() - Core.eval(Main, REPL.helpmode(buf, "α")) - String(take!(buf)) - end, "\"α\" can be typed by \\alpha\n") - - @test startswith(let buf = IOBuffer() - Core.eval(Main, REPL.helpmode(buf, "🐨")) - String(take!(buf)) - end, "\"🐨\" can be typed by \\:koala:\n") +function get_help_io(input) + buf = IOBuffer() + eval(REPL.helpmode(buf, input)) + String(take!(buf)) +end +get_help_standard(input) = string(eval(REPL.helpmode(IOBuffer(), input))) - @test startswith(let buf = IOBuffer() - Core.eval(Main, REPL.helpmode(buf, "ᵞ₁₂₃¹²³α")) - String(take!(buf)) - end, "\"ᵞ₁₂₃¹²³α\" can be typed by \\^gamma\\_123\\^123\\alpha\n") +@testset "symbol completion" begin + @test startswith(get_help_io("α"), "\"α\" can be typed by \\alpha\n") + @test startswith(get_help_io("🐨"), "\"🐨\" can be typed by \\:koala:\n") + @test startswith(get_help_io("ᵞ₁₂₃¹²³α"), "\"ᵞ₁₂₃¹²³α\" can be typed by \\^gamma\\_123\\^123\\alpha\n") # Check that all symbols with several completions have a canonical mapping (#39148) symbols = values(REPLCompletions.latex_symbols) @@ -27,10 +23,7 @@ import Markdown end @testset "quoting in doc search" begin - str = let buf = IOBuffer() - Core.eval(Main, REPL.helpmode(buf, "mutable s")) - String(take!(buf)) - end + str = get_help_io("mutable s") @test occursin("'mutable struct'", str) @test occursin("Couldn't find 'mutable s'", str) end @@ -75,6 +68,27 @@ end @test REPL.summarize(b, Tuple{}) isa Markdown.MD end +@testset "Struct field help (#51178)" begin + struct StructWithNoFields end + struct StructWithOneField + field1 + end + struct StructWithTwoFields + field1 + field2 + end + struct StructWithThreeFields + field1 + field2 + field3 + end + + @test endswith(get_help_standard("StructWithNoFields.not_a_field"), "StructWithNoFields` has no fields.\n") + @test endswith(get_help_standard("StructWithOneField.not_a_field"), "StructWithOneField` has field `field1`.\n") + @test endswith(get_help_standard("StructWithTwoFields.not_a_field"), "StructWithTwoFields` has fields `field1`, and `field2`.\n") + @test endswith(get_help_standard("StructWithThreeFields.not_a_field"), "StructWithThreeFields` has fields `field1`, `field2`, and `field3`.\n") +end + module InternalWarningsTests module A