github.com/ava-labs/avalanchego@v1.11.11/version/application_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package version
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestNewDefaultApplication(t *testing.T) {
    14  	require := require.New(t)
    15  
    16  	v := &Application{
    17  		Name:  Client,
    18  		Major: 1,
    19  		Minor: 2,
    20  		Patch: 3,
    21  	}
    22  
    23  	require.Equal("avalanchego/1.2.3", v.String())
    24  	require.NoError(v.Compatible(v))
    25  	require.False(v.Before(v))
    26  }
    27  
    28  func TestComparingVersions(t *testing.T) {
    29  	tests := []struct {
    30  		myVersion   *Application
    31  		peerVersion *Application
    32  		compatible  bool
    33  		before      bool
    34  	}{
    35  		{
    36  			myVersion: &Application{
    37  				Name:  Client,
    38  				Major: 1,
    39  				Minor: 2,
    40  				Patch: 3,
    41  			},
    42  			peerVersion: &Application{
    43  				Name:  Client,
    44  				Major: 1,
    45  				Minor: 2,
    46  				Patch: 3,
    47  			},
    48  			compatible: true,
    49  			before:     false,
    50  		},
    51  		{
    52  			myVersion: &Application{
    53  				Name:  Client,
    54  				Major: 1,
    55  				Minor: 2,
    56  				Patch: 4,
    57  			},
    58  			peerVersion: &Application{
    59  				Name:  Client,
    60  				Major: 1,
    61  				Minor: 2,
    62  				Patch: 3,
    63  			},
    64  			compatible: true,
    65  			before:     false,
    66  		},
    67  		{
    68  			myVersion: &Application{
    69  				Name:  Client,
    70  				Major: 1,
    71  				Minor: 2,
    72  				Patch: 3,
    73  			},
    74  			peerVersion: &Application{
    75  				Name:  Client,
    76  				Major: 1,
    77  				Minor: 2,
    78  				Patch: 4,
    79  			},
    80  			compatible: true,
    81  			before:     true,
    82  		},
    83  		{
    84  			myVersion: &Application{
    85  				Name:  Client,
    86  				Major: 1,
    87  				Minor: 3,
    88  				Patch: 3,
    89  			},
    90  			peerVersion: &Application{
    91  				Name:  Client,
    92  				Major: 1,
    93  				Minor: 2,
    94  				Patch: 3,
    95  			},
    96  			compatible: true,
    97  			before:     false,
    98  		},
    99  		{
   100  			myVersion: &Application{
   101  				Name:  Client,
   102  				Major: 1,
   103  				Minor: 2,
   104  				Patch: 3,
   105  			},
   106  			peerVersion: &Application{
   107  				Name:  Client,
   108  				Major: 1,
   109  				Minor: 3,
   110  				Patch: 3,
   111  			},
   112  			compatible: true,
   113  			before:     true,
   114  		},
   115  		{
   116  			myVersion: &Application{
   117  				Name:  Client,
   118  				Major: 2,
   119  				Minor: 2,
   120  				Patch: 3,
   121  			},
   122  			peerVersion: &Application{
   123  				Name:  Client,
   124  				Major: 1,
   125  				Minor: 2,
   126  				Patch: 3,
   127  			},
   128  			compatible: false,
   129  			before:     false,
   130  		},
   131  		{
   132  			myVersion: &Application{
   133  				Name:  Client,
   134  				Major: 1,
   135  				Minor: 2,
   136  				Patch: 3,
   137  			},
   138  			peerVersion: &Application{
   139  				Name:  Client,
   140  				Major: 2,
   141  				Minor: 2,
   142  				Patch: 3,
   143  			},
   144  			compatible: true,
   145  			before:     true,
   146  		},
   147  	}
   148  	for _, test := range tests {
   149  		t.Run(fmt.Sprintf("%s %s", test.myVersion, test.peerVersion), func(t *testing.T) {
   150  			require := require.New(t)
   151  			err := test.myVersion.Compatible(test.peerVersion)
   152  			if test.compatible {
   153  				require.NoError(err)
   154  			} else {
   155  				require.ErrorIs(err, errDifferentMajor)
   156  			}
   157  			require.Equal(test.before, test.myVersion.Before(test.peerVersion))
   158  		})
   159  	}
   160  }