Skip to content

Commit

Permalink
Add interpolation tests for resolvers outputting a dict/list
Browse files Browse the repository at this point in the history
  • Loading branch information
odelalleau committed Mar 5, 2021
1 parent 93f76ce commit fc1d2c3
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions tests/test_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import random
import re
from textwrap import dedent
from typing import Any, Optional, Tuple
from typing import Any, Dict, List, Optional, Tuple

import pytest
from _pytest.python_api import RaisesContext
Expand Down Expand Up @@ -924,18 +924,42 @@ def test_type_validation_error_no_throw() -> None:
assert bad_node._dereference_node(throw_on_resolution_failure=False) is None


def test_resolver_output_dictconfig(restore_resolvers: Any) -> None:
OmegaConf.register_new_resolver("dict", lambda: {"a": 0, "b": 1})
cfg = OmegaConf.create({"x": "${dict:}"})
assert isinstance(cfg.x, DictConfig)
assert cfg.x.a == 0 and cfg.x.b == 1
@pytest.mark.parametrize(
("cfg", "expected"),
[
({"a": 0, "b": 1}, {"a": 0, "b": 1}),
({"a": "${y}"}, {"a": -1}),
({"a": 0, "b": "${x.a}"}, {"a": 0, "b": 0}),
({"a": 0, "b": "${.a}"}, {"a": 0, "b": 0}),
({"a": "${..y}"}, {"a": -1}),
],
)
def test_resolver_output_dictconfig(
restore_resolvers: Any, cfg: Dict[str, Any], expected: Dict[str, Any]
) -> None:
OmegaConf.register_new_resolver("dict", lambda: cfg)
c = OmegaConf.create({"x": "${dict:}", "y": -1})
assert isinstance(c.x, DictConfig)
assert c.x == expected


def test_resolver_output_listconfig(restore_resolvers: Any) -> None:
OmegaConf.register_new_resolver("list", lambda: [0, 1])
cfg = OmegaConf.create({"x": "${list:}"})
assert isinstance(cfg.x, ListConfig)
assert cfg.x == [0, 1]
@pytest.mark.parametrize(
("cfg", "expected"),
[
([0, 1], [0, 1]),
(["${y}"], [-1]),
([0, "${x.0}"], [0, 0]),
([0, "${.0}"], [0, 0]),
(["${..y}"], [-1]),
],
)
def test_resolver_output_listconfig(
restore_resolvers: Any, cfg: List[Any], expected: List[Any]
) -> None:
OmegaConf.register_new_resolver("list", lambda: cfg)
c = OmegaConf.create({"x": "${list:}", "y": -1})
assert isinstance(c.x, ListConfig)
assert c.x == expected


def test_none_value_in_quoted_string(restore_resolvers: Any) -> None:
Expand Down

0 comments on commit fc1d2c3

Please sign in to comment.