From 0eb912a28f5eb40d62df3fe020266b38b3eae9e1 Mon Sep 17 00:00:00 2001 From: Gustavo Santos Ferreira Date: Thu, 29 Nov 2018 14:40:56 -0200 Subject: [PATCH] new function to help print line numbers on the page --- .../views/address_contract_view.ex | 16 ++++++ .../views/address_contract_view_test.exs | 55 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/apps/block_scout_web/lib/block_scout_web/views/address_contract_view.ex b/apps/block_scout_web/lib/block_scout_web/views/address_contract_view.ex index 3c51c70342..e772781277 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/address_contract_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/address_contract_view.ex @@ -14,4 +14,20 @@ defmodule BlockScoutWeb.AddressContractView do """ def format_optimization_text(true), do: gettext("true") def format_optimization_text(false), do: gettext("false") + + def contract_lines_with_index(contract_source_code) do + contract_lines = String.split(contract_source_code, "\n") + + max_digits = + contract_lines + |> Enum.count() + |> Integer.digits() + |> Enum.count() + + contract_lines + |> Enum.with_index(1) + |> Enum.map(fn {value, line} -> + {value, String.pad_leading(to_string(line), max_digits, " ")} + end) + end end diff --git a/apps/block_scout_web/test/block_scout_web/views/address_contract_view_test.exs b/apps/block_scout_web/test/block_scout_web/views/address_contract_view_test.exs index e483bfc7ea..4bcb1eb2bf 100644 --- a/apps/block_scout_web/test/block_scout_web/views/address_contract_view_test.exs +++ b/apps/block_scout_web/test/block_scout_web/views/address_contract_view_test.exs @@ -14,4 +14,59 @@ defmodule BlockScoutWeb.AddressContractViewTest do assert AddressContractView.format_optimization_text(false) == "false" end end + + describe "contract_lines_with_index/1" do + test "returns a list of tuples containing two strings each" do + code = """ + pragma solidity >=0.4.22 <0.6.0; + + struct Proposal { + uint voteCount; + } + + address chairperson; + mapping(address => Voter) voters; + Proposal[] proposals; + + constructor(uint8 _numProposals) public { + chairperson = msg.sender; + voters[chairperson].weight = 1; + proposals.length = _numProposals; + } + """ + + result = AddressContractView.contract_lines_with_index(code) + + assert result == [ + {"pragma solidity >=0.4.22 <0.6.0;", " 1"}, + {"", " 2"}, + {"struct Proposal {", " 3"}, + {" uint voteCount;", " 4"}, + {"}", " 5"}, + {"", " 6"}, + {"address chairperson;", " 7"}, + {"mapping(address => Voter) voters;", " 8"}, + {"Proposal[] proposals;", " 9"}, + {"", "10"}, + {"constructor(uint8 _numProposals) public {", "11"}, + {" chairperson = msg.sender;", "12"}, + {" voters[chairperson].weight = 1;", "13"}, + {" proposals.length = _numProposals;", "14"}, + {"}", "15"}, + {"", "16"} + ] + end + + test "returns a list of tuples and the second element always has n chars with x lines" do + chars = 3 + lines = 100 + result = AddressContractView.contract_lines_with_index(Enum.join(1..lines, "\n")) + assert Enum.all?(result, fn {_, number} -> String.length(number) == chars end) + end + + test "returns a list of tuples and the first element is just a line from the original string" do + result = AddressContractView.contract_lines_with_index("a\nb\nc\nd\ne") + assert Enum.map(result, fn {line, _number} -> line end) == ["a", "b", "c", "d", "e"] + end + end end