如何创建 GoogleAppsScript.Document.Text 的实例
How to create an instance of GoogleAppsScript.Document.Text
我想对这段代码进行单元测试,它是使用 Clasp 转译成 Google 应用程序脚本的 Typescript:
function onLinksInText(text: GoogleAppsScript.Document.Text, onLink: OnLinkCallback) {
const iterator = new TextIterator(text, onLink)
const characters = text.getText();
for (let i = 0; i < characters.length; i++) {
iterator.handle(i, text.getLinkUrl(i));
}
iterator.end()
}
为此,我需要创建一个 GoogleAppsScript.Document.Text
的实例
我该怎么做?
简答
您将不得不模拟 class。
长答案
要成功测试该功能,您只需要构造函数,getText
and getLinkUrl
methods mocked. Let us also mock setText
好的措施:
function TextFactory() {
const linkMatcher = /https?:\/\/(?:www\.)?(?:\.{0,1}(?:\w|-)+)+/ig;
let private = "";
return {
setText(text) {
private = text.toString();
return this;
},
getText() {
return private;
},
getLinkUrl(offset) {
const idx = private.search(linkMatcher);
if(idx < 0 || offset > idx ) { return null; }
return private.slice(idx);
}
};
}
const mock = TextFactory();
mock.setText("Find me at https://example.com ");
console.log(mock.getText());
console.log(mock.getLinkUrl(4));
console.log(mock.getLinkUrl(12));
我假设 TextIterator
是您的习惯 class,因此,为了演示,我做了一个简单的浅模拟:
function TextFactory() {
const linkMatcher = /https?:\/\/(?:www\.)?(?:\.{0,1}(?:\w|-)+)+/ig;
let private = "";
return {
setText(text) {
private = text.toString();
return this;
},
getText() {
return private;
},
getLinkUrl(offset) {
const idx = private.search(linkMatcher);
if(idx < 0 || offset > idx ) { return null; }
return private.slice(idx);
}
};
}
const mock = TextFactory();
mock.setText("Find me at https://example.com ");
class TextIterator {
constructor(text, onLink) {
this.text = text;
this.callback = onLink;
}
handle(index, link) {
this.callback(`${link} at index ${index}`);
}
end() {
console.log("ended");
}
}
function onLinksInText(text, onLink) {
const iterator = new TextIterator(text, onLink)
const characters = text.getText();
for (let i = 0; i < characters.length; i++) {
iterator.handle(i, text.getLinkUrl(i));
}
iterator.end()
}
onLinksInText(mock, console.log );
参考资料
Text
class documentation
我想对这段代码进行单元测试,它是使用 Clasp 转译成 Google 应用程序脚本的 Typescript:
function onLinksInText(text: GoogleAppsScript.Document.Text, onLink: OnLinkCallback) {
const iterator = new TextIterator(text, onLink)
const characters = text.getText();
for (let i = 0; i < characters.length; i++) {
iterator.handle(i, text.getLinkUrl(i));
}
iterator.end()
}
为此,我需要创建一个 GoogleAppsScript.Document.Text
我该怎么做?
简答
您将不得不模拟 class。
长答案
要成功测试该功能,您只需要构造函数,getText
and getLinkUrl
methods mocked. Let us also mock setText
好的措施:
function TextFactory() {
const linkMatcher = /https?:\/\/(?:www\.)?(?:\.{0,1}(?:\w|-)+)+/ig;
let private = "";
return {
setText(text) {
private = text.toString();
return this;
},
getText() {
return private;
},
getLinkUrl(offset) {
const idx = private.search(linkMatcher);
if(idx < 0 || offset > idx ) { return null; }
return private.slice(idx);
}
};
}
const mock = TextFactory();
mock.setText("Find me at https://example.com ");
console.log(mock.getText());
console.log(mock.getLinkUrl(4));
console.log(mock.getLinkUrl(12));
我假设 TextIterator
是您的习惯 class,因此,为了演示,我做了一个简单的浅模拟:
function TextFactory() {
const linkMatcher = /https?:\/\/(?:www\.)?(?:\.{0,1}(?:\w|-)+)+/ig;
let private = "";
return {
setText(text) {
private = text.toString();
return this;
},
getText() {
return private;
},
getLinkUrl(offset) {
const idx = private.search(linkMatcher);
if(idx < 0 || offset > idx ) { return null; }
return private.slice(idx);
}
};
}
const mock = TextFactory();
mock.setText("Find me at https://example.com ");
class TextIterator {
constructor(text, onLink) {
this.text = text;
this.callback = onLink;
}
handle(index, link) {
this.callback(`${link} at index ${index}`);
}
end() {
console.log("ended");
}
}
function onLinksInText(text, onLink) {
const iterator = new TextIterator(text, onLink)
const characters = text.getText();
for (let i = 0; i < characters.length; i++) {
iterator.handle(i, text.getLinkUrl(i));
}
iterator.end()
}
onLinksInText(mock, console.log );
参考资料
Text
class documentation