From f46b9f4e2d5b99c3d46a22432ae0ab347026e501 Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Fri, 21 Apr 2023 22:36:00 +0200 Subject: [PATCH] Update Berry tests from upstream (#18472) --- lib/libesp32/berry/tests/bytes.be | 9 ++++ lib/libesp32/berry/tests/class_static.be | 21 ++++++++ lib/libesp32/berry/tests/introspect.be | 11 ++++ lib/libesp32/berry/tests/json.be | 13 +++++ lib/libesp32/berry/tests/json_advanced.be | 51 +++++++++++++++++++ .../berry/tests/json_test_stack_size.be | 11 ++++ lib/libesp32/berry/tests/map.be | 7 +++ 7 files changed, 123 insertions(+) create mode 100644 lib/libesp32/berry/tests/json_advanced.be create mode 100644 lib/libesp32/berry/tests/json_test_stack_size.be diff --git a/lib/libesp32/berry/tests/bytes.be b/lib/libesp32/berry/tests/bytes.be index d2b2c1573..6a481d62c 100644 --- a/lib/libesp32/berry/tests/bytes.be +++ b/lib/libesp32/berry/tests/bytes.be @@ -248,3 +248,12 @@ assert(bytes("0011223344").reverse(1, nil) == bytes("0044332211")) assert(bytes("0011223344").reverse(nil, nil, 2) == bytes("2233001144")) assert(bytes("001122334455").reverse(nil, nil, 3) == bytes("334455001122")) + +# tobool returns `true` is bytes() is not null, `false` if empty +assert(bool(bytes()) == false) +assert(bytes().tobool() == false) +assert(!bytes()) + +assert(bool(bytes("00")) == true) +assert(bytes("01").tobool() == true) +assert(bytes("02")) diff --git a/lib/libesp32/berry/tests/class_static.be b/lib/libesp32/berry/tests/class_static.be index c86a4a4b3..1b3d0f928 100644 --- a/lib/libesp32/berry/tests/class_static.be +++ b/lib/libesp32/berry/tests/class_static.be @@ -120,3 +120,24 @@ assert(A.a == 1) assert(B.a == A) assert(B.b == 1) assert(B.c == A) + +#- static class get an implicit `_class` variable -# +class A + static def f(x) return _class end +end +assert(A.f() == A) + +#- static class within a class -# +class A + static class B + static def f() return 1 end + def g() return 2 end + end +end +a = A() +b = A.B() +assert(classname(a) == 'A') +assert(classname(b) == 'B') +assert(A.B.f() == 1) +assert(b.g() == 2) +assert(super(B) == nil) diff --git a/lib/libesp32/berry/tests/introspect.be b/lib/libesp32/berry/tests/introspect.be index cb4a612d8..c93033982 100644 --- a/lib/libesp32/berry/tests/introspect.be +++ b/lib/libesp32/berry/tests/introspect.be @@ -31,3 +31,14 @@ assert(a.a == 3) import introspect m = introspect.module("math") # load module `math`, assign to `m` and don't create a global variable assert(type(m.pi) == 'real') + +#- name -# +import string +assert(introspect.name(string) == 'string') +assert(introspect.name(print) == nil) # native C function don't have a registered name +assert(introspect.name("foo") == nil) +class A def a() end static def b() end static var c end +assert(introspect.name(A) == 'A') +assert(introspect.name(A.a) == 'a') +assert(introspect.name(A.b) == 'b') +assert(introspect.name(A.c) == nil) diff --git a/lib/libesp32/berry/tests/json.be b/lib/libesp32/berry/tests/json.be index 92df2f3e6..2165eda8d 100644 --- a/lib/libesp32/berry/tests/json.be +++ b/lib/libesp32/berry/tests/json.be @@ -80,3 +80,16 @@ class map2 : map def init() super(self).init() end end var m = map2() m['key'] = 1 assert_dump(m, '{"key":1}') + +# sweep dumping nested arrays of diffrent sizes +# this tests for any unexpanded stack conditions +for count : 10..200 + var arr = [[]] + var last_arr = arr + for i : 0..count + var pushed = [i] + last_arr.push(pushed) + last_arr = pushed + end + json.dump(arr) +end diff --git a/lib/libesp32/berry/tests/json_advanced.be b/lib/libesp32/berry/tests/json_advanced.be new file mode 100644 index 000000000..72206d9de --- /dev/null +++ b/lib/libesp32/berry/tests/json_advanced.be @@ -0,0 +1,51 @@ +import os +import json + + + +def assert_load_failed(text) + assert(json.load(text) == nil) +end + +var input_file = open("tests/json_test_cases.json", "r") +var test_cases = json.load(input_file.read()) + +# check positive cases +var has_failed_positives = false +for case_name : test_cases["positive"].keys() + var case = test_cases["positive"][case_name] + var val = json.load(case) + if val == nil && case != "null" + print("Failed to load case: " + case_name) + has_failed_positives = true + end +end + +if has_failed_positives + assert(false) +end + +# check negative cases + +var has_failed_negatives = false +for case_name : test_cases["negative"].keys() + var case = test_cases["negative"][case_name] + + var val = json.load(case) + if val != nil + print("Failed to fail case: " + case_name + ", got: " + json.dump(val)) + has_failed_negatives = true + end +end + +if has_failed_negatives + # assert(false) +end + +# check "any" cases, only for crashes + +for case_name : test_cases["any"].keys() + var case = test_cases["any"][case_name] + var val = json.load(case) +end + diff --git a/lib/libesp32/berry/tests/json_test_stack_size.be b/lib/libesp32/berry/tests/json_test_stack_size.be new file mode 100644 index 000000000..f76ff8bb4 --- /dev/null +++ b/lib/libesp32/berry/tests/json_test_stack_size.be @@ -0,0 +1,11 @@ +import json + +# this test must be in a separate file, so that the stack is not expanded yet by other tests + +arr = "{" +for i : 0..1000 + arr += '"k' + str(i) + '": "v' + str(i) + '",' +end +arr += "}" + +json.load(arr) diff --git a/lib/libesp32/berry/tests/map.be b/lib/libesp32/berry/tests/map.be index ca0c4399d..9029faed4 100644 --- a/lib/libesp32/berry/tests/map.be +++ b/lib/libesp32/berry/tests/map.be @@ -30,3 +30,10 @@ m.remove(2) assert(str(m) == '{1: 2}') m.remove(1) assert(str(m) == '{}') + +# allow booleans to be used as keys +m={true:10, false:20} +assert(m.contains(true)) +assert(m.contains(false)) +assert(m[true] == 10) +assert(m[false] == 20)