esphome/tests/unit_tests/test_cpp_helpers.py
Tim Savage d447548893
Tests for CPP Code generation and some Python3 improvements (#961)
* Basic pytest configuration

* Added unit_test script that triggers pytest

* Changed "fixtures" to fixture_path

This is consistent with pytest's tmp_path

* Initial unit tests for esphome.helpers

* Disabled coverage reporting for esphome/components.

Focus initial unittest efforts on the core code.

* Migrated some ip_address to hypothesis

* Added a hypothesis MAC address strategy

* Initial tests for core

* Added hypothesis to requirements

* Added tests for core classes

TestTimePeriod
Lambda
ID
DocumentLocation
DocumentRange
Define
Library

* Updated test config so package root is discovered

* Setup fixtures and inital tests for pins

* Added tests for validate GPIO

* Added tests for pin type

* Added initial config_validation tests

* Added more tests for config_validation

* Added comparison unit tests

* Added repr to core.TimePeriod. Simplified identifying faults in tests

* Fixed inverted gt/lt tests

* Some tests for Espcore

* Updated syntax for Python3

* Removed usage of kwarg that isn't required

* Started writing test cases

* Started writing test cases for cpp_generator

* Additional docs and more Python3 releated improvements

* More test cases for cpp_generator.

* Fixed linter errors

* Add codegen tests to ensure file API remains stable

* Add test cases for cpp_helpers
2020-04-19 21:05:58 -03:00

86 lines
2.2 KiB
Python

import pytest
from mock import Mock
from esphome import cpp_helpers as ch
from esphome import const
from esphome.cpp_generator import MockObj
def test_gpio_pin_expression__conf_is_none(monkeypatch):
target = ch.gpio_pin_expression(None)
actual = next(target)
assert actual is None
def test_gpio_pin_expression__new_pin(monkeypatch):
target = ch.gpio_pin_expression({
const.CONF_NUMBER: 42,
const.CONF_MODE: "input",
const.CONF_INVERTED: False
})
actual = next(target)
assert isinstance(actual, MockObj)
def test_register_component(monkeypatch):
var = Mock(base="foo.bar")
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)
target = ch.register_component(var, {})
actual = next(target)
assert actual is var
add_mock.assert_called_once()
app_mock.register_component.assert_called_with(var)
assert core_mock.component_ids == []
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"):
target = ch.register_component(var, {})
next(target)
def test_register_component__with_setup_priority(monkeypatch):
var = Mock(base="foo.bar")
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)
target = ch.register_component(var, {
const.CONF_SETUP_PRIORITY: "123",
const.CONF_UPDATE_INTERVAL: "456",
})
actual = next(target)
assert actual is var
add_mock.assert_called()
assert add_mock.call_count == 3
app_mock.register_component.assert_called_with(var)
assert core_mock.component_ids == []