github.com/vanus-labs/vanus/lib@v0.0.0-20231221070800-1334a7b9605e/bytes/escape_test.go (about)

     1  // Copyright 2023 Linkall Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bytes
    16  
    17  import (
    18  	// standard libraries.
    19  	"bytes"
    20  	"testing"
    21  
    22  	// third-party libraries.
    23  	. "github.com/smartystreets/goconvey/convey"
    24  )
    25  
    26  const escapePlan = "" +
    27  	`................................` + // 0x00
    28  	`................oooooooo....s.s.` + // 0x20, 0-7(0x30-37), angled brackets(0x3c,0x3e)
    29  	`............................s...` + // 0x40, backslash(0x5c)
    30  	"..\b...\f.......\n...\r.\tu..x......." + // 0x60
    31  	`................................` + // 0x80
    32  	`................................` + // 0xa0
    33  	`................................` + // 0xc0
    34  	`................................` //  0xe0
    35  
    36  func TestEscape(t *testing.T) {
    37  	Convey("unicode character", t, func() {
    38  		Convey("basic multilingual plane", func() {
    39  			r := bytes.NewReader([]byte(`20AC`))
    40  			ru, err := ExpectUnicodeChar(r)
    41  			So(err, ShouldBeNil)
    42  			So(ru, ShouldEqual, '\u20AC') // '€'
    43  		})
    44  
    45  		Convey("supplementary plane", func() {
    46  			r := bytes.NewReader([]byte(`D801\uDC37`))
    47  			ru, err := ExpectUnicodeChar(r)
    48  			So(err, ShouldBeNil)
    49  			So(ru, ShouldEqual, '\U00010437') // '𐐷'
    50  		})
    51  	})
    52  
    53  	Convey("hexadecimal character", t, func() {
    54  		r := bytes.NewReader([]byte(`12`))
    55  		c, err := ExpectHexChar(r)
    56  		So(err, ShouldBeNil)
    57  		So(c, ShouldEqual, '\x12')
    58  	})
    59  
    60  	Convey("octal character", t, func() {
    61  		r := bytes.NewReader([]byte(`23`))
    62  		c, err := ExpectOctCharExt('1', r)
    63  		So(err, ShouldBeNil)
    64  		So(c, ShouldEqual, '\123')
    65  	})
    66  
    67  	Convey("escaped character", t, func() {
    68  		Convey("invalid", func() {
    69  			r := bytes.NewReader([]byte{0})
    70  			buf := bytes.NewBuffer(nil)
    71  			err := ConsumeEscaped(r, buf, escapePlan)
    72  			So(err, ShouldNotBeNil)
    73  		})
    74  
    75  		Convey("self", func() {
    76  			r := bytes.NewReader([]byte{'\\'})
    77  			buf := bytes.NewBuffer(nil)
    78  			err := ConsumeEscaped(r, buf, escapePlan)
    79  			So(err, ShouldBeNil)
    80  			So(buf.String(), ShouldEqual, "\\")
    81  		})
    82  
    83  		Convey("unicode character", func() {
    84  			Convey("basic multilingual plane", func() {
    85  				r := bytes.NewReader([]byte(`u20AC`))
    86  				buf := bytes.NewBuffer(nil)
    87  				err := ConsumeEscaped(r, buf, escapePlan)
    88  				So(err, ShouldBeNil)
    89  				So(buf.String(), ShouldEqual, "\u20AC") // '€'
    90  			})
    91  
    92  			Convey("supplementary plane", func() {
    93  				r := bytes.NewReader([]byte(`uD801\uDC37`))
    94  				buf := bytes.NewBuffer(nil)
    95  				err := ConsumeEscaped(r, buf, escapePlan)
    96  				So(err, ShouldBeNil)
    97  				So(buf.String(), ShouldEqual, "\U00010437") // '𐐷'
    98  			})
    99  		})
   100  
   101  		Convey("hexadecimal character", func() {
   102  			r := bytes.NewReader([]byte(`x12`))
   103  			buf := bytes.NewBuffer(nil)
   104  			err := ConsumeEscaped(r, buf, escapePlan)
   105  			So(err, ShouldBeNil)
   106  			So(buf.String(), ShouldEqual, "\x12")
   107  		})
   108  
   109  		Convey("octal character", func() {
   110  			r := bytes.NewReader([]byte(`123`))
   111  			buf := bytes.NewBuffer(nil)
   112  			err := ConsumeEscaped(r, buf, escapePlan)
   113  			So(err, ShouldBeNil)
   114  			So(buf.String(), ShouldEqual, "\123")
   115  		})
   116  
   117  		Convey("escaped", func() {
   118  			r := bytes.NewReader([]byte(`n`))
   119  			buf := bytes.NewBuffer(nil)
   120  			err := ConsumeEscaped(r, buf, escapePlan)
   121  			So(err, ShouldBeNil)
   122  			So(buf.String(), ShouldEqual, "\n")
   123  		})
   124  	})
   125  }