github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/strings/encode_test.go (about) 1 package strings_test 2 3 import ( 4 "context" 5 "testing" 6 7 . "github.com/smartystreets/goconvey/convey" 8 9 "github.com/MontFerret/ferret/pkg/runtime/values" 10 "github.com/MontFerret/ferret/pkg/stdlib/strings" 11 ) 12 13 func TestEncodedURIComponent(t *testing.T) { 14 Convey("When args are not passed", t, func() { 15 Convey("It should return an error", func() { 16 _, err := strings.EncodeURIComponent(context.Background()) 17 18 So(err, ShouldBeError) 19 20 _, err = strings.EncodeURIComponent( 21 context.Background(), 22 values.NewString("https://github.com/MontFerret/ferret"), 23 values.NewString("https://github.com/MontFerret/ferret"), 24 ) 25 26 So(err, ShouldBeError) 27 }) 28 }) 29 30 Convey("When args are strings", t, func() { 31 Convey("EncodeURIComponent('https://github.com/MontFerret/ferret') should return encoded uri", func() { 32 out, _ := strings.EncodeURIComponent( 33 context.Background(), 34 values.NewString("https://github.com/MontFerret/ferret"), 35 ) 36 37 So(out, ShouldEqual, "https%3A%2F%2Fgithub.com%2FMontFerret%2Fferret") 38 }) 39 }) 40 } 41 42 func TestMd5(t *testing.T) { 43 Convey("When args are not passed", t, func() { 44 Convey("It should return an error", func() { 45 var err error 46 _, err = strings.Md5(context.Background()) 47 48 So(err, ShouldBeError) 49 }) 50 }) 51 52 Convey("Should return hash sum of a string", t, func() { 53 str := values.NewString("foobar") 54 out, _ := strings.Md5( 55 context.Background(), 56 str, 57 ) 58 59 So(out, ShouldNotEqual, str) 60 }) 61 } 62 63 func TestSha1(t *testing.T) { 64 Convey("When args are not passed", t, func() { 65 Convey("It should return an error", func() { 66 var err error 67 _, err = strings.Sha1(context.Background()) 68 69 So(err, ShouldBeError) 70 }) 71 }) 72 73 Convey("Should return hash sum of a string", t, func() { 74 str := values.NewString("foobar") 75 out, _ := strings.Sha1( 76 context.Background(), 77 str, 78 ) 79 80 So(out, ShouldNotEqual, str) 81 }) 82 } 83 84 func TestSha512(t *testing.T) { 85 Convey("When args are not passed", t, func() { 86 Convey("It should return an error", func() { 87 var err error 88 _, err = strings.Sha512(context.Background()) 89 90 So(err, ShouldBeError) 91 }) 92 }) 93 94 Convey("Should return hash sum of a string", t, func() { 95 str := values.NewString("foobar") 96 out, _ := strings.Sha512( 97 context.Background(), 98 str, 99 ) 100 101 So(out, ShouldNotEqual, str) 102 }) 103 } 104 105 func TestToBase64(t *testing.T) { 106 Convey("When args are not passed", t, func() { 107 Convey("It should return an error", func() { 108 var err error 109 _, err = strings.ToBase64(context.Background()) 110 111 So(err, ShouldBeError) 112 }) 113 }) 114 115 Convey("Should encode a given value", t, func() { 116 out, err := strings.ToBase64( 117 context.Background(), 118 values.NewString("foobar"), 119 ) 120 121 So(err, ShouldBeNil) 122 So(out, ShouldNotEqual, "foobar") 123 }) 124 }