github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/compileopts/options_test.go (about)

     1  package compileopts_test
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/tinygo-org/tinygo/compileopts"
     8  )
     9  
    10  func TestVerifyOptions(t *testing.T) {
    11  
    12  	expectedGCError := errors.New(`invalid gc option 'incorrect': valid values are none, leaking, conservative, custom, precise`)
    13  	expectedSchedulerError := errors.New(`invalid scheduler option 'incorrect': valid values are none, tasks, asyncify`)
    14  	expectedPrintSizeError := errors.New(`invalid size option 'incorrect': valid values are none, short, full`)
    15  	expectedPanicStrategyError := errors.New(`invalid panic option 'incorrect': valid values are print, trap`)
    16  
    17  	testCases := []struct {
    18  		name          string
    19  		opts          compileopts.Options
    20  		expectedError error
    21  	}{
    22  		{
    23  			name: "OptionsEmpty",
    24  			opts: compileopts.Options{},
    25  		},
    26  		{
    27  			name: "InvalidGCOption",
    28  			opts: compileopts.Options{
    29  				GC: "incorrect",
    30  			},
    31  			expectedError: expectedGCError,
    32  		},
    33  		{
    34  			name: "GCOptionNone",
    35  			opts: compileopts.Options{
    36  				GC: "none",
    37  			},
    38  		},
    39  		{
    40  			name: "GCOptionLeaking",
    41  			opts: compileopts.Options{
    42  				GC: "leaking",
    43  			},
    44  		},
    45  		{
    46  			name: "GCOptionConservative",
    47  			opts: compileopts.Options{
    48  				GC: "conservative",
    49  			},
    50  		},
    51  		{
    52  			name: "GCOptionCustom",
    53  			opts: compileopts.Options{
    54  				GC: "custom",
    55  			},
    56  		},
    57  		{
    58  			name: "InvalidSchedulerOption",
    59  			opts: compileopts.Options{
    60  				Scheduler: "incorrect",
    61  			},
    62  			expectedError: expectedSchedulerError,
    63  		},
    64  		{
    65  			name: "SchedulerOptionNone",
    66  			opts: compileopts.Options{
    67  				Scheduler: "none",
    68  			},
    69  		},
    70  		{
    71  			name: "SchedulerOptionTasks",
    72  			opts: compileopts.Options{
    73  				Scheduler: "tasks",
    74  			},
    75  		},
    76  		{
    77  			name: "InvalidPrintSizeOption",
    78  			opts: compileopts.Options{
    79  				PrintSizes: "incorrect",
    80  			},
    81  			expectedError: expectedPrintSizeError,
    82  		},
    83  		{
    84  			name: "PrintSizeOptionNone",
    85  			opts: compileopts.Options{
    86  				PrintSizes: "none",
    87  			},
    88  		},
    89  		{
    90  			name: "PrintSizeOptionShort",
    91  			opts: compileopts.Options{
    92  				PrintSizes: "short",
    93  			},
    94  		},
    95  		{
    96  			name: "PrintSizeOptionFull",
    97  			opts: compileopts.Options{
    98  				PrintSizes: "full",
    99  			},
   100  		},
   101  		{
   102  			name: "InvalidPanicOption",
   103  			opts: compileopts.Options{
   104  				PanicStrategy: "incorrect",
   105  			},
   106  			expectedError: expectedPanicStrategyError,
   107  		},
   108  		{
   109  			name: "PanicOptionPrint",
   110  			opts: compileopts.Options{
   111  				PanicStrategy: "print",
   112  			},
   113  		},
   114  		{
   115  			name: "PanicOptionTrap",
   116  			opts: compileopts.Options{
   117  				PanicStrategy: "trap",
   118  			},
   119  		},
   120  	}
   121  
   122  	for _, tc := range testCases {
   123  		t.Run(tc.name, func(t *testing.T) {
   124  			err := tc.opts.Verify()
   125  			if tc.expectedError != err {
   126  				if tc.expectedError.Error() != err.Error() {
   127  					t.Errorf("expected %v, got %v", tc.expectedError, err)
   128  				}
   129  			}
   130  		})
   131  	}
   132  }