Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'develop'

  • Loading branch information...
commit b1d7393c82598b2e803e9c4d91de556fbb6e976a 2 parents 8e8dc94 + ec0db66
@ongaeshi authored
View
7 Gemfile
@@ -19,7 +19,8 @@ gem 'thor', '~> 0.15.0'
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
- gem "bundler"
- gem "jeweler"
- gem "rack-test"
+ gem 'bundler'
+ gem 'jeweler'
+ gem 'rack-test'
+ gem 'sinatra-reloader'
end
View
17 HISTORY.ja.rdoc
@@ -1,3 +1,20 @@
+=== 0.9.9 2013/02/26
+
+* ワイド範囲検索のサポート
+ * 指定行内に複数のキーワードが含まれている場所を探す
+ * 複数のキーワードが一行でマッチしなかった場合、自動的に範囲を広げて再検索
+
+* 検索オプション w: を追加
+ * w:7 で7行以内に全てのキーワードが含まれている場所を返す
+ * w:1 で今までの検索
+ * w:0 の時は検索範囲をファイル全域に
+
+* Support sinatra-reloader
+ * export MILKODE_SINATRA_RELOADER=1
+
+* Delete 'milkode/cdweb/app_error.rb'
+ * Recommend 'RACK_ENV=production'
+
=== 0.9.8 2013/02/27
* Support MyGithub
9 HISTORY.rdoc
@@ -1,3 +1,12 @@
+=== 0.9.9 2013/02/26
+
+* Suppor wide range search
+ * Add 'w:' search option
+* Support sinatra-reloader
+ * export MILKODE_SINATRA_RELOADER=1
+* Delete 'milkode/cdweb/app_error.rb'
+ * Recommend 'RACK_ENV=production'
+
=== 0.9.8 2013/02/27
* Support MyGithub
View
2  VERSION
@@ -1 +1 @@
-0.9.8
+0.9.9
View
2  bin/gmilk 100644 → 100755
@@ -6,5 +6,5 @@
require 'rubygems'
require 'milkode/grep/cli_grep'
-Version = "0.9.8"
+Version = "0.9.9"
Milkode::CLI_Grep.execute(STDOUT, ARGV)
View
2  bin/milk
@@ -6,5 +6,5 @@
require 'rubygems'
require 'milkode/cli'
-Version = "0.9.8"
+Version = "0.9.9"
Milkode::CLI.start(ARGV)
View
30 lib/milkode/cdweb/app.rb
@@ -7,6 +7,10 @@
require 'rubygems'
require 'sinatra'
+if ENV['MILKODE_SINATRA_RELOADER']
+ require 'sinatra/reloader'
+ also_reload '../../**/*.rb'
+end
require 'sass'
require 'haml'
@@ -21,12 +25,20 @@
set :haml, :format => :html5
get '/' do
- @setting = WebSetting.new
- @version = "0.9.8"
- @package_num = Database.instance.yaml_package_num
- @file_num = Database.instance.totalRecords
- @package_list = PackageList.new(Database.instance.grndb)
- haml :index, :layout => false
+ if Database.validate?
+ @setting = WebSetting.new
+ @version = "0.9.9"
+
+ @package_num = Database.instance.yaml_package_num
+ @file_num = Database.instance.totalRecords
+ @package_list = PackageList.new(Database.instance.grndb)
+ haml :index, :layout => false
+ else
+ <<EOF
+<h1>Setup Error!</h1>
+Database Directory: #{Database.dbdir}<br>
+EOF
+ end
end
def package_path(path)
@@ -103,12 +115,6 @@ def package_path(path)
haml :help
end
-begin
- NO_REQUIRE_APP_ERROR
-rescue NameError
- require 'milkode/cdweb/app_error'
-end
-
# -- helper function --
helpers do
View
15 lib/milkode/cdweb/app_error.rb
@@ -1,15 +0,0 @@
-# -*- coding: utf-8 -*-
-#
-# @file
-# @brief
-# @author ongaeshi
-# @date 2013/02/17
-
-get '*' do
- @setting = WebSetting.new
- @path = ''
- haml :error
-end
-
-
-
View
3  lib/milkode/cdweb/cli_cdweb.rb
@@ -112,7 +112,8 @@ def self.execute_with_options(stdout, options)
end
def self.select_dbdir
- if (Dbdir.dbdir?('.') || !Dbdir.dbdir?(Dbdir.default_dir))
+ # if (Dbdir.dbdir?('.') || !Dbdir.dbdir?(Dbdir.default_dir))
+ if Dbdir.dbdir?('.')
'.'
else
Dbdir.default_dir
View
8 lib/milkode/cdweb/lib/command.rb
@@ -35,7 +35,13 @@ def view(record, params, before)
@record_content = CodeRayWrapper.new(record.content, record.shortpath, match_lines).to_html
else
grep = Grep.new(record.content)
- match_lines = grep.match_lines_and(q.keywords, is_sensitive)
+ match_lines = grep.match_lines_and(q.keywords, is_sensitive, q.wide_match_range)
+
+ if match_lines.empty? && q.wide_match_range_empty?
+ # 検索範囲を広げる
+ match_lines = grep.match_lines_and(q.keywords, is_sensitive, 7)
+ end
+
@record_content = CodeRayWrapper.new(record.content, record.shortpath, match_lines).to_html
end
else
View
4 lib/milkode/cdweb/lib/database.rb
@@ -191,6 +191,10 @@ def update_all
end
result
end
+
+ def self.validate?
+ YamlFileWrapper.load_if(Database.dbdir) != nil
+ end
private
View
67 lib/milkode/cdweb/lib/grep.rb
@@ -6,6 +6,7 @@
# @date 2010/10/18
require 'milkode/common/util'
+require 'milkode/common/wide_matcher'
module Milkode
class Grep
@@ -13,23 +14,22 @@ def initialize(content)
@content = content
end
- MatchLineResult = Struct.new(:index, :match_datas)
+ def match_lines_stopover(patterns, max_match, start_index, is_sensitive, wide_match_range)
+ regexps = strs2regs(patterns, is_sensitive)
+ result = []
+ index = start_index
- def match_lines_stopover(patterns, max_match, start_index, is_sensitive)
- result = []
- patternRegexps = strs2regs(patterns, is_sensitive)
- index = start_index
-
- lines = @content.split($/)
+ matcher = WideMatcher.create(wide_match_range)
+ lines = @content.split($/)
while (index < lines.size) do
line = lines[index]
- match_datas = []
- patternRegexps.each {|v| match_datas << v.match(line)}
+ matcher.add_line_matchs(index, match_regexps(line, regexps))
- if (match_datas.all?)
- result << MatchLineResult.new(index, match_datas)
+ if matcher.match?
+ result += matcher.match_lines
+
if result.size >= max_match
index += 1
break
@@ -40,44 +40,27 @@ def match_lines_stopover(patterns, max_match, start_index, is_sensitive)
end
index = 0 if (index >= lines.size)
- {:result => result, :next_line => index}
+ {:result => result.uniq, :next_line => index}
end
- def match_lines_and(patterns, is_sensitive)
- result = []
- patternRegexps = strs2regs(patterns, is_sensitive)
- index = 0
+ def match_lines_and(patterns, is_sensitive, wide_match_range)
+ regexps = strs2regs(patterns, is_sensitive)
+ result = []
+ index = 0
+
+ matcher = WideMatcher.create(wide_match_range)
@content.each_line do |line|
- match_datas = []
- patternRegexps.each {|v| match_datas << v.match(line)}
-
- if (match_datas.all?)
- result << MatchLineResult.new(index, match_datas)
- end
-
+ matcher.add_line_matchs(index, match_regexps(line, regexps))
+ result += matcher.match_lines if matcher.match?
index += 1
end
- result
+ result.uniq
end
- def one_match_and(patterns, is_sensitive)
- patternRegexps = strs2regs(patterns, is_sensitive)
- index = 0
-
- @content.each_line do |line|
- match_datas = []
- patternRegexps.each {|v| match_datas << v.match(line)}
-
- if (match_datas.all?)
- return MatchLineResult.new(index, match_datas)
- end
-
- index += 1
- end
-
- nil
+ def one_match_and(patterns, is_sensitive, wide_match_range)
+ match_lines_stopover(patterns, 1, 0, is_sensitive, wide_match_range)
end
private
@@ -93,6 +76,10 @@ def strs2regs(strs, is_sensitive)
regs
end
+
+ def match_regexps(line, regexps)
+ regexps.reduce([]) {|result, v| result << v.match(line); result}
+ end
end
end
View
42 lib/milkode/cdweb/lib/query.rb
@@ -13,18 +13,20 @@ class Query
attr_reader :query_string
OPTIONS = [
- ['package', 'p'],
- ['filepath', 'fpath', 'f'],
- ['suffix', 's'],
- ['fp'], # fpath or package
- ['keyword', 'k'],
- ['gotoline', 'g'],
+ ['package' , 'p'],
+ ['filepath' , 'fpath', 'f'],
+ ['suffix' , 's'],
+ ['fp'] , # fpath or package
+ ['keyword' , 'k'],
+ ['gotoline' , 'g'],
+ ['wide' , 'w'],
]
def initialize(str)
@query_string = str
init_hash
parse
+ @wide_match_range = calc_param(6)
end
def escape_html
@@ -32,11 +34,11 @@ def escape_html
end
def empty?
- keywords.size == 0 && packages.size == 0 && fpaths.size == 0 && suffixs.size == 0 && fpath_or_packages.size == 0 && gotolines.size == 0
+ keywords.size == 0 && only_keywords
end
def only_keywords
- packages.size == 0 && fpaths.size == 0 && suffixs.size == 0 && fpath_or_packages.size == 0 && gotolines.size == 0
+ packages.size == 0 && fpaths.size == 0 && suffixs.size == 0 && fpath_or_packages.size == 0 && gotolines.size == 0 && wide_match_range_empty?
end
def keywords
@@ -68,6 +70,26 @@ def gotolines
calc_param(5)
end
+ def wide_match_range
+ a = @wide_match_range
+
+ if a.empty?
+ 1
+ else
+ i = a[-1].to_i
+
+ if (i == 0)
+ 0
+ else
+ i
+ end
+ end
+ end
+
+ def wide_match_range_empty?
+ @wide_match_range.empty?
+ end
+
def conv_keywords_to_fpath
s = query_string.split.map {|v|
if keywords.include? v
@@ -118,6 +140,10 @@ def conv_fuzzy_gotoline
Query.new(s)
end
+ def conv_wide_match_range(match_range)
+ Query.new(query_string + " w:#{match_range}")
+ end
+
private
def calc_param(index)
View
147 lib/milkode/cdweb/lib/search_contents.rb
@@ -24,45 +24,59 @@ class SearchContents
MATH_FILE_DISP = 3 # マッチファイルの最大表示数
MATH_FILE_LIMIT = MATH_FILE_DISP + 1 # マッチファイルの検索リミット数
+ DEFAULT_WIDE_MATCH_RANGE = 7 # 未指定時のワイド検索範囲
+
def initialize(path, params, query)
- @path = path
- @params = params
- @q = query
- @page = params[:page].to_i || 0
- @offset = params[:offset].to_i
- @line = params[:line].to_i
- @is_onematch = params[:onematch] == 'on'
- @is_sensitive = params[:sensitive] == 'on'
+ @path = path
+ @params = params
+ @q = query
+ @page = params[:page].to_i || 0
+ @offset = params[:offset].to_i
+ @line = params[:line].to_i
+ @is_onematch = params[:onematch] == 'on'
+ @is_sensitive = params[:sensitive] == 'on'
+
@searcher_fuzzy_gotoline = nil
# 検索1 : クエリーそのまま
@records, @total_records = Database.instance.search(@q.keywords, @q.multi_match_keywords, @q.packages, path, @q.fpaths, @q.suffixs, @q.fpath_or_packages, @offset, LIMIT_NUM)
- grep_contents(@q.keywords)
+ grep_contents(@q.keywords, @q.wide_match_range)
# 検索2 : マッチしなかった時におすすめクエリーがある場合
- if @match_records.empty?
- if recommended_fuzzy_gotoline?
- # 専用の Searcher を作成
- @searcher_fuzzy_gotoline = SearchFuzzyGotoLine.new(@path, @params, @q)
-
- # 結果をコピーする
- @total_records = @searcher_fuzzy_gotoline.total_records
- @match_records = @searcher_fuzzy_gotoline.match_records
- @next_index = @searcher_fuzzy_gotoline.next_index
- @end_index = @searcher_fuzzy_gotoline.end_index
- @next_line = nil
-
- elsif recommended_fpath_or_packages?
- # おすすめクエリーに変換
- q2 = @q.conv_head_keyword_to_fpath_or_packages
-
- # 検索
- @records, @total_records = Database.instance.search(q2.keywords, q2.multi_match_keywords, q2.packages, path, q2.fpaths, q2.suffixs, q2.fpath_or_packages, @offset, LIMIT_NUM)
-
- # 再grep
- grep_contents(q2.keywords)
+
+ # gotolineモード (test_cdstk.rb:55)
+ if @match_records.empty? && recommended_fuzzy_gotoline?
+ # 専用の Searcher を作成
+ @searcher_fuzzy_gotoline = SearchFuzzyGotoLine.new(@path, @params, @q)
+
+ # 結果をコピーする
+ @total_records = @searcher_fuzzy_gotoline.total_records
+ @match_records = @searcher_fuzzy_gotoline.match_records
+ @next_index = @searcher_fuzzy_gotoline.next_index
+ @end_index = @searcher_fuzzy_gotoline.end_index
+ @next_line = nil
+ end
+
+ # ワイド検索範囲
+ if @match_records.empty? && recommended_wide_match_range?
+ grep_contents(@q.keywords, DEFAULT_WIDE_MATCH_RANGE)
+
+ if @match_records.empty?
+ grep_contents(@q.keywords, 0)
end
end
+
+ # 先頭をファイル名とみなす
+ if @match_records.empty? && recommended_fpath_or_packages?
+ # おすすめクエリーに変換
+ q2 = @q.conv_head_keyword_to_fpath_or_packages
+
+ # 検索
+ @records, @total_records = Database.instance.search(q2.keywords, q2.multi_match_keywords, q2.packages, path, q2.fpaths, q2.suffixs, q2.fpath_or_packages, @offset, LIMIT_NUM)
+
+ # 再grep
+ grep_contents(q2.keywords, q2.wide_match_range)
+ end
# 検索3 : マッチするファイル
@match_files = []
@@ -135,33 +149,58 @@ def recommended_fuzzy_gotoline?
@q.keywords.size == 1 && @q.only_keywords && Util::fuzzy_gotoline_keyword?(@q.keywords[0])
end
+ def recommended_wide_match_range?
+ @q.keywords.size >= 2 && @q.wide_match_range_empty?
+ end
+
def recommended_fpath_or_packages?
@q.keywords.size >= 2 && @q.only_keywords
end
def recommended_query_contents
+ result = []
+
if recommended_fuzzy_gotoline?
conv_query = @q.conv_fuzzy_gotoline
tmpp = @params.clone
tmpp[:query] = conv_query.query_string
url = Mkurl.new(@path, tmpp).inherit_query_shead
- <<EOS
-<dt class='result-file'>#{img_icon('document-new-4.png')}<a href='#{url}'>#{conv_query.query_string}</a></dt>
-<hr>
-EOS
- elsif recommended_fpath_or_packages?
+ result << "<dt class='result-file'>#{img_icon('document-new-4.png')}<a href='#{url}'>#{conv_query.query_string}</a></dt>"
+ end
+
+ if recommended_wide_match_range?
+ conv_query = @q.conv_wide_match_range(0)
+ tmpp = @params.clone
+ tmpp[:query] = conv_query.query_string
+ w0_url = Mkurl.new(@path, tmpp).inherit_query_shead
+
+ conv_query = @q.conv_wide_match_range(1)
+ tmpp = @params.clone
+ tmpp[:query] = conv_query.query_string
+ w1_url = Mkurl.new(@path, tmpp).inherit_query_shead
+
+ conv_query = @q.conv_wide_match_range(DEFAULT_WIDE_MATCH_RANGE)
+ tmpp = @params.clone
+ tmpp[:query] = conv_query.query_string
+ url = Mkurl.new(@path, tmpp).inherit_query_shead
+
+ result << "<dt class='result-file'>#{img_icon('document-new-4.png')}<a href='#{url}'>#{conv_query.query_string}</a> (<a href='#{w0_url}'>w:0</a>, <a href='#{w1_url}'>w:1</a>)</dt>"
+ end
+
+ if recommended_fpath_or_packages?
conv_query = @q.conv_head_keyword_to_fpath_or_packages
tmpp = @params.clone
tmpp[:query] = conv_query.query_string
url = Mkurl.new(@path, tmpp).inherit_query_shead
- <<EOS
-<dt class='result-file'>#{img_icon('document-new-4.png')}<a href='#{url}'>#{conv_query.query_string}</a></dt>
-<hr>
-EOS
+ result << "<dt class='result-file'>#{img_icon('document-new-4.png')}<a href='#{url}'>#{conv_query.query_string}</a></dt>"
+ end
+
+ unless result.empty?
+ result.join("\n") + "<hr>\n"
else
""
end
- end
+ end
def match_files_contents
unless @match_files.empty?
@@ -208,26 +247,15 @@ def directjump_url
MatchRecord = Struct.new(:record, :match_line)
- def grep_contents(keywords)
+ def grep_contents(keywords, wide_match_range)
@match_records = []
@end_index = @next_index = @records.size
@next_line = nil
@records.each_with_index do |record, index|
if (Util::larger_than_oneline(record.content))
-
- if @is_onematch
- grep = Grep.new(record.content)
- match_line = grep.one_match_and(keywords, @is_sensitive)
- @match_records << MatchRecord.new(record, match_line) if match_line
-
- if @match_records.size >= DISP_NUM
- @end_index = index
- @next_index = index + 1
- break
- end
- else
- break if grep_match_lines_stopover(record, index, keywords)
+ if grep_match_lines_stopover(record, index, keywords, wide_match_range)
+ break
end
else
@match_records << MatchRecord.new(record, Grep::MatchLineResult.new(0, nil))
@@ -241,9 +269,14 @@ def grep_contents(keywords)
end
end
- def grep_match_lines_stopover(record, index, keywords)
- grep = Grep.new(record.content)
- r = grep.match_lines_stopover(keywords, DISP_NUM - @match_records.size, (index == 0) ? @line : 0, @is_sensitive)
+ def grep_match_lines_stopover(record, index, keywords, wide_match_range)
+ grep = Grep.new(record.content)
+
+ if @is_onematch
+ r = grep.one_match_and(keywords, @is_sensitive, wide_match_range)
+ else
+ r = grep.match_lines_stopover(keywords, DISP_NUM - @match_records.size, (index == 0) ? @line : 0, @is_sensitive, wide_match_range)
+ end
r[:result].each do |match_line|
@match_records << MatchRecord.new(record, match_line) if match_line
View
2  lib/milkode/cli.rb
@@ -170,7 +170,7 @@ def web
:DbDir => options[:db],
}
opts[:customize] = options[:customize]
- cdstk(opts[:DbDir]).assert_compatible
+ # cdstk(opts[:DbDir]).assert_compatible
Milkode::CLI_Cdweb.execute_with_options($stdout, opts)
end
View
84 lib/milkode/common/wide_matcher.rb
@@ -0,0 +1,84 @@
+# -*- coding: utf-8 -*-
+#
+# @file
+# @brief
+# @author ongaeshi
+# @date 2013/03/15
+
+module Milkode
+ MatchLineResult = Struct.new(:index, :match_datas)
+
+ class WideMatcher
+ attr_reader :num_max
+
+ def self.create(num_max)
+ if num_max == 0
+ WideMatcherZero.new
+ else
+ WideMatcher.new(num_max)
+ end
+ end
+
+ def initialize(num_max)
+ @num_max = num_max
+ @container = []
+ end
+
+ def linenum
+ @container.size
+ end
+
+ def add_line_matchs(index, matches)
+ @last_index = index
+ @container.shift if linenum >= @num_max
+ @container << matches
+ # p @container
+ end
+
+ def match?
+ @container.reduce(Array.new(@container.first.size)) {|result, matches|
+ matches.each_with_index do |m, i|
+ result[i] |= m
+ end
+ result
+ }.all?
+ end
+
+ def match_lines
+ index = @last_index - @container.size + 1
+ @container.reduce([]) do |result, matches|
+ m = matches.compact
+ result << MatchLineResult.new(index, m) unless m.empty?
+ index += 1
+ result
+ end
+ end
+ end
+
+ class WideMatcherZero
+ attr_reader :num_max
+
+ def initialize
+ @num_max = 0
+ end
+
+ def linenum
+ 1
+ end
+
+ def add_line_matchs(index, matches)
+ @index = index
+ @matches = matches
+ end
+
+ def match?
+ @matches.any?
+ end
+
+ def match_lines
+ [MatchLineResult.new(@index, @matches.compact)]
+ end
+ end
+end
+
+
View
10 milkode.gemspec
@@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{milkode}
- s.version = "0.9.8"
+ s.version = "0.9.9"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["ongaeshi"]
- s.date = %q{2013-02-27}
+ s.date = %q{2013-03-26}
s.description = %q{Line based local source code search engine & grep-command & web-app.}
s.email = %q{ongaeshi0621@gmail.com}
s.executables = ["gmilk", "milk"]
@@ -35,7 +35,6 @@ Gem::Specification.new do |s|
"lib/milkode/cdstk/package.rb",
"lib/milkode/cdstk/yaml_file_wrapper.rb",
"lib/milkode/cdweb/app.rb",
- "lib/milkode/cdweb/app_error.rb",
"lib/milkode/cdweb/cli_cdweb.rb",
"lib/milkode/cdweb/config.ru",
"lib/milkode/cdweb/lib/coderay_html2.rb",
@@ -117,6 +116,7 @@ Gem::Specification.new do |s|
"lib/milkode/common/platform.rb",
"lib/milkode/common/string_snip.rb",
"lib/milkode/common/util.rb",
+ "lib/milkode/common/wide_matcher.rb",
"lib/milkode/database/document_record.rb",
"lib/milkode/database/document_table.rb",
"lib/milkode/database/groonga_database.rb",
@@ -184,6 +184,7 @@ Gem::Specification.new do |s|
"test/test_string_snip.rb",
"test/test_updater.rb",
"test/test_util.rb",
+ "test/test_wide_matcher.rb",
"test/test_yaml_file_wrapper.rb"
]
s.homepage = %q{http://github.com/ongaeshi/milkode}
@@ -211,6 +212,7 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<bundler>, [">= 0"])
s.add_development_dependency(%q<jeweler>, [">= 0"])
s.add_development_dependency(%q<rack-test>, [">= 0"])
+ s.add_development_dependency(%q<sinatra-reloader>, [">= 0"])
else
s.add_dependency(%q<termcolor>, [">= 1.2.0", "< 1.2.2"])
s.add_dependency(%q<rroonga>, [">= 1.1.0"])
@@ -226,6 +228,7 @@ Gem::Specification.new do |s|
s.add_dependency(%q<bundler>, [">= 0"])
s.add_dependency(%q<jeweler>, [">= 0"])
s.add_dependency(%q<rack-test>, [">= 0"])
+ s.add_dependency(%q<sinatra-reloader>, [">= 0"])
end
else
s.add_dependency(%q<termcolor>, [">= 1.2.0", "< 1.2.2"])
@@ -242,6 +245,7 @@ Gem::Specification.new do |s|
s.add_dependency(%q<bundler>, [">= 0"])
s.add_dependency(%q<jeweler>, [">= 0"])
s.add_dependency(%q<rack-test>, [">= 0"])
+ s.add_dependency(%q<sinatra-reloader>, [">= 0"])
end
end
View
2  test/test_cdweb_app.rb
@@ -61,7 +61,7 @@ def t_help
def t_not_found
get '/not_found'
- assert_equal 200, last_response.status
+ assert_equal 404, last_response.status
end
def t_view_empty_file
View
14 test/test_query.rb
@@ -78,6 +78,20 @@ def test_gotolines
assert_equal q.keywords , ['a.rb']
end
+ def test_wide_match_range
+ assert_equal 1 , create_query("").wide_match_range
+ assert_equal true , create_query("").wide_match_range_empty?
+ assert_equal 1 , create_query("w:").wide_match_range
+ assert_equal true , create_query("w:").wide_match_range_empty?
+ assert_equal 1 , create_query("w:1").wide_match_range
+ assert_equal false , create_query("w:1").wide_match_range_empty?
+ assert_equal 6 , create_query("w:5 w:6").wide_match_range
+ assert_equal 0 , create_query("w:0").wide_match_range
+ assert_equal 0 , create_query("w:aaa").wide_match_range
+ end
+
+ private
+
def create_query(query)
Query.new(query)
end
View
64 test/test_wide_matcher.rb
@@ -0,0 +1,64 @@
+# -*- coding: utf-8 -*-
+#
+# @file
+# @brief
+# @author ongaeshi
+# @date 2011/02/20
+
+require 'test_helper'
+require 'milkode/common/wide_matcher'
+
+class TestWideMatcher < Test::Unit::TestCase
+ include Milkode
+
+ SRC = <<EOF
+aaa
+bbb
+ccc
+ddd
+aaa
+EOF
+
+ def test_initialize
+ obj = WideMatcher.create(5)
+ assert_equal 5, obj.num_max
+ assert_equal 0, obj.linenum
+ end
+
+ def test_add_line_matches
+ lines = SRC.split("\n")
+ regexps = strs2regs(['a', 'c'])
+
+ obj = WideMatcher.create(3)
+
+ obj.add_line_matchs( 0, match_regexps(lines[0], regexps) )
+ assert_equal false, obj.match?
+
+ obj.add_line_matchs( 1, match_regexps(lines[1], regexps) )
+ assert_equal false, obj.match?
+
+ obj.add_line_matchs( 2, match_regexps(lines[2], regexps) )
+ assert_equal true, obj.match?
+ assert_equal [0, 2], obj.match_lines.map{|v| v.index}
+
+ obj.add_line_matchs( 3, match_regexps(lines[3], regexps) )
+ assert_equal false, obj.match?
+
+ obj.add_line_matchs( 4, match_regexps(lines[4], regexps) )
+ assert_equal true, obj.match?
+ assert_equal [2, 4], obj.match_lines.map{|v| v.index}
+ end
+
+ private
+
+ def strs2regs(strs)
+ strs.map do |v|
+ Regexp.new(Regexp.escape(v))
+ end
+ end
+
+ def match_regexps(line, regexps)
+ regexps.map {|v| v.match(line)}
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.