나는 Go에 JavaScript 테스트 스위트를 작성하려고한다.Google Go와 Chrome을 인터페이스하는 방법은 무엇입니까?
Go로 단위 테스트를 만들고 브라우저에로드 된 웹 응용 프로그램에 대해 실행하는 것을 상상해보십시오.
나는이 목적으로 test
JavaScript 개체를 전역 이름 공간에 넣었습니다. 그래서 테스트 할 여러 구성 요소에 "후크"가 있습니다.
이 스크립트를 Chrome에로드하고 test
에 인터페이스를 노출하여 Go에서 브라우저 내 JavaScript 기능에 액세스 할 수 있습니까?
예 :
// 자바 스크립트 함수
// Intentionally, and cautiously, emplace "test" into the global namespace
// In practice I would make this a singleton :^D
test = (typeof test == "undefined") ? {} : test;
// Employ the method invocation pattern to append "test" with function "cheesePrice()"
// ES6 fat arrow function
test.cheesePrice = (type) => {
let p = 0;
switch (type) {
case "cheddar" : { p = 9.99; break; };
case "swiss" : { p = 6.49; break; };
case "gouda" : { p = 7.49; break; };
default : { // throw; }
}
return p;
}
// Example call
test.cheesePrice("swiss"); //-> 6.49
가 나는 이동 스크립트 내에서, 위의 방법을 수 감안할 때, test.cheesePrice()를 호출하고 반환 값을받을 수? 이 같은 (의사 코드 된);
// Imagine here browserHook is an interface into Chrome
// Enabling us to invoke the method above
func getJsCheesePrice(cheeseType string) float32 {
return browserHook.test.getPrice(cheeseType);
}
func main() {
assert.Equal(getJsCheesePrice("cheddar"), 9.99);
}