Skip to content

Commit

Permalink
test: --var option
Browse files Browse the repository at this point in the history
  • Loading branch information
loqusion committed May 3, 2024
1 parent 5d79ddb commit 6b0cd0b
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/hyprshade/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def __to_dict_callback(
value: tuple[tuple[str, str], ...],
) -> dict[str, str]:
ret = dict(value)
if callback is not None:
if callback is not None: # pragma: no cover
return callback(ctx, param, ret)
return ret

Expand Down
8 changes: 8 additions & 0 deletions src/hyprshade/shader/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ def _extract_template_instance_metadata(path: str) -> TemplateInstanceMetadata:
).strip()
return TemplateInstanceMetadata.decode(metadata_str)

@staticmethod
def _get_template_instance_content_without_metadata(path: str) -> str:
with open(path) as f:
lines = f.readlines()
if not lines[0].startswith(Shader.TEMPLATE_METADATA_PREFIX):
return "".join(lines)
return "".join(lines[1:])


@dataclass
class TemplateInstanceMetadata:
Expand Down
30 changes: 30 additions & 0 deletions tests/cli/on/__snapshots__/test_on.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# serializer version: 1
# name: TestVarOption.test
'''
// This file was generated by Hyprshade.
// Do not edit it directly.

const int key = 3;
void main() {}
'''
# ---
# name: TestVarOption.test_multiple
'''
// This file was generated by Hyprshade.
// Do not edit it directly.

const int WORLD = 5;
const int key = WORLD;
void main() {}
'''
# ---
# name: TestVarOption.test_override
'''
// This file was generated by Hyprshade.
// Do not edit it directly.

const int WORLD = 5;
const int key = WORLD;
void main() {}
'''
# ---
102 changes: 102 additions & 0 deletions tests/cli/on/test_on.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import pytest
from click.testing import CliRunner
from syrupy.assertion import SnapshotAssertion

from hyprshade.cli import cli
from hyprshade.shader import hyprctl
from hyprshade.shader.core import Shader
from tests.types import ShaderPathFactory

