test_segment_mapper.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (c) 2014-18 Richard Hull and contributors
  4. # See LICENSE.rst for details.
  5. from luma.core.util import mutable_string
  6. from luma.led_matrix.segment_mapper import dot_muncher, regular
  7. def test_dot_muncher_without_dots():
  8. buf = mutable_string("Hello world")
  9. results = dot_muncher(buf, notfound='_')
  10. assert list(results) == [0x37, 0x6f, 0x06, 0x06, 0x1d, 0x00, 0x14, 0x1d, 0x05, 0x06, 0x3d]
  11. def test_dot_muncher_with_dot():
  12. buf = mutable_string("3.14159")
  13. results = dot_muncher(buf)
  14. assert list(results) == [0x79 | 0x80, 0x30, 0x33, 0x30, 0x5b, 0x7b]
  15. def test_dot_muncher_with_dot_at_end():
  16. buf = mutable_string(" 525920")
  17. buf[7:] = "0."
  18. print(buf)
  19. results = dot_muncher(buf)
  20. assert list(results) == [0x00, 0x00, 0x5b, 0x6d, 0x5b, 0x7b, 0x6d, 0x7e | 0x80]
  21. def test_dot_muncher_with_dot_at_start():
  22. buf = mutable_string(".PDF")
  23. results = dot_muncher(buf)
  24. assert list(results) == [0x80, 0x67, 0x7e, 0x47]
  25. def test_dot_muncher_with_multiple_dot():
  26. buf = mutable_string("127.0.0.1")
  27. results = dot_muncher(buf)
  28. assert list(results) == [0x30, 0x6d, 0x70 | 0x80, 0x7e | 0x80, 0x7e | 0x80, 0x30]
  29. def test_dot_muncher_with_consecutive_dot():
  30. buf = mutable_string("No...")
  31. results = dot_muncher(buf)
  32. assert list(results) == [0x76, 0x1d | 0x80, 0x80, 0x80]
  33. def test_dot_muncher_empty_buf():
  34. buf = mutable_string("")
  35. results = dot_muncher(buf)
  36. assert list(results) == []
  37. def test_dot_muncher_skips_unknown():
  38. buf = mutable_string("B&B")
  39. results = dot_muncher(buf, notfound=None)
  40. assert list(results) == [0x7f, 0x7f]
  41. def test_dot_muncher_with_notfound():
  42. buf = mutable_string("B&B")
  43. results = dot_muncher(buf, notfound='_')
  44. assert list(results) == [0x7f, 0x08, 0x7f]
  45. def test_regular_without_dots():
  46. buf = mutable_string("Hello world")
  47. results = regular(buf, notfound='_')
  48. assert list(results) == [0x37, 0x6f, 0x06, 0x06, 0x1d, 0x00, 0x14, 0x1d, 0x05, 0x06, 0x3d]
  49. def test_regular_with_dot():
  50. buf = mutable_string("3.14159")
  51. results = regular(buf)
  52. assert list(results) == [0x79, 0x80, 0x30, 0x33, 0x30, 0x5b, 0x7b]
  53. def test_regular_with_multiple_dot():
  54. buf = mutable_string("127.0.0.1")
  55. results = regular(buf)
  56. assert list(results) == [0x30, 0x6d, 0x70, 0x80, 0x7e, 0x80, 0x7e, 0x80, 0x30]
  57. def test_regular_empty_buf():
  58. buf = mutable_string("")
  59. results = regular(buf)
  60. assert list(results) == []
  61. def test_regular_skips_unknown():
  62. buf = mutable_string("B&B")
  63. results = regular(buf, notfound=None)
  64. assert list(results) == [0x7f, 0x7f]
  65. def test_regular_with_notfound():
  66. buf = mutable_string("B&B")
  67. results = regular(buf, notfound='_')
  68. assert list(results) == [0x7f, 0x08, 0x7f]
  69. def test_degrees_unicode():
  70. buf = mutable_string(u"29.12°C")
  71. results = dot_muncher(buf)
  72. assert list(results) == [0x6d, 0x7b | 0x80, 0x30, 0x6d, 0x63, 0x4e]
  73. def test_degrees_utf8():
  74. buf = mutable_string(u"29.12\xb0C")
  75. results = dot_muncher(buf)
  76. assert list(results) == [0x6d, 0x7b | 0x80, 0x30, 0x6d, 0x63, 0x4e]