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

     1  package usage
     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/check.v1"
    16  )
    17  
    18  // ////////////////////////////////////////////////////////////////////////////////// //
    19  
    20  func Test(t *testing.T) { TestingT(t) }
    21  
    22  type UsageSuite struct{}
    23  
    24  // ////////////////////////////////////////////////////////////////////////////////// //
    25  
    26  var _ = Suite(&UsageSuite{})
    27  
    28  // ////////////////////////////////////////////////////////////////////////////////// //
    29  
    30  func (s *UsageSuite) TestAbout(c *C) {
    31  	about := &About{
    32  		App:     "Application",
    33  		Version: "1.0.0",
    34  		Release: ".A45",
    35  		Build:   "37163",
    36  		Desc:    "Test application",
    37  		Year:    2010,
    38  		Owner:   "Some company",
    39  		License: "MIT",
    40  	}
    41  
    42  	about.Render()
    43  
    44  	testChecker := func(app, version, data string) (string, time.Time, bool) {
    45  		return "1.0.1", time.Now(), true
    46  	}
    47  
    48  	about = &About{
    49  		App:           "Application",
    50  		Version:       "1.0.0",
    51  		Release:       ".A45",
    52  		Desc:          "Test application",
    53  		Owner:         "Some company",
    54  		License:       "MIT",
    55  		UpdateChecker: UpdateChecker{"1", testChecker},
    56  	}
    57  
    58  	about.Render()
    59  }
    60  
    61  func (s *UsageSuite) TestUsage(c *C) {
    62  	info := NewInfo("", "file")
    63  
    64  	info.AddSpoiler("This is usage of spoiler with {g}c{c}o{r}l{m}o{b}r{g}s{!} support")
    65  
    66  	info.AddCommand() // will be ignored
    67  	info.AddCommand("print", "Print command")
    68  
    69  	info.AddGroup("Command group")
    70  
    71  	info.AddCommand("read")
    72  	info.AddCommand("read", "Read command")
    73  	info.AddCommand("read1", "Read command with arguments", "arg1", "arg2")
    74  	info.AddCommand("read2", "Read command with optional argument", "?arg")
    75  
    76  	info.AddOption("t:test")
    77  	info.AddOption("t:test", "Test option ")
    78  	info.AddOption("test1", "Test option with argument", "arg")
    79  	info.AddOption("test2", "Test option with optional argument", "?arg")
    80  
    81  	info.BoundOptions("read", "t:test", "test1")
    82  
    83  	info.AddExample() // will be ignored
    84  	info.AddExample("abc")
    85  	info.AddExample("abc", "Example with description")
    86  	info.AddRawExample() // will be ignored
    87  	info.AddRawExample("echo 123 | myapp")
    88  	info.AddRawExample("echo 123 | myapp", "Example with description")
    89  
    90  	info.Render()
    91  
    92  	info.Breadcrumbs = false
    93  	info.CommandsColorTag = "{m}"
    94  	info.OptionsColorTag = "{b}"
    95  
    96  	info.Render()
    97  
    98  	c.Assert(info.GetCommand("read"), NotNil)
    99  	c.Assert(info.GetCommand("read999"), IsNil)
   100  
   101  	c.Assert(info.GetOption("t:test"), NotNil)
   102  	c.Assert(info.GetOption("test"), NotNil)
   103  	c.Assert(info.GetOption("test999"), IsNil)
   104  
   105  	c.Assert(fmt.Sprintf("%s", info.GetCommand("read")), Equals, "read")
   106  	c.Assert(fmt.Sprintf("%s", info.GetCommand("unknown")), Equals, "")
   107  	c.Assert(fmt.Sprintf("%s", info.GetOption("t:test")), Equals, "--test")
   108  	c.Assert(fmt.Sprintf("%s", info.GetOption("u:unknown")), Equals, "")
   109  }
   110  
   111  func (s *UsageSuite) TestVersionInfo(c *C) {
   112  	c.Assert(isNewerVersion("ABC", "1.0.0"), Equals, false)
   113  	c.Assert(isNewerVersion("1.0.0", "ABC"), Equals, false)
   114  
   115  	d1 := time.Unix(time.Now().Unix()-3600, 0)
   116  	d2 := time.Unix(time.Now().Unix()-90000, 0)
   117  	d3 := time.Unix(time.Now().Unix()-1296000, 0)
   118  
   119  	printNewVersionInfo("ABC", "1.0.0", d1)
   120  	printNewVersionInfo("1.0.0", "ABC", d1)
   121  
   122  	printNewVersionInfo("1.0.0", "2.0.0", d1)
   123  	printNewVersionInfo("1.0.0", "1.1.0", d2)
   124  	printNewVersionInfo("1.0.0", "1.0.1", d3)
   125  }