Skip to content

Commit

Permalink
More tests for ValueTree
Browse files Browse the repository at this point in the history
  • Loading branch information
kunitoki committed Jan 20, 2024
1 parent a737bd3 commit 83c0884
Show file tree
Hide file tree
Showing 16 changed files with 363 additions and 87 deletions.
20 changes: 11 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ _juce_initialise_target (${PROJECT_NAME} VERSION "${VERSION_NUMBER}")
_juce_set_output_name (${PROJECT_NAME} $<TARGET_PROPERTY:${PROJECT_NAME},JUCE_PRODUCT_NAME>)

# Configure code coverage
#include (cmake/CodeCoverage.cmake)
#append_coverage_compiler_flags()
#set (COVERAGE_TARGET ${PROJECT_NAME}_coverage)
#setup_target_for_coverage_lcov (
# NAME ${COVERAGE_TARGET}
# EXECUTABLE pytest -s ${CMAKE_CURRENT_LIST_DIR}/tests
# DEPENDENCIES ${PROJECT_NAME}
# LCOV_ARGS "--keep-going" "--branch-coverage" "--no-external"
# GENHTML_ARGS "--keep-going" "--branch-coverage")
if (ENABLE_COVERAGE)
include (cmake/CodeCoverage.cmake)
append_coverage_compiler_flags()
set (COVERAGE_TARGET ${PROJECT_NAME}_coverage)
setup_target_for_coverage_lcov (
NAME ${COVERAGE_TARGET}
EXECUTABLE pytest -s ${CMAKE_CURRENT_LIST_DIR}/tests
DEPENDENCIES ${PROJECT_NAME}
LCOV_ARGS "--keep-going" "--branch-coverage" "--no-external"
GENHTML_ARGS "--keep-going" "--branch-coverage")
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,30 @@ void registerJuceDataStructuresBindings (pybind11::module_& m)
classValueTree
.def (py::init<>())
.def (py::init<const Identifier&>())
.def (py::init ([](py::str id, py::dict dict)
{
auto result = ValueTree (Identifier (static_cast<std::string> (id)));

for (auto item : dict)
{
py::detail::make_caster<Identifier> convKey;
py::detail::make_caster<var> convValue;

if (! convKey.load (item.first, true))
py::pybind11_fail("Invalid property type of a \"ValueTree\", needs to be \"str\" or \"Identifier\"");

if (! convValue.load (item.second, true))
py::pybind11_fail("Invalid property type of a \"ValueTree\", needs to be a \"var\" convertible");

result.setProperty (
py::detail::cast_op<juce::Identifier&&> (std::move (convKey)),
py::detail::cast_op<juce::var&&> (std::move (convValue)),
nullptr);
}

return result;
}))
.def (py::init<const ValueTree&>())
.def (py::self == py::self)
.def (py::self != py::self)
.def ("isEquivalentTo", &ValueTree::isEquivalentTo)
Expand All @@ -339,11 +363,12 @@ void registerJuceDataStructuresBindings (pybind11::module_& m)
.def ("hasType", &ValueTree::hasType)
.def ("getProperty", py::overload_cast<const Identifier&> (&ValueTree::getProperty, py::const_), py::return_value_policy::reference)
.def ("getProperty", py::overload_cast<const Identifier&, const var&> (&ValueTree::getProperty, py::const_), py::return_value_policy::reference)
.def ("getPropertyPointer", &ValueTree::getPropertyPointer, py::return_value_policy::reference)
//.def ("getPropertyPointer", &ValueTree::getPropertyPointer, py::return_value_policy::reference)
.def ("setProperty", &ValueTree::setProperty)
.def ("hasProperty", &ValueTree::hasProperty)
.def ("removeProperty", &ValueTree::removeProperty)
.def ("removeAllProperties", &ValueTree::removeAllProperties)
.def ("getNumProperties", &ValueTree::getNumProperties)
.def ("getPropertyName", &ValueTree::getPropertyName)
.def ("getPropertyAsValue", &ValueTree::getPropertyAsValue)
.def ("getNumChildren", &ValueTree::getNumChildren)
Expand Down
2 changes: 1 addition & 1 deletion modules/juce_python/bindings/ScriptJuceEventsBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void registerJuceEventsBindings (pybind11::module_& m)
}

