pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/usage/man/man_test.go (about)

     1  package man
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     6  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"fmt"
    12  	"testing"
    13  	"time"
    14  
    15  	"pkg.re/essentialkaos/ek.v12/timeutil"
    16  	"pkg.re/essentialkaos/ek.v12/usage"
    17  
    18  	. "pkg.re/essentialkaos/check.v1"
    19  )
    20  
    21  // ////////////////////////////////////////////////////////////////////////////////// //
    22  
    23  type ManSuite struct{}
    24  
    25  // ////////////////////////////////////////////////////////////////////////////////// //
    26  
    27  func Test(t *testing.T) { TestingT(t) }
    28  
    29  // ////////////////////////////////////////////////////////////////////////////////// //
    30  
    31  var _ = Suite(&ManSuite{})
    32  
    33  // ////////////////////////////////////////////////////////////////////////////////// //
    34  
    35  func (s *ManSuite) TestGenerator(c *C) {
    36  	about := &usage.About{
    37  		App:     "TestApp",
    38  		Version: "1.12.34",
    39  		Desc:    "My supper app",
    40  	}
    41  
    42  	info := usage.NewInfo()
    43  
    44  	c.Assert(Generate(info, about), Not(Equals), "")
    45  }
    46  
    47  func (s *ManSuite) TestHeaderGenerator(c *C) {
    48  	about := &usage.About{
    49  		App:     "TestApp",
    50  		Version: "1.12.34",
    51  	}
    52  
    53  	now := timeutil.Format(time.Now(), "%d %b %Y")
    54  	header := fmt.Sprintf(".TH TESTAPP 1 \"%s\" \"TestApp 1\\&.12\\&.34\" \"TestApp Manual\"\n\n", now)
    55  
    56  	c.Assert(genHeader(about), Equals, header)
    57  }
    58  
    59  func (s *ManSuite) TestNameGenerator(c *C) {
    60  	about := &usage.About{
    61  		App:  "TestApp",
    62  		Desc: "My supper app",
    63  	}
    64  
    65  	c.Assert(genName(about), Equals, ".SH NAME\nTestApp \\- My supper app\n")
    66  }
    67  
    68  func (s *ManSuite) TestSynopsisGenerator(c *C) {
    69  	info := usage.NewInfo("testapp", "args")
    70  
    71  	info.AddOption("t:test1", "Test1")
    72  	info.AddOption("T:test2", "Test2", "data")
    73  
    74  	info.AddCommand("test", "Test")
    75  
    76  	synopsis := ".SH SYNOPSIS\n.sp\n.nf\n"
    77  	synopsis += ".B testapp [\\fB\\-\\-test1\\fR] [\\fB\\-\\-test2\\fR=\\fIDATA\\fR] [\\fBCOMMAND\\fR] \\fIargs\\fR\n"
    78  	synopsis += ".fi\n.sp\n"
    79  
    80  	c.Assert(genSynopsis(info), Equals, synopsis)
    81  
    82  	info = usage.NewInfo()
    83  
    84  	info.AddOption("a:opta", "OptA")
    85  	info.AddOption("b:optb", "OptB")
    86  	info.AddOption("c:optc", "OptC")
    87  	info.AddOption("d:optd", "OptD")
    88  	info.AddOption("e:opte", "OptE")
    89  	info.AddOption("f:optf", "OptF")
    90  
    91  	synopsis = ".SH SYNOPSIS\n.sp\n.nf\n"
    92  	synopsis += ".B man.test [\\fB\\-\\-opta\\fR] [\\fB\\-\\-optb\\fR] [\\fB\\-\\-optc\\fR] [\\fB\\-\\-optd\\fR] [\\fB\\-\\-opte\\fR] \n"
    93  	synopsis += "         [\\fB\\-\\-optf\\fR] \n"
    94  	synopsis += ".fi\n.sp\n"
    95  
    96  	c.Assert(genSynopsis(info), Equals, synopsis)
    97  }
    98  
    99  func (s *ManSuite) TestOptionsGenerator(c *C) {
   100  	info := usage.NewInfo()
   101  
   102  	c.Assert(genOptions(info), Equals, "")
   103  
   104  	info.AddOption("t:test1", "Test1")
   105  	info.AddOption("T:test2", "Test2", "data")
   106  
   107  	options := ".SH OPTIONS\n"
   108  	options += ".TP\n.BR \\-t \", \" \\-\\-test1\nTest1\n"
   109  	options += ".TP\n.BR \\-T \", \" \\-\\-test2\\fR=\\fIDATA\\fR\nTest2\n"
   110  
   111  	c.Assert(genOptions(info), Equals, options)
   112  }
   113  
   114  func (s *ManSuite) TestCommandsGenerator(c *C) {
   115  	info := usage.NewInfo()
   116  
   117  	c.Assert(genCommands(info), Equals, "")
   118  
   119  	info.AddCommand("test1", "Test1 command")
   120  	info.AddGroup("Group1")
   121  	info.AddCommand("test2", "Test2 command", "arg1")
   122  	info.AddGroup("Group2")
   123  	info.AddCommand("test3", "Test3 command", "?arg1")
   124  
   125  	commands := ".SH COMMANDS\n"
   126  	commands += ".SS Commands\n.TP\n.B test1\nTest1 command\n"
   127  	commands += ".SS Group1\n"
   128  	commands += ".TP\n.B test2 \\fIarg1\\fP\nTest2 command\n"
   129  	commands += ".SS Group2\n"
   130  	commands += ".TP\n.B test3 \\fRarg1\\fP\nTest3 command\n"
   131  
   132  	c.Assert(genCommands(info), Equals, commands)
   133  }
   134  
   135  func (s *ManSuite) TestDescriptionGenerator(c *C) {
   136  	info := &usage.Info{}
   137  
   138  	c.Assert(genDescription(info), Equals, "")
   139  
   140  	info.AddSpoiler("Some text.")
   141  
   142  	c.Assert(genDescription(info), Equals, ".SH DESCRIPTION\n\nSome text.\n\n")
   143  }
   144  
   145  func (s *ManSuite) TestExamplesGenerator(c *C) {
   146  	info := &usage.Info{Name: "app"}
   147  
   148  	c.Assert(genExamples(info), Equals, "")
   149  
   150  	info.AddExample("test 123", "Test1")
   151  	info.AddExample("test 456")
   152  	info.AddRawExample("app test 789", "Test3")
   153  
   154  	examples := ".SH EXAMPLES\n"
   155  	examples += ".TP\n.B • Test1\napp test 123\n"
   156  	examples += ".TP\n.B • Example 2\napp test 456\n"
   157  	examples += ".TP\n.B • Test3\napp test 789\n"
   158  
   159  	c.Assert(genExamples(info), Equals, examples)
   160  }
   161  
   162  func (s *ManSuite) TestAuthorGenerator(c *C) {
   163  	about := &usage.About{}
   164  
   165  	c.Assert(genAuthor(about), Equals, "")
   166  
   167  	about = &usage.About{Owner: "John Doe"}
   168  
   169  	authorData := fmt.Sprintf(".SH AUTHOR\n\nCopyright (C) %d \\fBJohn Doe\\fP\n\n", time.Now().Year())
   170  
   171  	c.Assert(genAuthor(about), Equals, authorData)
   172  
   173  	about = &usage.About{Owner: "John Doe", Year: 2000}
   174  
   175  	authorData = fmt.Sprintf(
   176  		".SH AUTHOR\n\nCopyright (C) %d-%d \\fBJohn Doe\\fP\n\n",
   177  		about.Year, time.Now().Year(),
   178  	)
   179  
   180  	c.Assert(genAuthor(about), Equals, authorData)
   181  }
   182  
   183  func (s *ManSuite) TestLicenseGenerator(c *C) {
   184  	about := &usage.About{}
   185  
   186  	c.Assert(genLicense(about), Equals, "")
   187  
   188  	about = &usage.About{License: "MIT <https://opensource.org/licenses/MIT>"}
   189  
   190  	c.Assert(genLicense(about), Equals, ".SH LICENSE\n\nMIT <\\fBhttps://opensource.org/licenses/MIT\\fP>.\n\n")
   191  }
   192  
   193  func (s *ManSuite) TestBugTrackerGenerator(c *C) {
   194  	about := &usage.About{}
   195  
   196  	c.Assert(genBugTrackerInfo(about), Equals, "")
   197  
   198  	about = &usage.About{
   199  		BugTracker: "https://bugs.com",
   200  	}
   201  
   202  	info := ".SH BUGS\n.PD 0\n\nPlease send any comments or bug reports to <\\fBhttps://bugs.com\\fP>.\n\n"
   203  
   204  	c.Assert(genBugTrackerInfo(about), Equals, info)
   205  }