From 9c935dbad97e5f6c4e1811a9b17bfe2085767d93 Mon Sep 17 00:00:00 2001 From: Bastien CHAMAGNE Date: Thu, 9 Mar 2023 09:50:56 +0100 Subject: [PATCH] move version1 into interpreter --- lib/archethic/contracts/interpreter.ex | 66 ++++++++++++- .../{version1 => }/action_interpreter.ex | 12 +-- .../{version1 => }/common_interpreter.ex | 8 +- .../{version1 => }/condition_interpreter.ex | 12 +-- .../{version1 => }/condition_validator.ex | 4 +- .../interpreter/{version1 => }/library.ex | 2 +- .../{version1 => }/library/common/chain.ex | 4 +- .../{version1 => }/library/common/crypto.ex | 4 +- .../{version1 => }/library/common/json.ex | 4 +- .../{version1 => }/library/common/list.ex | 4 +- .../{version1 => }/library/common/map.ex | 4 +- .../{version1 => }/library/common/regex.ex | 4 +- .../{version1 => }/library/common/string.ex | 4 +- .../{version1 => }/library/common/time.ex | 4 +- .../{version1 => }/library/common/token.ex | 4 +- .../{version1 => }/library/contract.ex | 4 +- .../interpreter/{version1 => }/scope.ex | 2 +- .../contracts/interpreter/version1.ex | 97 ------------------- .../action_interpreter_test.exs | 4 +- .../condition_interpreter_test.exs | 6 +- .../action_interpreter_test.exs | 0 .../condition_interpreter_test.exs | 0 .../{version0 => legacy}/library_test.exs | 0 .../transaction_statements_test.exs | 0 .../{version0_test.exs => legacy_test.exs} | 0 .../library/common/chain_test.exs | 6 +- .../library/common/crypto_test.exs | 6 +- .../library/common/json_test.exs | 6 +- .../library/common/list_test.exs | 6 +- .../library/common/map_test.exs | 6 +- .../library/common/regex_test.exs | 6 +- .../library/common/string_test.exs | 6 +- .../library/common/time_test.exs | 6 +- .../library/common/token_test.exs | 6 +- .../{version1 => }/library/contract_test.exs | 6 +- .../interpreter/{version1 => }/scope_test.exs | 4 +- .../contracts/interpreter/version1_test.exs | 47 --------- test/archethic/contracts/interpreter_test.exs | 67 +++++++++++++ 38 files changed, 206 insertions(+), 225 deletions(-) rename lib/archethic/contracts/interpreter/{version1 => }/action_interpreter.ex (94%) rename lib/archethic/contracts/interpreter/{version1 => }/common_interpreter.ex (97%) rename lib/archethic/contracts/interpreter/{version1 => }/condition_interpreter.ex (93%) rename lib/archethic/contracts/interpreter/{version1 => }/condition_validator.ex (96%) rename lib/archethic/contracts/interpreter/{version1 => }/library.ex (95%) rename lib/archethic/contracts/interpreter/{version1 => }/library/common/chain.ex (88%) rename lib/archethic/contracts/interpreter/{version1 => }/library/common/crypto.ex (82%) rename lib/archethic/contracts/interpreter/{version1 => }/library/common/json.ex (91%) rename lib/archethic/contracts/interpreter/{version1 => }/library/common/list.ex (93%) rename lib/archethic/contracts/interpreter/{version1 => }/library/common/map.ex (92%) rename lib/archethic/contracts/interpreter/{version1 => }/library/common/regex.ex (93%) rename lib/archethic/contracts/interpreter/{version1 => }/library/common/string.ex (92%) rename lib/archethic/contracts/interpreter/{version1 => }/library/common/time.ex (71%) rename lib/archethic/contracts/interpreter/{version1 => }/library/common/token.ex (78%) rename lib/archethic/contracts/interpreter/{version1 => }/library/contract.ex (97%) rename lib/archethic/contracts/interpreter/{version1 => }/scope.ex (98%) delete mode 100644 lib/archethic/contracts/interpreter/version1.ex rename test/archethic/contracts/interpreter/{version1 => }/action_interpreter_test.exs (99%) rename test/archethic/contracts/interpreter/{version1 => }/condition_interpreter_test.exs (98%) rename test/archethic/contracts/interpreter/{version0 => legacy}/action_interpreter_test.exs (100%) rename test/archethic/contracts/interpreter/{version0 => legacy}/condition_interpreter_test.exs (100%) rename test/archethic/contracts/interpreter/{version0 => legacy}/library_test.exs (100%) rename test/archethic/contracts/interpreter/{version0 => legacy}/transaction_statements_test.exs (100%) rename test/archethic/contracts/interpreter/{version0_test.exs => legacy_test.exs} (100%) rename test/archethic/contracts/interpreter/{version1 => }/library/common/chain_test.exs (95%) rename test/archethic/contracts/interpreter/{version1 => }/library/common/crypto_test.exs (87%) rename test/archethic/contracts/interpreter/{version1 => }/library/common/json_test.exs (93%) rename test/archethic/contracts/interpreter/{version1 => }/library/common/list_test.exs (95%) rename test/archethic/contracts/interpreter/{version1 => }/library/common/map_test.exs (95%) rename test/archethic/contracts/interpreter/{version1 => }/library/common/regex_test.exs (93%) rename test/archethic/contracts/interpreter/{version1 => }/library/common/string_test.exs (93%) rename test/archethic/contracts/interpreter/{version1 => }/library/common/time_test.exs (84%) rename test/archethic/contracts/interpreter/{version1 => }/library/common/token_test.exs (91%) rename test/archethic/contracts/interpreter/{version1 => }/library/contract_test.exs (98%) rename test/archethic/contracts/interpreter/{version1 => }/scope_test.exs (92%) delete mode 100644 test/archethic/contracts/interpreter/version1_test.exs diff --git a/lib/archethic/contracts/interpreter.ex b/lib/archethic/contracts/interpreter.ex index 88ccca3fe..fb5503279 100644 --- a/lib/archethic/contracts/interpreter.ex +++ b/lib/archethic/contracts/interpreter.ex @@ -4,7 +4,9 @@ defmodule Archethic.Contracts.Interpreter do require Logger alias __MODULE__.Legacy - alias __MODULE__.Version1 + alias __MODULE__.ActionInterpreter + alias __MODULE__.ConditionInterpreter + alias __MODULE__.ConditionValidator alias Archethic.Contracts.Contract alias Archethic.Contracts.ContractConditions, as: Conditions @@ -23,7 +25,7 @@ defmodule Archethic.Contracts.Interpreter do {:ok, block} -> case block do {:__block__, [], [{:@, _, [{{:atom, "version"}, _, [version]}]} | rest]} -> - Version1.parse({:__block__, [], rest}, version) + parse_contract(version, rest) _ -> Legacy.parse(block) @@ -54,7 +56,7 @@ defmodule Archethic.Contracts.Interpreter do end def valid_conditions?(1, conditions, constants) do - Version1.valid_conditions?(conditions, constants) + ConditionValidator.valid_conditions?(conditions, constants) end @doc """ @@ -67,7 +69,7 @@ defmodule Archethic.Contracts.Interpreter do end def execute_trigger(1, ast, constants) do - Version1.execute_trigger(ast, constants) + ActionInterpreter.execute(ast, constants) end @doc """ @@ -126,6 +128,14 @@ defmodule Archethic.Contracts.Interpreter do do_format_error_reason(reason, key, []) end + # ------------------------------------------------------------ + # _ _ + # _ __ _ __(___ ____ _| |_ ___ + # | '_ \| '__| \ \ / / _` | __/ _ \ + # | |_) | | | |\ V | (_| | || __/ + # | .__/|_| |_| \_/ \__,_|\__\___| + # |_| + # ------------------------------------------------------------ defp do_format_error_reason(message, cause, metadata) do message = prepare_message(message) @@ -153,4 +163,52 @@ defmodule Archethic.Contracts.Interpreter do {:ok, {:atom, atom}} end end + + defp parse_contract(1, ast) do + case parse_ast_block(ast, %Contract{}) do + {:ok, contract} -> + {:ok, %{contract | version: 1}} + + {:error, node, reason} -> + {:error, format_error_reason(node, reason)} + end + end + + defp parse_contract(_version, _ast) do + {:error, "@version not supported"} + end + + defp parse_ast_block([ast | rest], contract) do + case parse_ast(ast, contract) do + {:ok, contract} -> + parse_ast_block(rest, contract) + + {:error, _, _} = e -> + e + end + end + + defp parse_ast_block([], contract), do: {:ok, contract} + + defp parse_ast(ast = {{:atom, "condition"}, _, _}, contract) do + case ConditionInterpreter.parse(ast) do + {:ok, condition_type, condition} -> + {:ok, Contract.add_condition(contract, condition_type, condition)} + + {:error, _, _} = e -> + e + end + end + + defp parse_ast(ast = {{:atom, "actions"}, _, _}, contract) do + case ActionInterpreter.parse(ast) do + {:ok, trigger_type, actions} -> + {:ok, Contract.add_trigger(contract, trigger_type, actions)} + + {:error, _, _} = e -> + e + end + end + + defp parse_ast(ast, _), do: {:error, ast, "unexpected term"} end diff --git a/lib/archethic/contracts/interpreter/version1/action_interpreter.ex b/lib/archethic/contracts/interpreter/action_interpreter.ex similarity index 94% rename from lib/archethic/contracts/interpreter/version1/action_interpreter.ex rename to lib/archethic/contracts/interpreter/action_interpreter.ex index 20f7593a2..7175a187d 100644 --- a/lib/archethic/contracts/interpreter/version1/action_interpreter.ex +++ b/lib/archethic/contracts/interpreter/action_interpreter.ex @@ -1,13 +1,13 @@ -defmodule Archethic.Contracts.Interpreter.Version1.ActionInterpreter do +defmodule Archethic.Contracts.Interpreter.ActionInterpreter do @moduledoc false alias Archethic.TransactionChain.Transaction alias Archethic.TransactionChain.TransactionData alias Archethic.Contracts.Interpreter.ASTHelper, as: AST - alias Archethic.Contracts.Interpreter.Version1.CommonInterpreter - alias Archethic.Contracts.Interpreter.Version1.Library - alias Archethic.Contracts.Interpreter.Version1.Scope + alias Archethic.Contracts.Interpreter.CommonInterpreter + alias Archethic.Contracts.Interpreter.Library + alias Archethic.Contracts.Interpreter.Scope @doc """ Parse the given node and return the trigger and the actions block. @@ -160,7 +160,7 @@ defmodule Archethic.Contracts.Interpreter.Version1.ActionInterpreter do # it is bound in the root scope new_node = quote do - Archethic.Contracts.Interpreter.Version1.Library.Contract.get_calls( + Archethic.Contracts.Interpreter.Library.Contract.get_calls( Scope.read_global(["contract", "address"]) ) end @@ -177,7 +177,7 @@ defmodule Archethic.Contracts.Interpreter.Version1.ActionInterpreter do {{:., _meta, [{:__aliases__, _, [atom: "Contract"]}, {:atom, function_name}]}, _, args}, acc ) do - absolute_module_atom = Archethic.Contracts.Interpreter.Version1.Library.Contract + absolute_module_atom = Archethic.Contracts.Interpreter.Library.Contract # check function exists unless Library.function_exists?(absolute_module_atom, function_name) do diff --git a/lib/archethic/contracts/interpreter/version1/common_interpreter.ex b/lib/archethic/contracts/interpreter/common_interpreter.ex similarity index 97% rename from lib/archethic/contracts/interpreter/version1/common_interpreter.ex rename to lib/archethic/contracts/interpreter/common_interpreter.ex index e218591e4..a7b231b42 100644 --- a/lib/archethic/contracts/interpreter/version1/common_interpreter.ex +++ b/lib/archethic/contracts/interpreter/common_interpreter.ex @@ -1,4 +1,4 @@ -defmodule Archethic.Contracts.Interpreter.Version1.CommonInterpreter do +defmodule Archethic.Contracts.Interpreter.CommonInterpreter do @moduledoc """ The prewalk and postwalk functions receive an `acc` for convenience. They should see it as an opaque variable and just forward it. @@ -7,8 +7,8 @@ defmodule Archethic.Contracts.Interpreter.Version1.CommonInterpreter do """ alias Archethic.Contracts.Interpreter.ASTHelper, as: AST - alias Archethic.Contracts.Interpreter.Version1.Library - alias Archethic.Contracts.Interpreter.Version1.Scope + alias Archethic.Contracts.Interpreter.Library + alias Archethic.Contracts.Interpreter.Scope @modules_whitelisted Library.list_common_modules() @@ -282,7 +282,7 @@ defmodule Archethic.Contracts.Interpreter.Version1.CommonInterpreter do absolute_module_atom = Code.ensure_loaded!( String.to_existing_atom( - "Elixir.Archethic.Contracts.Interpreter.Version1.Library.Common.#{module_name}" + "Elixir.Archethic.Contracts.Interpreter.Library.Common.#{module_name}" ) ) diff --git a/lib/archethic/contracts/interpreter/version1/condition_interpreter.ex b/lib/archethic/contracts/interpreter/condition_interpreter.ex similarity index 93% rename from lib/archethic/contracts/interpreter/version1/condition_interpreter.ex rename to lib/archethic/contracts/interpreter/condition_interpreter.ex index 1d8d1b74d..f6114a566 100644 --- a/lib/archethic/contracts/interpreter/version1/condition_interpreter.ex +++ b/lib/archethic/contracts/interpreter/condition_interpreter.ex @@ -1,9 +1,9 @@ -defmodule Archethic.Contracts.Interpreter.Version1.ConditionInterpreter do +defmodule Archethic.Contracts.Interpreter.ConditionInterpreter do @moduledoc false - alias Archethic.Contracts.Interpreter.Version1.CommonInterpreter - alias Archethic.Contracts.Interpreter.Version1.Library - alias Archethic.Contracts.Interpreter.Version1.Scope + alias Archethic.Contracts.Interpreter.CommonInterpreter + alias Archethic.Contracts.Interpreter.Library + alias Archethic.Contracts.Interpreter.Scope alias Archethic.Contracts.ContractConditions, as: Conditions alias Archethic.Contracts.Interpreter.ASTHelper, as: AST @@ -135,7 +135,7 @@ defmodule Archethic.Contracts.Interpreter.Version1.ConditionInterpreter do absolute_module_atom = String.to_existing_atom( - "Elixir.Archethic.Contracts.Interpreter.Version1.Library.Common.#{module_name}" + "Elixir.Archethic.Contracts.Interpreter.Library.Common.#{module_name}" ) new_node = @@ -180,7 +180,7 @@ defmodule Archethic.Contracts.Interpreter.Version1.ConditionInterpreter do ) do # new_node = # quote do - # Archethic.Contracts.Interpreter.Version1.Library.Contract.get_calls( + # Archethic.Contracts.Interpreter.Library.Contract.get_calls( # Scope.read_global([unquote(global_variable), "address"]) # ) # end diff --git a/lib/archethic/contracts/interpreter/version1/condition_validator.ex b/lib/archethic/contracts/interpreter/condition_validator.ex similarity index 96% rename from lib/archethic/contracts/interpreter/version1/condition_validator.ex rename to lib/archethic/contracts/interpreter/condition_validator.ex index e52ca7db9..05e997f8a 100644 --- a/lib/archethic/contracts/interpreter/version1/condition_validator.ex +++ b/lib/archethic/contracts/interpreter/condition_validator.ex @@ -1,4 +1,4 @@ -defmodule Archethic.Contracts.Interpreter.Version1.ConditionValidator do +defmodule Archethic.Contracts.Interpreter.ConditionValidator do @moduledoc """ This is pretty much a copy of Legacy.ConditionInterpreter. The difference is where the scope is stored (process dict VS global variable) @@ -7,7 +7,7 @@ defmodule Archethic.Contracts.Interpreter.Version1.ConditionValidator do alias Archethic.Contracts.ContractConditions, as: Conditions alias Archethic.Contracts.ContractConstants, as: Constants alias Archethic.Contracts.Interpreter - alias Archethic.Contracts.Interpreter.Version1.Scope + alias Archethic.Contracts.Interpreter.Scope require Logger diff --git a/lib/archethic/contracts/interpreter/version1/library.ex b/lib/archethic/contracts/interpreter/library.ex similarity index 95% rename from lib/archethic/contracts/interpreter/version1/library.ex rename to lib/archethic/contracts/interpreter/library.ex index cf7d6065a..3d0ac3467 100644 --- a/lib/archethic/contracts/interpreter/version1/library.ex +++ b/lib/archethic/contracts/interpreter/library.ex @@ -1,4 +1,4 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library do +defmodule Archethic.Contracts.Interpreter.Library do @moduledoc false @doc """ diff --git a/lib/archethic/contracts/interpreter/version1/library/common/chain.ex b/lib/archethic/contracts/interpreter/library/common/chain.ex similarity index 88% rename from lib/archethic/contracts/interpreter/version1/library/common/chain.ex rename to lib/archethic/contracts/interpreter/library/common/chain.ex index a24f8d929..d3bc01b5a 100644 --- a/lib/archethic/contracts/interpreter/version1/library/common/chain.ex +++ b/lib/archethic/contracts/interpreter/library/common/chain.ex @@ -1,6 +1,6 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.Chain do +defmodule Archethic.Contracts.Interpreter.Library.Common.Chain do @moduledoc false - @behaviour Archethic.Contracts.Interpreter.Version1.Library + @behaviour Archethic.Contracts.Interpreter.Library alias Archethic.Contracts.Interpreter.ASTHelper, as: AST alias Archethic.Contracts.Interpreter.Legacy diff --git a/lib/archethic/contracts/interpreter/version1/library/common/crypto.ex b/lib/archethic/contracts/interpreter/library/common/crypto.ex similarity index 82% rename from lib/archethic/contracts/interpreter/version1/library/common/crypto.ex rename to lib/archethic/contracts/interpreter/library/common/crypto.ex index 243d8d8c6..521c1c458 100644 --- a/lib/archethic/contracts/interpreter/version1/library/common/crypto.ex +++ b/lib/archethic/contracts/interpreter/library/common/crypto.ex @@ -1,6 +1,6 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.Crypto do +defmodule Archethic.Contracts.Interpreter.Library.Common.Crypto do @moduledoc false - @behaviour Archethic.Contracts.Interpreter.Version1.Library + @behaviour Archethic.Contracts.Interpreter.Library alias Archethic.Contracts.Interpreter.ASTHelper, as: AST alias Archethic.Contracts.Interpreter.Legacy diff --git a/lib/archethic/contracts/interpreter/version1/library/common/json.ex b/lib/archethic/contracts/interpreter/library/common/json.ex similarity index 91% rename from lib/archethic/contracts/interpreter/version1/library/common/json.ex rename to lib/archethic/contracts/interpreter/library/common/json.ex index f8cee9289..e85fbf6fc 100644 --- a/lib/archethic/contracts/interpreter/version1/library/common/json.ex +++ b/lib/archethic/contracts/interpreter/library/common/json.ex @@ -1,6 +1,6 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.Json do +defmodule Archethic.Contracts.Interpreter.Library.Common.Json do @moduledoc false - @behaviour Archethic.Contracts.Interpreter.Version1.Library + @behaviour Archethic.Contracts.Interpreter.Library alias Archethic.Contracts.Interpreter.ASTHelper, as: AST alias Archethic.Contracts.Interpreter.Legacy diff --git a/lib/archethic/contracts/interpreter/version1/library/common/list.ex b/lib/archethic/contracts/interpreter/library/common/list.ex similarity index 93% rename from lib/archethic/contracts/interpreter/version1/library/common/list.ex rename to lib/archethic/contracts/interpreter/library/common/list.ex index 9144ad9ed..64b69509b 100644 --- a/lib/archethic/contracts/interpreter/version1/library/common/list.ex +++ b/lib/archethic/contracts/interpreter/library/common/list.ex @@ -1,6 +1,6 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.List do +defmodule Archethic.Contracts.Interpreter.Library.Common.List do @moduledoc false - @behaviour Archethic.Contracts.Interpreter.Version1.Library + @behaviour Archethic.Contracts.Interpreter.Library alias Archethic.Contracts.Interpreter.ASTHelper, as: AST diff --git a/lib/archethic/contracts/interpreter/version1/library/common/map.ex b/lib/archethic/contracts/interpreter/library/common/map.ex similarity index 92% rename from lib/archethic/contracts/interpreter/version1/library/common/map.ex rename to lib/archethic/contracts/interpreter/library/common/map.ex index 1aa80da5b..797600f9d 100644 --- a/lib/archethic/contracts/interpreter/version1/library/common/map.ex +++ b/lib/archethic/contracts/interpreter/library/common/map.ex @@ -1,6 +1,6 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.Map do +defmodule Archethic.Contracts.Interpreter.Library.Common.Map do @moduledoc false - @behaviour Archethic.Contracts.Interpreter.Version1.Library + @behaviour Archethic.Contracts.Interpreter.Library alias Archethic.Contracts.Interpreter.ASTHelper, as: AST diff --git a/lib/archethic/contracts/interpreter/version1/library/common/regex.ex b/lib/archethic/contracts/interpreter/library/common/regex.ex similarity index 93% rename from lib/archethic/contracts/interpreter/version1/library/common/regex.ex rename to lib/archethic/contracts/interpreter/library/common/regex.ex index 28d5ae719..4625cbd0d 100644 --- a/lib/archethic/contracts/interpreter/version1/library/common/regex.ex +++ b/lib/archethic/contracts/interpreter/library/common/regex.ex @@ -1,6 +1,6 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.Regex do +defmodule Archethic.Contracts.Interpreter.Library.Common.Regex do @moduledoc false - @behaviour Archethic.Contracts.Interpreter.Version1.Library + @behaviour Archethic.Contracts.Interpreter.Library alias Archethic.Contracts.Interpreter.ASTHelper, as: AST alias Archethic.Contracts.Interpreter.Legacy diff --git a/lib/archethic/contracts/interpreter/version1/library/common/string.ex b/lib/archethic/contracts/interpreter/library/common/string.ex similarity index 92% rename from lib/archethic/contracts/interpreter/version1/library/common/string.ex rename to lib/archethic/contracts/interpreter/library/common/string.ex index 4fcab5e70..264f9773d 100644 --- a/lib/archethic/contracts/interpreter/version1/library/common/string.ex +++ b/lib/archethic/contracts/interpreter/library/common/string.ex @@ -1,6 +1,6 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.String do +defmodule Archethic.Contracts.Interpreter.Library.Common.String do @moduledoc false - @behaviour Archethic.Contracts.Interpreter.Version1.Library + @behaviour Archethic.Contracts.Interpreter.Library alias Archethic.Contracts.Interpreter.ASTHelper, as: AST diff --git a/lib/archethic/contracts/interpreter/version1/library/common/time.ex b/lib/archethic/contracts/interpreter/library/common/time.ex similarity index 71% rename from lib/archethic/contracts/interpreter/version1/library/common/time.ex rename to lib/archethic/contracts/interpreter/library/common/time.ex index c691dd809..19bcfc368 100644 --- a/lib/archethic/contracts/interpreter/version1/library/common/time.ex +++ b/lib/archethic/contracts/interpreter/library/common/time.ex @@ -1,6 +1,6 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.Time do +defmodule Archethic.Contracts.Interpreter.Library.Common.Time do @moduledoc false - @behaviour Archethic.Contracts.Interpreter.Version1.Library + @behaviour Archethic.Contracts.Interpreter.Library @doc """ Returns current time in unix timestamp format. diff --git a/lib/archethic/contracts/interpreter/version1/library/common/token.ex b/lib/archethic/contracts/interpreter/library/common/token.ex similarity index 78% rename from lib/archethic/contracts/interpreter/version1/library/common/token.ex rename to lib/archethic/contracts/interpreter/library/common/token.ex index 7f30bc1b0..99adf44b9 100644 --- a/lib/archethic/contracts/interpreter/version1/library/common/token.ex +++ b/lib/archethic/contracts/interpreter/library/common/token.ex @@ -1,6 +1,6 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.Token do +defmodule Archethic.Contracts.Interpreter.Library.Common.Token do @moduledoc false - @behaviour Archethic.Contracts.Interpreter.Version1.Library + @behaviour Archethic.Contracts.Interpreter.Library alias Archethic.Contracts.Interpreter.ASTHelper, as: AST alias Archethic.Contracts.Interpreter.Legacy diff --git a/lib/archethic/contracts/interpreter/version1/library/contract.ex b/lib/archethic/contracts/interpreter/library/contract.ex similarity index 97% rename from lib/archethic/contracts/interpreter/version1/library/contract.ex rename to lib/archethic/contracts/interpreter/library/contract.ex index efd22f458..f52c93109 100644 --- a/lib/archethic/contracts/interpreter/version1/library/contract.ex +++ b/lib/archethic/contracts/interpreter/library/contract.ex @@ -1,10 +1,10 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Contract do +defmodule Archethic.Contracts.Interpreter.Library.Contract do @moduledoc """ We are delegating to the legacy transaction statements. This is fine as long as we don't need to change anything. If there's something to change for version 1, do the change in here, not in legacy. """ - @behaviour Archethic.Contracts.Interpreter.Version1.Library + @behaviour Archethic.Contracts.Interpreter.Library alias Archethic.Contracts.Interpreter.ASTHelper, as: AST alias Archethic.TransactionChain.Transaction diff --git a/lib/archethic/contracts/interpreter/version1/scope.ex b/lib/archethic/contracts/interpreter/scope.ex similarity index 98% rename from lib/archethic/contracts/interpreter/version1/scope.ex rename to lib/archethic/contracts/interpreter/scope.ex index 75b5691f4..59cb2dfa2 100644 --- a/lib/archethic/contracts/interpreter/version1/scope.ex +++ b/lib/archethic/contracts/interpreter/scope.ex @@ -1,4 +1,4 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Scope do +defmodule Archethic.Contracts.Interpreter.Scope do @moduledoc """ Helper functions to deal with scopes """ diff --git a/lib/archethic/contracts/interpreter/version1.ex b/lib/archethic/contracts/interpreter/version1.ex deleted file mode 100644 index 75aff6d86..000000000 --- a/lib/archethic/contracts/interpreter/version1.ex +++ /dev/null @@ -1,97 +0,0 @@ -defmodule Archethic.Contracts.Interpreter.Version1 do - @moduledoc false - - alias Archethic.Contracts.Contract - alias Archethic.Contracts.ContractConditions, as: Conditions - alias Archethic.Contracts.Interpreter - - alias __MODULE__.ActionInterpreter - alias __MODULE__.ConditionInterpreter - alias __MODULE__.ConditionValidator - - alias Archethic.TransactionChain.Transaction - - @doc """ - Parse the code and return the parsed contract. - """ - @spec parse(Macro.t(), integer()) :: - {:ok, Contract.t()} | {:error, String.t()} - def parse(ast, version = 1) do - case parse_contract(ast, %Contract{}) do - {:ok, contract} -> - {:ok, %{contract | version: version}} - - {:error, node, reason} -> - {:error, Interpreter.format_error_reason(node, reason)} - end - end - - def parse(_, _), do: {:error, "@version not supported"} - - @doc """ - Return true if the given conditions are valid on the given constants - """ - @spec valid_conditions?(Conditions.t(), map()) :: bool() - def valid_conditions?(conditions, constants) do - ConditionValidator.valid_conditions?(conditions, constants) - end - - @doc """ - Execute the trigger/action code. - May return a new transaction or nil - """ - @spec execute_trigger(Macro.t(), map()) :: Transaction.t() | nil - def execute_trigger(ast, constants \\ %{}) do - ActionInterpreter.execute(ast, constants) - end - - # ------------------------------------------------------------ - # _ _ - # _ __ _ __(___ ____ _| |_ ___ - # | '_ \| '__| \ \ / / _` | __/ _ \ - # | |_) | | | |\ V | (_| | || __/ - # | .__/|_| |_| \_/ \__,_|\__\___| - # |_| - # ------------------------------------------------------------ - defp parse_contract({:__block__, _, ast}, contract) do - parse_ast_block(ast, contract) - end - - defp parse_contract(ast, contract) do - parse_ast(ast, contract) - end - - defp parse_ast_block([ast | rest], contract) do - case parse_ast(ast, contract) do - {:ok, contract} -> - parse_ast_block(rest, contract) - - {:error, _, _} = e -> - e - end - end - - defp parse_ast_block([], contract), do: {:ok, contract} - - defp parse_ast(ast = {{:atom, "condition"}, _, _}, contract) do - case ConditionInterpreter.parse(ast) do - {:ok, condition_type, condition} -> - {:ok, Contract.add_condition(contract, condition_type, condition)} - - {:error, _, _} = e -> - e - end - end - - defp parse_ast(ast = {{:atom, "actions"}, _, _}, contract) do - case ActionInterpreter.parse(ast) do - {:ok, trigger_type, actions} -> - {:ok, Contract.add_trigger(contract, trigger_type, actions)} - - {:error, _, _} = e -> - e - end - end - - defp parse_ast(ast, _), do: {:error, ast, "unexpected term"} -end diff --git a/test/archethic/contracts/interpreter/version1/action_interpreter_test.exs b/test/archethic/contracts/interpreter/action_interpreter_test.exs similarity index 99% rename from test/archethic/contracts/interpreter/version1/action_interpreter_test.exs rename to test/archethic/contracts/interpreter/action_interpreter_test.exs index d147cb5a6..282532b36 100644 --- a/test/archethic/contracts/interpreter/version1/action_interpreter_test.exs +++ b/test/archethic/contracts/interpreter/action_interpreter_test.exs @@ -1,8 +1,8 @@ -defmodule Archethic.Contracts.Interpreter.Version1.ActionInterpreterTest do +defmodule Archethic.Contracts.Interpreter.ActionInterpreterTest do use ArchethicCase alias Archethic.Contracts.Interpreter - alias Archethic.Contracts.Interpreter.Version1.ActionInterpreter + alias Archethic.Contracts.Interpreter.ActionInterpreter alias Archethic.TransactionChain.Transaction alias Archethic.TransactionChain.TransactionData diff --git a/test/archethic/contracts/interpreter/version1/condition_interpreter_test.exs b/test/archethic/contracts/interpreter/condition_interpreter_test.exs similarity index 98% rename from test/archethic/contracts/interpreter/version1/condition_interpreter_test.exs rename to test/archethic/contracts/interpreter/condition_interpreter_test.exs index a6feeb30e..f9b49f334 100644 --- a/test/archethic/contracts/interpreter/version1/condition_interpreter_test.exs +++ b/test/archethic/contracts/interpreter/condition_interpreter_test.exs @@ -1,10 +1,10 @@ -defmodule Archethic.Contracts.Interpreter.Version1.ConditionInterpreterTest do +defmodule Archethic.Contracts.Interpreter.ConditionInterpreterTest do use ArchethicCase alias Archethic.Contracts.ContractConditions, as: Conditions alias Archethic.Contracts.Interpreter - alias Archethic.Contracts.Interpreter.Version1.ConditionInterpreter - alias Archethic.Contracts.Interpreter.Version1.ConditionValidator + alias Archethic.Contracts.Interpreter.ConditionInterpreter + alias Archethic.Contracts.Interpreter.ConditionValidator doctest ConditionInterpreter diff --git a/test/archethic/contracts/interpreter/version0/action_interpreter_test.exs b/test/archethic/contracts/interpreter/legacy/action_interpreter_test.exs similarity index 100% rename from test/archethic/contracts/interpreter/version0/action_interpreter_test.exs rename to test/archethic/contracts/interpreter/legacy/action_interpreter_test.exs diff --git a/test/archethic/contracts/interpreter/version0/condition_interpreter_test.exs b/test/archethic/contracts/interpreter/legacy/condition_interpreter_test.exs similarity index 100% rename from test/archethic/contracts/interpreter/version0/condition_interpreter_test.exs rename to test/archethic/contracts/interpreter/legacy/condition_interpreter_test.exs diff --git a/test/archethic/contracts/interpreter/version0/library_test.exs b/test/archethic/contracts/interpreter/legacy/library_test.exs similarity index 100% rename from test/archethic/contracts/interpreter/version0/library_test.exs rename to test/archethic/contracts/interpreter/legacy/library_test.exs diff --git a/test/archethic/contracts/interpreter/version0/transaction_statements_test.exs b/test/archethic/contracts/interpreter/legacy/transaction_statements_test.exs similarity index 100% rename from test/archethic/contracts/interpreter/version0/transaction_statements_test.exs rename to test/archethic/contracts/interpreter/legacy/transaction_statements_test.exs diff --git a/test/archethic/contracts/interpreter/version0_test.exs b/test/archethic/contracts/interpreter/legacy_test.exs similarity index 100% rename from test/archethic/contracts/interpreter/version0_test.exs rename to test/archethic/contracts/interpreter/legacy_test.exs diff --git a/test/archethic/contracts/interpreter/version1/library/common/chain_test.exs b/test/archethic/contracts/interpreter/library/common/chain_test.exs similarity index 95% rename from test/archethic/contracts/interpreter/version1/library/common/chain_test.exs rename to test/archethic/contracts/interpreter/library/common/chain_test.exs index 76a2e1319..3ba71de90 100644 --- a/test/archethic/contracts/interpreter/version1/library/common/chain_test.exs +++ b/test/archethic/contracts/interpreter/library/common/chain_test.exs @@ -1,4 +1,4 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.ChainTest do +defmodule Archethic.Contracts.Interpreter.Library.Common.ChainTest do @moduledoc """ Here we test the module within the action block. Because there is AST modification (such as keywords to maps) in the ActionInterpreter and we want to test the whole thing. @@ -7,8 +7,8 @@ defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.ChainTest do use ArchethicCase alias Archethic.Contracts.Interpreter - alias Archethic.Contracts.Interpreter.Version1.ActionInterpreter - alias Archethic.Contracts.Interpreter.Version1.Library.Common.Chain + alias Archethic.Contracts.Interpreter.ActionInterpreter + alias Archethic.Contracts.Interpreter.Library.Common.Chain alias Archethic.Crypto diff --git a/test/archethic/contracts/interpreter/version1/library/common/crypto_test.exs b/test/archethic/contracts/interpreter/library/common/crypto_test.exs similarity index 87% rename from test/archethic/contracts/interpreter/version1/library/common/crypto_test.exs rename to test/archethic/contracts/interpreter/library/common/crypto_test.exs index cb5f775bb..347d97790 100644 --- a/test/archethic/contracts/interpreter/version1/library/common/crypto_test.exs +++ b/test/archethic/contracts/interpreter/library/common/crypto_test.exs @@ -1,4 +1,4 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.CryptoTest do +defmodule Archethic.Contracts.Interpreter.Library.Common.CryptoTest do @moduledoc """ Here we test the module within the action block. Because there is AST modification (such as keywords to maps) in the ActionInterpreter and we want to test the whole thing. @@ -7,8 +7,8 @@ defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.CryptoTest do use ArchethicCase alias Archethic.Contracts.Interpreter - alias Archethic.Contracts.Interpreter.Version1.ActionInterpreter - alias Archethic.Contracts.Interpreter.Version1.Library.Common.Crypto + alias Archethic.Contracts.Interpreter.ActionInterpreter + alias Archethic.Contracts.Interpreter.Library.Common.Crypto alias Archethic.TransactionChain.Transaction alias Archethic.TransactionChain.TransactionData diff --git a/test/archethic/contracts/interpreter/version1/library/common/json_test.exs b/test/archethic/contracts/interpreter/library/common/json_test.exs similarity index 93% rename from test/archethic/contracts/interpreter/version1/library/common/json_test.exs rename to test/archethic/contracts/interpreter/library/common/json_test.exs index 4439bd43a..53c9177ce 100644 --- a/test/archethic/contracts/interpreter/version1/library/common/json_test.exs +++ b/test/archethic/contracts/interpreter/library/common/json_test.exs @@ -1,4 +1,4 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.JsonTest do +defmodule Archethic.Contracts.Interpreter.Library.Common.JsonTest do @moduledoc """ Here we test the module within the action block. Because there is AST modification (such as keywords to maps) in the ActionInterpreter and we want to test the whole thing. @@ -7,8 +7,8 @@ defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.JsonTest do use ArchethicCase alias Archethic.Contracts.Interpreter - alias Archethic.Contracts.Interpreter.Version1.ActionInterpreter - alias Archethic.Contracts.Interpreter.Version1.Library.Common.Json + alias Archethic.Contracts.Interpreter.ActionInterpreter + alias Archethic.Contracts.Interpreter.Library.Common.Json alias Archethic.TransactionChain.Transaction alias Archethic.TransactionChain.TransactionData diff --git a/test/archethic/contracts/interpreter/version1/library/common/list_test.exs b/test/archethic/contracts/interpreter/library/common/list_test.exs similarity index 95% rename from test/archethic/contracts/interpreter/version1/library/common/list_test.exs rename to test/archethic/contracts/interpreter/library/common/list_test.exs index caf925640..5b17331d0 100644 --- a/test/archethic/contracts/interpreter/version1/library/common/list_test.exs +++ b/test/archethic/contracts/interpreter/library/common/list_test.exs @@ -1,4 +1,4 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.ListTest do +defmodule Archethic.Contracts.Interpreter.Library.Common.ListTest do @moduledoc """ Here we test the module within the action block. Because there is AST modification (such as keywords to maps) in the ActionInterpreter and we want to test the whole thing. @@ -7,8 +7,8 @@ defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.ListTest do use ArchethicCase alias Archethic.Contracts.Interpreter - alias Archethic.Contracts.Interpreter.Version1.ActionInterpreter - alias Archethic.Contracts.Interpreter.Version1.Library.Common.List + alias Archethic.Contracts.Interpreter.ActionInterpreter + alias Archethic.Contracts.Interpreter.Library.Common.List alias Archethic.TransactionChain.Transaction alias Archethic.TransactionChain.TransactionData diff --git a/test/archethic/contracts/interpreter/version1/library/common/map_test.exs b/test/archethic/contracts/interpreter/library/common/map_test.exs similarity index 95% rename from test/archethic/contracts/interpreter/version1/library/common/map_test.exs rename to test/archethic/contracts/interpreter/library/common/map_test.exs index 4c56141f9..198dfe498 100644 --- a/test/archethic/contracts/interpreter/version1/library/common/map_test.exs +++ b/test/archethic/contracts/interpreter/library/common/map_test.exs @@ -1,4 +1,4 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.MapTest do +defmodule Archethic.Contracts.Interpreter.Library.Common.MapTest do @moduledoc """ Here we test the module within the action block. Because there is AST modification (such as keywords to maps) in the ActionInterpreter and we want to test the whole thing. @@ -7,8 +7,8 @@ defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.MapTest do use ArchethicCase alias Archethic.Contracts.Interpreter - alias Archethic.Contracts.Interpreter.Version1.ActionInterpreter - alias Archethic.Contracts.Interpreter.Version1.Library.Common.Map + alias Archethic.Contracts.Interpreter.ActionInterpreter + alias Archethic.Contracts.Interpreter.Library.Common.Map alias Archethic.TransactionChain.Transaction alias Archethic.TransactionChain.TransactionData diff --git a/test/archethic/contracts/interpreter/version1/library/common/regex_test.exs b/test/archethic/contracts/interpreter/library/common/regex_test.exs similarity index 93% rename from test/archethic/contracts/interpreter/version1/library/common/regex_test.exs rename to test/archethic/contracts/interpreter/library/common/regex_test.exs index 7ba852fc1..c1bdb7197 100644 --- a/test/archethic/contracts/interpreter/version1/library/common/regex_test.exs +++ b/test/archethic/contracts/interpreter/library/common/regex_test.exs @@ -1,4 +1,4 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.RegexTest do +defmodule Archethic.Contracts.Interpreter.Library.Common.RegexTest do @moduledoc """ Here we test the module within the action block. Because there is AST modification (such as keywords to maps) in the ActionInterpreter and we want to test the whole thing. @@ -7,8 +7,8 @@ defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.RegexTest do use ArchethicCase alias Archethic.Contracts.Interpreter - alias Archethic.Contracts.Interpreter.Version1.ActionInterpreter - alias Archethic.Contracts.Interpreter.Version1.Library.Common.Regex + alias Archethic.Contracts.Interpreter.ActionInterpreter + alias Archethic.Contracts.Interpreter.Library.Common.Regex alias Archethic.TransactionChain.Transaction alias Archethic.TransactionChain.TransactionData diff --git a/test/archethic/contracts/interpreter/version1/library/common/string_test.exs b/test/archethic/contracts/interpreter/library/common/string_test.exs similarity index 93% rename from test/archethic/contracts/interpreter/version1/library/common/string_test.exs rename to test/archethic/contracts/interpreter/library/common/string_test.exs index 052038ad5..4431f2c35 100644 --- a/test/archethic/contracts/interpreter/version1/library/common/string_test.exs +++ b/test/archethic/contracts/interpreter/library/common/string_test.exs @@ -1,4 +1,4 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.StringTest do +defmodule Archethic.Contracts.Interpreter.Library.Common.StringTest do @moduledoc """ Here we test the module within the action block. Because there is AST modification (such as keywords to maps) in the ActionInterpreter and we want to test the whole thing. @@ -7,8 +7,8 @@ defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.StringTest do use ArchethicCase alias Archethic.Contracts.Interpreter - alias Archethic.Contracts.Interpreter.Version1.ActionInterpreter - alias Archethic.Contracts.Interpreter.Version1.Library.Common.String + alias Archethic.Contracts.Interpreter.ActionInterpreter + alias Archethic.Contracts.Interpreter.Library.Common.String alias Archethic.TransactionChain.Transaction alias Archethic.TransactionChain.TransactionData diff --git a/test/archethic/contracts/interpreter/version1/library/common/time_test.exs b/test/archethic/contracts/interpreter/library/common/time_test.exs similarity index 84% rename from test/archethic/contracts/interpreter/version1/library/common/time_test.exs rename to test/archethic/contracts/interpreter/library/common/time_test.exs index 7af2c51e5..f43c9fa0b 100644 --- a/test/archethic/contracts/interpreter/version1/library/common/time_test.exs +++ b/test/archethic/contracts/interpreter/library/common/time_test.exs @@ -1,4 +1,4 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.TimeTest do +defmodule Archethic.Contracts.Interpreter.Library.Common.TimeTest do @moduledoc """ Here we test the module within the action block. Because there is AST modification (such as keywords to maps) in the ActionInterpreter and we want to test the whole thing. @@ -7,8 +7,8 @@ defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.TimeTest do use ArchethicCase alias Archethic.Contracts.Interpreter - alias Archethic.Contracts.Interpreter.Version1.ActionInterpreter - alias Archethic.Contracts.Interpreter.Version1.Library.Common.Time + alias Archethic.Contracts.Interpreter.ActionInterpreter + alias Archethic.Contracts.Interpreter.Library.Common.Time alias Archethic.TransactionChain.Transaction alias Archethic.TransactionChain.TransactionData diff --git a/test/archethic/contracts/interpreter/version1/library/common/token_test.exs b/test/archethic/contracts/interpreter/library/common/token_test.exs similarity index 91% rename from test/archethic/contracts/interpreter/version1/library/common/token_test.exs rename to test/archethic/contracts/interpreter/library/common/token_test.exs index f1e748caf..9cd24b314 100644 --- a/test/archethic/contracts/interpreter/version1/library/common/token_test.exs +++ b/test/archethic/contracts/interpreter/library/common/token_test.exs @@ -1,4 +1,4 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.TokenTest do +defmodule Archethic.Contracts.Interpreter.Library.Common.TokenTest do @moduledoc """ Here we test the module within the action block. Because there is AST modification (such as keywords to maps) in the ActionInterpreter and we want to test the whole thing. @@ -7,8 +7,8 @@ defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.TokenTest do use ArchethicCase alias Archethic.Contracts.Interpreter - alias Archethic.Contracts.Interpreter.Version1.ActionInterpreter - alias Archethic.Contracts.Interpreter.Version1.Library.Common.Token + alias Archethic.Contracts.Interpreter.ActionInterpreter + alias Archethic.Contracts.Interpreter.Library.Common.Token alias Archethic.Crypto diff --git a/test/archethic/contracts/interpreter/version1/library/contract_test.exs b/test/archethic/contracts/interpreter/library/contract_test.exs similarity index 98% rename from test/archethic/contracts/interpreter/version1/library/contract_test.exs rename to test/archethic/contracts/interpreter/library/contract_test.exs index 462d477f3..ced826797 100644 --- a/test/archethic/contracts/interpreter/version1/library/contract_test.exs +++ b/test/archethic/contracts/interpreter/library/contract_test.exs @@ -1,4 +1,4 @@ -defmodule Archethic.Contracts.Interpreter.Version1.Library.ContractTest do +defmodule Archethic.Contracts.Interpreter.Library.ContractTest do @moduledoc """ Here we test the contract module within the action block. Because there is AST modification (such as keywords to maps) in the ActionInterpreter and we want to test the whole thing. @@ -7,8 +7,8 @@ defmodule Archethic.Contracts.Interpreter.Version1.Library.ContractTest do use ArchethicCase alias Archethic.Contracts.Interpreter - alias Archethic.Contracts.Interpreter.Version1.ActionInterpreter - alias Archethic.Contracts.Interpreter.Version1.Library.Contract + alias Archethic.Contracts.Interpreter.ActionInterpreter + alias Archethic.Contracts.Interpreter.Library.Contract alias Archethic.TransactionChain.Transaction alias Archethic.TransactionChain.TransactionData diff --git a/test/archethic/contracts/interpreter/version1/scope_test.exs b/test/archethic/contracts/interpreter/scope_test.exs similarity index 92% rename from test/archethic/contracts/interpreter/version1/scope_test.exs rename to test/archethic/contracts/interpreter/scope_test.exs index a8ec7f67e..237547534 100644 --- a/test/archethic/contracts/interpreter/version1/scope_test.exs +++ b/test/archethic/contracts/interpreter/scope_test.exs @@ -1,7 +1,7 @@ -defmodule Archethic.Contracts.Interpreter.Version1.ScopeTest do +defmodule Archethic.Contracts.Interpreter.ScopeTest do use ArchethicCase - alias Archethic.Contracts.Interpreter.Version1.Scope + alias Archethic.Contracts.Interpreter.Scope doctest Scope diff --git a/test/archethic/contracts/interpreter/version1_test.exs b/test/archethic/contracts/interpreter/version1_test.exs deleted file mode 100644 index 8f6ed0d33..000000000 --- a/test/archethic/contracts/interpreter/version1_test.exs +++ /dev/null @@ -1,47 +0,0 @@ -defmodule Archethic.Contracts.Interpreter.Version1Test do - use ArchethicCase - - @version 1 - - alias Archethic.Contracts.Interpreter - alias Archethic.Contracts.Contract - alias Archethic.Contracts.Interpreter.Version1 - - doctest Version1 - - describe "parse/1" do - test "should return an error if there are unexpected terms" do - assert {:error, _} = - """ - condition transaction: [ - uco_transfers: List.size() > 0 - ] - - some_unexpected_code - - actions triggered_by: transaction do - set_content "hello" - end - """ - |> Interpreter.sanitize_code() - |> elem(1) - |> Version1.parse(@version) - end - - test "should return the contract if format is OK" do - assert {:ok, %Contract{}} = - """ - condition transaction: [ - uco_transfers: List.size() > 0 - ] - - actions triggered_by: transaction do - Contract.set_content "hello" - end - """ - |> Interpreter.sanitize_code() - |> elem(1) - |> Version1.parse(@version) - end - end -end diff --git a/test/archethic/contracts/interpreter_test.exs b/test/archethic/contracts/interpreter_test.exs index 053e1a702..9ded1dfc6 100644 --- a/test/archethic/contracts/interpreter_test.exs +++ b/test/archethic/contracts/interpreter_test.exs @@ -2,6 +2,7 @@ defmodule Archethic.Contracts.InterpreterTest do @moduledoc false use ArchethicCase + alias Archethic.Contracts.Contract alias Archethic.Contracts.Interpreter alias Archethic.ContractFactory @@ -37,4 +38,70 @@ defmodule Archethic.Contracts.InterpreterTest do assert {:error, "@version not supported"} = Interpreter.parse(code_v0) end end + + describe "parse code v1" do + test "should return an error if there are unexpected terms" do + assert {:error, _} = + """ + @version 1 + condition transaction: [ + uco_transfers: List.size() > 0 + ] + + some_unexpected_code + + actions triggered_by: transaction do + Contract.set_content "hello" + end + """ + |> Interpreter.parse() + end + + test "should return the contract if format is OK" do + assert {:ok, %Contract{}} = + """ + @version 1 + condition transaction: [ + uco_transfers: List.size() > 0 + ] + + actions triggered_by: transaction do + Contract.set_content "hello" + end + """ + |> Interpreter.parse() + end + end + + describe "parse code v0" do + test "should return an error if there are unexpected terms" do + assert {:error, _} = + """ + condition transaction: [ + uco_transfers: size() > 0 + ] + + some_unexpected_code + + actions triggered_by: transaction do + set_content "hello" + end + """ + |> Interpreter.parse() + end + + test "should return the contract if format is OK" do + assert {:ok, %Contract{}} = + """ + condition transaction: [ + uco_transfers: size() > 0 + ] + + actions triggered_by: transaction do + set_content "hello" + end + """ + |> Interpreter.parse() + end + end end