Skip to content

Commit

Permalink
GapsPlugin YAML config (#1016)
Browse files Browse the repository at this point in the history
Added a declarative config for the GapsPlugin.
  • Loading branch information
dalyIsaac authored Sep 11, 2024
1 parent 8b60766 commit 7efb74c
Show file tree
Hide file tree
Showing 10 changed files with 443 additions and 18 deletions.
22 changes: 22 additions & 0 deletions docs/docs/customize/yaml.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,25 @@ routers:
value: ^Untitled - Notepad$
workspace_name: notepad
```

## Plugins

The plugins configuration tells Whim to load the specified plugins. Each plugin has is a key under the `plugins` top-Level key.

### Gaps Plugin

The `GapsPlugin` adds the config and commands to add gaps between each of the windows in the layout.

![Gaps plugin demo](../../images/gaps-demo.png)

```yaml
plugins:
gaps:
is_enabled: true
outer_gap: 10
inner_gap: 10
default_outer_delta: 2
default_inner_delta: 2
```

Commands for the `GapsPlugin` can be found at the [Gaps Plugin](../plugins/gaps.md#commands) page.
12 changes: 12 additions & 0 deletions src/Whim.Yaml.Tests/YamlLoaderTestUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using NSubstitute;

namespace Whim.Yaml.Tests;

public static class YamlLoaderTestUtils
{
public static void SetupFileConfig(IContext ctx, string config, bool isYaml)
{
ctx.FileManager.FileExists(Arg.Is<string>(s => s.EndsWith(isYaml ? "yaml" : "json"))).Returns(true);
ctx.FileManager.ReadAllText(Arg.Is<string>(s => s.EndsWith(isYaml ? "yaml" : "json"))).Returns(config);
}
}
6 changes: 2 additions & 4 deletions src/Whim.Yaml.Tests/YamlLoader_UpdateFiltersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public class YamlLoader_UpdateFiltersTests
public void Load_Filters(string config, bool isYaml, IContext ctx)
{
// Given a valid config with filters set
ctx.FileManager.FileExists(Arg.Is<string>(s => s.EndsWith(isYaml ? "yaml" : "json"))).Returns(true);
ctx.FileManager.ReadAllText(Arg.Any<string>()).Returns(config);
YamlLoaderTestUtils.SetupFileConfig(ctx, config, isYaml);

// When loading the config
bool result = YamlLoader.Load(ctx);
Expand Down Expand Up @@ -122,8 +121,7 @@ public void Load_Filters(string config, bool isYaml, IContext ctx)
public void Load_InvalidFilters(string config, bool isYaml, IContext ctx)
{
// Given an invalid config with filters set
ctx.FileManager.FileExists(Arg.Is<string>(s => s.EndsWith(isYaml ? "yaml" : "json"))).Returns(true);
ctx.FileManager.ReadAllText(Arg.Any<string>()).Returns(config);
YamlLoaderTestUtils.SetupFileConfig(ctx, config, isYaml);

// When loading the config
bool result = YamlLoader.Load(ctx);
Expand Down
9 changes: 3 additions & 6 deletions src/Whim.Yaml.Tests/YamlLoader_UpdateKeybindsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public class YamlLoader_UpdateKeybindsTests
public void Load_Keybinds(string config, bool isYaml, IContext ctx)
{
// Given a valid config with keybinds set
ctx.FileManager.FileExists(Arg.Is<string>(s => s.EndsWith(isYaml ? "yaml" : "json"))).Returns(true);
ctx.FileManager.ReadAllText(Arg.Any<string>()).Returns(config);
YamlLoaderTestUtils.SetupFileConfig(ctx, config, isYaml);

// When loading the config
bool result = YamlLoader.Load(ctx);
Expand Down Expand Up @@ -155,8 +154,7 @@ public void Load_Keybinds(string config, bool isYaml, IContext ctx)
public void Load_InvalidKeybinds(string config, bool isYaml, IContext ctx)
{
// Given an invalid config with keybinds set
ctx.FileManager.FileExists(Arg.Is<string>(s => s.EndsWith(isYaml ? "yaml" : "json"))).Returns(true);
ctx.FileManager.ReadAllText(Arg.Any<string>()).Returns(config);
YamlLoaderTestUtils.SetupFileConfig(ctx, config, isYaml);

// When loading the config
bool result = YamlLoader.Load(ctx);
Expand Down Expand Up @@ -209,8 +207,7 @@ public void Load_InvalidKeybinds(string config, bool isYaml, IContext ctx)
public void Load_UnifyKeyModifiers(string config, bool isYaml, IContext ctx)
{
// Given a valid config with unifyKeyModifiers set
ctx.FileManager.FileExists(Arg.Is<string>(s => s.EndsWith(isYaml ? "yaml" : "json"))).Returns(true);
ctx.FileManager.ReadAllText(Arg.Any<string>()).Returns(config);
YamlLoaderTestUtils.SetupFileConfig(ctx, config, isYaml);

// When loading the config
bool result = YamlLoader.Load(ctx);
Expand Down
6 changes: 2 additions & 4 deletions src/Whim.Yaml.Tests/YamlLoader_UpdateRoutersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public class YamlLoader_UpdateRoutersTests
public void Load_Routers(string config, bool isYaml, IContext ctx)
{
// Given a valid config with routers set
ctx.FileManager.FileExists(Arg.Is<string>(s => s.EndsWith(isYaml ? "yaml" : "json"))).Returns(true);
ctx.FileManager.ReadAllText(Arg.Any<string>()).Returns(config);
YamlLoaderTestUtils.SetupFileConfig(ctx, config, isYaml);

// When loading the config
bool result = YamlLoader.Load(ctx);
Expand Down Expand Up @@ -114,8 +113,7 @@ public void Load_Routers(string config, bool isYaml, IContext ctx)
public void Load_InvalidRouters(string config, bool isYaml, IContext ctx)
{
// Given an invalid config with routers set
ctx.FileManager.FileExists(Arg.Is<string>(s => s.EndsWith(isYaml ? "yaml" : "json"))).Returns(true);
ctx.FileManager.ReadAllText(Arg.Any<string>()).Returns(config);
YamlLoaderTestUtils.SetupFileConfig(ctx, config, isYaml);

// When loading the config
bool result = YamlLoader.Load(ctx);
Expand Down
284 changes: 284 additions & 0 deletions src/Whim.Yaml.Tests/YamlPluginLoader_GapsPluginTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
using NSubstitute;
using Whim.Gaps;
using Whim.TestUtils;
using Xunit;

namespace Whim.Yaml.Tests;

public class YamlPluginLoader_GapsPluginTests
{
private const int _defaultOuterGap = 0;
private const int _defaultInnerGap = 10;
private const int _defaultOuterDelta = 2;
private const int _defaultInnerDelta = 2;

public static TheoryData<string, bool, int, int, int, int> GapsConfig =>
new()
{
// YAML, all values set
{
"""
plugins:
gaps:
is_enabled: true
outer_gap: 1
inner_gap: 2
default_outer_delta: 3
default_inner_delta: 4
""",
true,
1,
2,
3,
4
},
// JSON, all values set
{
"""
{
"plugins": {
"gaps": {
"is_enabled": true,
"outer_gap": 1,
"inner_gap": 2,
"default_outer_delta": 3,
"default_inner_delta": 4
}
}
}
""",
false,
1,
2,
3,
4
},
// YAML, no values set
{
"""
plugins:
gaps: {}
""",
true,
_defaultOuterGap,
_defaultInnerGap,
_defaultOuterDelta,
_defaultInnerDelta
},
// JSON, no values set
{
"""
{
"plugins": {
"gaps": {}
}
}
""",
false,
_defaultOuterGap,
_defaultInnerGap,
_defaultOuterDelta,
_defaultInnerDelta
},
};

[Theory, MemberAutoSubstituteData<YamlLoaderCustomization>(nameof(GapsConfig))]
public void Load_GapsPlugin(
string config,
bool isYaml,
int outerGap,
int innerGap,
int defaultOuterDelta,
int defaultInnerDelta,
IContext ctx
)
{
// Given a valid config with the gaps plugin
YamlLoaderTestUtils.SetupFileConfig(ctx, config, isYaml);

// When loading the config
bool result = YamlLoader.Load(ctx);

// Then the result is true, and the gaps plugin is set
Assert.True(result);
ctx.PluginManager.Received(1)
.AddPlugin(
Arg.Is<GapsPlugin>(p =>
p.GapsConfig.OuterGap == outerGap
&& p.GapsConfig.InnerGap == innerGap
&& p.GapsConfig.DefaultOuterDelta == defaultOuterDelta
&& p.GapsConfig.DefaultInnerDelta == defaultInnerDelta
)
);
}

public static TheoryData<string, bool> GapsPluginNotEnabledConfig =>
new()
{
// YAML
{
"""
plugins:
gaps:
is_enabled: false
""",
true
},
// JSON
{
"""
{
"plugins": {
"gaps": {
"is_enabled": false
}
}
}
""",
false
},
};

[Theory, MemberAutoSubstituteData<YamlLoaderCustomization>(nameof(GapsPluginNotEnabledConfig))]
public void Load_GapsPlugin_NotEnabled(string config, bool isYaml, IContext ctx)
{
// Given a valid config with the gaps plugin not enabled
YamlLoaderTestUtils.SetupFileConfig(ctx, config, isYaml);

// When loading the config
bool result = YamlLoader.Load(ctx);

// Then the result is true, and the gaps plugin is not set
Assert.True(result);
ctx.PluginManager.DidNotReceive().AddPlugin(Arg.Any<GapsPlugin>());
}

public static TheoryData<string, bool> InvalidGapsPluginConfig =>
new()
{
// YAML, invalid outer gap
{
"""
plugins:
gaps:
outer_gap: 123.5
""",
true
},
// JSON, invalid outer gap
{
"""
{
"plugins": {
"gaps": {
"outer_gap": 123.5
}
}
}
""",
false
},
// YAML, invalid inner gap
{
"""
plugins:
gaps:
inner_gap: 123.5
""",
true
},
// JSON, invalid inner gap
{
"""
{
"plugins": {
"gaps": {
"inner_gap": 123.5
}
}
}
""",
false
},
// YAML, invalid default outer delta
{
"""
plugins:
gaps:
default_outer_delta: 123.5
""",
true
},
// JSON, invalid default outer delta
{
"""
{
"plugins": {
"gaps": {
"default_outer_delta": 123.5
}
}
}
""",
false
},
// YAML, invalid default inner delta
{
"""
plugins:
gaps:
default_inner_delta: 123.5
""",
true
},
// JSON, invalid default inner delta
{
"""
{
"plugins": {
"gaps": {
"default_inner_delta": 123.5
}
}
}
""",
false
},
// YAML, invalid is_enabled
{
"""
plugins:
gaps:
is_enabled: 1
""",
true
},
// JSON, invalid is_enabled
{
"""
{
"plugins": {
"gaps": {
"is_enabled": 1
}
}
}
""",
false
},
};

[Theory, MemberAutoSubstituteData<YamlLoaderCustomization>(nameof(InvalidGapsPluginConfig))]
public void Load_GapsPlugin_InvalidConfig(string config, bool isYaml, IContext ctx)
{
// Given an invalid config with the gaps plugin
YamlLoaderTestUtils.SetupFileConfig(ctx, config, isYaml);

// When loading the config
bool result = YamlLoader.Load(ctx);

// Then the result is false, and the gaps plugin is not set
Assert.True(result);
ctx.PluginManager.DidNotReceive().AddPlugin(Arg.Any<GapsPlugin>());
}
}
Loading

0 comments on commit 7efb74c

Please sign in to comment.