lab.nexedi.com/kirr/go123@v0.0.0-20240207185015-8299741fa871/xfmt/python_test.go (about)

     1  // Copyright (C) 2017  Nexedi SA and Contributors.
     2  //                     Kirill Smelkov <kirr@nexedi.com>
     3  //
     4  // This program is free software: you can Use, Study, Modify and Redistribute
     5  // it under the terms of the GNU General Public License version 3, or (at your
     6  // option) any later version, as published by the Free Software Foundation.
     7  //
     8  // You can also Link and Combine this program with other software covered by
     9  // the terms of any of the Free Software licenses or any of the Open Source
    10  // Initiative approved licenses and Convey the resulting work. Corresponding
    11  // source of such a combination shall include the source code for all other
    12  // software used.
    13  //
    14  // This program is distributed WITHOUT ANY WARRANTY; without even the implied
    15  // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    16  //
    17  // See COPYING file for full licensing terms.
    18  // See https://www.nexedi.com/licensing for rationale and options.
    19  
    20  package xfmt
    21  
    22  import (
    23  	"testing"
    24  )
    25  
    26  // byterange returns []byte with element [start,stop)
    27  func byterange(start, stop byte) []byte {
    28  	b := make([]byte, 0, stop-start)
    29  	for ; start < stop; start++ {
    30  		b = append(b, start)
    31  	}
    32  	return b
    33  }
    34  
    35  var pyQuoteTestv = []struct {in, quoted string} {
    36  	// empty
    37  	{``, `''`},
    38  
    39  	// special characters
    40  	{string(byterange(0, 32)), `'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f'`},
    41  
    42  	// " vs '
    43  	{`hello world`, `'hello world'`},
    44  	{`hello ' world`, `"hello ' world"`},
    45  	{`hello ' " world`, `'hello \' " world'`},
    46  
    47  	// \
    48  	{`hello \ world`, `'hello \\ world'`},
    49  
    50  	// utf-8
    51  	// XXX python escapes non-ascii, but since FileStorage connot
    52  	// commit such strings we take the freedom and output them as
    53  	// readable.
    54  	//{`привет мир`, `'\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82 \xd0\xbc\xd0\xb8\xd1\x80'`},
    55  	{`привет мир`, `'привет мир'`},
    56  
    57  	// invalid utf-8
    58  	{"\xd0a", `'\xd0a'`},
    59  
    60  	// non-printable utf-8
    61  	{"\u007f\u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087", `'\x7f\xc2\x80\xc2\x81\xc2\x82\xc2\x83\xc2\x84\xc2\x85\xc2\x86\xc2\x87'`},
    62  }
    63  
    64  func TestPyQuote(t *testing.T) {
    65  	buf := []byte{}
    66  	for _, tt := range pyQuoteTestv {
    67  		buf = buf[:0]
    68  		buf = AppendQuotePy(buf, tt.in)
    69  		quoted := string(buf)
    70  		if quoted != tt.quoted {
    71  			t.Errorf("pyQuote(%q) ->\nhave: %s\nwant: %s", tt.in, quoted, tt.quoted)
    72  		}
    73  	}
    74  }
    75  
    76  func BenchmarkPyQuote(b *testing.B) {
    77  	buf := []byte{}
    78  
    79  	for i := 0; i < b.N; i++ {
    80  		for _, tt := range pyQuoteTestv {
    81  			buf = buf[:0]
    82  			buf = AppendQuotePy(buf, tt.in)
    83  		}
    84  	}
    85  }