Add testcases for handling odata (#11321)
* Add testcases for handling odata * Formatting code * PR Comments * Minor Formatting * Update sdk/search/search-documents/src/odata.ts Co-authored-by: Jeff Fisher <xirzec@xirzec.com> * Update sdk/search/search-documents/src/odata.ts Co-authored-by: Jeff Fisher <xirzec@xirzec.com> Co-authored-by: Jeff Fisher <xirzec@xirzec.com>
This commit is contained in:
Родитель
b750841ec4
Коммит
4a96e96a67
|
@ -1,6 +1,14 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
function formatNullAndUndefined(input: unknown): string | unknown {
|
||||
if (input === null || input === undefined) {
|
||||
return "null";
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
function escapeQuotesIfString(input: unknown, previous: string): string | unknown {
|
||||
let result = input;
|
||||
|
||||
|
@ -11,6 +19,7 @@ function escapeQuotesIfString(input: unknown, previous: string): string | unknow
|
|||
result = `'${result}'`;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -31,7 +40,11 @@ export function odata(strings: TemplateStringsArray, ...values: unknown[]): stri
|
|||
for (let i = 0; i < strings.length; i++) {
|
||||
results.push(strings[i]);
|
||||
if (i < values.length) {
|
||||
results.push(escapeQuotesIfString(values[i], strings[i]));
|
||||
if (values[i] === null || values[i] === undefined) {
|
||||
results.push(formatNullAndUndefined(values[i]));
|
||||
} else {
|
||||
results.push(escapeQuotesIfString(values[i], strings[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
return results.join("");
|
||||
|
|
|
@ -30,6 +30,58 @@ describe("odata", () => {
|
|||
assert.strictEqual(result, "search.ismatch('you''re', 'Description')");
|
||||
});
|
||||
|
||||
it("no arguments", () => {
|
||||
assert.strictEqual(odata`Foo eq 2`, "Foo eq 2");
|
||||
});
|
||||
|
||||
it("one argument", () => {
|
||||
assert.strictEqual(odata`Foo eq ${2}`, "Foo eq 2");
|
||||
});
|
||||
|
||||
it("many arguments", () => {
|
||||
assert.strictEqual(
|
||||
odata`Foo eq ${2} and Bar eq ${3} and Baz eq ${4} and Qux eq ${5} and Quux eq ${6}`,
|
||||
"Foo eq 2 and Bar eq 3 and Baz eq 4 and Qux eq 5 and Quux eq 6"
|
||||
);
|
||||
});
|
||||
|
||||
it("null", () => {
|
||||
assert.strictEqual(odata`Foo eq ${null}`, "Foo eq null");
|
||||
});
|
||||
|
||||
it("bool", () => {
|
||||
let x: boolean = true;
|
||||
assert.strictEqual(odata`Foo eq ${x}`, "Foo eq true");
|
||||
assert.strictEqual(odata`Foo eq ${true}`, "Foo eq true");
|
||||
});
|
||||
|
||||
it("numbers", () => {
|
||||
assert.strictEqual(odata`Foo eq ${0}`, "Foo eq 0");
|
||||
assert.strictEqual(odata`Foo eq ${2}`, "Foo eq 2");
|
||||
assert.strictEqual(odata`Foo eq ${-2}`, "Foo eq -2");
|
||||
assert.strictEqual(odata`Foo eq ${2.5}`, "Foo eq 2.5");
|
||||
});
|
||||
|
||||
it("limits", () => {
|
||||
assert.strictEqual(odata`Foo eq ${Number.NEGATIVE_INFINITY}`, "Foo eq -Infinity");
|
||||
assert.strictEqual(odata`Foo eq ${Number.POSITIVE_INFINITY}`, "Foo eq Infinity");
|
||||
assert.strictEqual(odata`Foo eq ${Number.NaN}`, "Foo eq NaN");
|
||||
});
|
||||
|
||||
it("dates", () => {
|
||||
const result: string = odata`Foo eq ${new Date(1912, 6, 23, 11, 59, 59)}`;
|
||||
assert.strictEqual(result.includes("Tue Jul 23 1912 11:59:59"), true);
|
||||
});
|
||||
|
||||
it("text", () => {
|
||||
assert.strictEqual(odata`Foo eq ${"x"}`, "Foo eq 'x'");
|
||||
assert.strictEqual(odata`Foo eq ${"'"}`, "Foo eq ''''");
|
||||
assert.strictEqual(odata`Foo eq ${'"'}`, "Foo eq '\"'");
|
||||
assert.strictEqual(odata`Foo eq ${"bar"}`, "Foo eq 'bar'");
|
||||
assert.strictEqual(odata`Foo eq ${"bar's"}`, "Foo eq 'bar''s'");
|
||||
assert.strictEqual(odata`Foo eq ${'"bar"'}`, "Foo eq '\"bar\"'");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
sinon.restore();
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче