github.com/rigado/snapd@v2.42.5-go-mod+incompatible/strutil/strutil_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2015 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package strutil_test
    21  
    22  import (
    23  	"math"
    24  	"math/rand"
    25  	"sort"
    26  	"testing"
    27  
    28  	"gopkg.in/check.v1"
    29  
    30  	"github.com/snapcore/snapd/strutil"
    31  )
    32  
    33  func Test(t *testing.T) { check.TestingT(t) }
    34  
    35  type strutilSuite struct{}
    36  
    37  var _ = check.Suite(&strutilSuite{})
    38  
    39  func (ts *strutilSuite) TestMakeRandomString(c *check.C) {
    40  	// for our tests
    41  	rand.Seed(1)
    42  
    43  	s1 := strutil.MakeRandomString(10)
    44  	c.Assert(s1, check.Equals, "pw7MpXh0JB")
    45  
    46  	s2 := strutil.MakeRandomString(5)
    47  	c.Assert(s2, check.Equals, "4PQyl")
    48  }
    49  
    50  func (*strutilSuite) TestQuoted(c *check.C) {
    51  	for _, t := range []struct {
    52  		in  []string
    53  		out string
    54  	}{
    55  		{nil, ""},
    56  		{[]string{}, ""},
    57  		{[]string{"one"}, `"one"`},
    58  		{[]string{"one", "two"}, `"one", "two"`},
    59  		{[]string{"one", `tw"`}, `"one", "tw\""`},
    60  	} {
    61  		c.Check(strutil.Quoted(t.in), check.Equals, t.out, check.Commentf("expected %#v -> %s", t.in, t.out))
    62  	}
    63  }
    64  
    65  func (ts *strutilSuite) TestSizeToStr(c *check.C) {
    66  	for _, t := range []struct {
    67  		size int64
    68  		str  string
    69  	}{
    70  		{0, "0B"},
    71  		{1, "1B"},
    72  		{400, "400B"},
    73  		{1000, "1kB"},
    74  		{1000 + 1, "1kB"},
    75  		{900 * 1000, "900kB"},
    76  		{1000 * 1000, "1MB"},
    77  		{20 * 1000 * 1000, "20MB"},
    78  		{1000 * 1000 * 1000, "1GB"},
    79  		{31 * 1000 * 1000 * 1000, "31GB"},
    80  		{math.MaxInt64, "9EB"},
    81  	} {
    82  		c.Check(strutil.SizeToStr(t.size), check.Equals, t.str)
    83  	}
    84  }
    85  
    86  func (ts *strutilSuite) TestListContains(c *check.C) {
    87  	for _, xs := range [][]string{
    88  		{},
    89  		nil,
    90  		{"foo"},
    91  		{"foo", "baz", "barbar"},
    92  	} {
    93  		c.Check(strutil.ListContains(xs, "bar"), check.Equals, false)
    94  		sort.Strings(xs)
    95  		c.Check(strutil.SortedListContains(xs, "bar"), check.Equals, false)
    96  	}
    97  
    98  	for _, xs := range [][]string{
    99  		{"bar"},
   100  		{"foo", "bar", "baz"},
   101  		{"bar", "foo", "baz"},
   102  		{"foo", "baz", "bar"},
   103  		{"bar", "bar", "bar", "bar", "bar", "bar"},
   104  	} {
   105  		c.Check(strutil.ListContains(xs, "bar"), check.Equals, true)
   106  		sort.Strings(xs)
   107  		c.Check(strutil.SortedListContains(xs, "bar"), check.Equals, true)
   108  	}
   109  }
   110  
   111  func (ts *strutilSuite) TestTruncateOutput(c *check.C) {
   112  	data := []byte("ab\ncd\nef\ngh\nij")
   113  	out := strutil.TruncateOutput(data, 3, 500)
   114  	c.Assert(out, check.DeepEquals, []byte("ef\ngh\nij"))
   115  
   116  	out = strutil.TruncateOutput(data, 1000, 8)
   117  	c.Assert(out, check.DeepEquals, []byte("ef\ngh\nij"))
   118  
   119  	out = strutil.TruncateOutput(data, 1000, 1000)
   120  	c.Assert(out, check.DeepEquals, []byte("ab\ncd\nef\ngh\nij"))
   121  
   122  	out = strutil.TruncateOutput(data, 99, 5)
   123  	c.Assert(out, check.DeepEquals, []byte("gh\nij"))
   124  
   125  	out = strutil.TruncateOutput(data, 99, 6)
   126  	c.Assert(out, check.DeepEquals, []byte("\ngh\nij"))
   127  
   128  	out = strutil.TruncateOutput(data, 5, 1000)
   129  	c.Assert(out, check.DeepEquals, []byte("ab\ncd\nef\ngh\nij"))
   130  
   131  	out = strutil.TruncateOutput(data, 1000, len(data))
   132  	c.Assert(out, check.DeepEquals, []byte("ab\ncd\nef\ngh\nij"))
   133  
   134  	out = strutil.TruncateOutput(data, 1000, 1000)
   135  	c.Assert(out, check.DeepEquals, []byte("ab\ncd\nef\ngh\nij"))
   136  
   137  	out = strutil.TruncateOutput(data, 0, 0)
   138  	c.Assert(out, check.HasLen, 0)
   139  }
   140  
   141  func (ts *strutilSuite) TestParseByteSizeHappy(c *check.C) {
   142  	for _, t := range []struct {
   143  		str      string
   144  		expected int64
   145  	}{
   146  		{"0B", 0},
   147  		{"1B", 1},
   148  		{"400B", 400},
   149  		{"1kB", 1000},
   150  		// note the upper-case
   151  		{"1KB", 1000},
   152  		{"900kB", 900 * 1000},
   153  		{"1MB", 1000 * 1000},
   154  		{"20MB", 20 * 1000 * 1000},
   155  		{"1GB", 1000 * 1000 * 1000},
   156  		{"31GB", 31 * 1000 * 1000 * 1000},
   157  		{"4TB", 4 * 1000 * 1000 * 1000 * 1000},
   158  		{"6PB", 6 * 1000 * 1000 * 1000 * 1000 * 1000},
   159  		{"8EB", 8 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000},
   160  	} {
   161  		val, err := strutil.ParseByteSize(t.str)
   162  		c.Check(err, check.IsNil)
   163  		c.Check(val, check.Equals, t.expected, check.Commentf("incorrect result for input %q", t.str))
   164  	}
   165  }
   166  
   167  func (ts *strutilSuite) TestParseByteSizeUnhappy(c *check.C) {
   168  	for _, t := range []struct {
   169  		str    string
   170  		errStr string
   171  	}{
   172  		{"B", `cannot parse "B": no numerical prefix`},
   173  		{"1", `cannot parse "1": need a number with a unit as input`},
   174  		{"11", `cannot parse "11": need a number with a unit as input`},
   175  		{"400x", `cannot parse "400x": try 'kB' or 'MB'`},
   176  		{"400xx", `cannot parse "400xx": try 'kB' or 'MB'`},
   177  		{"1k", `cannot parse "1k": try 'kB' or 'MB'`},
   178  		{"200KiB", `cannot parse "200KiB": try 'kB' or 'MB'`},
   179  		{"-200KB", `cannot parse "-200KB": size cannot be negative`},
   180  		{"-200B", `cannot parse "-200B": size cannot be negative`},
   181  		{"-B", `cannot parse "-B": "-" is not a number`},
   182  		{"-", `cannot parse "-": "-" is not a number`},
   183  		{"", `cannot parse "": "" is not a number`},
   184  		// Digits outside of Latin1 range
   185  		// ARABIC-INDIC DIGIT SEVEN
   186  		{"٧kB", `cannot parse "٧kB": no numerical prefix`},
   187  		{"1٧kB", `cannot parse "1٧kB": try 'kB' or 'MB'`},
   188  	} {
   189  		_, err := strutil.ParseByteSize(t.str)
   190  		c.Check(err, check.ErrorMatches, t.errStr, check.Commentf("incorrect error for %q", t.str))
   191  	}
   192  }
   193  
   194  func (strutilSuite) TestCommaSeparatedList(c *check.C) {
   195  	table := []struct {
   196  		in  string
   197  		out []string
   198  	}{
   199  		{"", []string{}},
   200  		{",", []string{}},
   201  		{"foo,bar", []string{"foo", "bar"}},
   202  		{"foo , bar", []string{"foo", "bar"}},
   203  		{"foo ,, bar", []string{"foo", "bar"}},
   204  		{" foo ,, bar,baz", []string{"foo", "bar", "baz"}},
   205  		{" foo bar ,,,baz", []string{"foo bar", "baz"}},
   206  	}
   207  
   208  	for _, test := range table {
   209  		c.Check(strutil.CommaSeparatedList(test.in), check.DeepEquals, test.out, check.Commentf("%q", test.in))
   210  	}
   211  }
   212  
   213  func (strutilSuite) TestEllipt(c *check.C) {
   214  	type T struct {
   215  		in    string
   216  		n     int
   217  		right string
   218  		left  string
   219  	}
   220  	for _, t := range []T{
   221  		{"", 10, "", ""},
   222  		{"", -1, "", ""},
   223  		{"hello", -1, "…", "…"},
   224  		{"hello", 0, "…", "…"},
   225  		{"hello", 1, "…", "…"},
   226  		{"hello", 2, "h…", "…o"},
   227  		{"hello", 3, "he…", "…lo"},
   228  		{"hello", 4, "hel…", "…llo"},
   229  		{"hello", 5, "hello", "hello"},
   230  		{"hello", 10, "hello", "hello"},
   231  		{"héllo", 4, "hé…", "…llo"},
   232  		{"héllo", 3, "he…", "…lo"},
   233  		{"he🐧lo", 4, "he🐧…", "…🐧lo"},
   234  		{"he🐧lo", 3, "he…", "…lo"},
   235  	} {
   236  		c.Check(strutil.ElliptRight(t.in, t.n), check.Equals, t.right, check.Commentf("%q[:%d] -> %q", t.in, t.n, t.right))
   237  		c.Check(strutil.ElliptLeft(t.in, t.n), check.Equals, t.left, check.Commentf("%q[-%d:] -> %q", t.in, t.n, t.left))
   238  	}
   239  }