aas-core-codegen 0.0.16__py3-none-any.whl → 0.0.17__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,9 +4,68 @@ import textwrap
4
4
  import unittest
5
5
 
6
6
  import tests.common
7
+ from aas_core_codegen import intermediate
7
8
  from aas_core_codegen.common import Identifier
8
9
 
9
10
 
11
+ class TestMappingOfPrimitiveTypesProperlyExposed(unittest.TestCase):
12
+ def test_primitive_type_to_python_type(self) -> None:
13
+ self.assertEqual(
14
+ intermediate.PRIMITIVE_TYPE_TO_PYTHON_TYPE[intermediate.PrimitiveType.BOOL],
15
+ bool,
16
+ )
17
+
18
+ self.assertEqual(
19
+ intermediate.PRIMITIVE_TYPE_TO_PYTHON_TYPE[intermediate.PrimitiveType.INT],
20
+ int,
21
+ )
22
+
23
+ self.assertEqual(
24
+ intermediate.PRIMITIVE_TYPE_TO_PYTHON_TYPE[
25
+ intermediate.PrimitiveType.FLOAT
26
+ ],
27
+ float,
28
+ )
29
+
30
+ self.assertEqual(
31
+ intermediate.PRIMITIVE_TYPE_TO_PYTHON_TYPE[intermediate.PrimitiveType.STR],
32
+ str,
33
+ )
34
+
35
+ self.assertEqual(
36
+ intermediate.PRIMITIVE_TYPE_TO_PYTHON_TYPE[
37
+ intermediate.PrimitiveType.BYTEARRAY
38
+ ],
39
+ bytearray,
40
+ )
41
+
42
+ def test_python_type_to_primitive_type(self) -> None:
43
+ self.assertEqual(
44
+ intermediate.PYTHON_TYPE_TO_PRIMITIVE_TYPE[bool],
45
+ intermediate.PrimitiveType.BOOL,
46
+ )
47
+
48
+ self.assertEqual(
49
+ intermediate.PYTHON_TYPE_TO_PRIMITIVE_TYPE[int],
50
+ intermediate.PrimitiveType.INT,
51
+ )
52
+
53
+ self.assertEqual(
54
+ intermediate.PYTHON_TYPE_TO_PRIMITIVE_TYPE[float],
55
+ intermediate.PrimitiveType.FLOAT,
56
+ )
57
+
58
+ self.assertEqual(
59
+ intermediate.PYTHON_TYPE_TO_PRIMITIVE_TYPE[str],
60
+ intermediate.PrimitiveType.STR,
61
+ )
62
+
63
+ self.assertEqual(
64
+ intermediate.PYTHON_TYPE_TO_PRIMITIVE_TYPE[bytearray],
65
+ intermediate.PrimitiveType.BYTEARRAY,
66
+ )
67
+
68
+
10
69
  class TestIsSubclassOf(unittest.TestCase):
11
70
  def test_no_inheritances(self) -> None:
