Skip to content

Commit 1d4ea65

Browse files
committed
Add unit-tests for list-processing. Fix edge-cases.
1 parent 81218bf commit 1d4ea65

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

src/gmail.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,21 @@ var Gmail_ = function(localJQuery) {
147147
localePrefix.toUpperCase() === localePrefix;
148148
};
149149

150-
var arrayStartsWith = function(list, item) {
151-
return (list && list.length > 0 && list[0] === item);
150+
api.helper.array_starts_with = function(list, item) {
151+
if (list && list.length > 0 && list[0] === item) {
152+
return true;
153+
} else {
154+
return false;
155+
}
152156
};
153157

154-
var findArraySubList = function(nestedArray, itemKey) {
155-
for(var i=0; i<nestedArray.length; i++) {
156-
var list = nestedArray[i];
157-
if (arrayStartsWith(list, itemKey)) {
158-
return list;
158+
api.helper.get.array_sublist = function(nestedArray, itemKey) {
159+
if (nestedArray) {
160+
for(var i=0; i<nestedArray.length; i++) {
161+
var list = nestedArray[i];
162+
if (api.helper.array_starts_with(list, itemKey)) {
163+
return list;
164+
}
159165
}
160166
}
161167

@@ -200,7 +206,7 @@ var Gmail_ = function(localJQuery) {
200206

201207
// candidate is globals[17]-subarray which starts with "ui"
202208
// has historically been observed as [7], [8] and [9]!
203-
var localeList = findArraySubList(globals[17], "ui");
209+
var localeList = api.helper.get.array_sublist(globals[17], "ui");
204210
if (localeList !== null && localeList.length > 8) {
205211
var locale = getLocaleFromGlobalsItem(localeList);
206212
if (api.helper.get.is_locale(locale)) {

test/test.parsing.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,51 @@ describe("Name-parsing", () => {
130130
testName("Senõr Alapenõ on a stick");
131131
});
132132
});
133+
134+
describe("List-prefix checking", () => {
135+
const gmail = new Gmail();
136+
137+
const testCase = function(list, searchee, expected) {
138+
const result = gmail.helper.array_starts_with(list, searchee);
139+
assert.equal(expected, result);
140+
};
141+
142+
it("returns false for null or empty list", () => {
143+
testCase(null, "key", false);
144+
testCase([], "key", false);
145+
});
146+
147+
it("returns false for miss", () => {
148+
testCase(["ui", "yes"], "uiv", false);
149+
});
150+
151+
it("returns true for exact hit", () => {
152+
testCase(["ui", "yes"], "ui", true);
153+
});
154+
});
155+
156+
describe("Sub-list extraction", () => {
157+
const gmail = new Gmail();
158+
159+
const testCase = function(listlist, prefix, expected) {
160+
const result = gmail.helper.get.array_sublist(listlist, prefix);
161+
assert.deepEqual(expected, result);
162+
};
163+
164+
it("returns null for null or empty list", () => {
165+
testCase(null, "ui", null);
166+
testCase([], "ui", null);
167+
});
168+
169+
it("returns null for no match", () => {
170+
testCase([["uiv", "a"]], "ui", null);
171+
});
172+
173+
it("returns the full matching list on match", () => {
174+
testCase([
175+
["a", "b", "c"],
176+
["ui", "yeah"],
177+
["d", "e", "f"]
178+
], "ui", ["ui", "yeah"]);
179+
});
180+
});

0 commit comments

Comments
 (0)