pytestmark = [
Expand Down Expand Up @@ -54,3 +56,103 @@ def test_invalid_shader_dot(runner: CliRunner, shader_path_factory: ShaderPathFa
assert result.exit_code != 0
assert isinstance(result.exception, ValueError)
assert hyprctl.get_screen_shader() == initial_shader


class TestVarOption:
def test(
self,
runner: CliRunner,
shader_path_factory: ShaderPathFactory,
snapshot: SnapshotAssertion,
):
shader_path_factory(
"shader",
extension="glsl.mustache",
text=("""const int key = {{key}};\n""" """void main() {}"""),
)
result = runner.invoke(cli, ["on", "shader", "--var", "key=3"])

assert result.exit_code == 0
current_shader_path = hyprctl.get_screen_shader()
assert current_shader_path is not None
assert (
Shader._get_template_instance_content_without_metadata(current_shader_path)
== snapshot
)

def test_multiple(
self,
runner: CliRunner,
shader_path_factory: ShaderPathFactory,
snapshot: SnapshotAssertion,
):
shader_path_factory(
"shader",
extension="glsl.mustache",
text=(
"""const int WORLD = {{world}};\n"""
"""const int key = {{key}};\n"""
"""void main() {}"""
),
)
result = runner.invoke(
cli, ["on", "shader", "--var", "key=world", "--var", "world=5"]
)

assert result.exit_code == 0
current_shader_path = hyprctl.get_screen_shader()
assert current_shader_path is not None
assert (
Shader._get_template_instance_content_without_metadata(current_shader_path)
== snapshot
)

def test_override(
self,
runner: CliRunner,
shader_path_factory: ShaderPathFactory,
snapshot: SnapshotAssertion,
):
shader_path_factory(
"shader",
extension="glsl.mustache",
text=(
"""const int WORLD = {{world}};\n"""
"""const int key = {{key}};\n"""
"""void main() {}"""
),
)
result = runner.invoke(
cli,
[
"on",
"shader",
"--var",
"key=planet",
"--var",
"key=world",
"--var",
"world=5",
],
)

assert result.exit_code == 0
current_shader_path = hyprctl.get_screen_shader()
assert current_shader_path is not None
assert (
Shader._get_template_instance_content_without_metadata(current_shader_path)
== snapshot
)

def test_no_equals(
self,
runner: CliRunner,
shader_path_factory: ShaderPathFactory,
):
shader_path_factory(
"shader", extension="glsl.mustache", text="""void main() {}"""
)
result = runner.invoke(cli, ["on", "shader", "--var", "key"])

assert result.exit_code != 0
assert "Invalid value for '--var' / '-V'" in result.stderr
30 changes: 30 additions & 0 deletions tests/cli/toggle/__snapshots__/test_toggle.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# serializer version: 1
# name: TestVarOption.test
'''
// This file was generated by Hyprshade.
// Do not edit it directly.

const int key = 3;
void main() {}
'''
# ---
# name: TestVarOption.test_multiple
'''
// This file was generated by Hyprshade.
// Do not edit it directly.

const int WORLD = 5;
const int key = WORLD;
void main() {}
'''
# ---
# name: TestVarOption.test_override
'''
// This file was generated by Hyprshade.
// Do not edit it directly.

const int WORLD = 5;
const int key = WORLD;
void main() {}
'''
# ---
101 changes: 101 additions & 0 deletions tests/cli/toggle/test_toggle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest
from click.testing import CliRunner
from syrupy.assertion import SnapshotAssertion

from hyprshade.cli import cli
from hyprshade.shader import hyprctl
Expand Down Expand Up @@ -199,3 +200,103 @@ def test_multiple_fallback_options(

assert result.exit_code != 0
assert "--fallback" in result.stderr or "--fallback" in str(result.exception)


class TestVarOption:
def test(
self,
runner: CliRunner,
shader_path_factory: ShaderPathFactory,
snapshot: SnapshotAssertion,
):
shader_path_factory(
"shader",
extension="glsl.mustache",
text=("""const int key = {{key}};\n""" """void main() {}"""),
)
result = runner.invoke(cli, ["toggle", "shader", "--var", "key=3"])

assert result.exit_code == 0
current_shader_path = hyprctl.get_screen_shader()
assert current_shader_path is not None
assert (
Shader._get_template_instance_content_without_metadata(current_shader_path)
== snapshot
)

def test_multiple(
self,
runner: CliRunner,
shader_path_factory: ShaderPathFactory,
snapshot: SnapshotAssertion,
):
shader_path_factory(
"shader",
extension="glsl.mustache",
text=(
"""const int WORLD = {{world}};\n"""
"""const int key = {{key}};\n"""
"""void main() {}"""
),
)
result = runner.invoke(
cli, ["toggle", "shader", "--var", "key=world", "--var", "world=5"]
)

assert result.exit_code == 0
current_shader_path = hyprctl.get_screen_shader()
assert current_shader_path is not None
assert (
Shader._get_template_instance_content_without_metadata(current_shader_path)
== snapshot
)

def test_override(
self,
runner: CliRunner,
shader_path_factory: ShaderPathFactory,
snapshot: SnapshotAssertion,
):
shader_path_factory(
"shader",
extension="glsl.mustache",
text=(
"""const int WORLD = {{world}};\n"""
"""const int key = {{key}};\n"""
"""void main() {}"""
),
)
result = runner.invoke(
cli,
[
"toggle",
"shader",
"--var",
"key=planet",
"--var",
"key=world",
"--var",
"world=5",
],
)

assert result.exit_code == 0
current_shader_path = hyprctl.get_screen_shader()
assert current_shader_path is not None
assert (
Shader._get_template_instance_content_without_metadata(current_shader_path)
== snapshot
)

def test_no_equals(
self,
runner: CliRunner,
shader_path_factory: ShaderPathFactory,
):
shader_path_factory(
"shader", extension="glsl.mustache", text="""void main() {}"""
)
result = runner.invoke(cli, ["toggle", "shader", "--var", "key"])

assert result.exit_code != 0
assert "Invalid value for '--var' / '-V'" in result.stderr

0 comments on commit 6b0cd0b

Please sign in to comment.