github.com/wtfutil/wtf@v0.43.0/cfg/parsers_test.go (about)

     1  package cfg
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/olebedev/config"
     8  )
     9  
    10  func Test_ParseAsMapOrList(t *testing.T) {
    11  	tests := []struct {
    12  		name          string
    13  		configKey     string
    14  		yaml          string
    15  		expectedCount int
    16  	}{
    17  		{
    18  			name:          "as empty set",
    19  			configKey:     "data",
    20  			yaml:          "",
    21  			expectedCount: 0,
    22  		},
    23  		{
    24  			name:          "as map",
    25  			configKey:     "data",
    26  			yaml:          "data:\n  a: cat\n  b: dog",
    27  			expectedCount: 2,
    28  		},
    29  		{
    30  			name:          "as list",
    31  			configKey:     "data",
    32  			yaml:          "data:\n  - cat\n  - dog\n  - rat\n",
    33  			expectedCount: 3,
    34  		},
    35  	}
    36  
    37  	for _, tt := range tests {
    38  		t.Run(tt.name, func(t *testing.T) {
    39  			ymlConfig, err := config.ParseYaml(tt.yaml)
    40  			if err != nil {
    41  				t.Errorf("\nexpected: no error\n     got: %v", err)
    42  			}
    43  
    44  			actual := ParseAsMapOrList(ymlConfig, tt.configKey)
    45  
    46  			if tt.expectedCount != len(actual) {
    47  				t.Errorf("\nexpected: %d\n     got: %d", tt.expectedCount, len(actual))
    48  			}
    49  		})
    50  	}
    51  }
    52  
    53  func Test_ParseTimeString(t *testing.T) {
    54  	tests := []struct {
    55  		name          string
    56  		configKey     string
    57  		yaml          string
    58  		expectedCount time.Duration
    59  	}{
    60  		{
    61  			name:          "normal integer",
    62  			configKey:     "refreshInterval",
    63  			yaml:          "refreshInterval: 3",
    64  			expectedCount: 3 * time.Second,
    65  		},
    66  		{
    67  			name:          "microseconds",
    68  			configKey:     "refreshInterval",
    69  			yaml:          "refreshInterval: 5µs",
    70  			expectedCount: 5 * time.Microsecond,
    71  		},
    72  		{
    73  			name:          "microseconds different notation",
    74  			configKey:     "refreshInterval",
    75  			yaml:          "refreshInterval: 5us",
    76  			expectedCount: 5 * time.Microsecond,
    77  		},
    78  		{
    79  			name:          "mixed duration",
    80  			configKey:     "refreshInterval",
    81  			yaml:          "refreshInterval: 2h45m",
    82  			expectedCount: 2*time.Hour + 45*time.Minute,
    83  		},
    84  		{
    85  			name:          "default",
    86  			configKey:     "refreshInterval",
    87  			yaml:          "",
    88  			expectedCount: 60 * time.Second,
    89  		},
    90  		{
    91  			name:          "bad input",
    92  			configKey:     "refreshInterval",
    93  			yaml:          "refreshInterval: abc",
    94  			expectedCount: 1 * time.Second,
    95  		},
    96  	}
    97  
    98  	for _, tt := range tests {
    99  		t.Run(tt.name, func(t *testing.T) {
   100  			ymlConfig, err := config.ParseYaml(tt.yaml)
   101  			if err != nil {
   102  				t.Errorf("\nexpected: no error\n     got: %v", err)
   103  			}
   104  
   105  			actual := ParseTimeString(ymlConfig, tt.configKey, "60s")
   106  
   107  			if tt.expectedCount != actual {
   108  				t.Errorf("\nexpected: %d\n     got: %v", tt.expectedCount, actual)
   109  			}
   110  		})
   111  	}
   112  }