Skip to content

Commit

Permalink
Fix formatting of search filters which contain a single elements
Browse files Browse the repository at this point in the history
Closes #21.
  • Loading branch information
bbannier committed Sep 25, 2023
1 parent 832fab5 commit 104f41e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
13 changes: 9 additions & 4 deletions analyzer/ldap.spicy
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,6 @@ public function string_representation(search_filter: SearchFilter): string {
case FilterType::FILTER_AND, FilterType::FILTER_OR: {
local nestedObj: ParseNestedAndOr;
local printChar = "";
local printCount = 0;

if ( fType == FilterType::FILTER_AND ) {
printChar = "&";
Expand Down Expand Up @@ -554,10 +553,16 @@ public function string_representation(search_filter: SearchFilter): string {
# self.stringRepresentation = "(%s" % printChar + self.stringRepresentation + ")";
#

local i = 0;
for ( searchFilter in nestedObj.searchfilters ) {
switch ( printCount ) {
switch ( i ) {
case 0: {
repr = "(%s%s" % (printChar, searchFilter.stringRepresentation);
repr = "(%s%s%s" % (
printChar,
searchFilter.stringRepresentation,
# If we have exactly one element immediately close the statement since we are done.
|nestedObj.searchfilters| == 1 ? ")" : ""
);
}
case 1: {
repr = repr + searchFilter.stringRepresentation + ")";
Expand All @@ -566,7 +571,7 @@ public function string_representation(search_filter: SearchFilter): string {
repr = "(%s" % printChar + repr + searchFilter.stringRepresentation + ")";
}
}
printCount += 1;
i += 1;
}
}

Expand Down
35 changes: 26 additions & 9 deletions tests/analyzer/functions.spicy
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,38 @@ assert LDAP::uint32_to_hex_repr(b"\xff\xff\xff\xff") == "0xffffffff";

# ----------------------------------------------------------------------------------
# function string_representation()
function test_string_representation() {
local or_: LDAP::SearchFilter;
or_.filterType = LDAP::FilterType::FILTER_PRESENT;
or_.FILTER_PRESENT = "foo";
or_.stringRepresentation = LDAP::string_representation(or_);

function make_nested_repr(filters: vector<string>): string {
local nestedOr: LDAP::ParseNestedAndOr;
nestedOr.searchfilters = vector<LDAP::SearchFilter>();
nestedOr.searchfilters.push_back(or_);

for (f in filters) {
local or_: LDAP::SearchFilter;
or_.filterType = LDAP::FilterType::FILTER_PRESENT;
or_.FILTER_PRESENT = f;
or_.stringRepresentation = LDAP::string_representation(or_);

nestedOr.searchfilters.push_back(or_);
}

local searchFilter: LDAP::SearchFilter;
searchFilter.filterType = LDAP::FilterType::FILTER_OR;
searchFilter.FILTER_OR = nestedOr;

local repr = LDAP::string_representation(searchFilter);
assert repr == "(|(foo=*))": repr;
return LDAP::string_representation(searchFilter);
}

function test_string_representation() {
local repr0 = make_nested_repr(vector());
assert repr0 == "": repr0;

local repr1 = make_nested_repr(vector("foo"));
assert repr1 == "(|(foo=*))": repr1;

local repr2 = make_nested_repr(vector("foo", "bar"));
assert repr2 == "(|(foo=*)(bar=*))": repr2;

local repr3 = make_nested_repr(vector("foo", "bar", "baz"));
assert repr3 == "(|(|(foo=*)(bar=*))(baz=*))": repr3;
# "(|(|(foo=*)(bar=*))(baz=*))"
}
test_string_representation();

0 comments on commit 104f41e

Please sign in to comment.