github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/router_gen/misc_test.go (about)

     1  package main
     2  
     3  import (
     4  	"runtime/debug"
     5  	"testing"
     6  )
     7  
     8  func exp(t *testing.T) func(bool, string) {
     9  	return func(val bool, msg string) {
    10  		if !val {
    11  			debug.PrintStack()
    12  			t.Error(msg)
    13  		}
    14  	}
    15  }
    16  
    17  func expf(t *testing.T) func(bool, string, ...interface{}) {
    18  	return func(val bool, msg string, params ...interface{}) {
    19  		if !val {
    20  			debug.PrintStack()
    21  			t.Errorf(msg, params...)
    22  		}
    23  	}
    24  }
    25  
    26  func TestPerc(t *testing.T) {
    27  	ex, _, prec := exp(t), expf(t), NewPrec()
    28  	ex(!prec.GreaterThan("MemberOnly", "AdminOnly"), "MemberOnly should not be greater then AdminOnly")
    29  	ex(!prec.GreaterThan("AdminOnly", "MemberOnly"), "MemberOnly should not be greater then AdminOnly")
    30  	ex(!prec.GreaterThan("NotInSet", "AdminOnly"), "NotInSet should not be greater then AdminOnly")
    31  	ex(!prec.GreaterThan("AdminOnly", "NotInSet"), "AdminOnly should not be greater then NotInSet")
    32  	ex(!prec.InAnySet("MemberOnly"), "MemberOnly should not be in any set")
    33  	ex(!prec.InSameSet("MemberOnly", "AdminOnly"), "MemberOnly and AdminOnly should not be in the same set")
    34  	ex(!prec.InSameSet("MemberOnly", "NotInSet"), "MemberOnly and NotInSet should not be in the same set")
    35  
    36  	prec.AddSet("MemberOnly", "SuperModOnly", "AdminOnly", "SuperAdminOnly")
    37  	ex(!prec.GreaterThan("MemberOnly", "AdminOnly"), "MemberOnly should not be greater then AdminOnly")
    38  	ex(prec.GreaterThan("AdminOnly", "MemberOnly"), "AdminOnly should be greater then MemberOnly")
    39  	ex(!prec.GreaterThan("NotInSet", "AdminOnly"), "NotInSet should not be greater then AdminOnly")
    40  	ex(!prec.GreaterThan("AdminOnly", "NotInSet"), "AdminOnly should not be greater then NotInSet")
    41  	ex(prec.InAnySet("MemberOnly"), "MemberOnly should be in a set")
    42  	ex(!prec.InAnySet("NotInSet"), "NotInSet should not be in any set")
    43  	ex(prec.InSameSet("MemberOnly", "AdminOnly"), "MemberOnly and AdminOnly should be in the same set")
    44  	ex(!prec.InSameSet("MemberOnly", "NotInSet"), "MemberOnly and NotInSet should not be in the same set")
    45  
    46  	items := prec.LessThanItem("AdminOnly")
    47  	ex(len(items) > 0, "There should be items which are of a lower precedence than AdminOnly")
    48  	imap := make(map[string]bool)
    49  	for _, item := range items {
    50  		imap[item] = true
    51  	}
    52  	mex := func(n string, val bool, msg string) {
    53  		_, ok := imap[n]
    54  		ex(ok == val, msg)
    55  	}
    56  	mex("SuperModOnly", true, "SuperModOnly should be returned in a list of lower precedence items than AdminOnly")
    57  	mex("MemberOnly", true, "MemberOnly should be returned in a list of lower precedence items than AdminOnly")
    58  	mex("SuperAdminOnly", false, "SuperAdminOnly should not be returned in a list of lower precedence items than AdminOnly")
    59  	mex("NotInSet", false, "NotInSet should not be returned in a list of lower precedence items than AdminOnly")
    60  }