|
1 | 1 | # SPDX-FileCopyrightText: 2018, 2020, 2022, 2025 Jean-Philippe Cugnet <jean-philippe@cugnet.eu> |
2 | 2 | # SPDX-FileCopyrightText: 2018 Marcin Górnik <marcin.gornik@gmail.com> |
| 3 | +# SPDX-FileCopyrightText: 2022 Phil Chen <06fahchen@gmail.com> |
3 | 4 | # |
4 | 5 | # SPDX-License-Identifier: MIT |
5 | 6 |
|
@@ -79,6 +80,22 @@ defmodule TypedStructTest do |
79 | 80 | assert type1 == type2 |
80 | 81 | end |
81 | 82 |
|
| 83 | + test "generates a parameterized type for the struct" do |
| 84 | + # Get both types and standardise them (remove line numbers and rename |
| 85 | + # the second struct with the name of the first one). |
| 86 | + type1 = |
| 87 | + TestStruct.WithParameter |
| 88 | + |> extract_first_type() |
| 89 | + |> standardise(TestStruct.WithParameter) |
| 90 | + |
| 91 | + type2 = |
| 92 | + TestStruct.WithParameter.Expected |
| 93 | + |> extract_first_type() |
| 94 | + |> standardise(TestStruct.WithParameter.Expected) |
| 95 | + |
| 96 | + assert type1 == type2 |
| 97 | + end |
| 98 | + |
82 | 99 | test "generates the struct in a submodule if `module: ModuleName` is set" do |
83 | 100 | # credo:disable-for-next-line Credo.Check.Design.AliasUsage |
84 | 101 | assert TestStruct.AsSubmodule.Struct.__struct__() == |
@@ -163,6 +180,21 @@ defmodule TypedStructTest do |
163 | 180 | end) =~ "undefined function field/2" |
164 | 181 | end |
165 | 182 |
|
| 183 | + test "the name of a type parameter be an atom" do |
| 184 | + assert_raise ArgumentError, |
| 185 | + "the name of a type parameter must be an atom, got 3", |
| 186 | + fn -> |
| 187 | + defmodule InvalidStruct do |
| 188 | + use TypedStruct |
| 189 | + |
| 190 | + typedstruct do |
| 191 | + parameter 3 |
| 192 | + field :field, term() |
| 193 | + end |
| 194 | + end |
| 195 | + end |
| 196 | + end |
| 197 | + |
166 | 198 | test "the name of a field must be an atom" do |
167 | 199 | assert_raise ArgumentError, "a field name must be an atom, got 3", fn -> |
168 | 200 | defmodule InvalidStruct do |
@@ -237,14 +269,20 @@ defmodule TypedStructTest do |
237 | 269 | defp standardise({:type, _, type, params}, struct), |
238 | 270 | do: {:type, :line, type, standardise(params, struct)} |
239 | 271 |
|
| 272 | + defp standardise({:user_type, _, type, params}, struct), |
| 273 | + do: {:user_type, :line, type, standardise(params, struct)} |
| 274 | + |
240 | 275 | defp standardise({:remote_type, _, params}, struct), |
241 | 276 | do: {:remote_type, :line, standardise(params, struct)} |
242 | 277 |
|
243 | 278 | defp standardise({:atom, _, struct}, struct), |
244 | 279 | do: {:atom, :line, TestStruct} |
245 | 280 |
|
| 281 | + defp standardise({:var, _, name}, _), |
| 282 | + do: {:var, :line, name} |
| 283 | + |
246 | 284 | defp standardise({name, type, params}, struct) when is_tuple(type), |
247 | | - do: {name, standardise(type, struct), params} |
| 285 | + do: {name, standardise(type, struct), standardise(params, struct)} |
248 | 286 |
|
249 | 287 | defp standardise({type, _, literal}, _struct), |
250 | 288 | do: {type, :line, literal} |
|
0 commit comments