github.com/influx6/npkg@v0.8.8/nlexing/compact_directives_test.go (about)

     1  package nlexing
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestParseVariantDirectives(t *testing.T) {
    10  	var specs = []struct {
    11  		Directive  string
    12  		Expected   []string
    13  		ShouldFail bool
    14  	}{
    15  		{
    16  			Directive: "xs:(hover:(bg-color-500!))recon",
    17  			Expected: []string{
    18  				"xs:hover:bg-color-500!",
    19  				"xs:recon",
    20  			},
    21  		},
    22  		{
    23  			Directive: "xs:(hover:(bg-color-500!))",
    24  			Expected: []string{
    25  				"xs:hover:bg-color-500!",
    26  			},
    27  		},
    28  		{
    29  			Directive:  "xs:(text-color-400), fg-color-600)",
    30  			ShouldFail: true,
    31  			Expected:   []string{},
    32  		},
    33  		{
    34  			Directive: "text~(xs:(hover:(bg-color-500, flex:color-400), fg-color-600))",
    35  			Expected: []string{
    36  				"xs:hover:text-bg-color-500",
    37  				"xs:hover:flex:text-color-400",
    38  				"xs:text-fg-color-600",
    39  			},
    40  		},
    41  		{
    42  			Directive: "xs:(hover:(bg-color-500, flex:text-color-400), fg-color-600)",
    43  			Expected: []string{
    44  				"xs:hover:bg-color-500",
    45  				"xs:hover:flex:text-color-400",
    46  				"xs:fg-color-600",
    47  			},
    48  		},
    49  		{
    50  			Directive: "xs:(hover:(bg-color-500, text-color-400), fg-color-600)",
    51  			Expected: []string{
    52  				"xs:hover:bg-color-500",
    53  				"xs:hover:text-color-400",
    54  				"xs:fg-color-600",
    55  			},
    56  		},
    57  		{
    58  			Directive: "xs:(hover:(bg-color-500), fg-color-600)",
    59  			Expected: []string{
    60  				"xs:hover:bg-color-500",
    61  				"xs:fg-color-600",
    62  			},
    63  		},
    64  		{
    65  			Directive: "xs:(hover:bg-color-500, fg-color-600)",
    66  			Expected: []string{
    67  				"xs:hover:bg-color-500",
    68  				"xs:fg-color-600",
    69  			},
    70  		},
    71  		{
    72  			Directive: "xs~(bg-color-500!, fg-color-600)",
    73  			Expected: []string{
    74  				"xs-bg-color-500!",
    75  				"xs-fg-color-600",
    76  			},
    77  		},
    78  		{
    79  			Directive: "xs:(rg~(bg-color-500!), fg-color-600)",
    80  			Expected: []string{
    81  				"xs:rg-bg-color-500!",
    82  				"xs:fg-color-600",
    83  			},
    84  		},
    85  		{
    86  			Directive: "xs:(bg-color-500!, fg-color-600)",
    87  			Expected: []string{
    88  				"xs:bg-color-500!",
    89  				"xs:fg-color-600",
    90  			},
    91  		},
    92  		{
    93  			Directive: "xs:(bg-color-500, fg-color-600)",
    94  			Expected: []string{
    95  				"xs:bg-color-500",
    96  				"xs:fg-color-600",
    97  			},
    98  		},
    99  		{
   100  			Directive: "xs:(bg-color-500)",
   101  			Expected: []string{
   102  				"xs:bg-color-500",
   103  			},
   104  		},
   105  		{
   106  			Directive: "xs:bg-color-500",
   107  			Expected: []string{
   108  				"xs:bg-color-500",
   109  			},
   110  		},
   111  		{
   112  			Directive: "bg-color-500",
   113  			Expected: []string{
   114  				"bg-color-500",
   115  			},
   116  		},
   117  	}
   118  
   119  	for index, spec := range specs {
   120  		var parsedList, parseErr = ParseVariantDirectives(spec.Directive)
   121  		if spec.ShouldFail {
   122  			require.Error(t, parseErr, "Failed at index %d", index)
   123  			continue
   124  		}
   125  
   126  		require.NoError(t, parseErr, "Failed at index %d", index)
   127  		require.Equal(t, spec.Expected, parsedList, "Failed at index %d", index)
   128  	}
   129  }