From fefc7c59b0b3a3ab4a9b997af928b1f6d0c1c09b Mon Sep 17 00:00:00 2001 From: Scott Talbert Date: Thu, 4 Aug 2016 20:47:58 -0400 Subject: [PATCH 1/5] Add wrapper and unittests for wxPalette --- TODO.rst | 1 - docs/sphinx/itemToModuleMap.json | 4 +- etg/_core.py | 1 + etg/defs.py | 1 - etg/palette.py | 127 +++++++++++++++++++++++++++++++ unittests/test_palette.py | 41 ++++++++++ 6 files changed, 172 insertions(+), 3 deletions(-) create mode 100644 etg/palette.py create mode 100644 unittests/test_palette.py diff --git a/TODO.rst b/TODO.rst index c4c7b13f..33bc7d1b 100644 --- a/TODO.rst +++ b/TODO.rst @@ -131,7 +131,6 @@ Other Dev Stuff * dialup ?? * docmdi ?? * docview ?? - * palette ?? * persist ?? * Add a _msw module that will contain classes and such that are only diff --git a/docs/sphinx/itemToModuleMap.json b/docs/sphinx/itemToModuleMap.json index d734eb53..8e3e3d44 100644 --- a/docs/sphinx/itemToModuleMap.json +++ b/docs/sphinx/itemToModuleMap.json @@ -661,7 +661,7 @@ "DirPickerWidgetNameStr":"wx.", "DirSelector":"wx.", "DirSelectorPromptStr":"wx.", -"Direction":"wx.DataObject.", +"Direction":"wx.", "DisableAsserts":"wx.", "Display":"wx.", "DisplayChangedEvent":"wx.", @@ -2205,6 +2205,7 @@ "NullIcon":"wx.", "NullIconBundle":"wx.", "NullImage":"wx.", +"NullPalette":"wx.", "NullPen":"wx.", "NullRegion":"wx.", "ODCB_DCLICK_CYCLES":"wx.adv.", @@ -2460,6 +2461,7 @@ "PageSetupDialogData":"wx.", "PaintDC":"wx.", "PaintEvent":"wx.", +"Palette":"wx.", "PaletteChangedEvent":"wx.", "Panel":"wx.", "PanelNameStr":"wx.", diff --git a/etg/_core.py b/etg/_core.py index dcc1ffed..48edd664 100644 --- a/etg/_core.py +++ b/etg/_core.py @@ -94,6 +94,7 @@ INCLUDES = [ # base and core stuff 'graphics', 'imaglist', 'overlay', + 'palette', 'renderer', 'rawbmp', diff --git a/etg/defs.py b/etg/defs.py index d2fff99b..87ab3d3b 100644 --- a/etg/defs.py +++ b/etg/defs.py @@ -84,7 +84,6 @@ def run(): # TODO: Add these classes for real :-) module.insertItem(0, etgtools.WigCode("""\ // forward declarations - class wxPalette; class wxExecuteEnv; """)) diff --git a/etg/palette.py b/etg/palette.py new file mode 100644 index 00000000..46a02704 --- /dev/null +++ b/etg/palette.py @@ -0,0 +1,127 @@ +#--------------------------------------------------------------------------- +# Name: etg/palette.py +# Author: Scott Talbert +# +# Created: 31-Jul-2016 +# Copyright: (c) 2016 by Total Control Software +# License: wxWindows License +#--------------------------------------------------------------------------- + +import etgtools +import etgtools.tweaker_tools as tools + +PACKAGE = "wx" +MODULE = "_core" +NAME = "palette" # Base name of the file to generate to for this script +DOCSTRING = "" + +# The classes and/or the basename of the Doxygen XML files to be processed by +# this script. +ITEMS = [ 'wxPalette' ] + +#--------------------------------------------------------------------------- + +def run(): + # Parse the XML file(s) building a collection of Extractor objects + module = etgtools.ModuleDef(PACKAGE, MODULE, NAME, DOCSTRING) + etgtools.parseDoxyXML(module, ITEMS) + + #----------------------------------------------------------------- + # Tweak the parsed meta objects in the module object as needed for + # customizing the generated code and docstrings. + c = module.find('wxPalette') + + c.find('GetPixel.red').pyInt = True + c.find('GetPixel.green').pyInt = True + c.find('GetPixel.blue').pyInt = True + + c.find('GetRGB').ignore() + c.addCppMethod('PyObject*', 'GetRGB', '(int pixel)', + doc="Returns RGB values for a given palette index.", + body="""\ + unsigned char red; + unsigned char green; + unsigned char blue; + wxPyThreadBlocker blocker; + if (!self->GetRGB(pixel, &red, &green, &blue)) { + PyErr_SetString(PyExc_IndexError, "Pixel out of range"); + return NULL; + } + PyObject* rv = PyTuple_New(3); + PyTuple_SetItem(rv, 0, wxPyInt_FromLong(red)); + PyTuple_SetItem(rv, 1, wxPyInt_FromLong(green)); + PyTuple_SetItem(rv, 2, wxPyInt_FromLong(blue)); + return rv; + """) + + c.find('Create').ignore() + c.addCppMethod('PyObject*', 'Create', '(PyObject* red, PyObject* green, PyObject* blue)', + doc="Creates a palette from tuples of integers, one for each red, blue or green component.", + body="""\ + wxPyThreadBlocker blocker; + + char* errMsg = "Expected a tuple of integer objects"; + if (!PyTuple_Check(red)) { + PyErr_SetString(PyExc_TypeError, errMsg); + return NULL; + } + if (!PyTuple_Check(green)) { + PyErr_SetString(PyExc_TypeError, errMsg); + return NULL; + } + if (!PyTuple_Check(blue)) { + PyErr_SetString(PyExc_TypeError, errMsg); + return NULL; + } + + int count = PyTuple_Size(red); + if (PyTuple_Size(green) != count || PyTuple_Size(blue) != count) { + PyErr_SetString(PyExc_ValueError, "Tuple sizes must be equal"); + return NULL; + } + + unsigned char* redArray = new unsigned char[count]; + unsigned char* greenArray = new unsigned char[count]; + unsigned char* blueArray = new unsigned char[count]; + for (int i = 0; i < count; i++) { + PyObject* redItem = PyTuple_GET_ITEM(red, i); + PyObject* greenItem = PyTuple_GET_ITEM(green, i); + PyObject* blueItem = PyTuple_GET_ITEM(blue, i); + if (!wxPyInt_Check(redItem) || !wxPyInt_Check(greenItem) || !wxPyInt_Check(blueItem)) { + delete[] redArray; + delete[] greenArray; + delete[] blueArray; + PyErr_SetString(PyExc_TypeError, errMsg); + return NULL; + } + long redLong = wxPyInt_AsLong(redItem); + long greenLong = wxPyInt_AsLong(greenItem); + long blueLong = wxPyInt_AsLong(blueItem); + if (redLong < 0 || redLong > 256 || greenLong < 0 || greenLong > 256 || blueLong < 0 || blueLong > 256) { + delete[] redArray; + delete[] greenArray; + delete[] blueArray; + PyErr_SetString(PyExc_ValueError, "Tuple values must be > 0 and < 256"); + return NULL; + } + redArray[i] = redLong; + greenArray[i] = greenLong; + blueArray[i] = blueLong; + } + + if (self->Create(count, redArray, greenArray, blueArray)) { + Py_RETURN_TRUE; + } else { + Py_RETURN_FALSE; + } + """) + + + #----------------------------------------------------------------- + tools.doCommonTweaks(module) + tools.runGenerators(module) + + +#--------------------------------------------------------------------------- +if __name__ == '__main__': + run() diff --git a/unittests/test_palette.py b/unittests/test_palette.py new file mode 100644 index 00000000..e27c359b --- /dev/null +++ b/unittests/test_palette.py @@ -0,0 +1,41 @@ +import unittest +from unittests import wtc +import wx + +#--------------------------------------------------------------------------- + +class palette_Tests(wtc.WidgetTestCase): + + def test_palette(self): + rgb = bytearray(range(256)) + p = wx.Palette(256, rgb, rgb, rgb) + self.assertTrue(p.IsOk()) + self.assertTrue(p.GetColoursCount() == 256) + self.assertTrue(p.GetPixel(42, 42, 42) == 42) + self.assertTrue(p.GetRGB(64) == (64, 64, 64)) + with self.assertRaises(IndexError): + p.GetRGB(257) + + def test_paletteCreate(self): + p = wx.Palette() + with self.assertRaises(TypeError): + p.Create(1, 2, 3) + with self.assertRaises(ValueError): + p.Create((1, 2, 3), (1, 2, 3), (1, 2)) + with self.assertRaises(TypeError): + p.Create((1, 2, 3), ("1", "2", "3"), (1, 2, 3)) + with self.assertRaises(ValueError): + p.Create((257, 2, 3), (1, 2, 3), (1, 2, 3)) + with self.assertRaises(ValueError): + p.Create((1, 2, 3), (-1, 2, 3), (1, 2, 3)) + self.assertTrue(p.Create((1, 2, 3), (4, 5, 6), (7, 8, 9))) + self.assertTrue(p.GetRGB(1) == (2, 5, 8)) + + def test_nullPalette(self): + wx.NullPalette + +#--------------------------------------------------------------------------- + + +if __name__ == '__main__': + unittest.main() From fe29df05d4ea0ee689c2797bdc6d26cbaad8f45c Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Thu, 4 Aug 2016 22:42:32 -0700 Subject: [PATCH 2/5] Switch from Tuple to more general Sequence, and other refactoring. --- etg/palette.py | 63 ++++++++++++++++++++------------------- unittests/test_palette.py | 18 +++++++++-- 2 files changed, 48 insertions(+), 33 deletions(-) diff --git a/etg/palette.py b/etg/palette.py index 46a02704..aeef0494 100644 --- a/etg/palette.py +++ b/etg/palette.py @@ -31,6 +31,7 @@ def run(): # customizing the generated code and docstrings. c = module.find('wxPalette') + c.find('GetPixel.red').pyInt = True c.find('GetPixel.green').pyInt = True c.find('GetPixel.blue').pyInt = True @@ -56,53 +57,47 @@ def run(): c.find('Create').ignore() c.addCppMethod('PyObject*', 'Create', '(PyObject* red, PyObject* green, PyObject* blue)', - doc="Creates a palette from tuples of integers, one for each red, blue or green component.", + doc="Creates a palette from 3 sequences of integers, one for each red, blue or green component.", body="""\ wxPyThreadBlocker blocker; + PyObject* rval; - char* errMsg = "Expected a tuple of integer objects"; - if (!PyTuple_Check(red)) { - PyErr_SetString(PyExc_TypeError, errMsg); - return NULL; - } - if (!PyTuple_Check(green)) { - PyErr_SetString(PyExc_TypeError, errMsg); - return NULL; - } - if (!PyTuple_Check(blue)) { + char* errMsg = "Expected a sequence of integer objects"; + if (!PySequence_Check(red) || !PySequence_Check(green) || !PySequence_Check(blue)) { PyErr_SetString(PyExc_TypeError, errMsg); return NULL; } - int count = PyTuple_Size(red); - if (PyTuple_Size(green) != count || PyTuple_Size(blue) != count) { - PyErr_SetString(PyExc_ValueError, "Tuple sizes must be equal"); + Py_ssize_t count = PySequence_Size(red); + if (PySequence_Size(green) != count || PySequence_Size(blue) != count) { + PyErr_SetString(PyExc_ValueError, "Sequence lengths must be equal"); return NULL; } unsigned char* redArray = new unsigned char[count]; unsigned char* greenArray = new unsigned char[count]; unsigned char* blueArray = new unsigned char[count]; - for (int i = 0; i < count; i++) { - PyObject* redItem = PyTuple_GET_ITEM(red, i); - PyObject* greenItem = PyTuple_GET_ITEM(green, i); - PyObject* blueItem = PyTuple_GET_ITEM(blue, i); + + for (Py_ssize_t i = 0; i < count; i++) { + PyObject* redItem = PySequence_ITEM(red, i); + PyObject* greenItem = PySequence_ITEM(green, i); + PyObject* blueItem = PySequence_ITEM(blue, i); if (!wxPyInt_Check(redItem) || !wxPyInt_Check(greenItem) || !wxPyInt_Check(blueItem)) { - delete[] redArray; - delete[] greenArray; - delete[] blueArray; PyErr_SetString(PyExc_TypeError, errMsg); - return NULL; + rval = NULL; + goto exit; } + long redLong = wxPyInt_AsLong(redItem); long greenLong = wxPyInt_AsLong(greenItem); long blueLong = wxPyInt_AsLong(blueItem); - if (redLong < 0 || redLong > 256 || greenLong < 0 || greenLong > 256 || blueLong < 0 || blueLong > 256) { - delete[] redArray; - delete[] greenArray; - delete[] blueArray; - PyErr_SetString(PyExc_ValueError, "Tuple values must be > 0 and < 256"); - return NULL; + Py_DECREF(redItem); + Py_DECREF(greenItem); + Py_DECREF(blueItem); + if (redLong < 0 || redLong > 255 || greenLong < 0 || greenLong > 255 || blueLong < 0 || blueLong > 255) { + PyErr_SetString(PyExc_ValueError, "Sequence values must be >= 0 and < 256"); + rval = NULL; + goto exit; } redArray[i] = redLong; greenArray[i] = greenLong; @@ -110,10 +105,18 @@ def run(): } if (self->Create(count, redArray, greenArray, blueArray)) { - Py_RETURN_TRUE; + rval = Py_True; + Py_INCREF(rval); } else { - Py_RETURN_FALSE; + rval = Py_False; + Py_INCREF(rval); } + + exit: + delete[] redArray; + delete[] greenArray; + delete[] blueArray; + return rval; """) diff --git a/unittests/test_palette.py b/unittests/test_palette.py index e27c359b..8b3c3d77 100644 --- a/unittests/test_palette.py +++ b/unittests/test_palette.py @@ -6,7 +6,7 @@ import wx class palette_Tests(wtc.WidgetTestCase): - def test_palette(self): + def test_palette1(self): rgb = bytearray(range(256)) p = wx.Palette(256, rgb, rgb, rgb) self.assertTrue(p.IsOk()) @@ -16,7 +16,14 @@ class palette_Tests(wtc.WidgetTestCase): with self.assertRaises(IndexError): p.GetRGB(257) - def test_paletteCreate(self): + + # def test_palette2(self): + # rgb = list(range(256)) + # p = wx.Palette(len(rgb), rgb, rgb, rgb) + # self.assertTrue(p.IsOk()) + + + def test_paletteCreate1(self): p = wx.Palette() with self.assertRaises(TypeError): p.Create(1, 2, 3) @@ -28,9 +35,14 @@ class palette_Tests(wtc.WidgetTestCase): p.Create((257, 2, 3), (1, 2, 3), (1, 2, 3)) with self.assertRaises(ValueError): p.Create((1, 2, 3), (-1, 2, 3), (1, 2, 3)) - self.assertTrue(p.Create((1, 2, 3), (4, 5, 6), (7, 8, 9))) + + + def test_paletteCreate2(self): + p = wx.Palette() + self.assertTrue(p.Create((1, 2, 3), [4, 5, 6], bytearray([7, 8, 9]))) self.assertTrue(p.GetRGB(1) == (2, 5, 8)) + def test_nullPalette(self): wx.NullPalette From ab01a8981638824b56184366a4153732a5b1970f Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Thu, 4 Aug 2016 22:48:57 -0700 Subject: [PATCH 3/5] Fix wxPalette::Create on OSX --- ext/wxWidgets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/wxWidgets b/ext/wxWidgets index 24d5ff7d..eaf70ea3 160000 --- a/ext/wxWidgets +++ b/ext/wxWidgets @@ -1 +1 @@ -Subproject commit 24d5ff7dc1f6c9c14f001cf4004e342f52a19a3a +Subproject commit eaf70ea3cbc5b3ec3d29ffad9901f0ac6caefd24 From 1429bb0dd27c37620bf5a6bd73148543265f6fb2 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Fri, 5 Aug 2016 12:00:17 -0700 Subject: [PATCH 4/5] Add missing "PyErr_Clear();" for cppCtors --- etgtools/sip_generator.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/etgtools/sip_generator.py b/etgtools/sip_generator.py index 2aed9c07..cdab8b1f 100644 --- a/etgtools/sip_generator.py +++ b/etgtools/sip_generator.py @@ -824,7 +824,9 @@ from .%s import * stream.write('%s%%MethodCode\n' % indent) stream.write(indent+' '*4) if method.isCtor: - stream.write('sipCpp = %s(%s);\n' % (fname, pnames)) + # _THREAD macros are intentionally not used in this case + stream.write('PyErr_Clear();\n') + stream.write('%ssipCpp = %s(%s);\n' % (indent+' '*4, fname, pnames)) elif method.isDtor: stream.write('PyErr_Clear();\n') From a672913e97c776bcf051290ba194a03ff51f409e Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Fri, 5 Aug 2016 12:08:21 -0700 Subject: [PATCH 5/5] Refactor custom Create code to a separate helper function. Add a replacement for the constructor taking arrays of rgb values. Update and expand tests. --- docs/sphinx/itemToModuleMap.json | 2 +- etg/palette.py | 143 +++++++++++++++++++------------ unittests/test_palette.py | 43 +++++++--- 3 files changed, 120 insertions(+), 68 deletions(-) diff --git a/docs/sphinx/itemToModuleMap.json b/docs/sphinx/itemToModuleMap.json index 8e3e3d44..80d83e35 100644 --- a/docs/sphinx/itemToModuleMap.json +++ b/docs/sphinx/itemToModuleMap.json @@ -661,7 +661,7 @@ "DirPickerWidgetNameStr":"wx.", "DirSelector":"wx.", "DirSelectorPromptStr":"wx.", -"Direction":"wx.", +"Direction":"wx.DataObject.", "DisableAsserts":"wx.", "Display":"wx.", "DisplayChangedEvent":"wx.", diff --git a/etg/palette.py b/etg/palette.py index aeef0494..385f5c26 100644 --- a/etg/palette.py +++ b/etg/palette.py @@ -30,8 +30,88 @@ def run(): # Tweak the parsed meta objects in the module object as needed for # customizing the generated code and docstrings. c = module.find('wxPalette') + tools.removeVirtuals(c) + # Add a helper function for use with the Create method and the Ctor + # accepting RGB data. + c.addCppCode("""\ + // a helper function to be used in the Create method and one of the Ctors + bool _paletteCreateHelper(wxPalette* self, + PyObject* red, PyObject* green, PyObject* blue) { + bool rval = false; + wxPyThreadBlocker blocker; + char* errMsg = "Expected a sequence of integer objects"; + + if (!PySequence_Check(red) || !PySequence_Check(green) || !PySequence_Check(blue)) { + PyErr_SetString(PyExc_TypeError, errMsg); + return rval; + } + + Py_ssize_t count = PySequence_Size(red); + if (PySequence_Size(green) != count || PySequence_Size(blue) != count) { + PyErr_SetString(PyExc_ValueError, "Sequence lengths must be equal"); + return rval; + } + + unsigned char* redArray = new unsigned char[count]; + unsigned char* greenArray = new unsigned char[count]; + unsigned char* blueArray = new unsigned char[count]; + + for (Py_ssize_t i = 0; i < count; i++) { + PyObject* redItem = PySequence_ITEM(red, i); + PyObject* greenItem = PySequence_ITEM(green, i); + PyObject* blueItem = PySequence_ITEM(blue, i); + if (!wxPyInt_Check(redItem) || !wxPyInt_Check(greenItem) || !wxPyInt_Check(blueItem)) { + PyErr_SetString(PyExc_TypeError, errMsg); + goto pch_exit; + } + + long redLong = wxPyInt_AsLong(redItem); + long greenLong = wxPyInt_AsLong(greenItem); + long blueLong = wxPyInt_AsLong(blueItem); + Py_DECREF(redItem); + Py_DECREF(greenItem); + Py_DECREF(blueItem); + if (redLong < 0 || redLong > 255 || greenLong < 0 || greenLong > 255 || blueLong < 0 || blueLong > 255) { + PyErr_SetString(PyExc_ValueError, "Sequence values must be in the 0..255 range"); + goto pch_exit; + } + redArray[i] = redLong; + greenArray[i] = greenLong; + blueArray[i] = blueLong; + } + + rval = self->Create(count, redArray, greenArray, blueArray); + + pch_exit: + delete[] redArray; + delete[] greenArray; + delete[] blueArray; + return rval; + } + """) + + + #----------------------------------------------------------------- + + # Replace the constructor accepting the arrays of RGB values with one that + # understands any Python sequence. + c.find('wxPalette').findOverload('red').ignore() + c.addCppCtor('(PyObject* red, PyObject* green, PyObject* blue)', + doc="""\ + Creates a palette from a set of sequences of integers, + one for each red, green and blue color components.""", + body="""\ + wxPalette* pal = new wxPalette; + _paletteCreateHelper(pal, red, green, blue); + if (PyErr_Occurred()) { + delete pal; + return NULL; + } + return pal; + """) + c.find('GetPixel.red').pyInt = True c.find('GetPixel.green').pyInt = True c.find('GetPixel.blue').pyInt = True @@ -55,68 +135,19 @@ def run(): return rv; """) + # Replace the Create method with one that understands any kind of Python sequence c.find('Create').ignore() c.addCppMethod('PyObject*', 'Create', '(PyObject* red, PyObject* green, PyObject* blue)', doc="Creates a palette from 3 sequences of integers, one for each red, blue or green component.", body="""\ + bool rval = _paletteCreateHelper(self, red, green, blue); wxPyThreadBlocker blocker; - PyObject* rval; - - char* errMsg = "Expected a sequence of integer objects"; - if (!PySequence_Check(red) || !PySequence_Check(green) || !PySequence_Check(blue)) { - PyErr_SetString(PyExc_TypeError, errMsg); + if (PyErr_Occurred()) return NULL; - } - - Py_ssize_t count = PySequence_Size(red); - if (PySequence_Size(green) != count || PySequence_Size(blue) != count) { - PyErr_SetString(PyExc_ValueError, "Sequence lengths must be equal"); - return NULL; - } - - unsigned char* redArray = new unsigned char[count]; - unsigned char* greenArray = new unsigned char[count]; - unsigned char* blueArray = new unsigned char[count]; - - for (Py_ssize_t i = 0; i < count; i++) { - PyObject* redItem = PySequence_ITEM(red, i); - PyObject* greenItem = PySequence_ITEM(green, i); - PyObject* blueItem = PySequence_ITEM(blue, i); - if (!wxPyInt_Check(redItem) || !wxPyInt_Check(greenItem) || !wxPyInt_Check(blueItem)) { - PyErr_SetString(PyExc_TypeError, errMsg); - rval = NULL; - goto exit; - } - - long redLong = wxPyInt_AsLong(redItem); - long greenLong = wxPyInt_AsLong(greenItem); - long blueLong = wxPyInt_AsLong(blueItem); - Py_DECREF(redItem); - Py_DECREF(greenItem); - Py_DECREF(blueItem); - if (redLong < 0 || redLong > 255 || greenLong < 0 || greenLong > 255 || blueLong < 0 || blueLong > 255) { - PyErr_SetString(PyExc_ValueError, "Sequence values must be >= 0 and < 256"); - rval = NULL; - goto exit; - } - redArray[i] = redLong; - greenArray[i] = greenLong; - blueArray[i] = blueLong; - } - - if (self->Create(count, redArray, greenArray, blueArray)) { - rval = Py_True; - Py_INCREF(rval); - } else { - rval = Py_False; - Py_INCREF(rval); - } - - exit: - delete[] redArray; - delete[] greenArray; - delete[] blueArray; - return rval; + if (rval) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; """) diff --git a/unittests/test_palette.py b/unittests/test_palette.py index 8b3c3d77..3f68427b 100644 --- a/unittests/test_palette.py +++ b/unittests/test_palette.py @@ -6,9 +6,10 @@ import wx class palette_Tests(wtc.WidgetTestCase): - def test_palette1(self): + def test_paletteCtor1(self): + # test with a bytearray like the original impl rgb = bytearray(range(256)) - p = wx.Palette(256, rgb, rgb, rgb) + p = wx.Palette(rgb, rgb, rgb) self.assertTrue(p.IsOk()) self.assertTrue(p.GetColoursCount() == 256) self.assertTrue(p.GetPixel(42, 42, 42) == 42) @@ -17,28 +18,48 @@ class palette_Tests(wtc.WidgetTestCase): p.GetRGB(257) - # def test_palette2(self): - # rgb = list(range(256)) - # p = wx.Palette(len(rgb), rgb, rgb, rgb) - # self.assertTrue(p.IsOk()) + def test_paletteCtor2(self): + # and do the same using a list + rgb = list(range(256)) + p = wx.Palette(rgb, rgb, rgb) + self.assertTrue(p.IsOk()) + self.assertTrue(p.GetColoursCount() == 256) + self.assertTrue(p.GetPixel(42, 42, 42) == 42) + self.assertTrue(p.GetRGB(64) == (64, 64, 64)) + with self.assertRaises(IndexError): + p.GetRGB(257) + + + def test_paletteCtor3(self): + p = wx.Palette() + + + def test_paletteCtor4(self): + p1 = wx.Palette() + p2 = wx.Palette(p1) def test_paletteCreate1(self): p = wx.Palette() - with self.assertRaises(TypeError): + with self.assertRaises(TypeError): # not sequences p.Create(1, 2, 3) - with self.assertRaises(ValueError): + with self.assertRaises(ValueError): # not the same length p.Create((1, 2, 3), (1, 2, 3), (1, 2)) - with self.assertRaises(TypeError): + with self.assertRaises(TypeError): # not integers p.Create((1, 2, 3), ("1", "2", "3"), (1, 2, 3)) - with self.assertRaises(ValueError): + with self.assertRaises(ValueError): # outside of 0..255 range p.Create((257, 2, 3), (1, 2, 3), (1, 2, 3)) - with self.assertRaises(ValueError): + with self.assertRaises(ValueError): # outside of 0..255 range p.Create((1, 2, 3), (-1, 2, 3), (1, 2, 3)) + # range is okay, checking start and end + p.Create((1, 2, 3, 0), (1, 2, 3, 4), (1, 2, 3, 4)) + p.Create((1, 2, 3, 255), (1, 2, 3, 4), (1, 2, 3, 4)) + def test_paletteCreate2(self): p = wx.Palette() + # 3 different kinds of sequences :) self.assertTrue(p.Create((1, 2, 3), [4, 5, 6], bytearray([7, 8, 9]))) self.assertTrue(p.GetRGB(1) == (2, 5, 8))