Skip to content

Commit

Permalink
Added a few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jo1gi committed May 27, 2021
1 parent 1bc597c commit ff4354d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
32 changes: 32 additions & 0 deletions spec/openurl_spec.cr
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
require "yaml"
require "./spec_helper"
require "../src/rules"

describe "find_command" do
it "does not find a command on an empty config" do
Rules.find_command("https://duckduckgo.com", YAML.parse("")).should be_nil
end

it "can find a command with from a regex rule" do
Rules.find_command(
"https://duckduckgo.com",
YAML.parse [{"domain" => "duckduckgo.com", "command" => "a"}].to_yaml
).should eq "a"
end

it "prioritizes subrules over rules" do
Rules.find_command(
"https://duckduckgo.com",
YAML.parse [{"protocol" => "https",
"command" => "a",
"subrules" => [{"domain" => "duckduckgo.com", "command" => "b"}]
}].to_yaml
).should eq "b"
end

it "does not crash when no program is found" do
Rules.find_command(
"https://duckduckgo.com",
YAML.parse [{"protocol" => "magnet", "command" => "a"}].to_yaml
).should be_nil
end
end
5 changes: 4 additions & 1 deletion src/rules.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ module Rules
end

# Finds a command in the rules which the url matches
def find_command(url : String, config)
def find_command(url : String, config : YAML::Any)
if !config.as_a?
return nil
end
config.as_a.each do |c|
if test_rule(url, c)
if c["subrules"]?
Expand Down

0 comments on commit ff4354d

Please sign in to comment.