Skip to content

Commit

Permalink
fixes tests on zig docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ityonemo committed Aug 4, 2024
1 parent d403adc commit 11945ef
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 17 deletions.
14 changes: 7 additions & 7 deletions lib/zig.doc/generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Zig.Doc.Generator do
alias Zig.Parser.Var
@moduledoc false

def doc_ast(content, file_path, opts \\ []) do
defp doc_ast(content, file_path, opts \\ []) do
if document = content.doc_comment do
# trim each line of the documentation. This is necessary because sometimes
# early whitespaces are inserted into the documentation, and this causes the
Expand Down Expand Up @@ -37,20 +37,18 @@ defmodule Zig.Doc.Generator do
# options must include 'file' key
with {:ok, file_path} <- Keyword.fetch(options, :file),
{{:ok, file}, :read, _} <- {File.read(file_path), :read, file_path} do
sema = sema_module.run_sema_doc(file_path)
sema = sema_module.run_sema(file_path)
parsed_document = Zig.Parser.parse(file)

node = %ExDoc.ModuleNode{
id: "#{id}",
# doc_line: 1,
doc: doc_ast(parsed_document, file_path),
language: ExDoc.Language.Elixir,
title: "beam",
group: :"zig code",
module: :beam,
docs_groups: [:Functions, :Types, :Constants, :Variables],
type: :module,
# source_path: file_path,
source_url: source_url(file_path, 1, exdoc_config)
}

Expand Down Expand Up @@ -200,9 +198,9 @@ defmodule Zig.Doc.Generator do
signature: "#{name}",
doc: doc_ast(const, file_path, extras: extras),
spec: Spec.type_from_sema(this_type),
# source_path: file_path,
source_url: source_url(file_path, elem(const.location, 0), exdoc_config)
}
source_url: source_url(file_path, elem(const.location, 0), exdoc_config),
group: :Types
}

%{acc | typespecs: [node | acc.typespecs]}

Expand Down Expand Up @@ -323,6 +321,8 @@ defmodule Zig.Doc.Generator do

defp tts({:ref, list}), do: Enum.join(list, ".")

defp render_type(:erl_nif_binary), do: "e.ErlNifBinary"
defp render_type(:stacktrace), do: "std.builtin.StackTrace"
defp render_type(type) when is_atom(type), do: to_string(type)

defp render_type({:slice, opts, params}) do
Expand Down
5 changes: 5 additions & 0 deletions lib/zig.doc/spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ defmodule Zig.Doc.Spec do
{fun.name, [], params}
end

defp render_type(:erl_nif_binary), do: :"e.ErlNifBinary"
defp render_type(:stacktrace), do: :"std.builtin.StackTrace"

defp render_type(type) when is_atom(type), do: type

defp render_type(type = %struct{}) do
Expand Down Expand Up @@ -60,6 +63,8 @@ defmodule Zig.Doc.Spec do

defp wrap(name), do: {name, [], Elixir}

defp render_typedef(:erl_nif_binary), do: wrap(:"e.ErlNifBinary")
defp render_typedef(:stacktrace), do: wrap(:"std.builtin.StackTrace")
defp render_typedef(type) when is_atom(type), do: wrap(type)

defp render_typedef(type = %struct{}) do
Expand Down
2 changes: 1 addition & 1 deletion test/_support/sema.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Zig.SemaAPI do
@type json :: nil | boolean | number | String.t() | [json] | %{optional(String.t()) => json}
@callback run_sema(Path.t(), term, term) :: {:ok, json} | {:error, String.t()}
@callback run_sema(Path.t()) :: {:ok, json} | {:error, String.t()}
end

Mox.defmock(Zig.SemaMock, for: Zig.SemaAPI)
2 changes: 1 addition & 1 deletion test/_support/zig_doc_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule Zig.Doc.Case do
end

