Skip to content

Commit

Permalink
Refactor isIpPrefix() and add testIsIpPrefixUnsupported()
Browse files Browse the repository at this point in the history
  • Loading branch information
higebu committed Nov 1, 2023
1 parent 52523ac commit 9568997
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ static List<Decl> create() {
decls.add(
Decls.newFunction(
"isIpPrefix",
Decls.newInstanceOverload("is_ip_prefix_int_bool", Arrays.asList(Decls.String, Decls.Int, Decls.Bool), Decls.Bool),
Decls.newInstanceOverload("is_ip_prefix_int", Arrays.asList(Decls.String, Decls.Int), Decls.Bool),
Decls.newInstanceOverload("is_ip_prefix_bool", Arrays.asList(Decls.String, Decls.Bool), Decls.Bool),
Decls.newInstanceOverload(
"is_ip_prefix_int_bool",
Arrays.asList(Decls.String, Decls.Int, Decls.Bool),
Decls.Bool),
Decls.newInstanceOverload(
"is_ip_prefix_int", Arrays.asList(Decls.String, Decls.Int), Decls.Bool),
Decls.newInstanceOverload(
"is_ip_prefix_bool", Arrays.asList(Decls.String, Decls.Bool), Decls.Bool),
Decls.newInstanceOverload(
"is_ip_prefix", Collections.singletonList(Decls.String), Decls.Bool)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ private static Overload isIpPrefix() {
OVERLOAD_IS_IP_PREFIX,
null,
value -> {
if (value.type().typeEnum() != TypeEnum.String && value.type().typeEnum() != TypeEnum.Bool ) {
if (value.type().typeEnum() != TypeEnum.String
&& value.type().typeEnum() != TypeEnum.Bool) {
return Err.noSuchOverload(value, OVERLOAD_IS_IP_PREFIX, null);
}
String prefix = (String) value.value();
Expand All @@ -308,7 +309,9 @@ private static Overload isIpPrefix() {
return Types.boolOf(validateIPPrefix(prefix, 0L, false));
},
(lhs, rhs) -> {
if (lhs.type().typeEnum() != TypeEnum.String || (rhs.type().typeEnum() != TypeEnum.Int && rhs.type().typeEnum() != TypeEnum.Bool)) {
if (lhs.type().typeEnum() != TypeEnum.String
|| (rhs.type().typeEnum() != TypeEnum.Int
&& rhs.type().typeEnum() != TypeEnum.Bool)) {
return Err.noSuchOverload(lhs, OVERLOAD_IS_IP_PREFIX, rhs);
}
String prefix = (String) lhs.value();
Expand All @@ -317,22 +320,20 @@ private static Overload isIpPrefix() {
}
if (rhs.type().typeEnum() == TypeEnum.Int) {
return Types.boolOf(validateIPPrefix(prefix, rhs.intValue(), false));
} else if (rhs.type().typeEnum() == TypeEnum.Bool) {
return Types.boolOf(validateIPPrefix(prefix, 0L, rhs.booleanValue()));
}
return BoolT.False;
return Types.boolOf(validateIPPrefix(prefix, 0L, rhs.booleanValue()));
},
(values) -> {
if (values[0].type().typeEnum() != TypeEnum.String || values[1].type().typeEnum() != TypeEnum.Int || values[2].type().typeEnum() != TypeEnum.Bool) {
return Err.noSuchOverload(values[0], OVERLOAD_IS_IP_PREFIX, "", new Val[]{values[1], values[2]});
if (values.length != 3) {
return Err.noSuchOverload(values[0], OVERLOAD_IS_IP_PREFIX, "", values);
}
String prefix = (String) values[0].value();
if (prefix.isEmpty()) {
return BoolT.False;
}
return Types.boolOf(validateIPPrefix(prefix, values[1].intValue(), values[2].booleanValue()));
}
);
return Types.boolOf(
validateIPPrefix(prefix, values[1].intValue(), values[2].booleanValue()));
});
}

/**
Expand Down Expand Up @@ -551,7 +552,8 @@ private static boolean validateIP(String addr, long ver) {
*
* @param prefix The input string to validate as an IP prefix.
* @param ver The IP version to validate against (0 for any version, 4 for IPv4, 6 for IPv6).
* @param strict If strict is true and host bits are set in the supplied address, then false is returned.
* @param strict If strict is true and host bits are set in the supplied address, then false is
* returned.
* @return {@code true} if the input string is a valid IP prefix of the specified version, {@code
* false} otherwise.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ public void testIsIpPrefix() {
}
}

@Test
public void testIsIpPrefixUnsupported() {
List<String> testCases = ImmutableList.of("1.isIpPrefix()");
for (String testCase : testCases) {
Program.EvalResult result = eval(testCase);
Val val = result.getVal();
assertThat(Err.isError(val)).isTrue();
assertThatThrownBy(() -> val.convertToNative(Exception.class))
.isInstanceOf(UnsupportedOperationException.class);
}
}

private Program.EvalResult eval(String source) {
return eval(source, Activation.emptyActivation());
}
Expand Down

0 comments on commit 9568997

Please sign in to comment.