# HG changeset patch # User Matthew Wild # Date 1678819216 0 # Node ID c6dffebab2f89e1f439864ce412072610cad5eeb # Parent 3ab0bbb1dc35b666d2216fde0c638b7f5c576195 util.ip: Tests for truncate() diff -r 3ab0bbb1dc35 -r c6dffebab2f8 spec/util_ip_spec.lua --- a/spec/util_ip_spec.lua Sun Mar 12 01:24:59 2023 +0100 +++ b/spec/util_ip_spec.lua Tue Mar 14 18:40:16 2023 +0000 @@ -100,4 +100,26 @@ assert_cpl6("abcd::abcd", "abcd::abcd:abcd", 96); end); end); + + describe("#truncate()", function () + it("should work for IPv4", function () + local ip1 = ip.new_ip("192.168.0.1"); + local ip2 = ip.truncate(ip1, 16); + assert.truthy(ip.is_ip(ip2)); + assert.equal("192.168.0.0", ip2.normal); + assert.equal("192.168.0.1", ip1.normal); -- original unmodified + end); + + it("should work for IPv6", function () + local ip1 = ip.new_ip("2001:db8::ff00:42:8329"); + local ip2 = ip.truncate(ip1, 24); + assert.truthy(ip.is_ip(ip2)); + assert.equal("2001:d00::", ip2.normal); + assert.equal("2001:db8::ff00:42:8329", ip1.normal); -- original unmodified + end); + + it("accepts a string", function () + assert.equal("127.0.0.0", ip.truncate("127.0.0.1", 8).normal); + end); + end); end);