/ tests / test_palette_c_interface.py
test_palette_c_interface.py
  1  # The MIT License (MIT)
  2  #
  3  # Copyright (c) 2019 Matt Land
  4  #
  5  # Permission is hereby granted, free of charge, to any person obtaining a copy
  6  # of this software and associated documentation files (the "Software"), to deal
  7  # in the Software without restriction, including without limitation the rights
  8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9  # copies of the Software, and to permit persons to whom the Software is
 10  # furnished to do so, subject to the following conditions:
 11  #
 12  # The above copyright notice and this permission notice shall be included in
 13  # all copies or substantial portions of the Software.
 14  #
 15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21  # THE SOFTWARE.
 22  """
 23  `adafruit_imageload.tests.test_palette_c_interface`
 24  ====================================================
 25  
 26  These tests are to validate the displayio_shared_bindings classes that other tests are built on.
 27  
 28  * Author(s):  Matt Land
 29  
 30  """
 31  from unittest import TestCase
 32  from .displayio_shared_bindings import Palette_C_Interface
 33  
 34  
 35  class TestPalette_C_Interface(TestCase):
 36      def test_init_mono(self):
 37          Palette_C_Interface(1)
 38  
 39      def test_init_color(self):
 40          Palette_C_Interface(256)
 41  
 42      def test_set_int(self):
 43          palette = Palette_C_Interface(1)
 44          palette[0] = 0xFFFFFF
 45  
 46      def test_get_int(self):
 47          palette = Palette_C_Interface(1)
 48          palette[0] = 0xFFFFFF
 49          self.assertEqual(0xFFFFFF, palette[0])
 50  
 51      def test_set_byte(self):
 52          palette = Palette_C_Interface(1)
 53          palette[0] = b"\xFF\xFF\xFF"
 54  
 55      def test_get_byte(self):
 56          palette = Palette_C_Interface(1)
 57          palette[0] = b"\xFF\xFF\xFF"
 58          self.assertEqual(b"\xFF\xFF\xFF", palette[0])
 59  
 60      def test_set_bytearray(self):
 61          palette = Palette_C_Interface(1)
 62          palette[0] = bytearray(b"\xFF\xFF\xFF")
 63  
 64      def test_prevents_out_of_range(self):
 65          palette = Palette_C_Interface(1)
 66          try:
 67              palette[1] = 0xFFFFFF
 68              self.fail("exception should have already thrown")
 69          except ValueError as e:
 70              if "greater than allowed" not in str(e):
 71                  raise
 72  
 73      def test_prevents_set_non_allowed(self):
 74          palette = Palette_C_Interface(1)
 75          try:
 76              palette[0] = "\xFF\xFF\xFF"  # attempt with a string, which is not allowed
 77              self.fail("exception should have thrown")
 78          except ValueError as e:
 79              if "should be" not in str(e):
 80                  raise
 81  
 82      def test_validate_success(self):
 83          palette = Palette_C_Interface(1)
 84          palette[0] = b"\xFF\xFF\xFF"
 85          palette.validate()
 86  
 87      def test_validate_fails(self):
 88          palette = Palette_C_Interface(2)
 89          palette[1] = b"\xFF\xFF\xFF"
 90          try:
 91              palette.validate()
 92              self.fail("exception should have thrown")
 93          except IndexError as e:
 94              if "palette was initialized" not in str(e):
 95                  raise
 96  
 97      def test_str(self):
 98          palette = Palette_C_Interface(1)
 99          palette[0] = b"\xFF\xFF\xFF"
100          print(str(palette))