go.temporal.io/server@v1.23.0/common/predicates/and_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package predicates
    26  
    27  import (
    28  	"testing"
    29  
    30  	"github.com/stretchr/testify/require"
    31  	"github.com/stretchr/testify/suite"
    32  )
    33  
    34  type (
    35  	andSuite struct {
    36  		suite.Suite
    37  		*require.Assertions
    38  	}
    39  )
    40  
    41  func TestAndSuite(t *testing.T) {
    42  	s := new(andSuite)
    43  	suite.Run(t, s)
    44  }
    45  
    46  func (s *andSuite) SetupTest() {
    47  	s.Assertions = require.New(s.T())
    48  }
    49  
    50  func (s *andSuite) TestAnd_Normal() {
    51  	p1 := newTestPredicate(1, 2, 6)
    52  	p2 := And[int](
    53  		newTestPredicate(3, 4, 6),
    54  		newTestPredicate(4, 5, 6),
    55  	)
    56  	p := And[int](p1, p2)
    57  
    58  	for i := 1; i != 6; i++ {
    59  		s.False(p.Test(i))
    60  	}
    61  	s.True(p.Test(6))
    62  }
    63  
    64  func (s *andSuite) TestAnd_All() {
    65  	p := And[int](
    66  		newTestPredicate(1, 2, 3),
    67  		Universal[int](),
    68  	)
    69  
    70  	for i := 1; i != 4; i++ {
    71  		s.True(p.Test(i))
    72  	}
    73  	for i := 4; i != 7; i++ {
    74  		s.False(p.Test(i))
    75  	}
    76  
    77  	p = And(
    78  		Universal[int](),
    79  		Universal[int](),
    80  	)
    81  	for i := 1; i != 7; i++ {
    82  		s.True(p.Test(i))
    83  	}
    84  }
    85  
    86  func (s *andSuite) TestAnd_None() {
    87  	p := And[int](
    88  		newTestPredicate(1, 2, 3),
    89  		Empty[int](),
    90  	)
    91  
    92  	for i := 1; i != 7; i++ {
    93  		s.False(p.Test(i))
    94  	}
    95  }
    96  
    97  func (s *andSuite) TestAnd_Duplication() {
    98  	p1 := newTestPredicate(1, 2, 3)
    99  	p2 := newTestPredicate(2, 3, 4)
   100  	p3 := newTestPredicate(3, 4, 5)
   101  
   102  	_, ok := And[int](p1, p1).(*testPredicate)
   103  	s.True(ok)
   104  
   105  	p := And[int](p1, p2)
   106  	pAnd, ok := And[int](p, p1).(*AndImpl[int])
   107  	s.True(ok)
   108  	s.Len(pAnd.Predicates, 2)
   109  
   110  	pAnd, ok = And(p, p).(*AndImpl[int])
   111  	s.True(ok)
   112  	s.Len(pAnd.Predicates, 2)
   113  
   114  	pAnd, ok = And(p, And[int](p1, p2)).(*AndImpl[int])
   115  	s.True(ok)
   116  	s.Len(pAnd.Predicates, 2)
   117  
   118  	pAnd, ok = And(p, And[int](p1, p2, p3)).(*AndImpl[int])
   119  	s.True(ok)
   120  	s.Len(pAnd.Predicates, 3)
   121  }
   122  
   123  func (s *andSuite) TestAnd_Equals() {
   124  	p1 := newTestPredicate(1, 2, 3)
   125  	p2 := newTestPredicate(2, 3, 4)
   126  	p := And[int](p1, p2)
   127  
   128  	s.True(p.Equals(p))
   129  	s.True(p.Equals(And[int](p1, p2)))
   130  	s.True(p.Equals(And[int](p2, p1)))
   131  	s.True(p.Equals(And[int](p1, p1, p2)))
   132  	s.True(p.Equals(And[int](
   133  		newTestPredicate(4, 3, 2),
   134  		newTestPredicate(3, 2, 1),
   135  	)))
   136  
   137  	s.False(p.Equals(p1))
   138  	s.False(p.Equals(And[int](p2, p2)))
   139  	s.False(p.Equals(And[int](p2, newTestPredicate(5, 6, 7))))
   140  	s.False(p.Equals(Or[int](p1, p2)))
   141  	s.False(p.Equals(Not(p)))
   142  	s.False(p.Equals(Empty[int]()))
   143  	s.False(p.Equals(Universal[int]()))
   144  }