fix stupid bug in previous commit

This fixes a stupid bug in my previous commit to standardize on a new
`list_to_array_val()` function. This adds a unit test to keep this from
regressing.
This commit is contained in:
Kurtis Rader
2017-07-08 20:41:11 -07:00
parent 873cbc3c64
commit cf808674bf
2 changed files with 24 additions and 6 deletions

View File

@@ -1532,8 +1532,13 @@ std::unique_ptr<wcstring> list_to_array_val(const wcstring_list_t &list) {
// Zero element arrays are internally encoded as this placeholder string.
val->append(ENV_NULL);
} else {
bool need_sep = false;
for (auto it : list) {
if (!val->empty()) val->push_back(ARRAY_SEP);
if (need_sep) {
val->push_back(ARRAY_SEP);
} else {
need_sep = true;
}
val->append(it);
}
}

View File

@@ -948,21 +948,34 @@ static void test_list_to_array() {
err(L"test_list_to_array failed on line %lu", __LINE__);
}
list.push_back(L"abc");
list.push_back(L"");
val = list_to_array_val(list);
if (*val != list[0]) {
err(L"test_list_to_array failed on line %lu", __LINE__);
}
list.push_back(L"def");
list.push_back(L"abc");
val = list_to_array_val(list);
if (*val != L"abc" ARRAY_SEP_STR L"def") {
if (*val != L"" ARRAY_SEP_STR L"abc") {
err(L"test_list_to_array failed on line %lu", __LINE__);
}
list.push_back(L"ghi");
list.insert(list.begin(), L"ghi");
val = list_to_array_val(list);
if (*val != L"abc" ARRAY_SEP_STR L"def" ARRAY_SEP_STR L"ghi") {
if (*val != L"ghi" ARRAY_SEP_STR L"" ARRAY_SEP_STR L"abc") {
err(L"test_list_to_array failed on line %lu", __LINE__);
}
list.push_back(L"");
val = list_to_array_val(list);
if (*val != L"ghi" ARRAY_SEP_STR L"" ARRAY_SEP_STR L"abc" ARRAY_SEP_STR L"") {
err(L"test_list_to_array failed on line %lu", __LINE__);
}
list.push_back(L"def");
val = list_to_array_val(list);
if (*val !=
L"ghi" ARRAY_SEP_STR L"" ARRAY_SEP_STR L"abc" ARRAY_SEP_STR L"" ARRAY_SEP_STR L"def") {
err(L"test_list_to_array failed on line %lu", __LINE__);
}