test/wolverine/script_test.rb in wolverine-0.2.2 vs test/wolverine/script_test.rb in wolverine-0.2.3

- old
+ new

@@ -1,56 +1,54 @@ -require_relative '../test_helper' +require File.join(File.expand_path('../../test_helper', __FILE__)) require 'digest/sha1' module Wolverine class ScriptTest < MiniTest::Unit::TestCase CONTENT = "return 1" DIGEST = Digest::SHA1.hexdigest(CONTENT) def setup - Wolverine::Script.any_instance.stubs(load_lua: CONTENT) + base = Pathname.new('/a/b/c/d') + Wolverine.config.script_path = base + Wolverine::Script.any_instance.stubs(:load_lua => CONTENT) end - def script - @script ||= Wolverine::Script.new('file1') + def teardown + Wolverine.config.instrumentation = proc{} end - def test_compilation_error - base = Pathname.new('/a/b/c/d') - file = Pathname.new('/a/b/c/d/e/file1.lua') - Wolverine.config.script_path = base - begin - script = Wolverine::Script.new(file) - script.instance_variable_set("@content", "asdfasdfasdf+31f") - script.instance_variable_set("@digest", "79437f5edda13f9c1669b978dd7a9066dd2059f1") - script.call(Redis.new) - rescue Wolverine::LuaError => e - assert_equal "'=' expected near '+'", e.message - assert_equal "/a/b/c/d/e/file1.lua:1", e.backtrace.first - assert_match /script.rb/, e.backtrace[1] - end + def script + @script ||= Wolverine::Script.new('/a/b/c/d/e/file1.lua') end - def test_runtime_error - base = Pathname.new('/a/b/c/d') - file = Pathname.new('/a/b/c/d/e/file1.lua') - Wolverine.config.script_path = base + def test_error + redis = stub + redis.expects(:evalsha).raises(%q{ERR Error running script (call to f_178d75adaa46af3d8237cfd067c9fdff7b9d504f): [string "func definition"]:1: attempt to compare nil with number}) begin - script = Wolverine::Script.new(file) - script.instance_variable_set("@content", "return nil > 3") - script.instance_variable_set("@digest", "39437f5edda13f9c1669b978dd7a9066dd2059f1") - script.call(Redis.new) + script.call(redis) rescue Wolverine::LuaError => e - assert_equal "attempt to compare number with nil", e.message + assert_equal "attempt to compare nil with number", e.message assert_equal "/a/b/c/d/e/file1.lua:1", e.backtrace.first assert_match /script.rb/, e.backtrace[1] end end - def test_digest_and_content - content = "return 1" - assert_equal CONTENT, script.content - assert_equal DIGEST, script.digest + def test_instrumentation + callback = Object.new + tc = self + meta = class << callback ; self ; end + meta.send(:define_method, :call) { |a, b, c| + tc.assert_equal "e/file1.lua", a + tc.assert_operator b, :<, 1 + tc.assert_equal :evalsha, c + } + Wolverine.config.instrumentation = callback + redis = Class.new do + define_method(:evalsha) do |digest, size, *args| + nil + end + end + script.call(redis.new, :a, :b) end def test_call_with_cache_hit tc = self redis = Class.new do