Patchwork [3,of,5] byteify-strings: add test for byteify-strings.py

login
register
mail settings
Submitter Raphaël Gomès
Date Aug. 2, 2019, 3:01 p.m.
Message ID <91a17ea759c5f0fceaae.1564758084@alphare-carbon.lan>
Download mbox | patch
Permalink /patch/41130/
State Accepted
Headers show

Comments

Raphaël Gomès - Aug. 2, 2019, 3:01 p.m.
# HG changeset patch
# User Raphaël Gomès <rgomes@octobus.net>
# Date 1564757642 -7200
#      Fri Aug 02 16:54:02 2019 +0200
# Node ID 91a17ea759c5f0fceaae18fb558435ab379faac7
# Parent  fea16ec00093e73bf0aa4db3f2a709c385fc6336
# EXP-Topic byteify-strings
byteify-strings: add test for byteify-strings.py

This tests the basic features expected from this script, some cases may not
be covered yet.

A future patch will demonstrate an issue with multi-line `(`, `[` and `{`
alignment and propose a fix.
Yuya Nishihara - Aug. 3, 2019, 12:46 a.m.
On Fri, 02 Aug 2019 17:01:24 +0200, Raphaël Gomès wrote:
> # HG changeset patch
> # User Raphaël Gomès <rgomes@octobus.net>
> # Date 1564757642 -7200
> #      Fri Aug 02 16:54:02 2019 +0200
> # Node ID 91a17ea759c5f0fceaae18fb558435ab379faac7
> # Parent  fea16ec00093e73bf0aa4db3f2a709c385fc6336
> # EXP-Topic byteify-strings
> byteify-strings: add test for byteify-strings.py
> 
> This tests the basic features expected from this script, some cases may not
> be covered yet.
> 
> A future patch will demonstrate an issue with multi-line `(`, `[` and `{`
> alignment and propose a fix.
> 
> diff -r fea16ec00093 -r 91a17ea759c5 tests/test-byteify-strings.t
> --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
> +++ b/tests/test-byteify-strings.t	Fri Aug 02 16:54:02 2019 +0200
> @@ -0,0 +1,195 @@

Needs #require py3

> +Test in-place
> +
> +  $ cat > testfile.py <<EOF
> +  > obj['test'] = b"1234"
> +  > mydict.iteritems()
> +  > EOF
> +  $ python $BINDIR/contrib/byteify-strings.py testfile.py -i

Probably better to use $PYTHON.
And it should be $TESTDIR/../contrib. You can add a shell function.

  $ byteify-strings () {
  >   $PYTHON "$TESTDIR/../contrib/byteify-strings.py" "$@"
  > }

Patch

