# -*- encoding: utf-8 -*-
require "helper"
module Nokogiri
module XSLT
class TestCustomFunctions < Nokogiri::TestCase
def setup
super
@xml = Nokogiri.XML(<<-EOXML)
Foo
Foo
Lorem ipsum.
EOXML
end
def test_function
skip("Pure Java version doesn't support this feature.") if !Nokogiri.uses_libxml?
foo = Class.new do
def capitalize nodes
nodes.first.content.upcase
end
end
XSLT.register "http://e.org/functions", foo
xsl = Nokogiri.XSLT(<<-EOXSL)
EOXSL
result = xsl.transform @xml
assert_match(/FOO/, result.css('title').first.text)
end
def test_function_arguments
skip("Pure Java version doesn't support this feature.") if !Nokogiri.uses_libxml?
foo = Class.new do
include MiniTest::Assertions
# Minitest 5 uses `self.assertions` in `assert()` which is not
# defined in the Minitest::Assertions module :-(
attr_writer :assertions
def assertions; @assertions ||= 0; end
def multiarg *args
assert_equal ["abc", "xyz"], args
args.first
end
def numericarg arg
assert_equal 42, arg
arg
end
end
xsl = Nokogiri.XSLT(<<-EOXSL, "http://e.org/functions" => foo)
EOXSL
xsl.transform @xml
end
def test_function_XSLT
skip("Pure Java version doesn't support this feature.") if !Nokogiri.uses_libxml?
foo = Class.new do
def america nodes
nodes.first.content.upcase
end
end
xsl = Nokogiri.XSLT(<<-EOXSL, "http://e.org/functions" => foo)
EOXSL
result = xsl.transform @xml
assert_match(/FOO/, result.css('title').first.text)
end
end
end
end