github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/testutil/paddedchecker_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2015-2018 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 testutil_test
    21  
    22  import (
    23  	"errors"
    24  
    25  	"gopkg.in/check.v1"
    26  
    27  	. "github.com/snapcore/snapd/testutil"
    28  )
    29  
    30  type paddedCheckerSuite struct{}
    31  
    32  var _ = check.Suite(&paddedCheckerSuite{})
    33  
    34  func (*paddedCheckerSuite) TestPaddedChecker(c *check.C) {
    35  	type row struct {
    36  		lhs     string
    37  		checker check.Checker
    38  		rhs     string
    39  	}
    40  
    41  	table := []row{
    42  		{" a  b\tc", EqualsPadded, "a b c"},
    43  		{" a  b\nc", check.Not(EqualsPadded), "a b c"},
    44  
    45  		{" a  b\tc", EqualsWrapped, "a b c"},
    46  		{" a  b\nc", EqualsWrapped, "a b c"},
    47  
    48  		{" a  b\tc    d\t\te", ContainsPadded, "b c d"},
    49  		{" a  b\nc    d\t\te", check.Not(ContainsPadded), "b c d"},
    50  
    51  		{" a  b\tc    d\t\te", ContainsWrapped, "b c d"},
    52  		{" a  b\nc    d\t\te", ContainsWrapped, "b c d"},
    53  
    54  		{"\tfoo baah ", MatchesPadded, `fo+ b\S+`},
    55  		{"\tfoo\nbaah ", check.Not(MatchesPadded), `fo+ b\S+`},
    56  
    57  		{"\tfoo baah ", MatchesWrapped, `fo+ b\S+`},
    58  		{"\tfoo\nbaah ", MatchesWrapped, `fo+ b\S+`},
    59  	}
    60  
    61  	for i, test := range table {
    62  		for _, lhs := range []interface{}{test.lhs, []byte(test.lhs), errors.New(test.lhs)} {
    63  			for _, rhs := range []interface{}{test.rhs, []byte(test.rhs)} {
    64  				comm := check.Commentf("%d:%s:%T/%T", i, test.checker.Info().Name, lhs, rhs)
    65  				c.Check(lhs, test.checker, rhs, comm)
    66  			}
    67  		}
    68  	}
    69  
    70  	for _, checker := range []check.Checker{EqualsPadded, EqualsWrapped, ContainsPadded, ContainsWrapped, MatchesPadded, MatchesWrapped} {
    71  		testCheck(c, checker, false, "right-hand value must be a string or []byte", "a b c", 42)
    72  		testCheck(c, checker, false, "left-hand value must be a string or []byte or error", 42, "a b c")
    73  	}
    74  	for _, checker := range []check.Checker{MatchesPadded, MatchesWrapped} {
    75  		testCheck(c, checker, false, "right-hand value must be a valid regexp: error parsing regexp: missing argument to repetition operator: `+`", "a b c", "+a b c")
    76  	}
    77  }