From 25afc7da0db43754f6b4373a10b66551dd3cbd57 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Thu, 3 Sep 2015 23:06:18 +0100
Subject: [PATCH] tests: Add tests to improve coverage of objstr.c.

---
 tests/basics/builtin_ord.py           |  6 ++++++
 tests/basics/bytes_construct.py       | 12 ++++++++++++
 tests/basics/setattr1.py              |  5 +++++
 tests/basics/string1.py               |  4 ++++
 tests/basics/string_compare.py        |  5 +++++
 tests/basics/string_format.py         |  6 ++++++
 tests/basics/struct1.py               |  6 ++++++
 tests/extmod/ujson_dumps_extra.py     |  5 +++++
 tests/extmod/ujson_dumps_extra.py.exp |  1 +
 tests/misc/non_compliant.py           | 18 ++++++++++++++++++
 tests/misc/non_compliant.py.exp       |  3 +++
 tests/unix/extra_coverage.py.exp      |  2 ++
 unix/coverage.c                       |  8 ++++++++
 13 files changed, 81 insertions(+)
 create mode 100644 tests/extmod/ujson_dumps_extra.py
 create mode 100644 tests/extmod/ujson_dumps_extra.py.exp

diff --git a/tests/basics/builtin_ord.py b/tests/basics/builtin_ord.py
index 66e56e5c6..1556970ee 100644
--- a/tests/basics/builtin_ord.py
+++ b/tests/basics/builtin_ord.py
@@ -20,3 +20,9 @@ try:
     ord(b'')
 except TypeError:
     print("TypeError")
+
+# argument must be a string
+try:
+    ord(1)
+except TypeError:
+    print('TypeError')
diff --git a/tests/basics/bytes_construct.py b/tests/basics/bytes_construct.py
index e43c8179f..59e02f063 100644
--- a/tests/basics/bytes_construct.py
+++ b/tests/basics/bytes_construct.py
@@ -14,6 +14,18 @@ print(bytes(array('h', [0x101, 0x202])))
 # long ints
 print(ord(bytes([14953042807679334000 & 0xff])))
 
+# constructor value out of range
+try:
+    bytes([-1])
+except ValueError:
+    print('ValueError')
+
+# constructor value out of range
+try:
+    bytes([256])
+except ValueError:
+    print('ValueError')
+
 # error in construction
 try:
     a = bytes([1, 2, 3], 1)
diff --git a/tests/basics/setattr1.py b/tests/basics/setattr1.py
index acc329911..9693aca81 100644
--- a/tests/basics/setattr1.py
+++ b/tests/basics/setattr1.py
@@ -11,3 +11,8 @@ setattr(a, "var", 123)
 setattr(a, "var2", 56)
 print(a.var)
 print(a.var2)
+
+try:
+    setattr(a, b'var3', 1)
+except TypeError:
+    print('TypeError')
diff --git a/tests/basics/string1.py b/tests/basics/string1.py
index b8ca15c34..b3abfb9c6 100644
--- a/tests/basics/string1.py
+++ b/tests/basics/string1.py
@@ -24,6 +24,10 @@ try:
     '123' * '1'
 except TypeError:
     print('TypeError')
+try:
+    '123' + 1
+except TypeError:
+    print('TypeError')
 
 # subscription
 print('abc'[1])
diff --git a/tests/basics/string_compare.py b/tests/basics/string_compare.py
index 740e1959c..f011ed363 100644
--- a/tests/basics/string_compare.py
+++ b/tests/basics/string_compare.py
@@ -48,3 +48,8 @@ print("1" <= "10")
 print("1" <= "1/")
 print("10" <= "1")
 print("1/" <= "1")
+
+# this tests an internal string that doesn't have a hash with a string
+# that does have a hash, but the lengths of the two strings are different
+import sys
+print(sys.version == 'a long string that has a hash')
diff --git a/tests/basics/string_format.py b/tests/basics/string_format.py
index b0e49f530..d8724c947 100644
--- a/tests/basics/string_format.py
+++ b/tests/basics/string_format.py
@@ -207,3 +207,9 @@ try:
     '{:*"1"}'.format('zz')
 except ValueError:
     print('ValueError')