auto atexit = py::module_::import ("atexit");
atexit.attr ("register") (py::cpp_function([&]
atexit.attr ("register") (py::cpp_function([]
{
if (--numScopedInitInstances == 0)
shutdownJuce_GUI();
Expand Down
12 changes: 10 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def __init__(self, name):


class CMakeBuildExtension(build_ext):
build_for_coverage = os.environ.get("POPSICLE_COVERAGE", None) is not None

def build_extension(self, ext):
log.info("building with cmake")

Expand All @@ -90,7 +92,7 @@ def build_extension(self, ext):
output_path = extdir.parent
output_path.mkdir(parents=True, exist_ok=True)

config = "Debug" if self.debug else "Release"
config = "Debug" if self.debug or self.build_for_coverage else "Release"
cmake_args = [
f"-DCMAKE_BUILD_TYPE={config}",
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={output_path}",
Expand All @@ -99,6 +101,11 @@ def build_extension(self, ext):
f"-DPython_LIBRARY_DIRS={get_python_lib_path()}"
]

if self.build_for_coverage:
cmake_args += [
"-DENABLE_COVERAGE:BOOL=ON"
]

if platform.system() == 'Darwin':
cmake_args += [
"-DCMAKE_OSX_ARCHITECTURES:STRING=arm64;x86_64",
Expand All @@ -117,7 +124,8 @@ def build_extension(self, ext):
build_command += ["--", f"-j{os.cpu_count()}"]
self.spawn(build_command)

#self.spawn(["cmake", "--build", ".", "--target", f"{project_name}_coverage"])
if self.build_for_coverage:
self.spawn(["cmake", "--build", ".", "--target", f"{project_name}_coverage"])

finally:
os.chdir(str(cwd))
Expand Down
31 changes: 31 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest

from . import common
import popsicle as juce


def yield_test():
yield


@pytest.fixture
def juce_app():
class Application(juce.JUCEApplication):
def __init__(self):
super().__init__()

def getApplicationName(self):
return "TestApp"

def getApplicationVersion(self):
return "1.0"

def initialise(self, commandLineParameters: str):
juce.MessageManager.callAsync(yield_test)

def shutdown(self):
pass

with juce.TestApplication(Application) as app:
app.processEvents()
yield app
4 changes: 4 additions & 0 deletions tests/test_juce_core/test_BigInteger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import popsicle as juce


#==================================================================================================

def get_big_random(r: juce.Random) -> juce.BigInteger:
b = juce.BigInteger()

Expand All @@ -11,6 +13,7 @@ def get_big_random(r: juce.Random) -> juce.BigInteger:

return b

#==================================================================================================

def test_comparisons():
r = juce.Random.getSystemRandom()
Expand Down Expand Up @@ -39,6 +42,7 @@ def test_comparisons():
b5.loadFromMemoryBlock(b3.toMemoryBlock())
assert b3 == b5

#==================================================================================================

def test_bit_setting():
r = juce.Random.getSystemRandom()
Expand Down
25 changes: 17 additions & 8 deletions tests/test_juce_core/test_ByteOrder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,52 @@

import popsicle as juce

#==================================================================================================

def test_swap_python_integer():
a = 1337
assert a != juce.ByteOrder.swap(a)
assert a == juce.ByteOrder.swap(juce.ByteOrder.swap(a))

#==================================================================================================

def test_littleEndianShort_python_integer():
def test_little_endian_short_python_integer():
assert juce.ByteOrder.littleEndianShort(b'\x01\x00') == 1
assert juce.ByteOrder.littleEndianShort(b'\xff\x00') == 255

#==================================================================================================

def test_littleEndianInt_python_integer():
def test_little_endian_int_python_integer():
assert juce.ByteOrder.littleEndianInt(b'\x01\x00\x00\00') == 1
assert juce.ByteOrder.littleEndianInt(b'\xff\x00\x00\00') == 255

#==================================================================================================

def test_littleEndianInt64_python_integer():
def test_little_endian_int64_python_integer():
assert juce.ByteOrder.littleEndianInt64(b'\x01\x00\x00\00\x00\x00\x00\x00') == 1
assert juce.ByteOrder.littleEndianInt64(b'\xff\x00\x00\00\x00\x00\x00\x00') == 255

#==================================================================================================

def test_bigEndianShort_python_integer():
def test_big_endian_short_python_integer():
assert juce.ByteOrder.bigEndianShort(b'\x00\01') == 1
assert juce.ByteOrder.bigEndianShort(b'\x00\xff') == 255

#==================================================================================================

def test_bigEndianInt_python_integer():
def test_big_endian_int_python_integer():
assert juce.ByteOrder.bigEndianInt(b'\x00\x00\x00\01') == 1
assert juce.ByteOrder.bigEndianInt(b'\x00\x00\x00\xff') == 255

#==================================================================================================

def test_bigEndianInt64_python_integer():
def test_big_endian_int64_python_integer():
assert juce.ByteOrder.bigEndianInt64(b'\x00\x00\x00\x00\x00\x00\x00\01') == 1
assert juce.ByteOrder.bigEndianInt64(b'\x00\x00\x00\x00\x00\x00\x00\xff') == 255

#==================================================================================================

def test_makeInt():
def test_make_int():
assert juce.ByteOrder.makeInt(1, 0) == 1
assert juce.ByteOrder.makeInt(255, 0) == 255
assert juce.ByteOrder.makeInt(0, 1) == 256
Expand All @@ -54,7 +62,8 @@ def test_makeInt():
assert juce.ByteOrder.makeInt(0, 0, 0, 0, 0, 0, 0, 1) == 72057594037927936
assert juce.ByteOrder.makeInt(255, 0, 0, 0, 0, 0, 0, 1) == 72057594037928191

#==================================================================================================

def test_isBigEndian():
def test_is_big_endian():
is_big_endian = sys.byteorder == "big"
assert is_big_endian == juce.ByteOrder.isBigEndian()
11 changes: 9 additions & 2 deletions tests/test_juce_core/test_Identifier.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import popsicle as juce

#==================================================================================================

def test_empty_constructor():
a = juce.Identifier()
Expand All @@ -15,6 +16,7 @@ def test_empty_constructor():
assert c != a
assert c != b

#==================================================================================================

def test_constructor_python_str():
a = juce.Identifier("abc")
Expand All @@ -24,6 +26,7 @@ def test_constructor_python_str():
b = juce.Identifier("abc")
assert a == b

#==================================================================================================

def test_constructor_copy():
a = juce.Identifier("abc")
Expand All @@ -32,6 +35,7 @@ def test_constructor_copy():
assert a.isValid() == b.isValid()
assert a.toString() == b.toString()

#==================================================================================================

def test_constructor_juce_String():
a = juce.Identifier("abc")
Expand All @@ -41,6 +45,7 @@ def test_constructor_juce_String():
b = juce.Identifier("abc")
assert a == b

#==================================================================================================

def test_comparisons():
a = juce.Identifier("abc")
Expand All @@ -58,17 +63,19 @@ def test_comparisons():
assert a < c
assert not (a > c)

#==================================================================================================

def test_toString():
def test_to_string():
a = juce.Identifier("abc")
b = juce.Identifier("abc")
assert a.toString() == b.toString()

c = juce.Identifier("123")
assert a.toString() != c.toString()

#==================================================================================================

def test_isValidIdentifier():
def test_is_valid_identifier():
assert not juce.Identifier.isValidIdentifier("")
assert not juce.Identifier.isValidIdentifier(" ")
assert juce.Identifier.isValidIdentifier("abcf123")
Loading

0 comments on commit 83c0884

Please sign in to comment.