| 1 |
# (C) Copyright 2007-2025 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
""" Tests for extension points. """ |
| 11 |
|
| 12 |
# Standard library imports. |
| 13 |
import unittest |
| 14 |
|
| 15 |
from traits.api import HasTraits, Int, List, TraitError |
| 16 |
|
| 17 |
# Enthought library imports. |
| 18 |
from envisage.api import Application, ExtensionPoint, ExtensionRegistry |
| 19 |
|
| 20 |
|
| 21 |
class HasExtensionPoints(HasTraits): |
| 22 |
"""Base class for all test classes that use the 'ExtensionPoint' type.""" |
| 23 |
|
| 24 |
extension_registry = None |
| 25 |
|
| 26 |
|
| 27 |
class ExtensionPointTestCase(unittest.TestCase): |
| 28 |
"""Tests for extension points.""" |
| 29 |
|
| 30 |
def setUp(self): |
| 31 |
"""Prepares the test fixture before each test method is called.""" |
| 32 |
|
| 33 |
# We do all of the testing via the application to make sure it offers |
| 34 |
# the same interface! |
| 35 |
self.registry = Application(extension_registry=ExtensionRegistry()) |
| 36 |
|
| 37 |
# Set the extension registry used by the test classes. |
| 38 |
HasExtensionPoints.extension_registry = self.registry |
| 39 |
|
| 40 |
def test_invalid_extension_point_type(self): |
| 41 |
"""invalid extension point type""" |
| 42 |
|
| 43 |
# Extension points currently have to be 'List's of something! The |
| 44 |
# default is a list of anything. |
| 45 |
with self.assertRaises(TypeError): |
| 46 |
ExtensionPoint(Int, "my.ep") |
| 47 |
|
| 48 |
def test_no_reference_to_extension_registry(self): |
| 49 |
"""no reference to extension registry""" |
| 50 |
|
| 51 |
registry = self.registry |
| 52 |
|
| 53 |
# Add an extension point. |
| 54 |
registry.add_extension_point(self._create_extension_point("my.ep")) |
| 55 |
|
| 56 |
# Set the extensions. |
| 57 |
registry.set_extensions("my.ep", "xxx") |
| 58 |
|
| 59 |
# Declare a class that consumes the extension. |
| 60 |
class Foo(HasTraits): |
| 61 |
x = ExtensionPoint(List(Int), id="my.ep") |
| 62 |
|
| 63 |
# We should get an exception because the object does not have an |
| 64 |
# 'extension_registry' trait. |
| 65 |
f = Foo() |
| 66 |
with self.assertRaises(ValueError): |
| 67 |
getattr(f, "x") |
| 68 |
|
| 69 |
def test_extension_point_changed(self): |
| 70 |
"""extension point changed""" |
| 71 |
|
| 72 |
registry = self.registry |
| 73 |
|
| 74 |
# Add an extension point. |
| 75 |
registry.add_extension_point(self._create_extension_point("my.ep")) |
| 76 |
|
| 77 |
# Declare a class that consumes the extension. |
| 78 |
class Foo(HasExtensionPoints): |
| 79 |
x = ExtensionPoint(id="my.ep") |
| 80 |
|
| 81 |
def _x_changed(self): |
| 82 |
"""Static trait change handler.""" |
| 83 |
|
| 84 |
self.x_changed_called = True |
| 85 |
|
| 86 |
f = Foo() |
| 87 |
|
| 88 |
# Connect the extension points on the object so that it can listen |
| 89 |
# for changes. |
| 90 |
ExtensionPoint.connect_extension_point_traits(f) |
| 91 |
|
| 92 |
# Set the extensions. |
| 93 |
registry.set_extensions("my.ep", [42, "a string", True]) |
| 94 |
|
| 95 |
# Make sure that instances of the class pick up the extensions. |
| 96 |
self.assertEqual(3, len(f.x)) |
| 97 |
self.assertEqual([42, "a string", True], f.x) |
| 98 |
|
| 99 |
# Make sure the trait change handler was called. |
| 100 |
self.assertTrue(f.x_changed_called) |
| 101 |
|
| 102 |
# Reset the change handler flag. |
| 103 |
f.x_changed_called = False |
| 104 |
|
| 105 |
# Disconnect the extension points on the object. |
| 106 |
ExtensionPoint.disconnect_extension_point_traits(f) |
| 107 |
|
| 108 |
# Set the extensions. |
| 109 |
registry.set_extensions("my.ep", [98, 99, 100]) |
| 110 |
|
| 111 |
# Make sure the trait change handler was *not* called. |
| 112 |
self.assertEqual(False, f.x_changed_called) |
| 113 |
|
| 114 |
def test_mutate_extension_point_no_effect(self): |
| 115 |
"""Extension point is recomputed so mutation has no effect.""" |
| 116 |
|
| 117 |
registry = self.registry |
| 118 |
|
| 119 |
# Add an extension point. |
| 120 |
registry.add_extension_point(self._create_extension_point("my.ep")) |
| 121 |
|
| 122 |
# Set the extensions. |
| 123 |
registry.set_extensions("my.ep", [1, 2, 3]) |
| 124 |
|
| 125 |
# Declare a class that consumes the extension. |
| 126 |
class Foo(HasExtensionPoints): |
| 127 |
x = ExtensionPoint(List(Int), id="my.ep") |
| 128 |
|
| 129 |
# when |
| 130 |
f = Foo() |
| 131 |
f.x.append(42) |
| 132 |
|
| 133 |
# then |
| 134 |
# The registry is not changed, and the extension point is still the |
| 135 |
# same as before |
| 136 |
self.assertEqual(registry.get_extensions("my.ep"), [1, 2, 3]) |
| 137 |
self.assertEqual(f.x, [1, 2, 3]) |
| 138 |
|
| 139 |
def test_untyped_extension_point(self): |
| 140 |
"""untyped extension point""" |
| 141 |
|
| 142 |
registry = self.registry |
| 143 |
|
| 144 |
# Add an extension point. |
| 145 |
registry.add_extension_point(self._create_extension_point("my.ep")) |
| 146 |
|
| 147 |
# Set the extensions. |
| 148 |
registry.set_extensions("my.ep", [42, "a string", True]) |
| 149 |
|
| 150 |
# Declare a class that consumes the extension. |
| 151 |
class Foo(HasExtensionPoints): |
| 152 |
x = ExtensionPoint(id="my.ep") |
| 153 |
|
| 154 |
# Make sure that instances of the class pick up the extensions. |
| 155 |
f = Foo() |
| 156 |
self.assertEqual(3, len(f.x)) |
| 157 |
self.assertEqual([42, "a string", True], f.x) |
| 158 |
|
| 159 |
g = Foo() |
| 160 |
self.assertEqual(3, len(g.x)) |
| 161 |
self.assertEqual([42, "a string", True], g.x) |
| 162 |
|
| 163 |
def test_typed_extension_point(self): |
| 164 |
"""typed extension point""" |
| 165 |
|
| 166 |
registry = self.registry |
| 167 |
|
| 168 |
# Add an extension point. |
| 169 |
registry.add_extension_point(self._create_extension_point("my.ep")) |
| 170 |
|
| 171 |
# Set the extensions. |
| 172 |
registry.set_extensions("my.ep", [42, 43, 44]) |
| 173 |
|
| 174 |
# Declare a class that consumes the extension. |
| 175 |
class Foo(HasExtensionPoints): |
| 176 |
x = ExtensionPoint(List(Int), id="my.ep") |
| 177 |
|
| 178 |
# Make sure that instances of the class pick up the extensions. |
| 179 |
f = Foo() |
| 180 |
self.assertEqual(3, len(f.x)) |
| 181 |
self.assertEqual([42, 43, 44], f.x) |
| 182 |
|
| 183 |
g = Foo() |
| 184 |
self.assertEqual(3, len(g.x)) |
| 185 |
self.assertEqual([42, 43, 44], g.x) |
| 186 |
|
| 187 |
def test_invalid_extension_point(self): |
| 188 |
"""invalid extension point""" |
| 189 |
|
| 190 |
registry = self.registry |
| 191 |
|
| 192 |
# Add an extension point. |
| 193 |
registry.add_extension_point(self._create_extension_point("my.ep")) |
| 194 |
|
| 195 |
# Set the extensions. |
| 196 |
registry.set_extensions("my.ep", "xxx") |
| 197 |
|
| 198 |
# Declare a class that consumes the extension. |
| 199 |
class Foo(HasExtensionPoints): |
| 200 |
x = ExtensionPoint(List(Int), id="my.ep") |
| 201 |
|
| 202 |
# Make sure we get a trait error because the type of the extension |
| 203 |
# doesn't match that of the extension point. |
| 204 |
f = Foo() |
| 205 |
with self.assertRaises(TraitError): |
| 206 |
getattr(f, "x") |
| 207 |
|
| 208 |
def test_extension_point_with_no_id(self): |
| 209 |
"""extension point with no Id""" |
| 210 |
|
| 211 |
def factory(): |
| 212 |
class Foo(HasExtensionPoints): |
| 213 |
x = ExtensionPoint(List(Int)) |
| 214 |
|
| 215 |
with self.assertRaises(ValueError): |
| 216 |
factory() |
| 217 |
|
| 218 |
def test_set_untyped_extension_point(self): |
| 219 |
"""set untyped extension point""" |
| 220 |
|
| 221 |
registry = self.registry |
| 222 |
|
| 223 |
# Add an extension point. |
| 224 |
registry.add_extension_point(self._create_extension_point("my.ep")) |
| 225 |
|
| 226 |
# Declare a class that consumes the extension. |
| 227 |
class Foo(HasExtensionPoints): |
| 228 |
x = ExtensionPoint(id="my.ep") |
| 229 |
|
| 230 |
# Make sure that when we set the trait the extension registry gets |
| 231 |
# updated. |
| 232 |
f = Foo() |
| 233 |
f.x = [42] |
| 234 |
|
| 235 |
self.assertEqual([42], registry.get_extensions("my.ep")) |
| 236 |
|
| 237 |
def test_set_typed_extension_point(self): |
| 238 |
"""set typed extension point""" |
| 239 |
|
| 240 |
registry = self.registry |
| 241 |
|
| 242 |
# Add an extension point. |
| 243 |
registry.add_extension_point(self._create_extension_point("my.ep")) |
| 244 |
|
| 245 |
# Declare a class that consumes the extension. |
| 246 |
class Foo(HasExtensionPoints): |
| 247 |
x = ExtensionPoint(List(Int), id="my.ep") |
| 248 |
|
| 249 |
# Make sure that when we set the trait the extension registry gets |
| 250 |
# updated. |
| 251 |
f = Foo() |
| 252 |
f.x = [42] |
| 253 |
|
| 254 |
self.assertEqual([42], registry.get_extensions("my.ep")) |
| 255 |
|
| 256 |
def test_extension_point_str_representation(self): |
| 257 |
"""test the string representation of the extension point""" |
| 258 |
ep_repr = "ExtensionPoint(id={!r})" |
| 259 |
ep = self._create_extension_point("my.ep") |
| 260 |
self.assertEqual(ep_repr.format("my.ep"), str(ep)) |
| 261 |
self.assertEqual(ep_repr.format("my.ep"), repr(ep)) |
| 262 |
|
| 263 |
########################################################################### |
| 264 |
# Private interface. |
| 265 |
########################################################################### |
| 266 |
|
| 267 |
def _create_extension_point(self, id, trait_type=List, desc=""): |
| 268 |
"""Create an extension point.""" |
| 269 |
|
| 270 |
return ExtensionPoint(id=id, trait_type=trait_type, desc=desc) |