テストケースあれこれ
ふむ。本格的に触ってみる。
レベルあがるな、これ。
javaだけ知ってても書けない罠だ。
対象[javascript]
Function.prototype.bind = function(func){ return function(){ var bind = arguments; return function(){ var args = new Array(); args = args.concat.apply(args, bind); args = args.concat.apply(args, arguments); return func.apply(null, args); }; }; }
テストケース[java]
全然不完全だけど。
public void testBind() throws Exception { Scriptable funcPrototype = ScriptableObject .getFunctionPrototype(this.scriptable); Object bindFunc = ScriptableObject.getProperty(funcPrototype, "bind"); Context context = Context.enter(); Object result = null; try { Function f = (Function) bindFunc; result = f.call(context, this.scriptable, this.scriptable, new Object[] { new BaseFunction() }); } finally { Context.exit(); } if (!(result instanceof Function)) { fail(); } assertEquals(((ScriptableObject) result).getClassName(), "Function"); } protected ScriptableObject getScriptableObject() throws Exception { Context context = Context.enter(); String path = getPath(); ClassLoader loader = Thread.currentThread().getContextClassLoader(); InputStream in = null; Reader reader = null; ScriptableObject scope = null; try { scope = context.initStandardObjects(); this.scriptable = scope; in = loader.getResourceAsStream(path); reader = new InputStreamReader(in, "UTF-8"); Script script = null; try { script = context.compileReader(reader, new File(path).getName(), 0, null); } catch (EcmaError e) { // TODO javascript syntax error e.printStackTrace(); throw e; } script.exec(context, scope); } finally { if (in != null) { in.close(); } context.exit(); } return scope; } protected void setUp() throws Exception { super.setUp(); this.scriptable = getScriptableObject(); }
全然不十分なテストケースなんだけどね…。
(BaseFunctionの処理自体実装してないし。呼んでないしー)
でもこの時点だけでも…難しいッス…orz
書き方次第かも知れないがテストのために実装コードに制限でるのは個人的にNG。
そもそも違う言語でテストケースを書くとその差を埋めるコードを書かなくてはいけなくなる気がする。
不本意なコードをごちゃごちゃ書かないといけないくらいならば同言語で書いた方が楽だという罠。
あと実作業を考えると言語切り替えのオーバーヘッドも考慮すべきか?
if(obj){ }
で評価したりクロージャー書いて怒られてみたりとかするし。
(これはホントよく書いてしまう…)
js実装→javaでテストの行き来を繰り返すので頭を切り替えんの大変だ。
rhino知ってなきゃいかんし。
個人的には複雑なjs書くとすぐ破綻するので素直にjsでテストするのがいいかなと。
実作業で使えるレベルであるか再度検討する余地ありですなあ。
Djangoじゃないけど
実作業の何を解決してくれるのか?
って所をちゃんと考えることが大事だなって思う。
うくく。