github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/common/naturalsort_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common
     5  
     6  import (
     7  	"sort"
     8  
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/testing"
    12  )
    13  
    14  type naturalSortSuite struct {
    15  	testing.BaseSuite
    16  }
    17  
    18  var _ = gc.Suite(&naturalSortSuite{})
    19  
    20  func (s *naturalSortSuite) TestNaturallyEmpty(c *gc.C) {
    21  	s.assertNaturallySort(
    22  		c,
    23  		[]string{},
    24  		[]string{},
    25  	)
    26  }
    27  
    28  func (s *naturalSortSuite) TestNaturallyAlpha(c *gc.C) {
    29  	s.assertNaturallySort(
    30  		c,
    31  		[]string{"bac", "cba", "abc"},
    32  		[]string{"abc", "bac", "cba"},
    33  	)
    34  }
    35  
    36  func (s *naturalSortSuite) TestNaturallyAlphaNumeric(c *gc.C) {
    37  	s.assertNaturallySort(
    38  		c,
    39  		[]string{"a1", "a10", "a100", "a11"},
    40  		[]string{"a1", "a10", "a11", "a100"},
    41  	)
    42  }
    43  
    44  func (s *naturalSortSuite) TestNaturallySpecial(c *gc.C) {
    45  	s.assertNaturallySort(
    46  		c,
    47  		[]string{"a1", "a10", "a100", "a1/1", "1a"},
    48  		[]string{"1a", "a1", "a1/1", "a10", "a100"},
    49  	)
    50  }
    51  
    52  func (s *naturalSortSuite) TestNaturallyTagLike(c *gc.C) {
    53  	s.assertNaturallySort(
    54  		c,
    55  		[]string{"a1/1", "a1/11", "a1/2", "a1/7", "a1/100"},
    56  		[]string{"a1/1", "a1/2", "a1/7", "a1/11", "a1/100"},
    57  	)
    58  }
    59  
    60  func (s *naturalSortSuite) TestNaturallySeveralNumericParts(c *gc.C) {
    61  	s.assertNaturallySort(
    62  		c,
    63  		[]string{"x2-y08", "x2-g8", "x8-y8", "x2-y7"},
    64  		[]string{"x2-g8", "x2-y7", "x2-y08", "x8-y8"},
    65  	)
    66  }
    67  
    68  func (s *naturalSortSuite) TestNaturallyFoo(c *gc.C) {
    69  	s.assertNaturallySort(
    70  		c,
    71  		[]string{"foo2", "foo01"},
    72  		[]string{"foo01", "foo2"},
    73  	)
    74  }
    75  
    76  func (s *naturalSortSuite) TestNaturallyIPs(c *gc.C) {
    77  	s.assertNaturallySort(
    78  		c,
    79  		[]string{"100.001.010.123", "001.001.010.123", "001.002.010.123"},
    80  		[]string{"001.001.010.123", "001.002.010.123", "100.001.010.123"},
    81  	)
    82  }
    83  
    84  func (s *naturalSortSuite) TestNaturallyJuju(c *gc.C) {
    85  	s.assertNaturallySort(
    86  		c,
    87  		[]string{
    88  			"ubuntu/0",
    89  			"ubuntu/1",
    90  			"ubuntu/10",
    91  			"ubuntu/100",
    92  			"ubuntu/101",
    93  			"ubuntu/102",
    94  			"ubuntu/103",
    95  			"ubuntu/104",
    96  			"ubuntu/11"},
    97  		[]string{
    98  			"ubuntu/0",
    99  			"ubuntu/1",
   100  			"ubuntu/10",
   101  			"ubuntu/11",
   102  			"ubuntu/100",
   103  			"ubuntu/101",
   104  			"ubuntu/102",
   105  			"ubuntu/103",
   106  			"ubuntu/104"},
   107  	)
   108  }
   109  
   110  func (s *naturalSortSuite) assertNaturallySort(c *gc.C, sample, expected []string) {
   111  	sort.Sort(naturally(sample))
   112  	c.Assert(sample, gc.DeepEquals, expected)
   113  }