From abe810de48da22fbc8e28369f50e2863291fe505 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Wed, 5 Sep 2018 22:24:05 -0400 Subject: php: fix doc typo, ruby: add tests --- php/src/Template.php | 3 +- ruby/test/test_cache.rb | 28 +++++++++++++ ruby/test/test_default_filters.rb | 86 +++++++++++++++++++++++++++++++++++++++ ruby/test/test_errors.rb | 32 +++++++++++++++ ruby/test/test_filters.rb | 51 +++++++++++++++++++++++ ruby/test/test_template.rb | 85 -------------------------------------- 6 files changed, 198 insertions(+), 87 deletions(-) create mode 100644 ruby/test/test_cache.rb create mode 100644 ruby/test/test_default_filters.rb create mode 100644 ruby/test/test_errors.rb create mode 100644 ruby/test/test_filters.rb diff --git a/php/src/Template.php b/php/src/Template.php index 47bcf5d..bca260f 100644 --- a/php/src/Template.php +++ b/php/src/Template.php @@ -728,8 +728,7 @@ Filters::init(); * } * }; * - * # create template that converts name to lowercase and then - * # calculates the SHA-1 digest of the result + * # create template which uses custom "wrap" filter" * $tmpl = new Template('sandwich: %{meat | wrap slice heel}, taco: %{meat | wrap shell}'); * * # run template and print result diff --git a/ruby/test/test_cache.rb b/ruby/test/test_cache.rb new file mode 100644 index 0000000..4296410 --- /dev/null +++ b/ruby/test/test_cache.rb @@ -0,0 +1,28 @@ +require 'minitest/autorun' +require 'luigi-template' + +class TemplateTest < MiniTest::Test + def test_cache + cache = Luigi::Cache.new({ + foo: 'foo%{bar}', + }) + + r = cache.run(:foo, bar: 'foo') + + assert_equal 'foofoo', r + end + + def test_cache_with_custom_filters + cache = Luigi::Cache.new({ + foo: 'foo%{bar | barify}', + }, { + barify: proc { |v| + "bar-#{v}-bar" + }, + }) + + r = cache.run(:foo, bar: 'foo') + + assert_equal 'foobar-foo-bar', r + end +end diff --git a/ruby/test/test_default_filters.rb b/ruby/test/test_default_filters.rb new file mode 100644 index 0000000..e35c026 --- /dev/null +++ b/ruby/test/test_default_filters.rb @@ -0,0 +1,86 @@ +require 'minitest/autorun' +require 'luigi-template' + +class FiltersTest < MiniTest::Test + def test_uc + r = Luigi::Template.run('foo%{bar|uc}', { + bar: 'bar', + }) + + assert_equal 'fooBAR', r + end + + def test_lc + r = Luigi::Template.run('foo%{bar|lc}', { + bar: 'BAR', + }) + + assert_equal 'foobar', r + end + + def test_h + r = Luigi::Template.run('%{bar|h}', { + bar: "<>&\"'\x0f", + }) + + assert_equal '<>&"'', r + end + + def test_u + r = Luigi::Template.run('%{bar|u}', { + bar: "asdf<>&\"' \x0f", + }) + + assert_equal 'asdf%3C%3E%26%22%27+%0F', r + end + + def test_json + want = '{"true":true,"false":false,"null":null,"number":5,"string":"foo","hash":{"foo":"bar"},"array":[0,1]}'; + + r = Luigi::Template.run('%{bar|json}', { + bar: { + true: true, + false: false, + null: nil, + number: 5, + string: 'foo', + hash: { foo: 'bar' }, + array: [0, 1], + }, + }) + + assert_equal want, r + end + + def test_trim + r = Luigi::Template.run('foo%{bar|trim}', { + bar: "\r\n\t\v foo \r\n\t\v", + }) + + assert_equal 'foofoo', r + end + + def test_base64 + r = Luigi::Template.run('%{bar|base64}', { + bar: "foo", + }) + + assert_equal 'Zm9v', r + end + + def test_hash_md5 + r = Luigi::Template.run('%{bar|hash md5}', { + bar: "foo", + }) + + assert_equal 'acbd18db4cc2f85cedef654fccc4a4d8', r + end + + def test_hash_sha1 + r = Luigi::Template.run('%{bar|hash sha1}', { + bar: "foo", + }) + + assert_equal '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', r + end +end diff --git a/ruby/test/test_errors.rb b/ruby/test/test_errors.rb new file mode 100644 index 0000000..c196f04 --- /dev/null +++ b/ruby/test/test_errors.rb @@ -0,0 +1,32 @@ +require 'minitest/autorun' +require 'luigi-template' + +class ErrorsTest < MiniTest::Test + def test_unknown_key_error + assert_raises(Luigi::UnknownKeyError) do + Luigi::Template.run('foo%{unknown-key}', { + bar: 'foo', + }) + end + end + + def test_unknown_filter_error + assert_raises(Luigi::UnknownFilterError) do + Luigi::Template.run('foo%{bar | unknown-filter}', { + bar: 'foo', + }) + end + end + + def test_unknown_template_error + assert_raises(Luigi::UnknownTemplateError) do + cache = Luigi::Cache.new({ + foo: 'foo%{bar}', + }) + + cache.run('unknown-template', { + bar: 'foo' + }) + end + end +end diff --git a/ruby/test/test_filters.rb b/ruby/test/test_filters.rb new file mode 100644 index 0000000..9c34cd5 --- /dev/null +++ b/ruby/test/test_filters.rb @@ -0,0 +1,51 @@ +require 'minitest/autorun' +require 'luigi-template' + +class TemplateTest < MiniTest::Test + def test_filter + r = Luigi::Template.run('foo%{bar|h}', { + bar: '<', + }) + + assert_equal 'foo<', r + end + + def test_filter_chain + want = 'foofeab40e1fca77c7360ccca1481bb8ba5f919ce3a' + r = Luigi::Template.run('foo%{bar | uc | hash sha1}', { + bar: 'foo', + }) + + assert_equal want, r + end + + def test_custom_global_filter + Luigi::FILTERS[:barify] = proc { |v| 'BAR' } + + r = Luigi::Template.run('foo%{bar | barify}', { + bar: 'foo', + }) + + assert_equal 'fooBAR', r + end + + def test_custom_template_filter + r = Luigi::Template.run('foo%{bar | barify}', { + bar: 'foo', + }, { + barify: proc { |v| 'BAR' } + }) + + assert_equal 'fooBAR', r + end + + def test_filter_args + want = 'foo0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' + + r = Luigi::Template.run('foo%{bar | hash sha1}', { + bar: 'foo' + }) + + assert_equal want, r + end +end diff --git a/ruby/test/test_template.rb b/ruby/test/test_template.rb index 3c8dd10..23629ed 100644 --- a/ruby/test/test_template.rb +++ b/ruby/test/test_template.rb @@ -56,91 +56,6 @@ class TemplateTest < MiniTest::Test assert_equal 'foofoofoo', r end - def test_filter - r = Luigi::Template.run('foo%{bar|h}', { - bar: '<', - }) - - assert_equal 'foo<', r - end - - def test_filter_chain - want = 'foofeab40e1fca77c7360ccca1481bb8ba5f919ce3a' - r = Luigi::Template.run('foo%{bar | uc | hash sha1}', { - bar: 'foo', - }) - - assert_equal want, r - end - - def test_custom_global_filter - Luigi::FILTERS[:barify] = proc { |v| 'BAR' } - - r = Luigi::Template.run('foo%{bar | barify}', { - bar: 'foo', - }) - - assert_equal 'fooBAR', r - end - - def test_custom_template_filter - r = Luigi::Template.run('foo%{bar | barify}', { - bar: 'foo', - }, { - barify: proc { |v| 'BAR' } - }) - - assert_equal 'fooBAR', r - end - - def test_cache - cache = Luigi::Cache.new({ - foo: 'foo%{bar}', - }) - - r = cache.run(:foo, bar: 'foo') - - assert_equal 'foofoo', r - end - - def test_filter_args - want = 'foo0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' - - r = Luigi::Template.run('foo%{bar | hash sha1}', { - bar: 'foo' - }) - - assert_equal want, r - end - - def test_unknown_key_error - assert_raises(Luigi::UnknownKeyError) do - Luigi::Template.run('foo%{unknown-key}', { - bar: 'foo', - }) - end - end - - def test_unknown_filter_error - assert_raises(Luigi::UnknownFilterError) do - Luigi::Template.run('foo%{bar | unknown-filter}', { - bar: 'foo', - }) - end - end - - def test_unknown_template_error - assert_raises(Luigi::UnknownTemplateError) do - cache = Luigi::Cache.new({ - foo: 'foo%{bar}', - }) - - cache.run('unknown-template', { - bar: 'foo' - }) - end - end - def test_to_s want = '%{val | h}' t = Luigi::Template.new(want) -- cgit v1.2.3