3.6 KiB · text History 0d6bdf0
defmodule GitGud.Badges.SpecTest do
  use ExUnit.Case, async: true

  alias GitGud.Badges.Spec

  test "a bare kind takes every default" do
    assert {:ok,
            %Spec{kind: "ci", style: "split", corners: "round", scale: "1x", gg: false} = spec} =
             Spec.parse_path(["ci.svg"])

    assert spec.theme == "auto"
    assert spec.format == "svg"
  end

  test "options come in any order and write back in one" do
    assert {:ok, spec} = Spec.parse_path(["coverage", "forest", "gg", "2.5x", "dot.png"])

    assert %Spec{
             kind: "coverage",
             style: "dot",
             scale: "2.5x",
             gg: true,
             theme: "forest",
             format: "png"
           } = spec

    assert Spec.to_path(spec) == "coverage/dot/2.5x/gg/forest.png"
    assert Spec.parse_path(String.split(Spec.to_path(spec), "/")) == {:ok, spec}
  end

  test "defaults are dropped from the canonical path" do
    {:ok, spec} = Spec.parse_path(["ci", "split", "round", "1x", "auto.svg"])
    assert Spec.to_path(spec) == "ci.svg"
  end

  test "unknown, repeated or misplaced segments are refused" do
    assert Spec.parse_path(["ci", "sparkly.svg"]) == :error
    assert Spec.parse_path(["ci", "dot", "outline.svg"]) == :error
    assert Spec.parse_path(["ci.gif"]) == :error
    assert Spec.parse_path(["nope.svg"]) == :error
    assert Spec.parse_path([]) == :error
  end

  test "the sheet takes options but not a theme" do
    assert {:ok, %Spec{style: "outline", gg: true}} = Spec.parse_options(["gg", "outline"])
    assert Spec.parse_options(["forest"]) == :error
    assert {:ok, %Spec{}} = Spec.parse_options([])
  end

  test "option and explicit segments" do
    spec = %Spec{style: "dot", scale: "2x", gg: true, theme: "auto"}
    assert Spec.option_segments(spec) == ["dot", "2x", "gg"]
    assert Spec.explicit_segments(spec) == ["dot", "2x", "gg", "auto.svg"]
  end

  describe "workflow" do
    test "the name is the segment after the kind" do
      assert {:ok, %Spec{kind: "workflow", name: "audit", style: "split"} = spec} =
               Spec.parse_path(["workflow", "audit.svg"])

      assert Spec.to_path(spec) == "workflow/audit.svg"

      assert {:ok, %Spec{name: "audit", style: "dot", theme: "forest"} = spec} =
               Spec.parse_path(["workflow", "audit", "dot", "forest.png"])

      assert Spec.to_path(spec) == "workflow/audit/dot/forest.png"
    end

    test "a name that looks like an option is still the name" do
      assert {:ok, %Spec{name: "dot", style: "outline"}} =
               Spec.parse_path(["workflow", "dot", "outline.svg"])
    end

    test "without a name it's refused" do
      assert Spec.parse_path(["workflow.svg"]) == :error
    end
  end

  describe "package" do
    test "the format and the package's name follow the kind" do
      assert {:ok,
              %Spec{kind: "package", registry: "cargo", name: "gitgud-cli", scale: "2x"} = spec} =
               Spec.parse_path(["package", "cargo", "gitgud-cli", "2x.png"])

      assert Spec.to_path(spec) == "package/cargo/gitgud-cli/2x.png"
      assert {:ok, %Spec{name: "x"}} = Spec.parse_path(["package", "pypi", "x.svg"])
    end

    test "an unknown format or a missing name is refused" do
      assert Spec.parse_path(["package", "npm", "left-pad.svg"]) == :error
      assert Spec.parse_path(["package", "cargo.svg"]) == :error
    end
  end

  test "counts are plain kinds" do
    for kind <-
          ~w(issues pulls commits contributors ci-duration release license mirror language syms) do
      assert {:ok, %Spec{kind: ^kind}} = Spec.parse_path([kind <> ".svg"])
    end
  end
end