Skip to content

Commit a6b2562

Browse files
committed
#352 CodeRay.scan returns TokensProxy
1 parent 13e3123 commit a6b2562

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

lib/coderay.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ module CodeRay
134134

135135
# Tokens
136136
autoload :Tokens, 'coderay/tokens'
137+
autoload :TokensProxy, 'coderay/tokens_proxy'
137138
autoload :TokenKinds, 'coderay/token_kinds'
138139

139140
# Plugin system
@@ -159,7 +160,7 @@ class << self
159160
# See also demo/demo_simple.
160161
def scan code, lang, options = {}, &block
161162
# FIXME: return a proxy for direct-stream encoding
162-
scanner(lang, options, &block).tokenize code
163+
TokensProxy.new code, lang, options, block
163164
end
164165

165166
# Scans +filename+ (a path to a code file) with the Scanner for +lang+.

lib/coderay/tokens_proxy.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module CodeRay
2+
3+
class TokensProxy < Struct.new :code, :lang, :options, :block
4+
5+
def method_missing method, *args, &blk
6+
tokens.send(method, *args, &blk)
7+
end
8+
9+
def tokens
10+
@tokens ||= scanner.tokenize(code)
11+
end
12+
13+
def each *args, &blk
14+
tokens.each(*args, &blk)
15+
end
16+
17+
def count
18+
tokens.count
19+
end
20+
21+
def scanner
22+
@scanner ||= CodeRay.scanner(lang, options, &block)
23+
end
24+
25+
end
26+
27+
end

test/functional/examples.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def test_examples
4848

4949
# keep scanned tokens for later use
5050
tokens = CodeRay.scan('{ "just": "an", "example": 42 }', :json)
51+
assert_kind_of CodeRay::TokensProxy, tokens
52+
5153
assert_equal ["{", :operator, " ", :space, :begin_group, :key,
5254
"\"", :delimiter, "just", :content, "\"", :delimiter,
5355
:end_group, :key, ":", :operator, " ", :space,
@@ -56,8 +58,8 @@ def test_examples
5658
" ", :space, :begin_group, :key, "\"", :delimiter,
5759
"example", :content, "\"", :delimiter, :end_group, :key,
5860
":", :operator, " ", :space, "42", :integer,
59-
" ", :space, "}", :operator], tokens
60-
61+
" ", :space, "}", :operator], tokens.tokens
62+
6163
# produce a token statistic
6264
assert_equal <<-STATISTIC, tokens.statistic
6365
@@ -84,7 +86,7 @@ def test_examples
8486
STATISTIC
8587

8688
# count the tokens
87-
assert_equal 26, tokens.count # => 26
89+
assert_equal 26, tokens.count
8890

8991
# produce a HTML div, but with CSS classes
9092
div = tokens.div(:css => :class)

test/unit/debug.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ def test_creation
7070

7171
def test_filtering_text_tokens
7272
assert_equal TEST_OUTPUT, CodeRay::Scanners::Debug.new.tokenize(TEST_INPUT)
73-
assert_equal TEST_OUTPUT, CodeRay.scan(TEST_INPUT, :debug)
73+
assert_kind_of CodeRay::TokensProxy, CodeRay.scan(TEST_INPUT, :debug)
74+
assert_equal TEST_OUTPUT, CodeRay.scan(TEST_INPUT, :debug).tokens
7475
end
7576

7677
end

test/unit/lines_of_code.rb

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
require 'coderay'
33
$VERBOSE = true
44

5+
require File.expand_path('../../lib/assert_warning', __FILE__)
6+
57
class LinesOfCodeTest < Test::Unit::TestCase
68

79
def test_creation
@@ -39,17 +41,8 @@ def test_filtering_block_tokens
3941
tokens.concat ["\n", :space]
4042
tokens.concat ["Hello\n", :comment]
4143

42-
stderr, fake_stderr = $stderr, Object.new
43-
begin
44-
$err = ''
45-
def fake_stderr.write x
46-
$err << x
47-
end
48-
$stderr = fake_stderr
44+
assert_warning 'Tokens have no associated scanner, counting all nonempty lines.' do
4945
assert_equal 1, tokens.lines_of_code
50-
assert_equal "Tokens have no associated scanner, counting all nonempty lines.\n", $err
51-
ensure
52-
$stderr = stderr
5346
end
5447

5548
tokens.scanner = ScannerMockup.new

0 commit comments

Comments
 (0)