doc/commands: introduce CmdEntityLink command
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 1 May 2018 19:29:46 +0000 (15:29 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Wed, 2 May 2018 23:03:34 +0000 (19:03 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/doc/commands/commands_model.nit
src/doc/commands/tests/test_commands_model.nit

index c359546..a119d45 100644 (file)
@@ -93,6 +93,34 @@ class WarningNoMDoc
        redef fun to_s do return "No documentation for `{mentity.full_name}`."
 end
 
+# Get the link to a MEntity API documentation
+class CmdEntityLink
+       super CmdEntity
+
+       # The link text to display
+       var text: nullable String = null is optional, writable
+
+       # The link title to display when the link is hovered
+       var title: nullable String = null is optional, writable
+
+       redef fun init_command do
+               var res = super
+               if not res isa CmdSuccess then return res
+               var mentity = self.mentity.as(not null)
+
+               if text == null then
+                       text = mentity.name
+               end
+               if title == null then
+                       var mdoc = mentity.mdoc_or_fallback
+                       if mdoc != null then
+                               title = mdoc.synopsis
+                       end
+               end
+               return res
+       end
+end
+
 # MEntity ancestors command
 #
 # Retrieve all the ancestors (direct and indirect) of a MEntity.
index a785a09..901dd3e 100644 (file)
@@ -65,6 +65,41 @@ class TestCommandsModel
                assert res isa WarningNoMDoc
        end
 
+       # CmdLink
+
+       fun test_cmd_link is test do
+               var cmd = new CmdEntityLink(test_view, mentity_name = "test_prog::Character")
+               var res = cmd.init_command
+               assert res isa CmdSuccess
+               assert cmd.text == "Character"
+               assert cmd.title == "Characters can be played by both the human or the machine."
+       end
+
+       fun test_cmd_link_with_text is test do
+               var cmd = new CmdEntityLink(test_view, mentity_name = "test_prog::Character", text = "foo")
+               var res = cmd.init_command
+               assert res isa CmdSuccess
+               assert cmd.text == "foo"
+               assert cmd.title == "Characters can be played by both the human or the machine."
+       end
+
+       fun test_cmd_link_with_title is test do
+               var cmd = new CmdEntityLink(test_view, mentity_name = "test_prog::Character", title = "bar")
+               var res = cmd.init_command
+               assert res isa CmdSuccess
+               assert cmd.text == "Character"
+               assert cmd.title == "bar"
+       end
+
+       fun test_cmd_link_with_text_and_title is test do
+               var cmd = new CmdEntityLink(test_view, mentity_name = "test_prog::Character",
+                       text = "foo", title = "bar")
+               var res = cmd.init_command
+               assert res isa CmdSuccess
+               assert cmd.text == "foo"
+               assert cmd.title == "bar"
+       end
+
        # CmdInheritance
 
        fun test_cmd_parents is test do