Download test/ir_test.exs from Snapkitty/forge-code: direct link, hf CLI and curl.
- Browser
- Download file 14.7 kB
-
https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/test/ir_test.exs
- Command line
-
hf download hf://datasets/Snapkitty/forge-code/test/ir_test.exs
-
curl -L -o ir_test.exs https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/test/ir_test.exs
14.7 kB
| defmodule Forge.IRTest do | |
| use ExUnit.Case | |
| alias Forge.BinaryIR | |
| alias Forge.Beam | |
| @moduletag :ir | |
| describe "Forge.BinaryIR" do | |
| @tag :basic | |
| test "generate IR from BEAM file" do | |
| code = """ | |
| defmodule IRTestModule do | |
| def test_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, ir} = BinaryIR.from_beam(beam) | |
| assert ir.module == IRTestModule | |
| assert ir.hash | |
| assert String.length(ir.hash) == 64 | |
| assert is_list(ir.sections) | |
| assert is_list(ir.constants) | |
| assert is_list(ir.functions) | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :structure | |
| test "IR structure contains all required fields" do | |
| code = "defmodule StructureModule do; end" | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, ir} = BinaryIR.from_beam(beam) | |
| # Check all required fields | |
| assert ir.ir_version | |
| assert ir.module | |
| assert ir.source_hash | |
| assert ir.beam_hash | |
| assert ir.sections | |
| assert ir.constants | |
| assert ir.functions | |
| assert ir.metadata | |
| assert ir.hashes | |
| assert ir.provenance | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :sections | |
| test "IR contains section information" do | |
| code = """ | |
| defmodule SectionIRModule do | |
| def func, do: 42 | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, ir} = BinaryIR.from_beam(beam) | |
| assert length(ir.sections) > 0 | |
| # Each section should have required fields | |
| Enum.each(ir.sections, fn section -> | |
| assert section.name | |
| assert section.offset | |
| assert section.length | |
| assert is_list(section.fields) | |
| end) | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :fields | |
| test "IR fields preserve offset and length" do | |
| code = """ | |
| defmodule FieldIRModule do | |
| 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, ir} = BinaryIR.from_beam(beam) | |
| # Check that fields have offset and length | |
| Enum.each(ir.sections, fn section -> | |
| Enum.each(section.fields, fn field -> | |
| assert is_integer(field.offset) | |
| assert field.offset >= 0 | |
| assert is_integer(field.length) | |
| assert field.length >= 0 | |
| assert field.section | |
| end) | |
| end) | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :constants | |
| test "IR contains constant pool" do | |
| code = """ | |
| defmodule ConstantIRModule do | |
| @const 42 | |
| @str "hello" | |
| def get_const, do: @const | |
| def get_str, do: @str | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, ir} = BinaryIR.from_beam(beam) | |
| assert length(ir.constants) > 0 | |
| # Each constant should have type and value | |
| Enum.each(ir.constants, fn const -> | |
| assert const.type in [:atom, :integer, :string, :literal, :float] | |
| assert const.value != nil || const.index != nil | |
| end) | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :functions | |
| test "IR contains function information" do | |
| code = """ | |
| defmodule FunctionIRModule do | |
| def func1, do: 1 | |
| def func2(a), do: a + 1 | |
| def func3(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, ir} = BinaryIR.from_beam(beam) | |
| assert length(ir.functions) >= 3 | |
| # Each function should have required fields | |
| Enum.each(ir.functions, fn func -> | |
| assert func.name | |
| assert is_integer(func.arity) || func.arity == nil | |
| assert is_integer(func.instruction_count) | |
| assert is_list(func.instructions) | |
| end) | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :metadata | |
| test "IR contains metadata" do | |
| code = "defmodule MetadataIRModule do; end" | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, ir} = BinaryIR.from_beam(beam) | |
| assert ir.metadata | |
| assert ir.metadata.function_count | |
| assert ir.metadata.instruction_count | |
| assert ir.metadata.binary_size | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :provenance | |
| test "IR preserves provenance chain" do | |
| code = "defmodule ProvenanceIRModule do; end" | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, ir} = BinaryIR.from_beam(beam) | |
| assert ir.provenance | |
| assert ir.provenance.source_hash | |
| assert ir.provenance.beam_hash | |
| assert ir.provenance.ir_hash == ir.hash | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :hashing | |
| test "IR has valid hash" do | |
| code = "defmodule HashIRModule do; end" | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, ir} = BinaryIR.from_beam(beam) | |
| # Hash should be a valid SHA-256 | |
| assert ir.hash | |
| assert String.length(ir.hash) == 64 | |
| assert String.match?(ir.hash, ~r/^[a-f0-9]{64}$/) | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :normalization | |
| test "IR normalizes across different BEAM versions" do | |
| code = "defmodule NormalizeModule do; end" | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, ir} = BinaryIR.from_beam(beam) | |
| # IR version should be normalized | |
| assert ir.ir_version == "1.0" || ir.ir_version == :v1 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :comparison | |
| test "IR can be compared with other IRs" do | |
| code = """ | |
| defmodule CompareModule do | |
| def same_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, ir1} = BinaryIR.from_beam(beam) | |
| {:ok, ir2} = BinaryIR.from_beam(beam) | |
| # Same BEAM should produce same IR | |
| assert ir1 == ir2 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :serialization | |
| test "IR can be serialized to JSON" do | |
| code = "defmodule SerializeModule do; end" | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, ir} = BinaryIR.from_beam(beam) | |
| # Convert to map for JSON serialization | |
| ir_map = BinaryIR.to_map(ir) | |
| # Should be serializable to JSON | |
| json = Jason.encode!(ir_map) | |
| assert is_binary(json) | |
| # Should be parseable back | |
| parsed = Jason.decode!(json) | |
| assert parsed["module"] == Atom.to_string(SerializeModule) | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :validation | |
| test "IR can be validated" do | |
| code = "defmodule ValidateModule do; end" | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, ir} = BinaryIR.from_beam(beam) | |
| assert BinaryIR.valid?(ir) | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :error_handling | |
| test "handle invalid BEAM input" do | |
| assert {:error, _, _} = BinaryIR.from_beam(:invalid) | |
| end | |
| end | |
| describe "BinaryIR Generation" do | |
| @tag :instructions | |
| test "IR contains instruction sequences" do | |
| code = """ | |
| defmodule InstructionIRModule do | |
| def test_func(x, y) do | |
| a = x + y | |
| b = a * 2 | |
| b | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, ir} = BinaryIR.from_beam(beam) | |
| # Find the test function | |
| {_, func, _} = Enum.find(ir.functions, &(&1.name == :test_func && &1.arity == 2)) | |
| assert func | |
| assert func.instruction_count > 0 | |
| assert length(func.instructions) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :complexity | |
| test "IR captures complexity metrics" do | |
| code = """ | |
| defmodule ComplexityIRModule do | |
| def simple, do: 1 | |
| def complex(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, ir} = BinaryIR.from_beam(beam) | |
| # Complex function should have higher complexity | |
| simple_func = Enum.find_value(ir.functions, &(&1.name == :simple)) | |
| complex_func = Enum.find_value(ir.functions, &(&1.name == :complex)) | |
| assert simple_func | |
| assert complex_func | |
| assert complex_func.complexity >= simple_func.complexity | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :pattern_matching | |
| test "IR captures pattern matching" do | |
| code = """ | |
| defmodule PatternIRModule 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, ir} = BinaryIR.from_beam(beam) | |
| # Pattern matching should result in multiple functions | |
| match_funcs = Enum.filter(ir.functions, &(&1.name == :match)) | |
| assert length(match_funcs) > 1 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :recursion | |
| test "IR captures recursion" do | |
| code = """ | |
| defmodule RecursionIRModule do | |
| def factorial(0), do: 1 | |
| def factorial(n), do: n * factorial(n - 1) | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, ir} = BinaryIR.from_beam(beam) | |
| # Recursive function should have specific properties | |
| factorial_funcs = Enum.filter(ir.functions, &(&1.name == :factorial)) | |
| assert length(factorial_funcs) >= 1 | |
| [factorial_func | _] = factorial_funcs | |
| assert factorial_func.recursive == true | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :closures | |
| test "IR captures closures" do | |
| code = """ | |
| defmodule ClosureIRModule do | |
| def create_fn(multiplier) do | |
| fn x -> x * multiplier end | |
| end | |
| end | |
| """ | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, ir} = BinaryIR.from_beam(beam) | |
| # Should capture closure creation | |
| assert length(ir.functions) > 0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :state_tracking | |
| test "IR tracks field states" do | |
| code = "defmodule StateIRModule do; end" | |
| {:ok, path} = create_temp_file(code, ".ex") | |
| {:ok, beam_path} = compile_to_beam(path) | |
| {:ok, beam} = Beam.read(beam_path) | |
| {:ok, ir} = BinaryIR.from_beam(beam) | |
| # Fields should have state tracking | |
| Enum.each(ir.sections, fn section -> | |
| Enum.each(section.fields, fn field -> | |
| assert field.state in [:raw, :decoded, :normalized, :semantic] | |
| end) | |
| end) | |
| cleanup_temp([path, beam_path]) | |
| end | |
| end | |
| describe "BinaryIR Comparison" do | |
| @tag :diff | |
| test "IRs can be compared for differences" do | |
| code1 = """ | |
| defmodule Compare1Module do | |
| def func, do: 1 | |
| end | |
| """ | |
| code2 = """ | |
| defmodule Compare2Module do | |
| def func, do: 2 | |
| end | |
| """ | |
| {:ok, path1} = create_temp_file(code1, ".ex") | |
| {:ok, path2} = create_temp_file(code2, ".ex") | |
| {:ok, beam_path1} = compile_to_beam(path1) | |
| {:ok, beam_path2} = compile_to_beam(path2) | |
| {:ok, beam1} = Beam.read(beam_path1) | |
| {:ok, beam2} = Beam.read(beam_path2) | |
| {:ok, ir1} = BinaryIR.from_beam(beam1) | |
| {:ok, ir2} = BinaryIR.from_beam(beam2) | |
| # Different modules should produce different IRs | |
| assert ir1 != ir2 | |
| # But should be comparable | |
| assert BinaryIR.comparable?(ir1, ir2) | |
| cleanup_temp([path1, path2, beam_path1, beam_path2]) | |
| end | |
| @tag :similarity | |
| test "IR similarity can be calculated" do | |
| code = """ | |
| defmodule SimilarityModule 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, ir1} = BinaryIR.from_beam(beam) | |
| {:ok, ir2} = BinaryIR.from_beam(beam) | |
| # Same IR should have 100% similarity | |
| assert BinaryIR.similarity(ir1, ir2) == 1.0 | |
| cleanup_temp([path, beam_path]) | |
| end | |
| @tag :distance | |
| test "IR distance can be calculated" do | |
| code1 = "defmodule Distance1 do; def f, do: 1; end" | |
| code2 = "defmodule Distance2 do; def f, do: 2; end" | |
| {:ok, path1} = create_temp_file(code1, ".ex") | |
| {:ok, path2} = create_temp_file(code2, ".ex") | |
| {:ok, beam_path1} = compile_to_beam(path1) | |
| {:ok, beam_path2} = compile_to_beam(path2) | |
| {:ok, beam1} = Beam.read(beam_path1) | |
| {:ok, beam2} = Beam.read(beam_path2) | |
| {:ok, ir1} = BinaryIR.from_beam(beam1) | |
| {:ok, ir2} = BinaryIR.from_beam(beam2) | |
| # Different IRs should have non-zero distance | |
| distance = BinaryIR.distance(ir1, ir2) | |
| assert distance >= 0 | |
| cleanup_temp([path1, path2, beam_path1, beam_path2]) | |
| 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 | |