towerops/test/towerops/numeric_test.exs
Graham McIntire efaf5558ff refactor: convert 6 Gleam modules to idiomatic Elixir with TDD (#196)
Phase 1: Foundation Types (100% complete)
- query_helpers: SQL LIKE sanitization with pipe operators
- numeric: Integer parsing with pattern matching guards
- result: Pure Elixir Result monad (map, and_then, unwrap_or)

Phase 2: Ecto Domain Types (100% complete)
- ip_address: IPv4/IPv6 validation using :inet directly
- mac_address: Multi-format MAC parsing (colon/hyphen/dot/compact)
- snmp_oid: OID parsing/manipulation with recursive pattern matching

All 198 tests passing across converted modules.
API changed from Gleam-style {:some/:none to idiomatic {:ok/:error.
Refactored parse_numeric_oid to use with statement, reducing nesting depth.

Reviewed-on: graham/towerops-web#196
2026-03-28 09:52:07 -05:00

89 lines
2.4 KiB
Elixir

defmodule Towerops.NumericTest do
use ExUnit.Case, async: true
alias Towerops.Numeric
describe "parse_integer/1" do
test "parses integer values" do
assert Numeric.parse_integer(42) == {:ok, 42}
end
test "parses integer strings" do
assert Numeric.parse_integer("42") == {:ok, 42}
assert Numeric.parse_integer("0") == {:ok, 0}
assert Numeric.parse_integer("-5") == {:ok, -5}
end
test "parses integer from string with trailing chars" do
assert Numeric.parse_integer("42abc") == {:ok, 42}
end
test "returns error for nil" do
assert Numeric.parse_integer(nil) == :error
end
test "returns error for empty string" do
assert Numeric.parse_integer("") == :error
end
test "returns error for null string" do
assert Numeric.parse_integer("null") == :error
end
test "returns error for non-numeric string" do
assert Numeric.parse_integer("abc") == :error
end
test "returns error for floats" do
assert Numeric.parse_integer(4.2) == :error
end
test "returns error for other types" do
assert Numeric.parse_integer([]) == :error
assert Numeric.parse_integer(%{}) == :error
assert Numeric.parse_integer(:atom) == :error
end
end
describe "parse_float/1" do
test "parses float values" do
assert Numeric.parse_float(4.2) == {:ok, 4.2}
end
test "parses integer values as float" do
assert Numeric.parse_float(42) == {:ok, 42.0}
end
test "parses float strings" do
assert Numeric.parse_float("4.2") == {:ok, 4.2}
assert Numeric.parse_float("0.0") == {:ok, 0.0}
assert Numeric.parse_float("-5.5") == {:ok, -5.5}
end
test "parses integer strings as float" do
assert Numeric.parse_float("42") == {:ok, 42.0}
end
test "returns error for nil" do
assert Numeric.parse_float(nil) == :error
end
test "returns error for empty string" do
assert Numeric.parse_float("") == :error
end
test "returns error for null string" do
assert Numeric.parse_float("null") == :error
end
test "returns error for non-numeric string" do
assert Numeric.parse_float("abc") == :error
end
test "returns error for other types" do
assert Numeric.parse_float([]) == :error
assert Numeric.parse_float(%{}) == :error
assert Numeric.parse_float(:atom) == :error
end
end
end