Download test/semantics_test.exs from Snapkitty/forge-code: direct link, hf CLI and curl.
- Browser
- Download file 21.1 kB
-
https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/test/semantics_test.exs
- Command line
-
hf download hf://datasets/Snapkitty/forge-code/test/semantics_test.exs
-
curl -L -o semantics_test.exs https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/test/semantics_test.exs
21.1 kB
| defmodule Forge.SemanticsTest do | |
| use ExUnit.Case | |
| alias Forge.Semantics | |
| alias Forge.Beam | |
| @moduletag :semantics | |
| describe "Forge.Semantics" do | |
| @tag :basic | |
| test "analyze simple function semantics" do | |
| code = """ | |
| defmodule SimpleSemanticsModule do | |
| def add(a, b), do: a + b | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| assert semantics.module == SimpleSemanticsModule | |
| assert is_list(semantics.functions) | |
| assert is_list(semantics.operations) | |
| assert is_list(semantics.patterns) | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :operations | |
| test "detect arithmetic operations" do | |
| code = """ | |
| defmodule ArithmeticSemanticsModule do | |
| def compute(a, b, c) do | |
| sum = a + b | |
| product = sum * c | |
| result = product - 10 | |
| result / 2 | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect arithmetic operations | |
| arithmetic_ops = Enum.filter(semantics.operations, &(&1.operation in [:add, :subtract, :multiply, :divide])) | |
| assert length(arithmetic_ops) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :operations | |
| test "detect function calls" do | |
| code = """ | |
| defmodule CallSemanticsModule do | |
| def caller do | |
| :erlang.plus(1, 2) | |
| String.upcase("hello") | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect function calls | |
| call_ops = Enum.filter(semantics.operations, &(&1.operation == :call || &1.operation == :remote_call)) | |
| assert length(call_ops) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :pattern_matching | |
| test "detect pattern matching" do | |
| code = """ | |
| defmodule PatternSemanticsModule do | |
| def match({:ok, value}), do: value | |
| def match({:error, reason}), do: reason | |
| def match(_), do: :default | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect pattern matching | |
| assert length(semantics.patterns) > 0 | |
| # Should have tuple patterns | |
| tuple_patterns = Enum.filter(semantics.patterns, &(&1.type == :tuple)) | |
| assert length(tuple_patterns) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :control_flow | |
| test "detect conditional statements" do | |
| code = """ | |
| defmodule ConditionalSemanticsModule do | |
| def check(x) do | |
| if x > 0 do | |
| :positive | |
| else | |
| :non_positive | |
| end | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect conditional | |
| conditionals = Enum.filter(semantics.operations, &(&1.operation == :conditional || &1.operation == :if)) | |
| assert length(conditionals) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :control_flow | |
| test "detect case statements" do | |
| code = """ | |
| defmodule CaseSemanticsModule do | |
| def handle(x) do | |
| case x do | |
| 1 -> :one | |
| 2 -> :two | |
| _ -> :other | |
| end | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect case statement | |
| case_ops = Enum.filter(semantics.operations, &(&1.operation == :case || &1.operation == :pattern_match)) | |
| assert length(case_ops) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :control_flow | |
| test "detect loops" do | |
| code = """ | |
| defmodule LoopSemanticsModule do | |
| def loop(n, acc \\ 0) do | |
| if n <= 0 do | |
| acc | |
| else | |
| loop(n - 1, acc + n) | |
| end | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect recursion (form of loop) | |
| recursive_funcs = Enum.filter(semantics.functions, &(&1.recursive == true)) | |
| assert length(recursive_funcs) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :data_construction | |
| test "detect data construction" do | |
| code = """ | |
| defmodule DataSemanticsModule do | |
| def build do | |
| %{name: "test", value: 42} | |
| end | |
| def build_list do | |
| [1, 2, 3] | |
| end | |
| def build_tuple do | |
| {1, 2, 3} | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect map construction | |
| construct_ops = Enum.filter(semantics.operations, &(&1.operation in [:map_construct, :list_construct, :tuple_construct])) | |
| assert length(construct_ops) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :function_analysis | |
| test "analyze function boundaries" do | |
| code = """ | |
| defmodule FunctionBoundaryModule do | |
| def func1, do: 1 | |
| def func2, do: 2 | |
| def func3, do: 3 | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should have function analysis | |
| assert length(semantics.functions) >= 3 | |
| # Each function should have analysis | |
| Enum.each(semantics.functions, fn func -> | |
| assert func.name | |
| assert is_integer(func.line) || func.line == nil | |
| assert func.arity != nil | |
| end) | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :complexity | |
| test "calculate cyclomatic complexity" do | |
| code = """ | |
| defmodule ComplexitySemanticsModule do | |
| def simple, do: 1 | |
| def complex(x) do | |
| if x > 0 do | |
| if x < 10 do | |
| :small_positive | |
| else | |
| :large_positive | |
| end | |
| else | |
| :non_positive | |
| end | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Find functions by name | |
| simple_func = Enum.find_value(semantics.functions, &(&1.name == :simple)) | |
| complex_func = Enum.find_value(semantics.functions, &(&1.name == :complex)) | |
| assert simple_func | |
| assert complex_func | |
| # Complex function should have higher complexity | |
| assert complex_func.complexity >= simple_func.complexity | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :confidence | |
| test "track confidence levels" do | |
| code = """ | |
| defmodule ConfidenceSemanticsModule do | |
| def test, do: 42 | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Operations should have confidence levels | |
| assert length(semantics.operations) > 0 | |
| Enum.each(semantics.operations, fn op -> | |
| assert op.confidence in [:high, :medium, :low, :inferred] | |
| end) | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :state_tracking | |
| test "track state for semantic information" do | |
| code = """ | |
| defmodule StateSemanticsModule do | |
| def func, do: :ok | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Operations should have state tracking | |
| Enum.each(semantics.operations, fn op -> | |
| assert op.state in [:raw, :decoded, :normalized, :semantic] | |
| end) | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :metadata | |
| test "extract semantic metadata" do | |
| code = """ | |
| defmodule MetadataSemanticsModule do | |
| @moduledoc "Test module" | |
| @doc "Test function" | |
| def test, do: :ok | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| assert semantics.metadata | |
| assert semantics.metadata.module_doc != nil || semantics.metadata.function_count > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :provenance | |
| test "preserve provenance in semantics" do | |
| code = "defmodule ProvenanceSemanticsModule do; end" | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| assert semantics.provenance | |
| assert semantics.provenance.beam_hash == beam.hash | |
| cleanup_temp([path, beam_path]) | |
| end | |
| end | |
| describe "Pattern Detection" do | |
| @tag :patterns | |
| test "detect simple patterns" do | |
| code = """ | |
| defmodule SimplePatternModule do | |
| def match(42), do: :forty_two | |
| def match(_), do: :other | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect literal patterns | |
| literal_patterns = Enum.filter(semantics.patterns, &(&1.type == :literal || &1.type == :integer)) | |
| assert length(literal_patterns) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :patterns | |
| test "detect variable patterns" do | |
| code = """ | |
| defmodule VariablePatternModule do | |
| def match(x), do: x | |
| def match(x, y), do: x + y | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect variable patterns | |
| var_patterns = Enum.filter(semantics.patterns, &(&1.type == :variable || &1.variables != [])) | |
| assert length(var_patterns) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :patterns | |
| test "detect complex patterns" do | |
| code = """ | |
| defmodule ComplexPatternModule do | |
| def match({:result, {:ok, value}}), do: value | |
| def match([head | tail]), do: {head, tail} | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect complex patterns | |
| complex_patterns = Enum.filter(semantics.patterns, fn p -> | |
| p.type in [:tuple, :list, :cons] || (p.variables && length(p.variables) > 1) | |
| end) | |
| assert length(complex_patterns) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :guards | |
| test "detect guards" do | |
| code = """ | |
| defmodule GuardPatternModule do | |
| def match(x) when is_integer(x), do: :integer | |
| def match(x) when is_binary(x), do: :binary | |
| def match(_), do: :other | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect guards | |
| guard_ops = Enum.filter(semantics.operations, &(&1.operation == :guard || &1.guard != nil)) | |
| assert length(guard_ops) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| end | |
| describe "Operation Detection" do | |
| @tag :arithmetic | |
| test "detect all arithmetic operations" do | |
| code = """ | |
| defmodule AllArithmeticModule do | |
| def all_ops do | |
| a + b | |
| a - b | |
| a * b | |
| a / b | |
| div(a, b) | |
| rem(a, b) | |
| -a | |
| +a | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| arithmetic_ops = Enum.filter(semantics.operations, &(&1.operation in [:add, :subtract, :multiply, :divide, :negate, :integer_divide, :modulo])) | |
| assert length(arithmetic_ops) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :boolean | |
| test "detect boolean operations" do | |
| code = """ | |
| defmodule BooleanModule do | |
| def test(a, b) do | |
| a and b | |
| a or b | |
| not a | |
| a == b | |
| a != b | |
| a < b | |
| a > b | |
| a <= b | |
| a >= b | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| boolean_ops = Enum.filter(semantics.operations, &(&1.operation in [:and, :or, :not, :equal, :not_equal, :less, :greater, :less_equal, :greater_equal])) | |
| assert length(boolean_ops) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :binary | |
| test "detect binary operations" do | |
| code = """ | |
| defmodule BinaryOpModule do | |
| def test do | |
| binary = <<1, 2, 3>> | |
| String.to_integer("42") | |
| Integer.to_string(42) | |
| binary_part(binary, 0, 2) | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect binary-related operations | |
| binary_ops = Enum.filter(semantics.operations, &(&1.operation in [:binary_construct, :binary_part, :binary_match])) | |
| assert length(semantics.operations) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :message_passing | |
| test "detect message passing" do | |
| code = """ | |
| defmodule MessageModule do | |
| def test(pid) do | |
| send(pid, {:msg, :data}) | |
| receive do | |
| {:msg, data} -> data | |
| after | |
| 100 -> :timeout | |
| end | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect message passing | |
| message_ops = Enum.filter(semantics.operations, &(&1.operation in [:send, :receive])) | |
| assert length(message_ops) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :list_operations | |
| test "detect list operations" do | |
| code = """ | |
| defmodule ListOpModule do | |
| def test(list) do | |
| [1 | list] | |
| list ++ [2, 3] | |
| Enum.map(list, &(&1 * 2)) | |
| Enum.filter(list, &(&1 > 0)) | |
| Enum.reduce(list, 0, &(&1 + &2)) | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect list operations | |
| list_ops = Enum.filter(semantics.operations, &(&1.operation in [:list_construct, :list_concat, :enum_map, :enum_filter, :enum_reduce, :cons])) | |
| assert length(list_ops) > 0 || length(semantics.operations) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :map_operations | |
| test "detect map operations" do | |
| code = """ | |
| defmodule MapOpModule do | |
| def test do | |
| map = %{a: 1, b: 2} | |
| Map.get(map, :a) | |
| Map.put(map, :c, 3) | |
| Map.update(map, :a, 1, &(&1 + 1)) | |
| map[:b] | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect map operations | |
| map_ops = Enum.filter(semantics.operations, &(&1.operation in [:map_construct, :map_get, :map_put, :map_update])) | |
| assert length(map_ops) > 0 || length(semantics.operations) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| end | |
| describe "Semantic Analysis Features" do | |
| @tag :data_flow | |
| test "track data flow" do | |
| code = """ | |
| defmodule DataFlowModule do | |
| def compute(x, y) do | |
| sum = x + y | |
| doubled = sum * 2 | |
| result = doubled - 10 | |
| {result, sum} | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should track data flow | |
| assert semantics.data_flow != nil || length(semantics.operations) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :control_flow_graph | |
| test "build control flow graph" do | |
| code = """ | |
| defmodule CFGModule do | |
| def branch(x) do | |
| if x > 0 do | |
| :positive | |
| else | |
| :non_positive | |
| end | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should have control flow information | |
| assert semantics.control_flow != nil || length(semantics.functions) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :dependency_analysis | |
| test "analyze function dependencies" do | |
| code = """ | |
| defmodule DependencyModule do | |
| def helper(x), do: x * 2 | |
| def main(x) do | |
| helper(x) | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should have function dependencies | |
| assert semantics.dependencies != nil || length(semantics.functions) >= 2 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :type_inference | |
| test "infer basic types" do | |
| code = """ | |
| defmodule TypeInferenceModule do | |
| def integers, do: 1 + 2 | |
| def strings, do: "hello" <> " world" | |
| def atoms, do: :symbol | |
| def lists, do: [1, 2, 3] | |
| def maps, do: %{a: 1} | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should have type information | |
| assert length(semantics.functions) >= 5 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :side_effects | |
| test "detect side effects" do | |
| code = """ | |
| defmodule SideEffectModule do | |
| def impure do | |
| IO.puts("hello") | |
| :ok | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect IO operations as side effects | |
| io_ops = Enum.filter(semantics.operations, &(&1.operation in [:io_put, :io_get, :io_inspect] || String.contains?(to_string(&1.operation), "io"))) | |
| assert length(io_ops) > 0 || length(semantics.operations) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :recursion_detection | |
| test "detect tail recursion" do | |
| code = """ | |
| defmodule TailRecursionModule do | |
| def factorial(n, acc \\ 1) do | |
| if n <= 1 do | |
| acc | |
| else | |
| factorial(n - 1, acc * n) | |
| end | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect tail recursion | |
| [func | _] = semantics.functions | |
| assert func.recursive == true | |
| assert func.tail_recursive == true | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :error_handling | |
| test "detect error handling patterns" do | |
| code = """ | |
| defmodule ErrorHandlingModule do | |
| def safe_divide(a, b) do | |
| try do | |
| a / b | |
| rescue | |
| ArithmeticError -> :error | |
| end | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, semantics} = Semantics.analyze(beam) | |
| # Should detect try/rescue | |
| try_ops = Enum.filter(semantics.operations, &(&1.operation in [:try, :rescue, :catch, :after])) | |
| assert length(try_ops) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| end | |
| # Helper functions | |
| defp create_temp_file(content, extension) do | |
| temp_dir = Path.join(System.tmp_dir(), "forge_test") | |
| File.mkdir_p!(temp_dir) | |
| name = "test_" <> Integer.to_string(:erlang.unique_integer([:positive])) <> extension | |
| path = Path.join(temp_dir, name) | |
| File.write!(path, content) | |
| {:ok, path} | |
| end | |
| defp compile_to_beam(path) do | |
| output_dir = Path.join(System.tmp_dir(), "forge_test", "ebin") | |
| File.mkdir_p!(output_dir) | |
| # Compile the file | |
| {:ok, module, _binary} = Code.eval_file(path) | |
| # Get the beam path | |
| beam_name = Atom.to_string(module) <> ".beam" | |
| beam_path = Path.join(output_dir, beam_name) | |
| # Force compilation to beam file | |
| Code.compile_file(path, beam_path, [:return_errors]) | |
| {:ok, beam_path} | |
| rescue | |
| _ -> {:error, :compilation_failed} | |
| end | |
| defp cleanup_temp(files) do | |
| Enum.each(files, &File.rm!/1) | |
| end | |
| end | |