Files
esphome/tests/unit_tests/test_cpp_helpers.py
J. Nick Koston a1d91ac779
Some checks failed
CI / Create common environment (push) Has been cancelled
CI / Check pylint (push) Has been cancelled
CI / Run script/ci-custom (push) Has been cancelled
CI / Run pytest (macOS-latest, 3.11) (push) Has been cancelled
CI / Run pytest (macOS-latest, 3.14) (push) Has been cancelled
CI / Run pytest (ubuntu-latest, 3.11) (push) Has been cancelled
CI / Run pytest (ubuntu-latest, 3.13) (push) Has been cancelled
CI / Run pytest (ubuntu-latest, 3.14) (push) Has been cancelled
CI / Run pytest (windows-latest, 3.11) (push) Has been cancelled
CI / Run pytest (windows-latest, 3.14) (push) Has been cancelled
CI / Determine which jobs to run (push) Has been cancelled
CI / Run integration tests (push) Has been cancelled
CI / Run C++ unit tests (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 IDF (push) Has been cancelled
CI / Run script/clang-tidy for ESP8266 (push) Has been cancelled
CI / Run script/clang-tidy for ZEPHYR (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 1/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 2/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 3/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 4/4 (push) Has been cancelled
CI / Test components batch (${{ matrix.components }}) (push) Has been cancelled
CI / pre-commit.ci lite (push) Has been cancelled
CI / Build target branch for memory impact (push) Has been cancelled
CI / Build PR branch for memory impact (push) Has been cancelled
CI / Comment memory impact (push) Has been cancelled
CI / CI Status (push) Has been cancelled
Stale / stale (push) Has been cancelled
Lock closed issues and PRs / lock (push) Has been cancelled
[core] Compile-time detection of loop() overrides (#14405)
2026-03-02 06:59:23 -10:00

81 lines
2.2 KiB
Python

from unittest.mock import Mock
import pytest
from esphome import const, cpp_helpers as ch
@pytest.mark.asyncio
async def test_gpio_pin_expression__conf_is_none(monkeypatch):
actual = await ch.gpio_pin_expression(None)
assert actual is None
@pytest.mark.asyncio
async def test_register_component(monkeypatch):
base_mock = Mock()
base_mock.__str__ = lambda self: "foo.bar"
base_mock.type = Mock()
base_mock.type.__str__ = lambda self: "foo::Bar"
var = Mock(base=base_mock)
app_mock = Mock(register_component_=Mock(return_value=var))
monkeypatch.setattr(ch, "App", app_mock)
core_mock = Mock(component_ids=["foo.bar"])
monkeypatch.setattr(ch, "CORE", core_mock)
add_mock = Mock()
monkeypatch.setattr(ch, "add", add_mock)
actual = await ch.register_component(var, {})
assert actual is var
assert add_mock.call_count == 2
app_mock.register_component_.assert_called_with(var)
assert core_mock.component_ids == []
@pytest.mark.asyncio
async def test_register_component__no_component_id(monkeypatch):
var = Mock(base="foo.eek")
core_mock = Mock(component_ids=["foo.bar"])
monkeypatch.setattr(ch, "CORE", core_mock)
with pytest.raises(ValueError, match="Component ID foo.eek was not declared to"):
await ch.register_component(var, {})
@pytest.mark.asyncio
async def test_register_component__with_setup_priority(monkeypatch):
base_mock = Mock()
base_mock.__str__ = lambda self: "foo.bar"
base_mock.type = Mock()
base_mock.type.__str__ = lambda self: "foo::Bar"
var = Mock(base=base_mock)
app_mock = Mock(register_component_=Mock(return_value=var))
monkeypatch.setattr(ch, "App", app_mock)
core_mock = Mock(component_ids=["foo.bar"])
monkeypatch.setattr(ch, "CORE", core_mock)
add_mock = Mock()
monkeypatch.setattr(ch, "add", add_mock)
actual = await ch.register_component(
var,
{
const.CONF_SETUP_PRIORITY: "123",
const.CONF_UPDATE_INTERVAL: "456",
},
)
assert actual is var
add_mock.assert_called()
assert add_mock.call_count == 4
app_mock.register_component_.assert_called_with(var)
assert core_mock.component_ids == []