github.com/prebid/prebid-server/v2@v2.18.0/util/iosutil/iosutil_test.go (about)

     1  package iosutil
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestParseVersion(t *testing.T) {
    10  	tests := []struct {
    11  		description     string
    12  		given           string
    13  		expectedVersion Version
    14  		expectedError   string
    15  	}{
    16  		{
    17  			description:     "Valid - major.minor format",
    18  			given:           "14.2",
    19  			expectedVersion: Version{Major: 14, Minor: 2},
    20  		},
    21  		{
    22  			description:     "Valid - major.minor.patch format",
    23  			given:           "14.2.1",
    24  			expectedVersion: Version{Major: 14, Minor: 2},
    25  		},
    26  		{
    27  			description:   "Invalid Parts - Empty",
    28  			given:         "",
    29  			expectedError: "expected either major.minor or major.minor.patch format",
    30  		},
    31  		{
    32  			description:   "Invalid Parts - Too Few",
    33  			given:         "14",
    34  			expectedError: "expected either major.minor or major.minor.patch format",
    35  		},
    36  		{
    37  			description:   "Invalid Parts - Too Many",
    38  			given:         "14.2.1.3",
    39  			expectedError: "expected either major.minor or major.minor.patch format",
    40  		},
    41  		{
    42  			description:   "Invalid Parts - Too Few",
    43  			given:         "14",
    44  			expectedError: "expected either major.minor or major.minor.patch format",
    45  		},
    46  		{
    47  			description:   "Invalid Major",
    48  			given:         "xxx.2",
    49  			expectedError: "major version is not an integer",
    50  		},
    51  		{
    52  			description:   "Invalid Minor",
    53  			given:         "14.xxx",
    54  			expectedError: "minor version is not an integer",
    55  		},
    56  	}
    57  
    58  	for _, test := range tests {
    59  		version, err := ParseVersion(test.given)
    60  
    61  		if test.expectedError == "" {
    62  			assert.NoError(t, err, test.description+":err")
    63  		} else {
    64  			assert.EqualError(t, err, test.expectedError, test.description+":err")
    65  		}
    66  
    67  		assert.Equal(t, test.expectedVersion, version, test.description+":version")
    68  	}
    69  }
    70  
    71  func TestEqualOrGreater(t *testing.T) {
    72  	givenMajor := 14
    73  	givenMinor := 2
    74  
    75  	tests := []struct {
    76  		description  string
    77  		givenVersion Version
    78  		expected     bool
    79  	}{
    80  		{
    81  			description:  "Less Than By Major + Minor",
    82  			givenVersion: Version{Major: 13, Minor: 1},
    83  			expected:     false,
    84  		},
    85  		{
    86  			description:  "Less Than By Major",
    87  			givenVersion: Version{Major: 13, Minor: 2},
    88  			expected:     false,
    89  		},
    90  		{
    91  			description:  "Less Than By Minor",
    92  			givenVersion: Version{Major: 14, Minor: 1},
    93  			expected:     false,
    94  		},
    95  		{
    96  			description:  "Equal",
    97  			givenVersion: Version{Major: 14, Minor: 2},
    98  			expected:     true,
    99  		},
   100  		{
   101  			description:  "Greater By Major + Minor",
   102  			givenVersion: Version{Major: 15, Minor: 3},
   103  			expected:     true,
   104  		},
   105  		{
   106  			description:  "Greater By Major",
   107  			givenVersion: Version{Major: 15, Minor: 2},
   108  			expected:     true,
   109  		},
   110  		{
   111  			description:  "Greater By Minor",
   112  			givenVersion: Version{Major: 14, Minor: 3},
   113  			expected:     true,
   114  		},
   115  	}
   116  
   117  	for _, test := range tests {
   118  		result := test.givenVersion.EqualOrGreater(givenMajor, givenMinor)
   119  		assert.Equal(t, test.expected, result, test.description)
   120  	}
   121  }
   122  
   123  func TestEqual(t *testing.T) {
   124  	givenMajor := 14
   125  	givenMinor := 2
   126  
   127  	tests := []struct {
   128  		description  string
   129  		givenVersion Version
   130  		expected     bool
   131  	}{
   132  		{
   133  			description:  "Less Than By Major + Minor",
   134  			givenVersion: Version{Major: 13, Minor: 1},
   135  			expected:     false,
   136  		},
   137  		{
   138  			description:  "Less Than By Major",
   139  			givenVersion: Version{Major: 13, Minor: 2},
   140  			expected:     false,
   141  		},
   142  		{
   143  			description:  "Less Than By Minor",
   144  			givenVersion: Version{Major: 14, Minor: 1},
   145  			expected:     false,
   146  		},
   147  		{
   148  			description:  "Equal",
   149  			givenVersion: Version{Major: 14, Minor: 2},
   150  			expected:     true,
   151  		},
   152  		{
   153  			description:  "Greater By Major + Minor",
   154  			givenVersion: Version{Major: 15, Minor: 3},
   155  			expected:     false,
   156  		},
   157  		{
   158  			description:  "Greater By Major",
   159  			givenVersion: Version{Major: 15, Minor: 2},
   160  			expected:     false,
   161  		},
   162  		{
   163  			description:  "Greater By Minor",
   164  			givenVersion: Version{Major: 14, Minor: 3},
   165  			expected:     false,
   166  		},
   167  	}
   168  
   169  	for _, test := range tests {
   170  		result := test.givenVersion.Equal(givenMajor, givenMinor)
   171  		assert.Equal(t, test.expected, result, test.description)
   172  	}
   173  }
   174  
   175  func TestDetectVersionClassification(t *testing.T) {
   176  
   177  	tests := []struct {
   178  		given    string
   179  		expected VersionClassification
   180  	}{
   181  		{
   182  			given:    "13.0",
   183  			expected: VersionUnknown,
   184  		},
   185  		{
   186  			given:    "14.0",
   187  			expected: Version140,
   188  		},
   189  		{
   190  			given:    "14.0.1",
   191  			expected: Version140,
   192  		},
   193  		{
   194  			given:    "14.1",
   195  			expected: Version141,
   196  		},
   197  		{
   198  			given:    "14.1.2",
   199  			expected: Version141,
   200  		},
   201  		{
   202  			given:    "14.2",
   203  			expected: Version142OrGreater,
   204  		},
   205  		{
   206  			given:    "14.2.3",
   207  			expected: Version142OrGreater,
   208  		},
   209  		{
   210  			given:    "14.3",
   211  			expected: Version142OrGreater,
   212  		},
   213  		{
   214  			given:    "14.3.2",
   215  			expected: Version142OrGreater,
   216  		},
   217  		{
   218  			given:    "15.0",
   219  			expected: Version142OrGreater,
   220  		},
   221  		{
   222  			given:    "15.0.1",
   223  			expected: Version142OrGreater,
   224  		},
   225  	}
   226  
   227  	for _, test := range tests {
   228  		result := DetectVersionClassification(test.given)
   229  		assert.Equal(t, test.expected, result, test.given)
   230  	}
   231  }