github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/testutil/intcheckers.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
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"gopkg.in/check.v1"
    26  )
    27  
    28  type intChecker struct {
    29  	*check.CheckerInfo
    30  	rel string
    31  }
    32  
    33  func (checker *intChecker) Check(params []interface{}, names []string) (result bool, error string) {
    34  	a, ok := params[0].(int)
    35  	if !ok {
    36  		return false, "left-hand-side argument must be an int"
    37  	}
    38  	b, ok := params[1].(int)
    39  	if !ok {
    40  		return false, "right-hand-side argument must be an int"
    41  	}
    42  	switch checker.rel {
    43  	case "<":
    44  		result = a < b
    45  	case "<=":
    46  		result = a <= b
    47  	case "==":
    48  		result = a == b
    49  	case "!=":
    50  		result = a != b
    51  	case ">":
    52  		result = a > b
    53  	case ">=":
    54  		result = a >= b
    55  	default:
    56  		return false, fmt.Sprintf("unexpected relation %q", checker.rel)
    57  	}
    58  	if !result {
    59  		error = fmt.Sprintf("relation %d %s %d is not true", a, checker.rel, b)
    60  	}
    61  	return result, error
    62  }
    63  
    64  // IntLessThan checker verifies that one integer is less than other integer.
    65  //
    66  // For example:
    67  //     c.Assert(1, IntLessThan, 2)
    68  var IntLessThan = &intChecker{CheckerInfo: &check.CheckerInfo{Name: "IntLessThan", Params: []string{"a", "b"}}, rel: "<"}
    69  
    70  // IntLessEqual checker verifies that one integer is less than or equal to other integer.
    71  //
    72  // For example:
    73  //     c.Assert(1, IntLessEqual, 1)
    74  var IntLessEqual = &intChecker{CheckerInfo: &check.CheckerInfo{Name: "IntLessEqual", Params: []string{"a", "b"}}, rel: "<="}
    75  
    76  // IntEqual checker verifies that one integer is equal to other integer.
    77  //
    78  // For example:
    79  //     c.Assert(1, IntEqual, 1)
    80  var IntEqual = &intChecker{CheckerInfo: &check.CheckerInfo{Name: "IntEqual", Params: []string{"a", "b"}}, rel: "=="}
    81  
    82  // IntNotEqual checker verifies that one integer is not equal to other integer.
    83  //
    84  // For example:
    85  //     c.Assert(1, IntNotEqual, 2)
    86  var IntNotEqual = &intChecker{CheckerInfo: &check.CheckerInfo{Name: "IntNotEqual", Params: []string{"a", "b"}}, rel: "!="}
    87  
    88  // IntGreaterThan checker verifies that one integer is greater than other integer.
    89  //
    90  // For example:
    91  //     c.Assert(2, IntGreaterThan, 1)
    92  var IntGreaterThan = &intChecker{CheckerInfo: &check.CheckerInfo{Name: "IntGreaterThan", Params: []string{"a", "b"}}, rel: ">"}
    93  
    94  // IntGreaterEqual checker verifies that one integer is greater than or equal to other integer.
    95  //
    96  // For example:
    97  //     c.Assert(1, IntGreaterEqual, 2)
    98  var IntGreaterEqual = &intChecker{CheckerInfo: &check.CheckerInfo{Name: "IntGreaterEqual", Params: []string{"a", "b"}}, rel: ">="}