github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/stringlib/stringlib_test.go (about)

     1  package stringlib
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kylelemons/godebug/diff"
     7  )
     8  
     9  type AssertSimilarProvider struct {
    10  	In   string
    11  	Out  string
    12  	Pass bool
    13  }
    14  
    15  type TestStringProvider struct {
    16  	In   string
    17  	Want string
    18  }
    19  
    20  var TestNormalizeCases = []TestStringProvider{
    21  	{
    22  		In:   "\n\n Hi 1 2 3\n\n\n\n 4\n\r\n\n\n (xyz) \n\n\r\n\n\r\nHi\n\n",
    23  		Want: "Hi 1 2 3\n4\n(xyz)\nHi",
    24  	},
    25  }
    26  
    27  var AssertSimilarCases = []AssertSimilarProvider{
    28  	{
    29  		In:   "Hello World \n",
    30  		Out:  "Hello World xyz",
    31  		Pass: false,
    32  	},
    33  	{
    34  		In:   "Hello World \n",
    35  		Out:  "Hello World",
    36  		Pass: true,
    37  	},
    38  	{
    39  		In:   "Hello World \n",
    40  		Out:  "Hello World",
    41  		Pass: true,
    42  	},
    43  }
    44  
    45  var TestSimilarCases = []TestStringProvider{
    46  	{
    47  		In:   "Hello World \n",
    48  		Want: "Hello World",
    49  	},
    50  	{
    51  		In:   "Hello World \n\n",
    52  		Want: "Hello World",
    53  	},
    54  	{
    55  		In:   "Hello World \n",
    56  		Want: "Hello World",
    57  	},
    58  	{
    59  		In:   "  \nHello  World \n",
    60  		Want: " Hello  World",
    61  	},
    62  	{
    63  		In:   "\n\n Hello World \n\n",
    64  		Want: "Hello World",
    65  	},
    66  	{
    67  		In:   "\n\n Hello World        \nHi",
    68  		Want: "Hello World    \nHi",
    69  	},
    70  	{
    71  		In:   "\n\n Hello World\n\n\n\r\r\n\n \n\r\n\n\n (xyz) \n\n\r\n\n\n\r\n\r\nHi\n\n",
    72  		Want: "Hello World\n(xyz)\nHi",
    73  	},
    74  	{
    75  		In:   "\n\n Hello World \nHow are you doing?",
    76  		Want: "Hello World\nHow are you doing?",
    77  	},
    78  	{
    79  		In:   "\n\n Hello World \nHow are you doing?\n\n\n",
    80  		Want: "Hello World\nHow are you doing?",
    81  	},
    82  }
    83  
    84  func TestAssertSimilar(t *testing.T) {
    85  	for _, c := range AssertSimilarCases {
    86  		var mockTest = &testing.T{}
    87  		AssertSimilar(mockTest, c.In, c.Out)
    88  
    89  		if mockTest.Failed() == c.Pass {
    90  			t.Errorf("Mock test did not meet passing status = %v assertion", c.Pass)
    91  		}
    92  	}
    93  }
    94  
    95  func TestNormalize(t *testing.T) {
    96  	for _, c := range TestNormalizeCases {
    97  		var got = Normalize(c.In)
    98  
    99  		if got != c.Want {
   100  			t.Errorf("Wanted string %v, got %v instead", c.Want, got)
   101  		}
   102  	}
   103  }
   104  
   105  func TestSimilar(t *testing.T) {
   106  	for _, c := range TestSimilarCases {
   107  		if !Similar(c.In, c.Want) {
   108  			t.Errorf(
   109  				"Strings doesn't match after normalization:\n%s",
   110  				diff.Diff(Normalize(c.Want), Normalize(c.In)))
   111  		}
   112  	}
   113  }