def expect_sema(sema) do
Mox.expect(Zig.SemaMock, :run_sema, fn _, _, _ -> sema end)
Mox.expect(Zig.SemaMock, :run_sema, fn _ -> sema end)
end

defmacro assert_code(string, data) do
Expand Down
2 changes: 1 addition & 1 deletion test/documentation/const_function_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ZigDocTest.Documentation.ConstFunctionTest do
alias Zig.Doc.Sema

test "documentation is generated for consts that are functions" do
expect_sema({:ok, Sema.new(functions: [%{name: :bar, return: :i32, params: [:i32]}])})
expect_sema(Sema.new(functions: [%{name: :bar, return: :i32, params: [:i32]}]))

assert %{docs: [function]} = get_module("test/_sources/const_function.zig")

Expand Down
2 changes: 1 addition & 1 deletion test/documentation/const_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ZigDocTest.Documentation.ConstTest do
alias Zig.Doc.Sema

test "const documentation is generated" do
expect_sema({:ok, Sema.new(decls: [%{name: :foo, type: :i32}])})
expect_sema(Sema.new(decls: [%{name: :foo, type: :i32}]))

assert %{docs: [function]} = get_module("test/_sources/const.zig")

Expand Down
2 changes: 1 addition & 1 deletion test/documentation/function_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ZigDocTest.Documentation.FunctionTest do
alias Zig.Doc.Sema

test "function-level documentation is generated" do
expect_sema({:ok, Sema.new(functions: [%{name: :foo, return: :i32, params: [:i32]}])})
expect_sema(Sema.new(functions: [%{name: :foo, return: :i32, params: [:i32]}]))

assert %{docs: [function]} = get_module("test/_sources/function.zig")

Expand Down
2 changes: 1 addition & 1 deletion test/documentation/module_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ZigDocTest.Documentation.ModuleTest do
alias Zig.Doc.Sema

test "module-level documentation is generated" do
expect_sema({:ok, Sema.new()})
expect_sema(Sema.new())

assert %{doc: [{:p, [], ["tests module-level comment content"], %{}}]} =
get_module("test/_sources/module.zig")
Expand Down
2 changes: 1 addition & 1 deletion test/documentation/type_basic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ZigDocTest.Documentation.TypeBasicTest do
alias Zig.Doc.Sema

test "type-level documentation is generated" do
expect_sema({:ok, Sema.new(types: [%{name: :foo, type: :i32}])})
expect_sema(Sema.new(types: [%{name: :foo, type: :i32}]))

assert %{typespecs: [type]} = get_module("test/_sources/type_basic.zig")

Expand Down
2 changes: 1 addition & 1 deletion test/documentation/type_direct_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule ZigDocTest.Documentation.TypeDirectTest do
required: %{}
}

expect_sema({:ok, Sema.new(types: [%{name: :foo, type: type}])})
expect_sema(Sema.new(types: [%{name: :foo, type: type}]))

assert %{typespecs: [type]} = get_module("test/_sources/type_direct.zig")

Expand Down
2 changes: 1 addition & 1 deletion test/documentation/type_indirect_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule ZigDocTest.Documentation.TypeIndirectTest do
required: %{}
}

expect_sema({:ok, Sema.new(types: [%{name: :foo, type: type}])})
expect_sema(Sema.new(types: [%{name: :foo, type: type}]))

assert %{typespecs: [type]} = get_module("test/_sources/type_indirect.zig")

Expand Down
2 changes: 1 addition & 1 deletion test/documentation/var_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ZigDocTest.Documentation.VarTest do
alias Zig.Doc.Sema

test "var documentation is generated" do
expect_sema({:ok, Sema.new(decls: [%{name: :foo, type: :i32}])})
expect_sema(Sema.new(decls: [%{name: :foo, type: :i32}]))

assert %{docs: [function]} = get_module("test/_sources/var.zig")

Expand Down

0 comments on commit 11945ef

Please sign in to comment.