+
+# unknown format code for str arg
+try:
+    '{:X}'.format('zz')
+except ValueError:
+    print('ValueError')
diff --git a/tests/basics/struct1.py b/tests/basics/struct1.py
index 87e2f0702..686251c44 100644
--- a/tests/basics/struct1.py
+++ b/tests/basics/struct1.py
@@ -52,3 +52,9 @@ print(struct.unpack("<Q", b"\xff\xff\xff\xff\xff\xff\xff\xff"))
 
 # network byte order
 print(struct.pack('!i', 123))
+
+# first arg must be a string
+try:
+    struct.pack(1, 2)
+except TypeError:
+    print('TypeError')
diff --git a/tests/extmod/ujson_dumps_extra.py b/tests/extmod/ujson_dumps_extra.py
new file mode 100644
index 000000000..0e593c3e9
--- /dev/null
+++ b/tests/extmod/ujson_dumps_extra.py
@@ -0,0 +1,5 @@
+# test uPy ujson behaviour that's not valid in CPy
+
+import ujson
+
+print(ujson.dumps(b'1234'))
diff --git a/tests/extmod/ujson_dumps_extra.py.exp b/tests/extmod/ujson_dumps_extra.py.exp
new file mode 100644
index 000000000..51712af1b
--- /dev/null
+++ b/tests/extmod/ujson_dumps_extra.py.exp
@@ -0,0 +1 @@
+"1234"
diff --git a/tests/misc/non_compliant.py b/tests/misc/non_compliant.py
index 79f1e70c8..09abac238 100644
--- a/tests/misc/non_compliant.py
+++ b/tests/misc/non_compliant.py
@@ -40,8 +40,26 @@ try:
 except NotImplementedError:
     print('NotImplementedError')
 
+# str(...) with keywords not implemented
+try:
+    str(b'abc', encoding='utf8')
+except NotImplementedError:
+    print('NotImplementedError')
+
 # str.rsplit(None, n) not implemented
 try:
     'a a a'.rsplit(None, 1)
 except NotImplementedError:
     print('NotImplementedError')
+
+# bytes(...) with keywords not implemented
+try:
+    bytes('abc', encoding='utf8')
+except NotImplementedError:
+    print('NotImplementedError')
+
+# bytes subscr with step!=1 not implemented
+try:
+    b'123'[0:3:2]
+except NotImplementedError:
+    print('NotImplementedError')
diff --git a/tests/misc/non_compliant.py.exp b/tests/misc/non_compliant.py.exp
index 3790f4f34..9278034aa 100644
--- a/tests/misc/non_compliant.py.exp
+++ b/tests/misc/non_compliant.py.exp
@@ -5,3 +5,6 @@ True
 TypeError, ValueError
 NotImplementedError
 NotImplementedError
+NotImplementedError
+NotImplementedError
+NotImplementedError
diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp
index 651a47819..00ddf0daf 100644
--- a/tests/unix/extra_coverage.py.exp
+++ b/tests/unix/extra_coverage.py.exp
@@ -25,3 +25,5 @@ stderr          exc_info        print_exception
 ementation
 # attrtuple
 (start=1, stop=2, step=3)
+# str
+1
diff --git a/unix/coverage.c b/unix/coverage.c
index eabd0097d..1f52d9cf8 100644
--- a/unix/coverage.c
+++ b/unix/coverage.c
@@ -75,6 +75,14 @@ STATIC mp_obj_t extra_coverage(void) {
         printf("\n");
     }
 
+    // str
+    {
+        printf("# str\n");
+
+        // intern string
+        printf("%d\n", MP_OBJ_IS_QSTR(mp_obj_str_intern(mp_obj_new_str("intern me", 9, false))));
+    }
+
     return mp_const_none;
 }
 MP_DEFINE_CONST_FUN_OBJ_0(extra_coverage_obj, extra_coverage);
-- 
GitLab