diff options
Diffstat (limited to 'lib/coderay/duo.rb')
-rw-r--r-- | lib/coderay/duo.rb | 67 |
1 files changed, 64 insertions, 3 deletions
diff --git a/lib/coderay/duo.rb b/lib/coderay/duo.rb index c11bd3f..d8b064d 100644 --- a/lib/coderay/duo.rb +++ b/lib/coderay/duo.rb @@ -1,3 +1,4 @@ +($:.unshift '..'; require 'coderay') unless defined? CodeRay module CodeRay # = Duo @@ -44,12 +45,12 @@ module CodeRay end @options = options end - + class << self # To allow calls like Duo[:ruby, :html].highlight. alias [] new end - + # The scanner of the duo. Only created once. def scanner @scanner ||= CodeRay.scanner @lang, CodeRay.get_scanner_options(@options) @@ -66,8 +67,68 @@ module CodeRay encoder.encode(code, @lang, options) end alias highlight encode - + + # Allows to use Duo like a proc object: + # + # CodeRay::Duo[:python => :yaml].call(code) + # + # or, in Ruby 1.9 and later: + # + # CodeRay::Duo[:python => :yaml].(code) + alias call encode + end + +end +if $0 == __FILE__ + $VERBOSE = true + $: << File.join(File.dirname(__FILE__), '..') + eval DATA.read, nil, $0, __LINE__ + 4 end +__END__ +require 'test/unit' + +class DuoTest < Test::Unit::TestCase + + def test_two_arguments + duo = CodeRay::Duo[:ruby, :html] + assert_kind_of CodeRay::Scanners[:ruby], duo.scanner + assert_kind_of CodeRay::Encoders[:html], duo.encoder + end + + def test_two_hash + duo = CodeRay::Duo[:ruby => :html] + assert_kind_of CodeRay::Scanners[:ruby], duo.scanner + assert_kind_of CodeRay::Encoders[:html], duo.encoder + end + + def test_call + duo = CodeRay::Duo[:python => :yaml] + assert_equal <<-'YAML', duo.call('def test: "pass"') +--- +- - def + - :keyword +- - " " + - :space +- - test + - :method +- - ":" + - :operator +- - " " + - :space +- - :begin_group + - :string +- - "\"" + - :delimiter +- - pass + - :content +- - "\"" + - :delimiter +- - :end_group + - :string + YAML + end + +end |