12
71
  source = textwrap.dedent(
@@ -165,5 +224,400 @@ class TestIsSubclassOf(unittest.TestCase):
165
224
  self.assertFalse(concrete.is_subclass_of(cls=another_concrete))
166
225
 
167
226
 
227
+ class TestMustFindConstant(unittest.TestCase):
228
+ def test_empty(self) -> None:
229
+ source = """\
230
+ __version__ = "dummy"
231
+ __xml_namespace__ = "https://dummy.com"
232
+ """
233
+ symbol_table, error = tests.common.translate_source_to_intermediate(
234
+ source=source
235
+ )
236
+ assert error is None, tests.common.most_underlying_messages(error)
237
+ assert symbol_table is not None
238
+
239
+ with self.assertRaises(KeyError):
240
+ _ = symbol_table.must_find_constant(Identifier("Something"))
241
+
242
+ def test_constant(self) -> None:
243
+ source = """\
244
+ Something: int = constant_int(value=1984)
245
+
246
+ __version__ = "dummy"
247
+ __xml_namespace__ = "https://dummy.com"
248
+ """
249
+ symbol_table, error = tests.common.translate_source_to_intermediate(
250
+ source=source
251
+ )
252
+ assert error is None, tests.common.most_underlying_messages(error)
253
+ assert symbol_table is not None
254
+
255
+ result = symbol_table.must_find_constant(Identifier("Something"))
256
+ assert isinstance(result, intermediate.Constant)
257
+
258
+ def test_constant_primitive(self) -> None:
259
+ source = """\
260
+ Something: int = constant_int(value=1984)
261
+
262
+ __version__ = "dummy"
263
+ __xml_namespace__ = "https://dummy.com"
264
+ """
265
+ symbol_table, error = tests.common.translate_source_to_intermediate(
266
+ source=source
267
+ )
268
+ assert error is None, tests.common.most_underlying_messages(error)
269
+ assert symbol_table is not None
270
+
271
+ result = symbol_table.must_find_constant_primitive(Identifier("Something"))
272
+ assert isinstance(result, intermediate.ConstantPrimitive)
273
+
274
+ with self.assertRaises(TypeError):
275
+ _ = symbol_table.must_find_constant_set_of_primitives(
276
+ Identifier("Something")
277
+ )
278
+
279
+ with self.assertRaises(TypeError):
280
+ _ = symbol_table.must_find_constant_set_of_enumeration_literals(
281
+ Identifier("Something")
282
+ )
283
+
284
+ def test_constant_set_of_primitives(self) -> None:
285
+ source = """\
286
+ Something: Set[str] = constant_set(
287
+ values=["hello", "world"]
288
+ )
289
+
290
+ __version__ = "dummy"
291
+ __xml_namespace__ = "https://dummy.com"
292
+ """
293
+ symbol_table, error = tests.common.translate_source_to_intermediate(
294
+ source=source
295
+ )
296
+ assert error is None, tests.common.most_underlying_messages(error)
297
+ assert symbol_table is not None
298
+
299
+ result = symbol_table.must_find_constant_set_of_primitives(
300
+ Identifier("Something")
301
+ )
302
+ assert isinstance(result, intermediate.ConstantSetOfPrimitives)
303
+
304
+ with self.assertRaises(TypeError):
305
+ _ = symbol_table.must_find_constant_primitive(Identifier("Something"))
306
+
307
+ with self.assertRaises(TypeError):
308
+ _ = symbol_table.must_find_constant_set_of_enumeration_literals(
309
+ Identifier("Something")
310
+ )
311
+
312
+ def test_constant_set_of_enumeration_literals(self) -> None:
313
+ source = """\
314
+ class SomeEnum(Enum):
315
+ Some_literal = "SOME-LITERAL"
316
+ Another_literal = "ANOTHER-LITERAL"
317
+ Yet_another_literal = "YET-ANOTHER-LITERAL"
318
+
319
+
320
+ Something: Set[SomeEnum] = constant_set(
321
+ values=[SomeEnum.Some_literal, SomeEnum.Another_literal]
322
+ )
323
+
324
+ __version__ = "dummy"
325
+ __xml_namespace__ = "https://dummy.com"
326
+ """
327
+ symbol_table, error = tests.common.translate_source_to_intermediate(
328
+ source=source
329
+ )
330
+ assert error is None, tests.common.most_underlying_messages(error)
331
+ assert symbol_table is not None
332
+
333
+ result = symbol_table.must_find_constant_set_of_enumeration_literals(
334
+ Identifier("Something")
335
+ )
336
+ assert isinstance(result, intermediate.ConstantSetOfEnumerationLiterals)
337
+
338
+ with self.assertRaises(TypeError):
339
+ _ = symbol_table.must_find_constant_primitive(Identifier("Something"))
340
+
341
+ with self.assertRaises(TypeError):
342
+ _ = symbol_table.must_find_constant_set_of_primitives(
343
+ Identifier("Something")
344
+ )
345
+
346
+
347
+ class TestIsEnumerationLiteralOf(unittest.TestCase):
348
+ def test_enumeration_literal_in_enumeration(self) -> None:
349
+ source = """\
350
+ class SomeEnum(Enum):
351
+ Some_literal = "SOME-LITERAL"
352
+ Another_literal = "ANOTHER-LITERAL"
353
+
354
+ __version__ = "dummy"
355
+ __xml_namespace__ = "https://dummy.com"
356
+ """
357
+ symbol_table, error = tests.common.translate_source_to_intermediate(
358
+ source=source
359
+ )
360
+ assert error is None, tests.common.most_underlying_messages(error)
361
+ assert symbol_table is not None
362
+
363
+ some_enum = symbol_table.must_find_enumeration(Identifier("SomeEnum"))
364
+ some_literal = some_enum.literals_by_name["Some_literal"]
365
+ another_literal = some_enum.literals_by_name["Another_literal"]
366
+
367
+ self.assertTrue(
368
+ symbol_table.is_enumeration_literal_of(
369
+ literal=some_literal,
370
+ enumeration_or_constant_set_name=Identifier("SomeEnum"),
371
+ )
372
+ )
373
+ self.assertTrue(
374
+ symbol_table.is_enumeration_literal_of(
375
+ literal=another_literal,
376
+ enumeration_or_constant_set_name=Identifier("SomeEnum"),
377
+ )
378
+ )
379
+
380
+ def test_enumeration_literal_not_in_different_enumeration(self) -> None:
381
+ source = """\
382
+ class SomeEnum(Enum):
383
+ Some_literal = "SOME-LITERAL"
384
+
385
+ class AnotherEnum(Enum):
386
+ Different_literal = "DIFFERENT-LITERAL"
387
+
388
+ __version__ = "dummy"
389
+ __xml_namespace__ = "https://dummy.com"
390
+ """
391
+ symbol_table, error = tests.common.translate_source_to_intermediate(
392
+ source=source
393
+ )
394
+ assert error is None, tests.common.most_underlying_messages(error)
395
+ assert symbol_table is not None
396
+
397
+ some_enum = symbol_table.must_find_enumeration(Identifier("SomeEnum"))
398
+ another_enum = symbol_table.must_find_enumeration(Identifier("AnotherEnum"))
399
+
400
+ some_literal = some_enum.literals_by_name["Some_literal"]
401
+ different_literal = another_enum.literals_by_name["Different_literal"]
402
+
403
+ self.assertFalse(
404
+ symbol_table.is_enumeration_literal_of(
405
+ literal=some_literal,
406
+ enumeration_or_constant_set_name=Identifier("AnotherEnum"),
407
+ )
408
+ )
409
+ self.assertFalse(
410
+ symbol_table.is_enumeration_literal_of(
411
+ literal=different_literal,
412
+ enumeration_or_constant_set_name=Identifier("SomeEnum"),
413
+ )
414
+ )
415
+
416
+ def test_enumeration_literal_in_constant_set(self) -> None:
417
+ source = """\
418
+ class SomeEnum(Enum):
419
+ Some_literal = "SOME-LITERAL"
420
+ Another_literal = "ANOTHER-LITERAL"
421
+ Yet_another_literal = "YET-ANOTHER-LITERAL"
422
+
423
+ SomeSet: Set[SomeEnum] = constant_set(
424
+ values=[SomeEnum.Some_literal, SomeEnum.Another_literal]
425
+ )
426
+
427
+ __version__ = "dummy"
428
+ __xml_namespace__ = "https://dummy.com"
429
+ """
430
+ symbol_table, error = tests.common.translate_source_to_intermediate(
431
+ source=source
432
+ )
433
+ assert error is None, tests.common.most_underlying_messages(error)
434
+ assert symbol_table is not None
435
+
436
+ some_enum = symbol_table.must_find_enumeration(Identifier("SomeEnum"))
437
+ some_literal = some_enum.literals_by_name["Some_literal"]
438
+ another_literal = some_enum.literals_by_name["Another_literal"]
439
+ yet_another_literal = some_enum.literals_by_name["Yet_another_literal"]
440
+
441
+ # NOTE (mristin):
442
+ # We check here for membership.
443
+ self.assertTrue(
444
+ symbol_table.is_enumeration_literal_of(
445
+ literal=some_literal,
446
+ enumeration_or_constant_set_name=Identifier("SomeSet"),
447
+ )
448
+ )
449
+ self.assertTrue(
450
+ symbol_table.is_enumeration_literal_of(
451
+ literal=another_literal,
452
+ enumeration_or_constant_set_name=Identifier("SomeSet"),
453
+ )
454
+ )
455
+
456
+ # NOTE (mristin):
457
+ # We check here for out-of-membership.
458
+ self.assertFalse(
459
+ symbol_table.is_enumeration_literal_of(
460
+ literal=yet_another_literal,
461
+ enumeration_or_constant_set_name=Identifier("SomeSet"),
462
+ )
463
+ )
464
+
465
+ def test_type_error_for_concrete_class(self) -> None:
466
+ source = """\
467
+ class SomeEnum(Enum):
468
+ Some_literal = "SOME-LITERAL"
469
+
470
+ class SomeClass:
471
+ pass
472
+
473
+ __version__ = "dummy"
474
+ __xml_namespace__ = "https://dummy.com"
475
+ """
476
+ symbol_table, error = tests.common.translate_source_to_intermediate(
477
+ source=source
478
+ )
479
+ assert error is None, tests.common.most_underlying_messages(error)
480
+ assert symbol_table is not None
481
+
482
+ some_enum = symbol_table.must_find_enumeration(Identifier("SomeEnum"))
483
+ some_literal = some_enum.literals_by_name["Some_literal"]
484
+
485
+ with self.assertRaises(TypeError):
486
+ _ = symbol_table.is_enumeration_literal_of(
487
+ literal=some_literal,
488
+ enumeration_or_constant_set_name=Identifier("SomeClass"),
489
+ )
490
+
491
+ def test_type_error_for_constant_primitive(self) -> None:
492
+ source = """\
493
+ class SomeEnum(Enum):
494
+ Some_literal = "SOME-LITERAL"
495
+
496
+ SomeConstant: int = constant_int(value=42)
497
+
498
+ __version__ = "dummy"
499
+ __xml_namespace__ = "https://dummy.com"
500
+ """
501
+ symbol_table, error = tests.common.translate_source_to_intermediate(
502
+ source=source
503
+ )
504
+ assert error is None, tests.common.most_underlying_messages(error)
505
+ assert symbol_table is not None
506
+
507
+ some_enum = symbol_table.must_find_enumeration(Identifier("SomeEnum"))
508
+ some_literal = some_enum.literals_by_name["Some_literal"]
509
+
510
+ with self.assertRaises(TypeError):
511
+ _ = symbol_table.is_enumeration_literal_of(
512
+ literal=some_literal,
513
+ enumeration_or_constant_set_name=Identifier("SomeConstant"),
514
+ )
515
+
516
+ def test_type_error_for_constant_set_of_primitives(self) -> None:
517
+ source = """\
518
+ class SomeEnum(Enum):
519
+ Some_literal = "SOME-LITERAL"
520
+
521
+ SomeSet: Set[str] = constant_set(
522
+ values=["hello", "world"]
523
+ )
524
+
525
+ __version__ = "dummy"
526
+ __xml_namespace__ = "https://dummy.com"
527
+ """
528
+ symbol_table, error = tests.common.translate_source_to_intermediate(
529
+ source=source
530
+ )
531
+ assert error is None, tests.common.most_underlying_messages(error)
532
+ assert symbol_table is not None
533
+
534
+ some_enum = symbol_table.must_find_enumeration(Identifier("SomeEnum"))
535
+ some_literal = some_enum.literals_by_name["Some_literal"]
536
+
537
+ # Test that TypeError is raised when name refers to a constant set of primitives
538
+ with self.assertRaises(TypeError):
539
+ _ = symbol_table.is_enumeration_literal_of(
540
+ literal=some_literal,
541
+ enumeration_or_constant_set_name=Identifier("SomeSet"),
542
+ )
543
+
544
+
545
+ class TestLiteralValueSet(unittest.TestCase):
546
+ def test_enumeration(self) -> None:
547
+ source = """\
548
+ class SomeEnum(Enum):
549
+ Some_literal = "SOME-LITERAL"
550
+ Another_literal = "ANOTHER-LITERAL"
551
+ Yet_another_literal = "YET-ANOTHER-LITERAL"
552
+
553
+ __version__ = "dummy"
554
+ __xml_namespace__ = "https://dummy.com"
555
+ """
556
+ symbol_table, error = tests.common.translate_source_to_intermediate(
557
+ source=source
558
+ )
559
+ assert error is None, tests.common.most_underlying_messages(error)
560
+ assert symbol_table is not None
561
+
562
+ some_enum = symbol_table.must_find_enumeration(Identifier("SomeEnum"))
563
+
564
+ expected_literal_values = frozenset(
565
+ ["SOME-LITERAL", "ANOTHER-LITERAL", "YET-ANOTHER-LITERAL"]
566
+ )
567
+
568
+ self.assertEqual(expected_literal_values, some_enum.literal_value_set)
569
+
570
+ def test_constant_set_of_primitives(self) -> None:
571
+ source = """\
572
+ SomeSet: Set[str] = constant_set(
573
+ values=["hello", "world", "test"]
574
+ )
575
+
576
+ __version__ = "dummy"
577
+ __xml_namespace__ = "https://dummy.com"
578
+ """
579
+ symbol_table, error = tests.common.translate_source_to_intermediate(
580
+ source=source
581
+ )
582
+ assert error is None, tests.common.most_underlying_messages(error)
583
+ assert symbol_table is not None
584
+
585
+ some_set = symbol_table.must_find_constant_set_of_primitives(
586
+ Identifier("SomeSet")
587
+ )
588
+
589
+ expected_literal_values = frozenset(["hello", "world", "test"])
590
+
591
+ self.assertEqual(expected_literal_values, some_set.literal_value_set)
592
+
593
+ def test_constant_set_of_enumeration_literals(self) -> None:
594
+ source = """\
595
+ class SomeEnum(Enum):
596
+ Some_literal = "SOME-LITERAL"
597
+ Another_literal = "ANOTHER-LITERAL"
598
+ Yet_another_literal = "YET-ANOTHER-LITERAL"
599
+
600
+ SomeSet: Set[SomeEnum] = constant_set(
601
+ values=[SomeEnum.Some_literal, SomeEnum.Another_literal]
602
+ )
603
+
604
+ __version__ = "dummy"
605
+ __xml_namespace__ = "https://dummy.com"
606
+ """
607
+ symbol_table, error = tests.common.translate_source_to_intermediate(
608
+ source=source
609
+ )
610
+ assert error is None, tests.common.most_underlying_messages(error)
611
+ assert symbol_table is not None
612
+
613
+ some_set = symbol_table.must_find_constant_set_of_enumeration_literals(
614
+ Identifier("SomeSet")
615
+ )
616
+
617
+ expected_literal_values = frozenset(["SOME-LITERAL", "ANOTHER-LITERAL"])
618
+
619
+ self.assertEqual(expected_literal_values, some_set.literal_value_set)
620
+
621
+
168
622
  if __name__ == "__main__":
169
623
  unittest.main()