diff -r fea16ec00093 -r 91a17ea759c5 tests/test-byteify-strings.t
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-byteify-strings.t	Fri Aug 02 16:54:02 2019 +0200
@@ -0,0 +1,195 @@ 
+Test in-place
+
+  $ cat > testfile.py <<EOF
+  > obj['test'] = b"1234"
+  > mydict.iteritems()
+  > EOF
+  $ python $BINDIR/contrib/byteify-strings.py testfile.py -i
+  $ cat testfile.py
+  obj[b'test'] = b"1234"
+  mydict.iteritems()
+
+Test with dictiter
+
+  $ cat > testfile.py <<EOF
+  > obj['test'] = b"1234"
+  > mydict.iteritems()
+  > EOF
+  $ python $BINDIR/contrib/byteify-strings.py testfile.py --dictiter
+  obj[b'test'] = b"1234"
+  mydict.items()
+
+Test kwargs-like objects
+
+  $ cat > testfile.py <<EOF
+  > kwargs['test'] = "123"
+  > kwargs[test['testing']]
+  > kwargs[test[[['testing']]]]
+  > kwargs[kwargs['testing']]
+  > kwargs.get('test')
+  > kwargs.pop('test')
+  > kwargs.get('test', 'testing')
+  > kwargs.pop('test', 'testing')
+  > kwargs.setdefault('test', 'testing')
+  > 
+  > opts['test'] = "123"
+  > opts[test['testing']]
+  > opts[test[[['testing']]]]
+  > opts[opts['testing']]
+  > opts.get('test')
+  > opts.pop('test')
+  > opts.get('test', 'testing')
+  > opts.pop('test', 'testing')
+  > opts.setdefault('test', 'testing')
+  > 
+  > commitopts['test'] = "123"
+  > commitopts[test['testing']]
+  > commitopts[test[[['testing']]]]
+  > commitopts[commitopts['testing']]
+  > commitopts.get('test')
+  > commitopts.pop('test')
+  > commitopts.get('test', 'testing')
+  > commitopts.pop('test', 'testing')
+  > commitopts.setdefault('test', 'testing')
+  > EOF
+  $ python $BINDIR/contrib/byteify-strings.py testfile.py --treat-as-kwargs kwargs opts commitopts
+  kwargs['test'] = b"123"
+  kwargs[test[b'testing']]
+  kwargs[test[[[b'testing']]]]
+  kwargs[kwargs['testing']]
+  kwargs.get('test')
+  kwargs.pop('test')
+  kwargs.get('test', b'testing')
+  kwargs.pop('test', b'testing')
+  kwargs.setdefault('test', b'testing')
+  
+  opts['test'] = b"123"
+  opts[test[b'testing']]
+  opts[test[[[b'testing']]]]
+  opts[opts['testing']]
+  opts.get('test')
+  opts.pop('test')
+  opts.get('test', b'testing')
+  opts.pop('test', b'testing')
+  opts.setdefault('test', b'testing')
+  
+  commitopts['test'] = b"123"
+  commitopts[test[b'testing']]
+  commitopts[test[[[b'testing']]]]
+  commitopts[commitopts['testing']]
+  commitopts.get('test')
+  commitopts.pop('test')
+  commitopts.get('test', b'testing')
+  commitopts.pop('test', b'testing')
+  commitopts.setdefault('test', b'testing')
+
+Test attr*() as methods
+
+  $ cat > testfile.py <<EOF
+  > setattr(o, 'a', 1)
+  > util.setattr(o, 'ae', 1)
+  > util.getattr(o, 'alksjdf', 'default')
+  > util.addattr(o, 'asdf')
+  > util.hasattr(o, 'lksjdf', 'default')
+  > util.safehasattr(o, 'lksjdf', 'default')
+  > @eh.wrapfunction(func, 'lksjdf')
+  > @eh.wrapclass(klass, 'lksjdf')
+  > EOF
+  $ python $BINDIR/contrib/byteify-strings.py testfile.py --allow-attr-methods
+  setattr(o, 'a', 1)
+  util.setattr(o, 'ae', 1)
+  util.getattr(o, 'alksjdf', b'default')
+  util.addattr(o, 'asdf')
+  util.hasattr(o, 'lksjdf', b'default')
+  util.safehasattr(o, 'lksjdf', b'default')
+  @eh.wrapfunction(func, 'lksjdf')
+  @eh.wrapclass(klass, 'lksjdf')
+
+Test without attr*() as methods
+
+  $ cat > testfile.py <<EOF
+  > setattr(o, 'a', 1)
+  > util.setattr(o, 'ae', 1)
+  > util.getattr(o, 'alksjdf', 'default')
+  > util.addattr(o, 'asdf')
+  > util.hasattr(o, 'lksjdf', 'default')
+  > util.safehasattr(o, 'lksjdf', 'default')
+  > @eh.wrapfunction(func, 'lksjdf')
+  > @eh.wrapclass(klass, 'lksjdf')
+  > EOF
+  $ python $BINDIR/contrib/byteify-strings.py testfile.py
+  setattr(o, 'a', 1)
+  util.setattr(o, b'ae', 1)
+  util.getattr(o, b'alksjdf', b'default')
+  util.addattr(o, b'asdf')
+  util.hasattr(o, b'lksjdf', b'default')
+  util.safehasattr(o, b'lksjdf', b'default')
+  @eh.wrapfunction(func, b'lksjdf')
+  @eh.wrapclass(klass, b'lksjdf')
+
+Test ignore comments
+
+  $ cat > testfile.py <<EOF
+  > #py3-transform: off
+  > "none"
+  > "of"
+  > 'these'
+  > s = """should"""
+  > d = '''be'''
+  > #py3-transform: on
+  > "this should"
+  > 'and this also'
+  > 
+  > #no-py3-transform
+  > l = "this should be ignored"
+  > l2 = "this shouldn't"
+  > 
+  > EOF
+  $ python $BINDIR/contrib/byteify-strings.py testfile.py
+  #py3-transform: off
+  "none"
+  "of"
+  'these'
+  s = """should"""
+  d = '''be'''
+  #py3-transform: on
+  b"this should"
+  b'and this also'
+  
+  #no-py3-transform
+  l = "this should be ignored"
+  l2 = b"this shouldn't"
+  
+Test triple-quoted strings
+
+  $ cat > testfile.py <<EOF
+  > """This is ignored
+  > """
+  > 
+  > line = """
+  >   This should not be
+  > """
+  > line = '''
+  > Neither should this
+  > '''
+  > EOF
+  $ python $BINDIR/contrib/byteify-strings.py testfile.py
+  """This is ignored
+  """
+  
+  line = b"""
+    This should not be
+  """
+  line = b'''
+  Neither should this
+  '''
+
+Test prefixed strings
+
+  $ cat > testfile.py <<EOF
+  > obj['test'] = b"1234"
+  > obj[r'test'] = u"1234"
+  > EOF
+  $ python $BINDIR/contrib/byteify-strings.py testfile.py
+  obj[b'test'] = b"1234"
+  obj[r'test'] = u"1234"