#!/usr/bin/env python3
# Copyright: 2026, Maximiliano Curia <maxy@debian.org>
#
# License: ISC
#  Permission to use, copy, modify, and/or distribute this software for any
#  purpose with or without fee is hereby granted, provided that the above
#  copyright notice and this permission notice appear in all copies.
#  .
#  THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
#  REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
#  AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
#  INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
#  LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
#  OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
#  PERFORMANCE OF THIS SOFTWARE.

"""Tests for License class extraction to core_types.py"""

import unittest

import debian.copyright as dc

try:
    from decopy.common.core_types import License, LicenseRegistry
except ImportError as e:
    msg = "Cannot import License from core_types module"
    raise ImportError(msg) from e


class TestLicenseClass(unittest.TestCase):
    """Test the License value type in core_types (the registry lives in
    TestLicenseRegistry)."""

    def test_license_creation(self):
        """Test License class can be created with a name"""
        license_ = License("MIT")
        self.assertIsNotNone(license_)
        self.assertEqual(license_.name, "MIT")
        self.assertIsInstance(license_.name, str)
        self.assertIsInstance(license_, License)

    def test_license_stored_defaults_to_none(self):
        self.assertIsNone(License("MIT").stored)

    def test_license_string_representation(self):
        """str(License) is the bare identity (its name).

        Rendering the DEP-5 stanza is the output layer's job
        (renderer.Dep5Renderer.license_block), not the License value type's.
        """
        license_ = License("MIT")
        self.assertEqual(str(license_), "MIT")

    def test_license_name_property(self):
        """Test License name property works correctly"""
        license_ = License("BSD-3-Clause")
        self.assertEqual(license_.name, "BSD-3-Clause")

        # name is in __slots__, so it can be assigned but not property-set
        license_.name = "New Name"
        self.assertEqual(license_.name, "New Name")


class TestLicenseRegistry(unittest.TestCase):
    """Test the per-run LicenseRegistry."""

    def test_get_creates_named_license(self):
        registry = LicenseRegistry()
        license_ = registry.get("MIT")
        self.assertEqual(license_.name, "MIT")

    def test_get_is_idempotent_within_a_registry(self):
        registry = LicenseRegistry()
        self.assertIs(registry.get("GPL"), registry.get("GPL"))

    def test_registries_do_not_share_instances(self):
        first = LicenseRegistry()
        second = LicenseRegistry()
        self.assertIsNot(first.get("MIT"), second.get("MIT"))

    def test_get_preserves_stored(self):
        registry = LicenseRegistry()
        stored = dc.LicenseParagraph.create(
            dc.License(synopsis="MIT", text="license text"),
        )
        registry.get("MIT").stored = stored
        self.assertIs(registry.get("MIT").stored, stored)


if __name__ == "__main__":
    unittest.main()
