github.com/hedzr/evendeep@v0.4.8/flags/flags_test.go (about)

     1  package flags
     2  
     3  import (
     4  	"github.com/hedzr/evendeep/flags/cms"
     5  
     6  	"testing"
     7  )
     8  
     9  func subtest1(t *testing.T) {
    10  	flags := newFlags().WithFlags(
    11  		cms.ByName,
    12  		cms.SliceCopyAppend,
    13  		cms.OmitIfEmpty, cms.OmitIfTargetEmpty,
    14  		cms.MapMerge,
    15  		cms.Ignore)
    16  
    17  	t.Logf("flags: %v", flags)
    18  
    19  	for _, coll := range mKnownStrategyGroup {
    20  		for _, f := range coll {
    21  			if ret := flags.testGroupedFlag(f); ret != coll[1] {
    22  				t.Fatalf("bad: ret = %v, expect %v", ret, coll[0])
    23  			}
    24  		}
    25  	}
    26  }
    27  
    28  func subtest2(t *testing.T) {
    29  	flags := newFlags()
    30  
    31  	for _, coll := range mKnownStrategyGroup {
    32  		for _, f := range coll {
    33  			if flags.testGroupedFlag(f) != coll[0] {
    34  				t.Fatal("bad")
    35  			}
    36  		}
    37  	}
    38  }
    39  
    40  func subtest3(t *testing.T) {
    41  	flags := newFlags()
    42  
    43  	for _, coll := range mKnownStrategyGroup {
    44  		for i, f := range coll {
    45  			if flags.IsGroupedFlagOK(f) {
    46  				if i != 0 {
    47  					t.Fatal("bad")
    48  				}
    49  			} else {
    50  				if i == 0 {
    51  					t.Fatal("bad")
    52  				}
    53  			}
    54  		}
    55  	}
    56  }
    57  
    58  func TestFlags_testGroupedFlag(t *testing.T) {
    59  	lazyInitFieldTagsFlags()
    60  
    61  	t.Run("dirty flags - testGroupedFlag returns the dirty flag when testing any flags of its group", subtest1)
    62  	t.Run("cleaning flags - testGroupedFlag returns the leader in a group", subtest2)
    63  	t.Run("cleaning flags - isGroupedFlagOK returns ok if testing a leader", subtest3)
    64  
    65  	t.Run("parse nonexisted flag", func(t *testing.T) {
    66  		cms.Default.Parse("??")
    67  	})
    68  	t.Run("stringify nonexisted flag", func(t *testing.T) {
    69  		println(cms.CopyMergeStrategy(99999).String())
    70  	})
    71  }
    72  
    73  func TestFlags1(t *testing.T) {
    74  	lazyInitFieldTagsFlags()
    75  
    76  	t.Run("normal flags", func(t *testing.T) {
    77  		flags := newFlags(cms.SliceMerge, cms.MapMerge)
    78  		flags.WithFlags(cms.SliceCopy)
    79  
    80  		if flags.testGroupedFlag(cms.SliceCopy) != cms.SliceCopy {
    81  			t.Fatalf("expect SliceCopy test ok")
    82  		}
    83  		if flags.testGroupedFlag(cms.SliceCopyAppend) != cms.SliceCopy {
    84  			t.Fatalf("expect SliceCopy test ok 1")
    85  		}
    86  		if flags.testGroupedFlag(cms.SliceMerge) != cms.SliceCopy {
    87  			t.Fatalf("expect SliceCopy test ok 2")
    88  		}
    89  		if flags.testGroupedFlag(cms.MapCopy) != cms.MapMerge {
    90  			t.Fatalf("expect MapMerge test ok")
    91  		}
    92  		if flags.testGroupedFlag(cms.MapMerge) != cms.MapMerge {
    93  			t.Fatalf("expect MapMerge test ok 1")
    94  		}
    95  
    96  		if !flags.IsFlagOK(cms.SliceCopy) {
    97  			t.Fatalf("expect isFlagOK(SliceCopy) test ok")
    98  		}
    99  		if flags.IsFlagOK(cms.SliceMerge) {
   100  			t.Fatalf("expect isFlagOK(SliceMerge) test failure")
   101  		}
   102  	})
   103  }
   104  
   105  func TestFlags2(t *testing.T) {
   106  	lazyInitFieldTagsFlags()
   107  
   108  	t.Run("normal flags", func(t *testing.T) {
   109  		flags := newFlags(cms.SliceMerge, cms.MapMerge)
   110  		flags.WithFlags(cms.SliceCopy)
   111  
   112  		if !flags.IsAllFlagsOK(cms.SliceCopy, cms.MapMerge) {
   113  			t.Fatalf("expect isAllFlagsOK(SliceCopy, MapMerge) test ok")
   114  		}
   115  		if flags.IsAllFlagsOK(cms.SliceCopyAppend, cms.MapMerge) {
   116  			t.Fatalf("expect isAllFlagsOK(SliceCopyAppend, MapMerge) test failure")
   117  		}
   118  
   119  		if !flags.IsAnyFlagsOK(cms.SliceCopyAppend, cms.MapMerge) {
   120  			t.Fatalf("expect isAnyFlagsOK(SliceCopyAppend, MapMerge) test ok")
   121  		}
   122  
   123  		if !flags.IsGroupedFlagOK(cms.Default) {
   124  			t.Fatalf("expect isGroupedFlagOK(Default) test ok")
   125  		}
   126  		if flags.IsGroupedFlagOK(cms.ByName) {
   127  			t.Fatalf("expect isGroupedFlagOK(ByName) test failure")
   128  		}
   129  
   130  		if !flags.IsGroupedFlagOK(cms.NoOmit) {
   131  			t.Fatalf("expect isGroupedFlagOK(NoOmit) test ok")
   132  		}
   133  		if flags.IsGroupedFlagOK(cms.OmitIfZero) {
   134  			t.Fatalf("expect isGroupedFlagOK(OmitIfZero) test failure")
   135  		}
   136  
   137  		if !flags.IsGroupedFlagOK(cms.NoOmitTarget) {
   138  			t.Fatalf("expect isGroupedFlagOK(NoOmitTarget) test ok")
   139  		}
   140  	})
   141  }